Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add node identity #3125

Merged
merged 39 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
7caedf3
Remove code duplication
islamaliev Oct 8, 2024
a1ea081
Assign identity to a node
islamaliev Oct 8, 2024
f916707
WIP
islamaliev Oct 8, 2024
d916c33
Return RawIdentity, add test
islamaliev Oct 10, 2024
1238aba
Fix lint
islamaliev Oct 10, 2024
1b92c70
Update docs
islamaliev Oct 10, 2024
61c8fb0
Update mocks
islamaliev Oct 10, 2024
82dc3b2
Minor refactor
islamaliev Oct 10, 2024
30a028f
PR fixup
islamaliev Oct 12, 2024
2bd361a
Polish
islamaliev Oct 12, 2024
940177d
Update mocks
islamaliev Oct 12, 2024
55413a3
PR fixup
islamaliev Oct 15, 2024
5ecf6ab
Polish
islamaliev Oct 15, 2024
a32a1f4
PR fixup
islamaliev Oct 15, 2024
62e38c5
PR fixup
islamaliev Oct 15, 2024
ed20e57
PR fixup
islamaliev Oct 17, 2024
a3396bc
Update docs
islamaliev Oct 17, 2024
3f03aa5
Rename command to node-identity
islamaliev Oct 18, 2024
5b2f935
Add assign-node-identity command
islamaliev Oct 20, 2024
b9ebd23
Update docs
islamaliev Oct 20, 2024
d158f83
Lint fix
islamaliev Oct 20, 2024
ab5dc33
Update mocks
islamaliev Oct 20, 2024
4c73fb4
Create parent command node-identity
islamaliev Oct 20, 2024
5946638
PR fixup
islamaliev Oct 21, 2024
8e39ec3
Merge remote-tracking branch 'upstream/develop' into feat/node-identity
islamaliev Oct 21, 2024
0530ab7
Make identity token updatable
islamaliev Oct 22, 2024
ab3a9ea
Update docs
islamaliev Oct 22, 2024
0e0a252
Fix lint
islamaliev Oct 22, 2024
5b3a5c4
Merge remote-tracking branch 'upstream/develop' into feat/node-identity
islamaliev Oct 22, 2024
a0f173f
Turn 2d array of identities into 1d (WIP)
islamaliev Oct 12, 2024
b6d148b
Add clear distinction between user and node identity
islamaliev Oct 13, 2024
95fc645
Pass ctx explicitly
islamaliev Oct 22, 2024
b154dbe
Remove duration from node's identity
islamaliev Oct 23, 2024
af4e2f9
Remove node-identity assign command
islamaliev Oct 23, 2024
2869d87
Polish
islamaliev Oct 24, 2024
3210fbf
Merge remote-tracking branch 'upstream/develop' into feat/node-identity
islamaliev Oct 24, 2024
e4fc548
Make identityRef optional
islamaliev Oct 25, 2024
7fc8e3d
Rename UserIdentity to ClientIdentity
islamaliev Oct 25, 2024
f3ca301
Merge remote-tracking branch 'upstream/develop' into feat/node-identity
islamaliev Oct 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,18 @@ func NewDefraCommand() *cobra.Command {
MakeCollectionPatchCommand(),
)

nodeIdentity := MakeNodeIdentityCommand()
nodeIdentity.AddCommand(
MakeNodeIdentityGetCommand(),
MakeNodeIdentityAssignCommand(),
islamaliev marked this conversation as resolved.
Show resolved Hide resolved
)

