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.
## Relevant issue(s) Resolves sourcenetwork#2556 ## Description This PR adds a keyring to the cli. Notable changes: - created `keyring` package - file backed keystore uses a password based key generation to encrypt - system backed keystore uses the OS managed keyring - added `keyring` root command - `--no-keyring` disables the keyring and generates ephemeral keys - `--keyring-backend` flag sets the keyring backend (file or system) - `--keyring-path` flag sets the keyring directory when using file backend - `--keyring-namespace` flag sets the service name when using system backend - added `keyring generate` generates required keys - `--no-encryption-key` skips generating encryption key (disables datastore encryption) - added `keyring import` import external keys (hexadecimal for now) - added `keyring export` export existing key (hexadecimal for now) - moved key generation to `crypto` package ## Tasks - [x] I made sure the code is well commented, particularly hard-to-understand areas. - [x] I made sure the repository-held documentation is changed accordingly. - [x] I made sure the pull request title adheres to the conventional commit style (the subset used in the project can be found in [tools/configs/chglog/config.yml](tools/configs/chglog/config.yml)). - [x] I made sure to discuss its limitations such as threats to validity, vulnerability to mistake and misuse, robustness to invalidation of assumptions, resource requirements, ... ## How has this been tested? Manually tested on MacOS. Specify the platform(s) on which this was tested: - MacOS
- Loading branch information
Showing
87 changed files
with
1,259 additions
and
72 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
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,25 @@ | ||
// 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 MakeKeyringCommand() *cobra.Command { | ||
var cmd = &cobra.Command{ | ||
Use: "keyring", | ||
Short: "Manage DefraDB private keys", | ||
Long: `Manage DefraDB private keys. | ||
Generate, import, and export private keys.`, | ||
} | ||
return cmd | ||
} |
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,41 @@ | ||
// 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 MakeKeyringExportCommand() *cobra.Command { | ||
var cmd = &cobra.Command{ | ||
Use: "export <name>", | ||
Short: "Export a private key", | ||
Long: `Export a private key. | ||
Prints the hexadecimal representation of a private key. | ||
Example: | ||
defradb keyring export encryption-key`, | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
keyring, err := openKeyring(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
keyBytes, err := keyring.Get(args[0]) | ||
if err != nil { | ||
return err | ||
} | ||
cmd.Printf("%x\n", keyBytes) | ||
return nil | ||
}, | ||
} | ||
return cmd | ||
} |
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,51 @@ | ||
// 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 ( | ||
"bytes" | ||
"encoding/hex" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/sourcenetwork/defradb/crypto" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestKeyringExport(t *testing.T) { | ||
rootdir := t.TempDir() | ||
readPassword = func(_ *cobra.Command, _ string) ([]byte, error) { | ||
return []byte("secret"), nil | ||
} | ||
|
||
keyBytes, err := crypto.GenerateAES256() | ||
require.NoError(t, err) | ||
keyHex := hex.EncodeToString(keyBytes) | ||
|
||
cmd := NewDefraCommand() | ||
cmd.SetArgs([]string{"keyring", "import", "--rootdir", rootdir, encryptionKeyName, keyHex}) | ||
|
||
err = cmd.Execute() | ||
require.NoError(t, err) | ||
|
||
var output bytes.Buffer | ||
cmd.SetOut(&output) | ||
cmd.SetArgs([]string{"keyring", "export", "--rootdir", rootdir, encryptionKeyName}) | ||
|
||
err = cmd.Execute() | ||
require.NoError(t, err) | ||
|
||
actualKeyHex := strings.TrimSpace(output.String()) | ||
assert.Equal(t, keyHex, actualKeyHex) | ||
} |
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,63 @@ | ||
// 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" | ||
|
||
"github.com/sourcenetwork/defradb/crypto" | ||
) | ||
|
||
func MakeKeyringGenerateCommand() *cobra.Command { | ||
var noEncryption bool | ||
var cmd = &cobra.Command{ | ||
Use: "generate", | ||
Short: "Generate private keys", | ||
Long: `Generate private keys. | ||
Randomly generate and store private keys in the keyring. | ||
WARNING: This will overwrite existing keys in the keyring. | ||
Example: | ||
defradb keyring generate | ||
Example: with no encryption key | ||
defradb keyring generate --no-encryption-key | ||
Example: with system keyring | ||
defradb keyring generate --keyring-backend system`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
keyring, err := openKeyring(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
if !noEncryption { | ||
// generate optional encryption key | ||
encryptionKey, err := crypto.GenerateAES256() | ||
if err != nil { | ||
return err | ||
} | ||
err = keyring.Set(encryptionKeyName, encryptionKey) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
peerKey, err := crypto.GenerateEd25519() | ||
if err != nil { | ||
return err | ||
} | ||
return keyring.Set(peerKeyName, peerKey) | ||
}, | ||
} | ||
cmd.Flags().BoolVar(&noEncryption, "no-encryption-key", false, | ||
"Skip generating an encryption. Encryption at rest will be disabled") | ||
return cmd | ||
} |
Oops, something went wrong.