Skip to content

Commit

Permalink
Specify multiple paths to fetch for SSM
Browse files Browse the repository at this point in the history
  • Loading branch information
qbart committed Jun 24, 2023
1 parent 91bf67b commit 892552a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
31 changes: 17 additions & 14 deletions awscmd/ssm.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

type InputSSMGetParameters struct {
Path string
Paths []string
Region string
}

Expand All @@ -31,20 +31,23 @@ func SSMGetParameters(ctx context.Context, input *InputSSMGetParameters) (*Outpu
return nil, err
}
svc := ssm.New(sess)
err = svc.GetParametersByPathPagesWithContext(ctx, &ssm.GetParametersByPathInput{
Path: aws.String(fmt.Sprint(input.Path, "/")),
WithDecryption: aws.Bool(true),
}, func(o *ssm.GetParametersByPathOutput, lastPage bool) bool {
for _, v := range o.Parameters {
splits := strings.Split(*v.Name, "/") // split "a/b/c/ENV" by "/" to extract ENV
env := splits[len(splits)-1]
value := *v.Value
parameters[env] = string(value)

for _, path := range input.Paths {
err = svc.GetParametersByPathPagesWithContext(ctx, &ssm.GetParametersByPathInput{
Path: aws.String(fmt.Sprint(path, "/")),
WithDecryption: aws.Bool(true),
}, func(o *ssm.GetParametersByPathOutput, lastPage bool) bool {
for _, v := range o.Parameters {
splits := strings.Split(*v.Name, "/") // split "a/b/c/ENV" by "/" to extract ENV
env := splits[len(splits)-1]
value := *v.Value
parameters[env] = string(value)
}
return !lastPage
})
if err != nil {
return nil, err
}
return !lastPage
})
if err != nil {
return nil, err
}

return out, nil
Expand Down
16 changes: 11 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"embed"
"fmt"
"os"
"strings"
"time"

"github.com/Selleo/cli/awscmd"
Expand Down Expand Up @@ -194,18 +195,23 @@ func main() {
Usage: "Start a service with SSM secrets",
Flags: []cli.Flag{
&cli.StringFlag{Name: "region", Usage: "AWS region", Required: true},
&cli.StringFlag{Name: "path", Usage: "SSM Path", Required: true},
&cli.StringSliceFlag{Name: "path", Usage: "Store Parameters Path (multiple use of flag allowed)", Required: true},
},
Action: func(c *cli.Context) error {
input := &awscmd.InputSSMGetParameters{
Region: c.String("region"),
Path: c.String("path"),
Paths: c.StringSlice("path"),
}
paths := strings.Builder{}
for _, p := range input.Paths {
paths.WriteString(p)
paths.WriteString("/*\n")
}
fmt.Fprintf(
c.App.Writer,
"%sFetching secrets %s%s\n",
ctc.ForegroundYellow,
fmt.Sprint(input.Path, "/*"),
paths.String(),
ctc.Reset,
)
out, err := awscmd.SSMGetParameters(context.TODO(), input)
Expand All @@ -220,12 +226,12 @@ func main() {
Usage: "Export SSM",
Flags: []cli.Flag{
&cli.StringFlag{Name: "region", Usage: "AWS region", Required: true},
&cli.StringFlag{Name: "path", Usage: "SSM Path", Required: true},
&cli.StringSliceFlag{Name: "path", Usage: "Store Parameters Path (multiple use of flag allowed)", Required: true},
},
Action: func(c *cli.Context) error {
input := &awscmd.InputSSMGetParameters{
Region: c.String("region"),
Path: c.String("path"),
Paths: c.StringSlice("path"),
}
out, err := awscmd.SSMGetParameters(context.TODO(), input)
if err != nil {
Expand Down

0 comments on commit 892552a

Please sign in to comment.