Skip to content

Commit

Permalink
Merge branch 'develop' into nasdf/feat/ci-docker-deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
nasdf authored Jul 24, 2023
2 parents 10bf160 + e6c5770 commit 67d06f5
Show file tree
Hide file tree
Showing 77 changed files with 4,937 additions and 230 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-dependencies.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
- name: Setup Go environment explicitly
uses: actions/setup-go@v3
with:
go-version: "1.19"
go-version: "1.20"
check-latest: true

- name: Build all dependencies
Expand Down
39 changes: 39 additions & 0 deletions .github/workflows/check-vulnerabilities.yml
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
2 changes: 1 addition & 1 deletion .github/workflows/code-test-coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
- name: Setup Go environment explicitly
uses: actions/setup-go@v3
with:
go-version: "1.19"
go-version: "1.20"
check-latest: true

- name: Generate full test coverage report using go-acc
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/detect-change.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
- name: Setup Go environment explicitly
uses: actions/setup-go@v3
with:
go-version: "1.19"
go-version: "1.20"
check-latest: true

- name: Build dependencies
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lint-then-benchmark.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:
- name: Setup Go environment explicitly
uses: actions/setup-go@v3
with:
go-version: "1.19"
go-version: "1.20"
check-latest: true

- name: Run the golangci-lint
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
- name: Setup Go environment explicitly
uses: actions/setup-go@v3
with:
go-version: "1.19"
go-version: "1.20"
check-latest: true

- name: Check linting through golangci-lint
Expand All @@ -46,7 +46,7 @@ jobs:
# Required: the version of golangci-lint is required.
# Note: The version should not pick the patch version as the latest patch
# version is what will always be used.
version: v1.51
version: v1.53

# Optional: working directory, useful for monorepos or if we wanted to run this
# on a non-root directory.
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
- name: Setup Go environment explicitly
uses: actions/setup-go@v3
with:
go-version: "1.19"
go-version: "1.20"
check-latest: true

- name: Build dependencies
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/start-binary.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
- name: Setup Go environment explicitly
uses: actions/setup-go@v3
with:
go-version: "1.19"
go-version: "1.20"
check-latest: true

- name: Build modules
Expand Down
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ ifdef BUILD_TAGS
BUILD_FLAGS+=-tags $(BUILD_TAGS)
endif

TEST_FLAGS=-race -shuffle=on -timeout 70s
TEST_FLAGS=-race -shuffle=on -timeout 120s

PLAYGROUND_DIRECTORY=playground
LENS_TEST_DIRECTORY=tests/integration/schema/migrations
Expand Down Expand Up @@ -76,7 +76,7 @@ client\:add-schema:

.PHONY: deps\:lint
deps\:lint:
go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.51
go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.53

.PHONY: deps\:test
deps\:test:
Expand Down Expand Up @@ -158,7 +158,7 @@ verify:

.PHONY: tidy
tidy:
go mod tidy -go=1.19
go mod tidy -go=1.20

.PHONY: clean
clean:
Expand Down Expand Up @@ -243,6 +243,7 @@ test\:lens:

.PHONY: test\:cli
test\:cli:
@$(MAKE) deps:lens
gotestsum --format testname -- ./$(CLI_TEST_DIRECTORY)/... $(TEST_FLAGS)

# Using go-acc to ensure integration tests are included.
Expand Down
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ Read the documentation on [docs.source.network](https://docs.source.network/).
- [Collection subscription example](#collection-subscription-example)
- [Replicator example](#replicator-example)
- [Securing the HTTP API with TLS](#securing-the-http-api-with-tls)
- [Supporting CORS](#supporting-cors)
- [Backing up and restoring](#backing-up-and-restoring)
- [Licensing](#licensing)
- [Contributors](#contributors)

Expand Down Expand Up @@ -387,6 +389,25 @@ defradb start --allowed-origins=http://localhost:3000

The catch-all `*` is also a valid origin.

## Backing up and restoring

It is currently not possible to do a full backup of DefraDB that includes the history of changes through the Merkle DAG. However, DefraDB currently supports a simple backup of the current data state in JSON format that can be used to seed a database or help with transitioning from one DefraDB version to another.

To backup the data, run the following command:
```shell
defradb client backup export path/to/backup.json
```

To pretty print the JSON content when exporting, run the following command:
```shell
defradb client backup export --pretty path/to/backup.json
```

To restore the data, run the following command:
```shell
defradb client backup import path/to/backup.json
```

## Community

Discuss on [Discord](https://discord.source.network/) or [Github Discussions](https://github.com/sourcenetwork/defradb/discussions). The Source project is on [Twitter](https://twitter.com/sourcenetwrk).
Expand Down
2 changes: 2 additions & 0 deletions api/http/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ var (
ErrPeerIdUnavailable = errors.New("no PeerID available. P2P might be disabled")
ErrStreamingUnsupported = errors.New("streaming unsupported")
ErrNoEmail = errors.New("email address must be specified for tls with autocert")
ErrPayloadFormat = errors.New("invalid payload format")
ErrMissingNewKey = errors.New("missing _newKey for imported doc")
)

// ErrorResponse is the GQL top level object holding error items for the response payload.
Expand Down
2 changes: 1 addition & 1 deletion api/http/handlerfuncs.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func pingHandler(rw http.ResponseWriter, req *http.Request) {
sendJSON(
req.Context(),
rw,
simpleDataResponse("response", "pong", "test"),
simpleDataResponse("response", "pong"),
http.StatusOK,
)
}
Expand Down
123 changes: 123 additions & 0 deletions api/http/handlerfuncs_backup.go
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
}
Loading

0 comments on commit 67d06f5

Please sign in to comment.