client := MakeClientCommand()
client.AddCommand(
MakePurgeCommand(),
MakeDumpCommand(),
MakeRequestCommand(),
MakeNodeIdentityCommand(),
MakeAssignNodeIdentityCommand(),
nodeIdentity,
schema,
acp,
view,
Expand Down
27 changes: 3 additions & 24 deletions cli/node_identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,9 @@ import (
func MakeNodeIdentityCommand() *cobra.Command {
var cmd = &cobra.Command{
Use: "node-identity",
Short: "Get the information public about the node's identity",
Long: `Get the information public about the node's identity.

Node uses the identity to be able to exchange encryption keys with other nodes.

A public identity contains:
- A compressed 33-byte secp256k1 public key in HEX format.
- A "did:key" generated from the public key.
`,
RunE: func(cmd *cobra.Command, args []string) error {
db := mustGetContextDB(cmd)
identity, err := db.GetNodeIdentity(cmd.Context())
if err != nil {
return err
}

if identity.HasValue() {
return writeJSON(cmd, identity.Value())
}

out := cmd.OutOrStdout()
_, err = out.Write([]byte("Node has no identity assigned to it\n"))
return err
},
Short: "Manage DefraDB node's identity",
Long: `Manage DefraDB node's identity`,
}

return cmd
}
6 changes: 3 additions & 3 deletions cli/assign_node_identity.go → cli/node_identity_assign.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,31 @@
acpIdentity "github.com/sourcenetwork/defradb/acp/identity"
)

func MakeAssignNodeIdentityCommand() *cobra.Command {
func MakeNodeIdentityAssignCommand() *cobra.Command {
var cmd = &cobra.Command{
Use: "assign-node-identity [identity]",
Use: "assign [identity]",
Short: "Assign an identity to the node",
Long: `Assign an identity to the node.

Identity is hex-formatted private key.
Node uses the identity to be able to exchange encryption keys with other nodes.

Example to assign an identity to the node:
defradb client assign-node-identity 028d53f37a19afb9a0dbc5b4be30c65731479ee8cfa0c9bc8f8bf198cc3c075f
defradb client node-identity assign 028d53f37a19afb9a0dbc5b4be30c65731479ee8cfa0c9bc8f8bf198cc3c075f

`,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return fmt.Errorf("only 1 [identity] argument is allowed")
}

Check warning on line 41 in cli/node_identity_assign.go

View check run for this annotation

Codecov / codecov/patch

cli/node_identity_assign.go#L40-L41

Added lines #L40 - L41 were not covered by tests

cfg := mustGetContextConfig(cmd)

db := mustGetContextDB(cmd)
data, err := hex.DecodeString(args[0])
if err != nil {
return err
}

Check warning on line 49 in cli/node_identity_assign.go

View check run for this annotation

Codecov / codecov/patch

cli/node_identity_assign.go#L48-L49

Added lines #L48 - L49 were not covered by tests
privKey := secp256k1.PrivKeyFromBytes(data)
identity, err := acpIdentity.FromPrivateKey(
privKey,
Expand All @@ -56,8 +56,8 @@
false,
)
if err != nil {
return err
}

Check warning on line 60 in cli/node_identity_assign.go

View check run for this annotation

Codecov / codecov/patch

cli/node_identity_assign.go#L59-L60

Added lines #L59 - L60 were not covered by tests
return db.AssignNodeIdentity(cmd.Context(), identity)
},
}
Expand Down
50 changes: 50 additions & 0 deletions cli/node_identity_get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2024 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 cli

import (
"github.com/spf13/cobra"
)

func MakeNodeIdentityGetCommand() *cobra.Command {
var cmd = &cobra.Command{
Use: "get",
Short: "Get the information public about the node's identity",
Long: `Get the information public about the node's identity.

Node uses the identity to be able to exchange encryption keys with other nodes.

A public identity contains:
- A compressed 33-byte secp256k1 public key in HEX format.
- A "did:key" generated from the public key.

Example to get the identity of the node:
defradb client node-identity get

`,
RunE: func(cmd *cobra.Command, args []string) error {
db := mustGetContextDB(cmd)
identity, err := db.GetNodeIdentity(cmd.Context())
if err != nil {
return err
}

Check warning on line 38 in cli/node_identity_get.go

View check run for this annotation

Codecov / codecov/patch

cli/node_identity_get.go#L37-L38

Added lines #L37 - L38 were not covered by tests

if identity.HasValue() {
return writeJSON(cmd, identity.Value())
}

out := cmd.OutOrStdout()
_, err = out.Write([]byte("Node has no identity assigned to it\n"))
return err

Check warning on line 46 in cli/node_identity_get.go

View check run for this annotation

Codecov / codecov/patch

cli/node_identity_get.go#L44-L46

Added lines #L44 - L46 were not covered by tests
},
}
return cmd
}
3 changes: 1 addition & 2 deletions docs/website/references/cli/defradb_client.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,11 @@ Execute queries, add schema types, obtain node info, etc.

* [defradb](defradb.md) - DefraDB Edge Database
* [defradb client acp](defradb_client_acp.md) - Interact with the access control system of a DefraDB node
* [defradb client assign-node-identity](defradb_client_assign-node-identity.md) - Assign an identity to the node
* [defradb client backup](defradb_client_backup.md) - Interact with the backup utility
* [defradb client collection](defradb_client_collection.md) - Interact with a collection.
* [defradb client dump](defradb_client_dump.md) - Dump the contents of DefraDB node-side
* [defradb client index](defradb_client_index.md) - Manage collections' indexes of a running DefraDB instance
* [defradb client node-identity](defradb_client_node-identity.md) - Get the information public about the node's identity
* [defradb client node-identity](defradb_client_node-identity.md) - Manage DefraDB node's identity
* [defradb client p2p](defradb_client_p2p.md) - Interact with the DefraDB P2P system
* [defradb client purge](defradb_client_purge.md) - Delete all persisted data and restart
* [defradb client query](defradb_client_query.md) - Send a DefraDB GraphQL query request
Expand Down
17 changes: 4 additions & 13 deletions docs/website/references/cli/defradb_client_node-identity.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
## defradb client node-identity

Get the information public about the node's identity
Manage DefraDB node's identity

### Synopsis

Get the information public about the node's identity.

Node uses the identity to be able to exchange encryption keys with other nodes.

A public identity contains:
- A compressed 33-byte secp256k1 public key in HEX format.
- A "did:key" generated from the public key.


```
defradb client node-identity [flags]
```
Manage DefraDB node's identity

### Options

Expand Down Expand Up @@ -48,4 +37,6 @@ defradb client node-identity [flags]
### SEE ALSO

* [defradb client](defradb_client.md) - Interact with a DefraDB node
* [defradb client node-identity assign](defradb_client_node-identity_assign.md) - Assign an identity to the node
* [defradb client node-identity get](defradb_client_node-identity_get.md) - Get the information public about the node's identity

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## defradb client assign-node-identity
## defradb client node-identity assign

Assign an identity to the node

Expand All @@ -10,18 +10,18 @@ Identity is hex-formatted private key.
Node uses the identity to be able to exchange encryption keys with other nodes.

Example to assign an identity to the node:
defradb client assign-node-identity 028d53f37a19afb9a0dbc5b4be30c65731479ee8cfa0c9bc8f8bf198cc3c075f
defradb client node-identity assign 028d53f37a19afb9a0dbc5b4be30c65731479ee8cfa0c9bc8f8bf198cc3c075f



```
defradb client assign-node-identity [identity] [flags]
defradb client node-identity assign [identity] [flags]
```

### Options

```
-h, --help help for assign-node-identity
-h, --help help for assign
```

### Options inherited from parent commands
Expand All @@ -48,5 +48,5 @@ defradb client assign-node-identity [identity] [flags]

### SEE ALSO

* [defradb client](defradb_client.md) - Interact with a DefraDB node
* [defradb client node-identity](defradb_client_node-identity.md) - Manage DefraDB node's identity

55 changes: 55 additions & 0 deletions docs/website/references/cli/defradb_client_node-identity_get.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
## defradb client node-identity get

Get the information public about the node's identity

### Synopsis

Get the information public about the node's identity.

Node uses the identity to be able to exchange encryption keys with other nodes.

A public identity contains:
- A compressed 33-byte secp256k1 public key in HEX format.
- A "did:key" generated from the public key.

Example to get the identity of the node:
defradb client node-identity get



```
defradb client node-identity get [flags]
```

### Options

```
-h, --help help for get
```

### Options inherited from parent commands

```
-i, --identity string Hex formatted private key used to authenticate with ACP
--keyring-backend string Keyring backend to use. Options are file or system (default "file")
--keyring-namespace string Service name to use when using the system backend (default "defradb")
--keyring-path string Path to store encrypted keys when using the file backend (default "keys")
--log-format string Log format to use. Options are text or json (default "text")
--log-level string Log level to use. Options are debug, info, error, fatal (default "info")
--log-output string Log output path. Options are stderr or stdout. (default "stderr")
--log-overrides string Logger config overrides. Format <name>,<key>=<val>,...;<name>,...
--log-source Include source location in logs
--log-stacktrace Include stacktrace in error and fatal logs
--no-keyring Disable the keyring and generate ephemeral keys
--no-log-color Disable colored log output
--rootdir string Directory for persistent data (default: $HOME/.defradb)
--secret-file string Path to the file containing secrets (default ".env")
--source-hub-address string The SourceHub address authorized by the client to make SourceHub transactions on behalf of the actor
--tx uint Transaction ID
--url string URL of HTTP endpoint to listen on or connect to (default "127.0.0.1:9181")
```

### SEE ALSO

* [defradb client node-identity](defradb_client_node-identity.md) - Manage DefraDB node's identity

36 changes: 17 additions & 19 deletions docs/website/references/http/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -1688,13 +1688,20 @@
]
}
},
"/node/assign-identity": {
"post": {
"description": "Assign node's identity",
"operationId": "assign_node_identity",
"/node/identity": {
"get": {
"description": "Get node's public identity",
"operationId": "node_identity",
"responses": {
"200": {
"$ref": "#/components/responses/success"
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/identity"
}
}
},
"description": "Identity"
},
"400": {
"$ref": "#/components/responses/error"
Expand All @@ -1707,22 +1714,13 @@
"node",
"identity"
]
}
},
"/node/identity": {
"get": {
"description": "Get node's public identity",
"operationId": "node_identity",
},
"post": {
"description": "Assign node's identity",
"operationId": "assign_node_identity",
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/identity"
}
}
},
"description": "Identity"
"$ref": "#/components/responses/success"
},
"400": {
"$ref": "#/components/responses/error"
Expand Down
2 changes: 1 addition & 1 deletion http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,23 +515,23 @@

req, err := http.NewRequestWithContext(ctx, http.MethodGet, methodURL.String(), nil)
if err != nil {
return immutable.None[identity.PublicRawIdentity](), err
}

Check warning on line 519 in http/client.go

View check run for this annotation

Codecov / codecov/patch

http/client.go#L518-L519

Added lines #L518 - L519 were not covered by tests
var ident immutable.Option[identity.PublicRawIdentity]
if err := c.http.requestJson(req, &ident); err != nil {
return immutable.None[identity.PublicRawIdentity](), err
}

Check warning on line 523 in http/client.go

View check run for this annotation

Codecov / codecov/patch

http/client.go#L522-L523

Added lines #L522 - L523 were not covered by tests
return ident, err
}

