Skip to content

Commit 6feccd1

Browse files
author
Rahman
committed
Init source
1 parent 5eb345a commit 6feccd1

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed

go.mod

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module kustomize-aws-ssm-secret-generator-plugin
2+
3+
go 1.12
4+
5+
require (
6+
github.com/aws/aws-sdk-go v1.19.26
7+
github.com/stretchr/testify v1.3.0 // indirect
8+
golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c // indirect
9+
)

go.sum

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
github.com/aws/aws-sdk-go v1.19.26 h1:GavKlzJDfYQGoS4jn2F+KYYZlR8QEhrLPfpf8+oJhS4=
2+
github.com/aws/aws-sdk-go v1.19.26/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
3+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
4+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5+
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM=
6+
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
7+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
8+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
9+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
10+
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
11+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
12+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
13+
golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c h1:uOCk1iQW6Vc18bnC13MfzScl+wdKBmM9Y9kU7Z83/lw=
14+
golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
15+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
16+
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
17+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

kvMaker.go

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"reflect"
6+
"strings"
7+
8+
"github.com/aws/aws-sdk-go/aws"
9+
"github.com/aws/aws-sdk-go/aws/credentials"
10+
"github.com/aws/aws-sdk-go/aws/session"
11+
"github.com/aws/aws-sdk-go/service/ssm"
12+
)
13+
14+
type plugin struct{}
15+
16+
var KVSource plugin
17+
18+
type options struct {
19+
AwsRegion string `alias:"AWS_REGION"`
20+
AwsAccessKeyID string `alias:"AWS_ACCESS_KEY_ID"`
21+
AwsSecretAccessKey string `alias:"AWS_SECRET_ACCESS_KEY"`
22+
AwsSessionToken string `alias:"AWS_SESSION_TOKEN"`
23+
AwsParameterStorePath string `alias:"AWS_SSM_PATH"`
24+
25+
UppercaseKeys string `alias:"UPPERCASE_KEY"`
26+
}
27+
28+
func (p plugin) Get(root string, args []string) (map[string]string, error) {
29+
r := make(map[string]string)
30+
opts := &options{}
31+
cfg := &aws.Config{}
32+
opts.parseArgs(&args)
33+
34+
if opts.AwsParameterStorePath == "" {
35+
return r, fmt.Errorf("AWS_SSM_PATH is required")
36+
}
37+
38+
if opts.AwsRegion != "" {
39+
cfg.Region = aws.String(opts.AwsRegion)
40+
}
41+
42+
if opts.AwsAccessKeyID != "" && opts.AwsSecretAccessKey != "" {
43+
staticCreds := credentials.NewStaticCredentials(opts.AwsAccessKeyID, opts.AwsSecretAccessKey, opts.AwsSessionToken)
44+
cfg.WithCredentials(staticCreds)
45+
}
46+
47+
sess, err := session.NewSession(cfg)
48+
if err != nil {
49+
return nil, err
50+
}
51+
52+
svc := ssm.New(sess)
53+
54+
getParamsInput := &ssm.GetParametersByPathInput{
55+
Path: aws.String(opts.AwsParameterStorePath),
56+
WithDecryption: aws.Bool(true),
57+
}
58+
59+
for {
60+
resp, err := svc.GetParametersByPath(getParamsInput)
61+
62+
if err != nil {
63+
return nil, err
64+
}
65+
66+
params := resp.Parameters
67+
for _, p := range params {
68+
name := sanitizeKey(p.Name, opts.UppercaseKeys == "true")
69+
r[name] = sanitizeValue(p.Value)
70+
}
71+
72+
nextToken := resp.NextToken
73+
if nextToken == nil {
74+
break
75+
}
76+
}
77+
78+
return r, nil
79+
}
80+
81+
func sanitizeKey(path *string, ensureUppercase bool) string {
82+
pathBrokenDown := strings.Split(aws.StringValue(path), "/")
83+
name := pathBrokenDown[len(pathBrokenDown)-1]
84+
85+
if ensureUppercase {
86+
name = strings.ToUpper(name)
87+
}
88+
89+
return name
90+
}
91+
92+
func sanitizeValue(path *string) string {
93+
return aws.StringValue(path)
94+
}
95+
96+
func (opts *options) parseArgs(args *[]string) error {
97+
ov := reflect.ValueOf(opts).Elem()
98+
typeOfOpts := ov.Type()
99+
100+
for _, arg := range *args {
101+
argKeyValuePair := strings.Split(arg, "=")
102+
if len(argKeyValuePair) == 2 {
103+
for i := 0; i < ov.NumField(); i++ {
104+
field := ov.Field(i)
105+
fieldTag := typeOfOpts.Field(i).Tag
106+
107+
if argKeyValuePair[0] == fieldTag.Get("alias") {
108+
field.SetString(argKeyValuePair[1])
109+
}
110+
}
111+
}
112+
}
113+
114+
return nil
115+
}

0 commit comments

Comments
 (0)