Skip to content

Commit

Permalink
Add encrypt param to create gql mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
islamaliev committed Jun 28, 2024
1 parent 2dd65d0 commit 34bef3f
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 48 deletions.
13 changes: 0 additions & 13 deletions cli/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,11 @@ 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 @@ -81,7 +71,6 @@ 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 @@ -100,8 +89,6 @@ 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
}
2 changes: 2 additions & 0 deletions client/request/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const (
FieldIDName = "fieldId"
ShowDeleted = "showDeleted"

EncryptArgName = "encrypt"

FilterClause = "filter"
GroupByClause = "groupBy"
LimitClause = "limit"
Expand Down
5 changes: 4 additions & 1 deletion client/request/mutation.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,14 @@ type ObjectMutation struct {
// This is ignored for [DeleteObjects] mutations.
Input map[string]any

// Inputs is the array of json representations of the fieldName-value pairs of document
// Inputs is the array of json representations of the fieldName-value pairs of document
// properties to mutate.
//
// This is ignored for [DeleteObjects] mutations.
Inputs []map[string]any

// Encrypt is a boolean flag that indicates whether the input data should be encrypted.
Encrypt bool
}

// ToSelect returns a basic Select object, with the same Name, Alias, and Fields as
Expand Down
10 changes: 0 additions & 10 deletions docs/website/references/cli/defradb_client_query.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,6 @@ Send a DefraDB GraphQL query request

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 All @@ -39,7 +30,6 @@ defradb client query [-i --identity] [request] [flags]
### Options

```
-e, --encrypt Flag to enable encryption of the document
-f, --file string File containing the query request
-h, --help help for query
```
Expand Down
17 changes: 9 additions & 8 deletions http/client_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,7 @@ func (c *Collection) Create(
return err
}

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

_, err = c.http.request(req)
if err != nil {
Expand Down Expand Up @@ -120,10 +117,7 @@ func (c *Collection) CreateMany(
return err
}

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

_, err = c.http.request(req)
if err != nil {
Expand All @@ -136,6 +130,13 @@ func (c *Collection) CreateMany(
return nil
}

func setDocEncryptionHeaderIfNeeded(ctx context.Context, req *http.Request) {
encConf := encryption.GetContextConfig(ctx)
if encConf.HasValue() && encConf.Value().IsEncrypted {
req.Header.Set(DocEncryptionHeader, "1")
}
}

func (c *Collection) Update(
ctx context.Context,
doc *client.Document,
Expand Down
8 changes: 1 addition & 7 deletions http/handler_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/sourcenetwork/immutable"

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

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

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

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

if result.Subscription == nil {
responseJSON(rw, http.StatusOK, GraphQLResponse{result.GQL.Data, result.GQL.Errors})
Expand Down
5 changes: 5 additions & 0 deletions internal/planner/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/sourcenetwork/defradb/client/request"
"github.com/sourcenetwork/defradb/internal/core"
"github.com/sourcenetwork/defradb/internal/db/base"
"github.com/sourcenetwork/defradb/internal/encryption"
"github.com/sourcenetwork/defradb/internal/planner/mapper"
)

Expand Down Expand Up @@ -160,6 +161,10 @@ func (p *Planner) CreateDocs(parsed *mapper.Mutation) (planNode, error) {
create.input = []map[string]any{parsed.Input}
}

if parsed.Encrypt {
p.ctx = encryption.SetContextConfig(p.ctx, encryption.DocEncConfig{IsEncrypted: true})
}

// get collection
col, err := p.db.GetCollectionByName(p.ctx, parsed.Name)
if err != nil {
Expand Down
9 changes: 5 additions & 4 deletions internal/planner/mapper/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -1165,10 +1165,11 @@ func ToMutation(ctx context.Context, store client.Store, mutationRequest *reques
}

return &Mutation{
Select: *underlyingSelect,
Type: MutationType(mutationRequest.Type),
Input: mutationRequest.Input,
Inputs: mutationRequest.Inputs,
Select: *underlyingSelect,
Type: MutationType(mutationRequest.Type),
Input: mutationRequest.Input,
Inputs: mutationRequest.Inputs,
Encrypt: mutationRequest.Encrypt,
}, nil
}

Expand Down
3 changes: 3 additions & 0 deletions internal/planner/mapper/mutation.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,7 @@ type Mutation struct {

// Inputs is the array of maps of fields and values used for the mutation.
Inputs []map[string]any

// Encrypt is a flag to indicate if the input data should be encrypted.
Encrypt bool
}
2 changes: 2 additions & 0 deletions internal/request/graphql/parser/mutation.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ func parseMutation(schema gql.Schema, parent *gql.Object, field *ast.Field) (*re
ids[i] = id.Value
}
mut.DocIDs = immutable.Some(ids)
} else if prop == request.EncryptArgName {
mut.Encrypt = argument.Value.(*ast.BooleanValue).Value
}
}

Expand Down
5 changes: 3 additions & 2 deletions internal/request/graphql/schema/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -1061,8 +1061,9 @@ func (g *Generator) GenerateMutationInputForGQLType(obj *gql.Object) ([]*gql.Fie
Description: createDocumentDescription,
Type: obj,
Args: gql.FieldConfigArgument{
"input": schemaTypes.NewArgConfig(mutationInput, "Create field values"),
"inputs": schemaTypes.NewArgConfig(mutationInputs, "Create field values"),
"input": schemaTypes.NewArgConfig(mutationInput, "Create field values"),
"inputs": schemaTypes.NewArgConfig(mutationInputs, "Create field values"),
"encrypt": schemaTypes.NewArgConfig(gql.Boolean, "Encrypt input document(s)"),
},
}

Expand Down
11 changes: 8 additions & 3 deletions tests/integration/utils2.go
Original file line number Diff line number Diff line change
Expand Up @@ -1314,15 +1314,20 @@ func createDocViaGQL(

var docs []*client.Document

params := paramName + ": " + input

if action.IsEncrypted {
params = params + ", encrypt: true"
}

request := fmt.Sprintf(
`mutation {
create_%s(%s: %s) {
create_%s(%s) {
_docID
}
}`,
collection.Name().Value(),
paramName,
input,
params,
)

txn := getTransaction(s, node, immutable.None[int](), action.ExpectedError)
Expand Down

0 comments on commit 34bef3f

Please sign in to comment.