func (c *Client) AssignNodeIdentity(ctx context.Context, ident identity.Identity) error {
methodURL := c.http.baseURL.JoinPath("node", "assign-identity")
methodURL := c.http.baseURL.JoinPath("node", "identity")
islamaliev marked this conversation as resolved.
Show resolved Hide resolved

ctx = identity.WithContext(ctx, immutable.Some(ident))
req, err := http.NewRequestWithContext(ctx, http.MethodPost, methodURL.String(), nil)
if err != nil {
return err
}

Check warning on line 534 in http/client.go

View check run for this annotation

Codecov / codecov/patch

http/client.go#L533-L534

Added lines #L533 - L534 were not covered by tests
_, err = c.http.request(req)
return err
}
2 changes: 1 addition & 1 deletion http/handler_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,9 @@

identity, err := db.GetNodeIdentity(req.Context())
if err != nil {
responseJSON(rw, http.StatusBadRequest, errorResponse{err})
return
}

Check warning on line 353 in http/handler_store.go

View check run for this annotation

Codecov / codecov/patch

http/handler_store.go#L351-L353

Added lines #L351 - L353 were not covered by tests
responseJSON(rw, http.StatusOK, identity)
islamaliev marked this conversation as resolved.
Show resolved Hide resolved
}

Expand All @@ -360,15 +360,15 @@
ident := identity.FromContext(req.Context())

