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

add secretstore.Plaintext convenience function #143

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## Unreleased

### Added

- secretstore: add Plaintext toplevel convenience function

## 1.3.3 (2024-09-12)

Expand Down
20 changes: 2 additions & 18 deletions _examples/secret-store/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,16 @@ import (

func main() {
fsthttp.ServeFunc(func(ctx context.Context, w fsthttp.ResponseWriter, r *fsthttp.Request) {
st, err := secretstore.Open("example_secretstore")
v, err := secretstore.Plaintext("example_secretstore", "my_secret")
switch {
case errors.Is(err, secretstore.ErrSecretStoreNotFound):
case errors.Is(err, secretstore.ErrSecretStoreNotFound) || errors.Is(err, secretstore.ErrSecretNotFound):
fsthttp.Error(w, err.Error(), fsthttp.StatusNotFound)
return
case err != nil:
fsthttp.Error(w, err.Error(), fsthttp.StatusBadGateway)
return
}

s, err := st.Get("my_secret")
switch {
case errors.Is(err, secretstore.ErrSecretNotFound):
fsthttp.Error(w, err.Error(), fsthttp.StatusNotFound)
return
case err != nil:
fsthttp.Error(w, err.Error(), fsthttp.StatusBadGateway)
return
}

v, err := s.Plaintext()
if err != nil {
fsthttp.Error(w, err.Error(), fsthttp.StatusBadGateway)
return
}

fmt.Fprintf(w, "secret value: %q", v)
})
}
12 changes: 1 addition & 11 deletions integration_tests/secret_store/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,7 @@ import (
)

func TestSecretStore(t *testing.T) {
st, err := secretstore.Open("phrases")
if err != nil {
t.Fatal(err)
}

s, err := st.Get("my_phrase")
if err != nil {
t.Fatal(err)
}

v, err := s.Plaintext()
v, err := secretstore.Plaintext("phrases", "my_phrase")
if err != nil {
t.Fatal(err)
}
Expand Down
19 changes: 19 additions & 0 deletions secretstore/secretstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,22 @@ func SecretFromBytes(b []byte) (*Secret, error) {

return &Secret{s: s}, nil
}

// Plaintext decrypts and returns the secret value as a byte slice for
// the given secret store and secret name.
//
// This is a convenience function that combines the functionality of
// [Open], [Store.Get], and [Secret.Plaintext].
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to add a caveat if that if you're obtaining multiple secrets from a store to actually call Open/Get/Plaintext etc instead of calling this repeatedly?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO we can skip it, but I'll add it if you feel strongly about it. There's very little overhead, basically just the open hostcall itself and copying a handle or error back into guest space.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, 🚲 🏠 ⚠️ but I prefer phrasing it as returns the secret value as a []byte for

func Plaintext(storeName, secretName string) ([]byte, error) {
st, err := Open(storeName)
if err != nil {
return nil, err
}

s, err := st.Get(secretName)
if err != nil {
return nil, err
}

return s.Plaintext()
}