From 892552aeba0a9a3f8594bcd185681a54bd3e178d Mon Sep 17 00:00:00 2001 From: qbart Date: Sat, 24 Jun 2023 14:52:34 +0200 Subject: [PATCH] Specify multiple paths to fetch for SSM --- awscmd/ssm.go | 31 +++++++++++++++++-------------- main.go | 16 +++++++++++----- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/awscmd/ssm.go b/awscmd/ssm.go index 897b15a..644816f 100644 --- a/awscmd/ssm.go +++ b/awscmd/ssm.go @@ -10,7 +10,7 @@ import ( ) type InputSSMGetParameters struct { - Path string + Paths []string Region string } @@ -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 diff --git a/main.go b/main.go index 6b0f53b..6adb205 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "embed" "fmt" "os" + "strings" "time" "github.com/Selleo/cli/awscmd" @@ -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) @@ -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 {