if !ident.HasValue() {
responseJSON(rw, http.StatusBadRequest, errorResponse{ErrMissingIdentity})
return
}

Check warning on line 365 in http/handler_store.go

View check run for this annotation

Codecov / codecov/patch

http/handler_store.go#L363-L365

Added lines #L363 - L365 were not covered by tests

err := db.AssignNodeIdentity(req.Context(), ident.Value())
if err != nil {
responseJSON(rw, http.StatusBadRequest, errorResponse{err})
return
}

Check warning on line 371 in http/handler_store.go

View check run for this annotation

Codecov / codecov/patch

http/handler_store.go#L369-L371

Added lines #L369 - L371 were not covered by tests
}

func (h *storeHandler) bindRoutes(router *Router) {
Expand Down Expand Up @@ -695,5 +695,5 @@
router.AddRoute("/schema/default", http.MethodPost, setActiveSchemaVersion, h.SetActiveSchemaVersion)
router.AddRoute("/lens", http.MethodPost, setMigration, h.SetMigration)
router.AddRoute("/node/identity", http.MethodGet, nodeIdentity, h.GetNodeIdentity)
router.AddRoute("/node/assign-identity", http.MethodPost, assignNodeIdentity, h.AssignNodeIdentity)
router.AddRoute("/node/identity", http.MethodPost, assignNodeIdentity, h.AssignNodeIdentity)
}
4 changes: 2 additions & 2 deletions tests/clients/cli/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ func (w *Wrapper) Host() string {
}

