Skip to content

Commit

Permalink
Fix GQL mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
islamaliev committed Jun 27, 2024
1 parent 91892e5 commit 550d97a
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 3 deletions.
4 changes: 2 additions & 2 deletions cli/collection_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Options:
and permissions are controlled by ACP (Access Control Policy).
-e, --encrypt
Encrypt flag specified if the document needs to be encrypted. If set DefraDB will generate a
Encrypt flag specified if the document needs to be encrypted. If set, DefraDB will generate a
symmetric key for encryption using AES-GCM.
Example: create from string:
Expand Down Expand Up @@ -99,7 +99,7 @@ Example: create from stdin:
},
}
cmd.PersistentFlags().BoolVarP(&shouldEncrypt, "encrypt", "e", false,
"Encryption key used to encrypt/decrypt the document")
"Flag to enable encryption of the document")
cmd.Flags().StringVarP(&file, "file", "f", "", "File containing document(s)")
return cmd
}
13 changes: 13 additions & 0 deletions cli/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,21 @@ const (

func MakeRequestCommand() *cobra.Command {
var filePath string
var shouldEncrypt bool
var cmd = &cobra.Command{
Use: "query [-i --identity] [request]",
Short: "Send a DefraDB GraphQL query request",
Long: `Send a DefraDB GraphQL query request to the database.
Options:
-i, --identity
Marks the document as private and set the identity as the owner. The access to the document
and permissions are controlled by ACP (Access Control Policy).
-e, --encrypt
Encrypt flag specified if the document needs to be encrypted. If set, DefraDB will generate a
symmetric key for encryption using AES-GCM.
A query request can be sent as a single argument. Example command:
defradb client query 'query { ... }'
Expand Down Expand Up @@ -71,6 +81,7 @@ To learn more about the DefraDB GraphQL Query Language, refer to https://docs.so
}

store := mustGetContextStore(cmd)
setContextDocEncryption(cmd, shouldEncrypt, nil)
result := store.ExecRequest(cmd.Context(), request)

var errors []string
Expand All @@ -89,6 +100,8 @@ To learn more about the DefraDB GraphQL Query Language, refer to https://docs.so
},
}

cmd.PersistentFlags().BoolVarP(&shouldEncrypt, "encrypt", "e", false,
"Flag to enable encryption of the document")
cmd.Flags().StringVarP(&filePath, "file", "f", "", "File containing the query request")
return cmd
}
7 changes: 7 additions & 0 deletions http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/sourcenetwork/defradb/client"
"github.com/sourcenetwork/defradb/datastore"
"github.com/sourcenetwork/defradb/event"
"github.com/sourcenetwork/defradb/internal/encryption"
)

var _ client.DB = (*Client)(nil)
Expand Down Expand Up @@ -355,6 +356,12 @@ func (c *Client) ExecRequest(
return result
}
err = c.http.setDefaultHeaders(req)

encConf := encryption.GetContextConfig(ctx)
if encConf.HasValue() && encConf.Value().IsEncrypted {
req.Header.Set(DocEncryptionHeader, "1")
}

if err != nil {
result.GQL.Errors = []error{err}
return result
Expand Down
8 changes: 7 additions & 1 deletion http/handler_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/sourcenetwork/immutable"

"github.com/sourcenetwork/defradb/client"
"github.com/sourcenetwork/defradb/internal/encryption"
)

type storeHandler struct{}
Expand Down Expand Up @@ -312,7 +313,12 @@ func (s *storeHandler) ExecRequest(rw http.ResponseWriter, req *http.Request) {
return
}

result := store.ExecRequest(req.Context(), request.Query)
ctx := req.Context()
if req.Header.Get(DocEncryptionHeader) == "1" {
ctx = encryption.SetContextConfig(ctx, encryption.DocEncConfig{IsEncrypted: true})
}

result := store.ExecRequest(ctx, request.Query)

if result.Subscription == nil {
responseJSON(rw, http.StatusOK, GraphQLResponse{result.GQL.Data, result.GQL.Errors})
Expand Down
6 changes: 6 additions & 0 deletions tests/clients/cli/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/sourcenetwork/defradb/datastore"
"github.com/sourcenetwork/defradb/event"
"github.com/sourcenetwork/defradb/http"
"github.com/sourcenetwork/defradb/internal/encryption"
"github.com/sourcenetwork/defradb/net"
)

Expand Down Expand Up @@ -399,6 +400,11 @@ func (w *Wrapper) ExecRequest(

result := &client.RequestResult{}

encCond := encryption.GetContextConfig(ctx)
if encCond.HasValue() && encCond.Value().IsEncrypted {
args = append(args, "--encrypt")
}

stdOut, stdErr, err := w.cmd.executeStream(ctx, args)
if err != nil {
result.GQL.Errors = []error{err}
Expand Down

0 comments on commit 550d97a

Please sign in to comment.