Skip to content

Commit

Permalink
Make secret backend configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Fuentes committed Aug 2, 2018
1 parent dcb44cf commit bf4dbb4
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func delete(cmd *cobra.Command, args []string) error {
return errors.Wrap(err, "Failed to validate key")
}

secretStore := store.NewSSMStore(numRetries)
secretStore := getSecretStore()
secretId := store.SecretId{
Service: service,
Key: key,
Expand Down
3 changes: 1 addition & 2 deletions cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"strings"

"github.com/pkg/errors"
"github.com/segmentio/chamber/store"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -39,7 +38,7 @@ func execRun(cmd *cobra.Command, args []string) error {
services, command, commandArgs := args[:dashIx], args[dashIx], args[dashIx+1:]

env := environ(os.Environ())
secretStore := store.NewSSMStore(numRetries)
secretStore := getSecretStore()
for _, service := range services {
if err := validateService(service); err != nil {
return errors.Wrap(err, "Failed to validate service")
Expand Down
3 changes: 1 addition & 2 deletions cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

"github.com/magiconair/properties"
"github.com/pkg/errors"
"github.com/segmentio/chamber/store"
"github.com/spf13/cobra"
)

Expand All @@ -38,7 +37,7 @@ func init() {
func runExport(cmd *cobra.Command, args []string) error {
var err error

secretStore := store.NewSSMStore(numRetries)
secretStore := getSecretStore()
params := make(map[string]string)
for _, service := range args {
if err := validateService(service); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func history(cmd *cobra.Command, args []string) error {
return errors.Wrap(err, "Failed to validate key")
}

secretStore := store.NewSSMStore(numRetries)
secretStore := getSecretStore()
secretId := store.SecretId{
Service: service,
Key: key,
Expand Down
2 changes: 1 addition & 1 deletion cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func importRun(cmd *cobra.Command, args []string) error {
return errors.Wrap(err, "Failed to decode input as json")
}

secretStore := store.NewSSMStore(numRetries)
secretStore := getSecretStore()

for key, value := range toBeImported {
secretId := store.SecretId{
Expand Down
3 changes: 1 addition & 2 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"text/tabwriter"

"github.com/pkg/errors"
"github.com/segmentio/chamber/store"
"github.com/spf13/cobra"
)

Expand All @@ -34,7 +33,7 @@ func list(cmd *cobra.Command, args []string) error {
return errors.Wrap(err, "Failed to validate service")
}

secretStore := store.NewSSMStore(numRetries)
secretStore := getSecretStore()
secrets, err := secretStore.List(service, withValues)
if err != nil {
return errors.Wrap(err, "Failed to list store contents")
Expand Down
3 changes: 2 additions & 1 deletion cmd/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ func read(cmd *cobra.Command, args []string) error {
return errors.Wrap(err, "Failed to validate key")
}

secretStore := store.NewSSMStore(numRetries)
secretStore := getSecretStore()

secretId := store.SecretId{
Service: service,
Key: key,
Expand Down
42 changes: 42 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"regexp"
"strings"

"github.com/segmentio/chamber/store"
"github.com/spf13/cobra"
)

Expand All @@ -26,6 +27,15 @@ const (
DefaultNumRetries = 10
)

const (
SSMBackend = "SSM"
S3Backend = "S3"

BackendEnvVar = "CHAMBER_SECRET_BACKEND"
)

var Backends = []string{SSMBackend, S3Backend}

// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: "chamber",
Expand Down Expand Up @@ -62,3 +72,35 @@ func validateKey(key string) error {
}
return nil
}

func getSecretStore() store.Store {
backend, ok := os.LookupEnv(BackendEnvVar)
if !ok {
backend = SSMBackend
}

backend = strings.ToUpper(backend)
if !stringInSlice(backend, Backends) {
// TODO: warn user
backend = SSMBackend
}

switch backend {
case SSMBackend:
return store.NewSSMStore(numRetries)
case S3Backend:
return store.NewS3Store(numRetries)
}

// This line is unreachable, but necessary to satisfy the compiler
panic("unreachable")
}

func stringInSlice(v string, sl []string) bool {
for _, val := range sl {
if v == val {
return true
}
}
return false
}
2 changes: 1 addition & 1 deletion cmd/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func write(cmd *cobra.Command, args []string) error {
}
}

secretStore := store.NewSSMStore(numRetries)
secretStore := getSecretStore()
secretId := store.SecretId{
Service: service,
Key: key,
Expand Down

0 comments on commit bf4dbb4

Please sign in to comment.