-
Notifications
You must be signed in to change notification settings - Fork 256
Add storage backend for configs based on OS provided secret store #3333
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
10da087
Add storage backend for configs based on OS provided secret store
anjannath f61af1c
Add Secret storage to Config struct along with Viper storage
anjannath 7335240
Add a Secret type to register secret settings
anjannath 6ca0fba
Add a way to easily identify a secret setting value
anjannath 97025ac
Add boolean flag 'show-secrets' to config sub command 'view'
anjannath c64123d
Don't include Secret configs in the response from /config http api
anjannath File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,83 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| "github.com/code-ready/crc/pkg/crc/logging" | ||
| "github.com/spf13/cast" | ||
| "github.com/zalando/go-keyring" | ||
| ) | ||
|
|
||
| const secretServiceName = "crc" | ||
|
|
||
| var ErrSecretsNotAccessible = errors.New("secret store is not accessible") | ||
|
|
||
| type Secret string | ||
|
|
||
| func (s Secret) String() string { | ||
| return string(s) | ||
| } | ||
|
|
||
| type SecretStorage struct { | ||
| secretService string | ||
| storeAccessible bool | ||
| } | ||
|
|
||
| func NewSecretStorage() *SecretStorage { | ||
| return &SecretStorage{ | ||
| secretService: secretServiceName, | ||
| storeAccessible: keyringAccessible(), | ||
| } | ||
| } | ||
|
|
||
| func (c *SecretStorage) Get(key string) interface{} { | ||
| if !c.storeAccessible { | ||
| return nil | ||
| } | ||
| secret, err := keyring.Get(c.secretService, key) | ||
| if errors.Is(err, keyring.ErrNotFound) { | ||
| return nil | ||
| } | ||
| return secret | ||
| } | ||
|
|
||
| func (c *SecretStorage) Set(key string, value interface{}) error { | ||
| if !c.storeAccessible { | ||
| return ErrSecretsNotAccessible | ||
| } | ||
| secret, err := cast.ToStringE(value) | ||
| if err != nil { | ||
| return fmt.Errorf("Failed to cast secret value to string: %w", err) | ||
| } | ||
| return keyring.Set(c.secretService, key, secret) | ||
| } | ||
|
|
||
| func (c *SecretStorage) Unset(key string) error { | ||
| if !c.storeAccessible { | ||
| return ErrSecretsNotAccessible | ||
| } | ||
| err := keyring.Delete(c.secretService, key) | ||
| if errors.Is(err, keyring.ErrNotFound) { | ||
| return nil | ||
| } | ||
| return err | ||
| } | ||
|
|
||
| func keyringAccessible() bool { | ||
| err := keyring.Set("crc-test", "foo", "bar") | ||
| if err == nil { | ||
| _ = keyring.Delete("crc-test", "foo") | ||
| return true | ||
| } | ||
| logging.Debugf("Keyring is not accessible: %v", err) | ||
| return false | ||
| } | ||
|
|
||
| func NewEmptyInMemorySecretStorage() *SecretStorage { | ||
| keyring.MockInit() | ||
| return &SecretStorage{ | ||
| secretService: secretServiceName, | ||
| storeAccessible: true, | ||
| } | ||
| } |
This file contains hidden or 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,69 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| const ( | ||
| password = "password" | ||
| secret = "secret" | ||
| ) | ||
|
|
||
| func newTestConfigSecret() (*Config, error) { | ||
| cfg := New(NewEmptyInMemoryStorage(), NewEmptyInMemorySecretStorage()) | ||
anjannath marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| cfg.AddSetting(password, Secret(""), ValidateString, SuccessfullyApplied, "") | ||
| cfg.AddSetting(secret, Secret("apples"), ValidateString, SuccessfullyApplied, "") | ||
| return cfg, nil | ||
| } | ||
|
|
||
| func TestGetSecret(t *testing.T) { | ||
| cfg, err := newTestConfigSecret() | ||
| require.NoError(t, err) | ||
|
|
||
| assert.Equal(t, SettingValue{ | ||
| Value: Secret("apples"), | ||
| IsDefault: true, | ||
| IsSecret: true, | ||
| }, cfg.Get(secret)) | ||
| } | ||
|
|
||
| func TestSecretConfigUnknown(t *testing.T) { | ||
| cfg, err := newTestConfigSecret() | ||
| require.NoError(t, err) | ||
|
|
||
| assert.Equal(t, SettingValue{ | ||
| Invalid: true, | ||
| }, cfg.Get("baz")) | ||
| } | ||
|
|
||
| func TestSecretConfigSetAndGet(t *testing.T) { | ||
| cfg, err := newTestConfigSecret() | ||
| require.NoError(t, err) | ||
|
|
||
| _, err = cfg.Set(password, "pass123") | ||
| assert.NoError(t, err) | ||
|
|
||
| assert.Equal(t, SettingValue{ | ||
| Value: Secret("pass123"), | ||
| IsDefault: false, | ||
| IsSecret: true, | ||
| }, cfg.Get(password)) | ||
| } | ||
|
|
||
| func TestSecretConfigUnsetAndGet(t *testing.T) { | ||
| cfg, err := newTestConfigSecret() | ||
| require.NoError(t, err) | ||
|
|
||
| _, err = cfg.Unset(secret) | ||
| assert.NoError(t, err) | ||
|
|
||
| assert.Equal(t, SettingValue{ | ||
| Value: Secret("apples"), | ||
| IsDefault: true, | ||
| IsSecret: true, | ||
| }, cfg.Get(secret)) | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.