forked from sourcenetwork/defradb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into nasdf/feat/ci-docker-deploy
- Loading branch information
Showing
77 changed files
with
4,937 additions
and
230 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Copyright 2023 Democratized Data Foundation | ||
# | ||
# Use of this software is governed by the Business Source License | ||
# included in the file licenses/BSL.txt. | ||
# | ||
# As of the Change Date specified in that file, in accordance with | ||
# the Business Source License, use of this software will be governed | ||
# by the Apache License, Version 2.0, included in the file | ||
# licenses/APL.txt. | ||
|
||
name: Check Vulnerabilities Workflow | ||
|
||
on: | ||
pull_request: | ||
branches: | ||
- master | ||
- develop | ||
|
||
push: | ||
tags: | ||
- 'v[0-9]+.[0-9]+.[0-9]+' | ||
branches: | ||
- master | ||
- develop | ||
|
||
jobs: | ||
check-vulnerabilities: | ||
name: Check vulnerabilities job | ||
|
||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Run govulncheck | ||
uses: golang/govulncheck-action@v1 | ||
with: | ||
go-version-input: "1.20" | ||
go-package: ./... | ||
check-latest: true | ||
cache: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// Copyright 2023 Democratized Data Foundation | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package http | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"os" | ||
"strings" | ||
|
||
"github.com/sourcenetwork/defradb/client" | ||
"github.com/sourcenetwork/defradb/errors" | ||
) | ||
|
||
func exportHandler(rw http.ResponseWriter, req *http.Request) { | ||
db, err := dbFromContext(req.Context()) | ||
if err != nil { | ||
handleErr(req.Context(), rw, err, http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
cfg := &client.BackupConfig{} | ||
err = getJSON(req, cfg) | ||
if err != nil { | ||
handleErr(req.Context(), rw, err, http.StatusBadRequest) | ||
return | ||
} | ||
|
||
err = validateBackupConfig(req.Context(), cfg, db) | ||
if err != nil { | ||
handleErr(req.Context(), rw, err, http.StatusBadRequest) | ||
return | ||
} | ||
|
||
err = db.BasicExport(req.Context(), cfg) | ||
if err != nil { | ||
handleErr(req.Context(), rw, err, http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
sendJSON( | ||
req.Context(), | ||
rw, | ||
simpleDataResponse("result", "success"), | ||
http.StatusOK, | ||
) | ||
} | ||
|
||
func importHandler(rw http.ResponseWriter, req *http.Request) { | ||
db, err := dbFromContext(req.Context()) | ||
if err != nil { | ||
handleErr(req.Context(), rw, err, http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
cfg := &client.BackupConfig{} | ||
err = getJSON(req, cfg) | ||
if err != nil { | ||
handleErr(req.Context(), rw, err, http.StatusBadRequest) | ||
return | ||
} | ||
|
||
err = validateBackupConfig(req.Context(), cfg, db) | ||
if err != nil { | ||
handleErr(req.Context(), rw, err, http.StatusBadRequest) | ||
return | ||
} | ||
|
||
err = db.BasicImport(req.Context(), cfg.Filepath) | ||
if err != nil { | ||
handleErr(req.Context(), rw, err, http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
sendJSON( | ||
req.Context(), | ||
rw, | ||
simpleDataResponse("result", "success"), | ||
http.StatusOK, | ||
) | ||
} | ||
|
||
func validateBackupConfig(ctx context.Context, cfg *client.BackupConfig, db client.DB) error { | ||
if !isValidPath(cfg.Filepath) { | ||
return errors.New("invalid file path") | ||
} | ||
|
||
if cfg.Format != "" && strings.ToLower(cfg.Format) != "json" { | ||
return errors.New("only JSON format is supported at the moment") | ||
} | ||
for _, colName := range cfg.Collections { | ||
_, err := db.GetCollectionByName(ctx, colName) | ||
if err != nil { | ||
return errors.Wrap("collection does not exist", err) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func isValidPath(filepath string) bool { | ||
// if a file exists, return true | ||
if _, err := os.Stat(filepath); err == nil { | ||
return true | ||
} | ||
|
||
// if not, attempt to write to the path and if successful, | ||
// remove the file and return true | ||
var d []byte | ||
if err := os.WriteFile(filepath, d, 0o644); err == nil { | ||
_ = os.Remove(filepath) | ||
return true | ||
} | ||
|
||
return false | ||
} |
Oops, something went wrong.