func (w *Wrapper) GetNodeIdentity(ctx context.Context) (immutable.Option[identity.PublicRawIdentity], error) {
args := []string{"client", "node-identity"}
args := []string{"client", "node-identity", "get"}

data, err := w.cmd.execute(ctx, args)
if err != nil {
Expand All @@ -582,7 +582,7 @@ func (w *Wrapper) GetNodeIdentity(ctx context.Context) (immutable.Option[identit

func (w *Wrapper) AssignNodeIdentity(ctx context.Context, ident identity.Identity) error {
privateKeyHex := hex.EncodeToString(ident.PrivateKey.Serialize())
args := []string{"client", "assign-node-identity", privateKeyHex}
args := []string{"client", "node-identity", "assign", privateKeyHex}

_, err := w.cmd.execute(ctx, args)
return err
Expand Down
18 changes: 9 additions & 9 deletions tests/integration/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,15 @@ func ExecuteTestCase(
skipIfViewCacheTypeUnsupported(t, testCase.SupportedViewTypes)

var clients []ClientType
if httpClient {
clients = append(clients, HTTPClientType)
}
if goClient {
clients = append(clients, GoClientType)
}
if cliClient {
clients = append(clients, CLIClientType)
}
//if httpClient {
clients = append(clients, HTTPClientType)
//}
//if goClient {
clients = append(clients, GoClientType)
//}
//if cliClient {
clients = append(clients, CLIClientType)
//}

var databases []DatabaseType
if badgerInMemory {
Expand Down
Loading