-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a secret storage that will allow a primary and secondary store. The secondary store is used only if the primary is broken for non-expected reasons. Notably, LoadSecret returning ErrNotFound is not an unexpected breakage.
- Loading branch information
Showing
2 changed files
with
74 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package secret | ||
|
||
import "errors" | ||
|
||
// FallbackStash is a secret stash that falls back to a secondary stash | ||
// if the primary stash fails. | ||
type FallbackStash struct { | ||
Primary, Secondary Stash // required | ||
} | ||
|
||
// SaveSecret saves a secret to the primary stash. | ||
// If the operation fails, it falls back to the secondary stash. | ||
func (f *FallbackStash) SaveSecret(service, key, secret string) error { | ||
if err := f.Primary.SaveSecret(service, key, secret); err != nil { | ||
return f.Secondary.SaveSecret(service, key, secret) | ||
} | ||
return nil | ||
} | ||
|
||
// LoadSecret loads a secret from the primary stash. | ||
// If the operation fails NOT because the secret is not found, | ||
// it falls back to the secondary stash. | ||
func (f *FallbackStash) LoadSecret(service, key string) (string, error) { | ||
secret, err := f.Primary.LoadSecret(service, key) | ||
if err != nil && !errors.Is(err, ErrNotFound) { | ||
secret, err = f.Secondary.LoadSecret(service, key) | ||
} | ||
return secret, err | ||
} | ||
|
||
// DeleteSecret deletes a secret from the primary stash, | ||
// and if that fails, from the secondary stash. | ||
func (f *FallbackStash) DeleteSecret(service, key string) error { | ||
if err := f.Primary.DeleteSecret(service, key); err != nil { | ||
return f.Secondary.DeleteSecret(service, key) | ||
} | ||
return nil | ||
} |
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