Skip to content

Commit

Permalink
feat: add ability to push secret
Browse files Browse the repository at this point in the history
  • Loading branch information
adikari committed Oct 2, 2022
1 parent 81ba81e commit 23337c6
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 8 deletions.
28 changes: 27 additions & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package cmd

import (
"fmt"
"log"

"github.com/adikari/safebox/v2/store"
"github.com/manifoldco/promptui"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
Expand All @@ -22,7 +24,7 @@ var (

func init() {
rootCmd.AddCommand(deployCmd)
deployCmd.Flags().BoolVarP(&removeOrphans, "remove-orphans", "r", true, "remove orphan configurations")
deployCmd.Flags().BoolVarP(&removeOrphans, "remove-orphans", "r", false, "remove orphan configurations")
deployCmd.Flags().StringVarP(&prompt, "prompt", "p", "missing", "prompt for configurations (missing or all)")
}

Expand All @@ -47,6 +49,12 @@ func deploy(cmd *cobra.Command, args []string) error {
return errors.Wrap(err, "failed to instantiate store")
}

for i, c := range config.Configs {
if c.Value == "" {
config.Configs[i].Value = promptConfig(c)
}
}

err = store.PutMany(config.Configs)

if err != nil {
Expand All @@ -55,3 +63,21 @@ func deploy(cmd *cobra.Command, args []string) error {

return nil
}

func promptConfig(config store.ConfigInput) string {
validate := func(input string) error {
if len(input) < 1 {
return fmt.Errorf("%s must not be empty", config.Name)
}
return nil
}

prompt := promptui.Prompt{
Label: config.Name,
Validate: validate,
}

result, _ := prompt.Run()

return result
}
9 changes: 9 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type rawConfig struct {
Service string

Config map[string]map[string]string
Secret map[string]string
}

type Config struct {
Expand Down Expand Up @@ -85,6 +86,14 @@ func parseConfig(rc rawConfig, c *Config, param LoadParam) {
Secret: false,
})
}

for key, value := range rc.Secret {
c.Configs = append(c.Configs, store.ConfigInput{
Name: key,
Description: value,
Secret: true,
})
}
}

func formatSharedPath(stage string, key string) string {
Expand Down
3 changes: 3 additions & 0 deletions example/safebox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ config:
SHARED_KEY: "shared key"


secret:
API_KEY: "key of the api endpoint"
DB_SECRET: "database secret"
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ go 1.19

require (
github.com/aws/aws-sdk-go v1.44.107
github.com/manifoldco/promptui v0.9.0
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.5.0
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
github.com/aws/aws-sdk-go v1.44.107 h1:VP7Rq3wzsOV7wrfHqjAAKRksD4We58PaoVSDPKhm8nw=
github.com/aws/aws-sdk-go v1.44.107/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo=
github.com/chzyer/logex v1.1.10 h1:Swpa1K6QvQznwJRcfTfQJmTE72DqScAa40E+fbHEXEE=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5OhCuC+XN+r/bBCmeuuJtjz+bCNIf8=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1 h1:q763qf9huN11kDQavWsoZXJNW3xEE4JJyHa5Q25/sd8=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -10,6 +16,8 @@ github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9Y
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/manifoldco/promptui v0.9.0 h1:3V4HzJk1TtXW1MTZMP7mdlwbBpIinw3HztaIlYthEiA=
github.com/manifoldco/promptui v0.9.0/go.mod h1:ka04sppxSGFAtxX0qhlYQjISsg9mR4GWtQEhdbn6Pgg=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -21,7 +29,9 @@ github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/sys v0.0.0-20181122145206-62eef0e2fa9b/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e h1:fLOSk5Q00efkSvAm+4xcoXD+RRmLmmulPn5I3Y9F2EM=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
Expand Down
9 changes: 5 additions & 4 deletions store/ssmstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ func (s *SSMStore) Put(input ConfigInput) error {
}

putParameterInput := &ssm.PutParameterInput{
Name: aws.String(input.Name),
Type: aws.String(configType),
Value: aws.String(input.Value),
Overwrite: aws.Bool(true),
Name: aws.String(input.Name),
Type: aws.String(configType),
Value: aws.String(input.Value),
Description: aws.String(input.Description),
Overwrite: aws.Bool(true),
}

_, err := s.svc.PutParameter(putParameterInput)
Expand Down
7 changes: 4 additions & 3 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ const (
)

type ConfigInput struct {
Name string
Value string
Secret bool
Name string
Value string
Secret bool
Description string
}

var (
Expand Down

0 comments on commit 23337c6

Please sign in to comment.