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 context to SecretProvider interface #10

Merged
merged 7 commits into from
Aug 4, 2020
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
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ A list of secret stores currently supported:
package main

import (
"context"
"os"

"github.com/wingocard/serum"
Expand All @@ -46,7 +47,7 @@ import (

func main() {
//create a new secret provider
gsm, err := gsmanager.New()
gsm, err := gsmanager.New(context.Background())
if err != nil {
//...
}
Expand All @@ -65,7 +66,7 @@ func main() {
}

//Inject the serum...
if err := ij.Inject(); err != nil {
if err := ij.Inject(context.Background()); err != nil {
//...
}

Expand All @@ -77,4 +78,4 @@ func main() {
## Running Tests

Run all tests using the Makefile:
`make tests`
`make tests`
8 changes: 4 additions & 4 deletions secretprovider/gsmanager/gsmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ type GSManager struct {
}

// New return's an initialized GSManager using a new secret manager client.
func New() (*GSManager, error) {
c, err := secretmanager.NewClient(context.Background())
func New(ctx context.Context) (*GSManager, error) {
c, err := secretmanager.NewClient(ctx)
if err != nil {
return nil, fmt.Errorf("gsmanager: failed to initialize client: %w", err)
}
Expand All @@ -32,12 +32,12 @@ func New() (*GSManager, error) {
}

// Decrypt will access the secret on GCP Secret Manager and return the plain text string.
func (g *GSManager) Decrypt(secret string) (string, error) {
func (g *GSManager) Decrypt(ctx context.Context, secret string) (string, error) {
req := &secretmanagerpb.AccessSecretVersionRequest{
Name: secret,
}

result, err := g.smClient.AccessSecretVersion(context.Background(), req)
result, err := g.smClient.AccessSecretVersion(ctx, req)
if err != nil {
return "", fmt.Errorf("gsmanager: failed to access secret version: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion secretprovider/gsmanager/gsmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestDecrypt(t *testing.T) {
smClient: tc,
}

dec, err := gsm.Decrypt(secretIdentifier)
dec, err := gsm.Decrypt(context.Background(), secretIdentifier)
g.Expect(err).To(BeNil())
g.Expect(tc.accessSecretVersionCalled).To(BeTrue())
g.Expect(dec).To(Equal(string(decrypted)))
Expand Down
4 changes: 3 additions & 1 deletion secretprovider/secretprovider.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package secretprovider

import "context"

//SecretProvider is an interface that wraps the decrypt and close methods.
//Close should be called when the secret provier is no longer needed.
//It may be a no-op in cases where there's no underlying connection to be closed.
type SecretProvider interface {
Decrypt(secret string) (string, error)
Decrypt(ctx context.Context, secret string) (string, error)
Close() error
}
7 changes: 4 additions & 3 deletions serum.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package serum

import (
"context"
"fmt"
"os"

Expand All @@ -16,16 +17,16 @@ type Injector struct {
}

// Inject will inject the loaded environment variables into the current running process' environment.
// Any secret values found will attempt to be decrypted using the provided secret provider.
// Any secret values found will attempt to be decrypted using the provided SecretProvider.
// The presence of secrets with a nil SecretProvider will return an error.
func (in *Injector) Inject() error {
func (in *Injector) Inject(ctx context.Context) error {
if len(in.envVars.Secrets) > 0 && in.SecretProvider == nil {
return fmt.Errorf("serum: error injecting env vars: secrets were loaded but the SecretProvider is nil")
}

// inject secrets
for k, v := range in.envVars.Secrets {
decrypted, err := in.SecretProvider.Decrypt(v)
decrypted, err := in.SecretProvider.Decrypt(ctx, v)
if err != nil {
return fmt.Errorf("serum: error decrypting secret %s: %s", v, err)
}
Expand Down
11 changes: 6 additions & 5 deletions serum_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package serum

import (
"context"
"errors"
"os"
"testing"
Expand Down Expand Up @@ -30,7 +31,7 @@ type testSecretProvider struct {
returnErr error
}

func (ts *testSecretProvider) Decrypt(secret string) (string, error) {
func (ts *testSecretProvider) Decrypt(ctx context.Context, secret string) (string, error) {
if ts.returnErr != nil {
return "", ts.returnErr
}
Expand Down Expand Up @@ -68,7 +69,7 @@ func TestInject(t *testing.T) {
},
}

err := ij.Inject()
err := ij.Inject(context.Background())
g.Expect(err).To(BeNil())

for k, v := range envVars.Plain {
Expand Down Expand Up @@ -113,7 +114,7 @@ func TestInjectEnvError(t *testing.T) {
},
}

err := ij.Inject()
err := ij.Inject(context.Background())
g.Expect(err).ToNot(BeNil())
g.Expect(err.Error()).To(ContainSubstring("serum: error setting env var"))
})
Expand All @@ -133,7 +134,7 @@ func TestInjectNilSecretProviderError(t *testing.T) {
envVars: envVars,
}

err := ij.Inject()
err := ij.Inject(context.Background())
g.Expect(err).ToNot(BeNil())
g.Expect(err.Error()).
To(ContainSubstring("serum: error injecting env vars: secrets were loaded but the SecretProvider is nil"))
Expand All @@ -155,7 +156,7 @@ func TestInjectDecryptError(t *testing.T) {
},
}

err := ij.Inject()
err := ij.Inject(context.Background())
g.Expect(err).ToNot(BeNil())
g.Expect(err.Error()).To(ContainSubstring("serum: error decrypting secret"))
}
Expand Down