Skip to content

Commit 0f6e1c6

Browse files
committed
Go version of parameters script
1 parent 7337591 commit 0f6e1c6

3 files changed

Lines changed: 212 additions & 0 deletions

File tree

scripts/sources/parameters/go.mod

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module github.com/bugout-dev/checkenv/scripts/sources/parameters
2+
3+
go 1.17
4+
5+
require (
6+
github.com/aws/aws-sdk-go-v2/config v1.11.0
7+
github.com/aws/aws-sdk-go-v2/service/ssm v1.17.1
8+
)
9+
10+
require (
11+
github.com/aws/aws-sdk-go-v2 v1.11.2 // indirect
12+
github.com/aws/aws-sdk-go-v2/credentials v1.6.4 // indirect
13+
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.8.2 // indirect
14+
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.2 // indirect
15+
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.0.2 // indirect
16+
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.2 // indirect
17+
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.5.2 // indirect
18+
github.com/aws/aws-sdk-go-v2/service/sso v1.6.2 // indirect
19+
github.com/aws/aws-sdk-go-v2/service/sts v1.11.1 // indirect
20+
github.com/aws/smithy-go v1.9.0 // indirect
21+
github.com/jmespath/go-jmespath v0.4.0 // indirect
22+
)

scripts/sources/parameters/go.sum

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
github.com/aws/aws-sdk-go-v2 v1.11.2 h1:SDiCYqxdIYi6HgQfAWRhgdZrdnOuGyLDJVRSWLeHWvs=
2+
github.com/aws/aws-sdk-go-v2 v1.11.2/go.mod h1:SQfA+m2ltnu1cA0soUkj4dRSsmITiVQUJvBIZjzfPyQ=
3+
github.com/aws/aws-sdk-go-v2/config v1.11.0 h1:Czlld5zBB61A3/aoegA9/buZulwL9mHHfizh/Oq+Kqs=
4+
github.com/aws/aws-sdk-go-v2/config v1.11.0/go.mod h1:VrQDJGFBM5yZe+IOeenNZ/DWoErdny+k2MHEIpwDsEY=
5+
github.com/aws/aws-sdk-go-v2/credentials v1.6.4 h1:2hvbUoHufns0lDIsaK8FVCMukT1WngtZPavN+W2FkSw=
6+
github.com/aws/aws-sdk-go-v2/credentials v1.6.4/go.mod h1:tTrhvBPHyPde4pdIPSba4Nv7RYr4wP9jxXEDa1bKn/8=
7+
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.8.2 h1:KiN5TPOLrEjbGCvdTQR4t0U4T87vVwALZ5Bg3jpMqPY=
8+
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.8.2/go.mod h1:dF2F6tXEOgmW5X1ZFO/EPtWrcm7XkW07KNcJUGNtt4s=
9+
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.2 h1:XJLnluKuUxQG255zPNe+04izXl7GSyUVafIsgfv9aw4=
10+
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.2/go.mod h1:SgKKNBIoDC/E1ZCDhhMW3yalWjwuLjMcpLzsM/QQnWo=
11+
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.0.2 h1:EauRoYZVNPlidZSZJDscjJBQ22JhVF2+tdteatax2Ak=
12+
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.0.2/go.mod h1:xT4XX6w5Sa3dhg50JrYyy3e4WPYo/+WjY/BXtqXVunU=
13+
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.2 h1:IQup8Q6lorXeiA/rK72PeToWoWK8h7VAPgHNWdSrtgE=
14+
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.2/go.mod h1:VITe/MdW6EMXPb0o0txu/fsonXbMHUU2OC2Qp7ivU4o=
15+
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.5.2 h1:CKdUNKmuilw/KNmO2Q53Av8u+ZyXMC2M9aX8Z+c/gzg=
16+
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.5.2/go.mod h1:FgR1tCsn8C6+Hf+N5qkfrE4IXvUL1RgW87sunJ+5J4I=
17+
github.com/aws/aws-sdk-go-v2/service/ssm v1.17.1 h1:E/2WewR1wegBnthK8Yz+E87E8Mm4RJC/7R6vg6oAfl0=
18+
github.com/aws/aws-sdk-go-v2/service/ssm v1.17.1/go.mod h1:jqRk4h1lv2pV4G1DTYRj71JIMEoU/gEGvLU5O6ZnpLM=
19+
github.com/aws/aws-sdk-go-v2/service/sso v1.6.2 h1:2IDmvSb86KT44lSg1uU4ONpzgWLOuApRl6Tg54mZ6Dk=
20+
github.com/aws/aws-sdk-go-v2/service/sso v1.6.2/go.mod h1:KnIpszaIdwI33tmc/W/GGXyn22c1USYxA/2KyvoeDY0=
21+
github.com/aws/aws-sdk-go-v2/service/sts v1.11.1 h1:QKR7wy5e650q70PFKMfGF9sTo0rZgUevSSJ4wxmyWXk=
22+
github.com/aws/aws-sdk-go-v2/service/sts v1.11.1/go.mod h1:UV2N5HaPfdbDpkgkz4sRzWCvQswZjdO1FfqCWl0t7RA=
23+
github.com/aws/smithy-go v1.9.0 h1:c7FUdEqrQA1/UVKKCNDFQPNKGp4FQg3YW4Ck5SLTG58=
24+
github.com/aws/smithy-go v1.9.0/go.mod h1:SObp3lf9smib00L/v3U2eAKG8FyQ7iLrJnQiAmR5n+E=
25+
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
26+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
27+
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
28+
github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
29+
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
30+
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
31+
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
32+
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
33+
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
34+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
35+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
36+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
37+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
38+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
39+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
40+
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
41+
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

scripts/sources/parameters/main.go

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/*
2+
Based on: https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/gov2/ssm/GetParameter/GetParameterv2.go
3+
*/
4+
package main
5+
6+
import (
7+
"context"
8+
"flag"
9+
"fmt"
10+
"log"
11+
12+
"github.com/aws/aws-sdk-go-v2/config"
13+
"github.com/aws/aws-sdk-go-v2/service/ssm"
14+
"github.com/aws/aws-sdk-go-v2/service/ssm/types"
15+
)
16+
17+
// SSMGetParametersAPI defines the interface for the GetParameters function.
18+
// We use this interface to test the function using a mocked service.
19+
type SSMGetParametersAPI interface {
20+
GetParameters(
21+
ctx context.Context,
22+
params *ssm.GetParametersInput,
23+
optFns ...func(*ssm.Options),
24+
) (*ssm.GetParametersOutput, error)
25+
}
26+
27+
type SSMDescribeParametersAPI interface {
28+
DescribeParameters(
29+
ctx context.Context,
30+
params *ssm.DescribeParametersInput,
31+
optFns ...func(*ssm.Options),
32+
) (*ssm.DescribeParametersOutput, error)
33+
}
34+
35+
// FindParameters retrieves an AWS Systems Manager string parameter
36+
// Inputs:
37+
// c is the context of the method call, which includes the AWS Region
38+
// api is the interface that defines the method call
39+
// input defines the input arguments to the service call.
40+
// Output:
41+
// If success, a GetParametersOutput object containing the result of the service call and nil
42+
// Otherwise, nil and an error from the call to GetParameter
43+
func FindParameters(c context.Context, api SSMGetParametersAPI, input *ssm.GetParametersInput) (*ssm.GetParametersOutput, error) {
44+
return api.GetParameters(c, input)
45+
}
46+
47+
func FindParameterKeys(c context.Context, api SSMDescribeParametersAPI, input *ssm.DescribeParametersInput) (*ssm.DescribeParametersOutput, error) {
48+
return api.DescribeParameters(c, input)
49+
}
50+
51+
// Split list of reports on nested lists
52+
func generateChunks(flatSlice []string, chunkSize int) [][]string {
53+
if len(flatSlice) == 0 {
54+
return nil
55+
}
56+
57+
chunks := make([][]string, 0, len(flatSlice)/chunkSize+1)
58+
59+
for i, v := range flatSlice {
60+
if i%chunkSize == 0 {
61+
chunks = append(chunks, make([]string, 0, chunkSize))
62+
}
63+
chunks[len(chunks)-1] = append(chunks[len(chunks)-1], v)
64+
}
65+
66+
return chunks
67+
}
68+
69+
type Parameter struct {
70+
Name string
71+
Value string
72+
}
73+
74+
func main() {
75+
var maxResults int
76+
var productTag string
77+
flag.IntVar(&maxResults, "max", 3, "The maximum number of items to return for call to AWS")
78+
flag.StringVar(&productTag, "product", "", "Product tag")
79+
flag.Parse()
80+
81+
if productTag == "" {
82+
log.Fatalln("Please specify the tag of product")
83+
}
84+
85+
// Load the Shared AWS Configuration (~/.aws/config)
86+
cfg, err := config.LoadDefaultConfig(context.TODO())
87+
if err != nil {
88+
log.Fatal(err)
89+
}
90+
client := ssm.NewFromConfig(cfg)
91+
92+
// Set parameter filters
93+
filterKey := "tag:Product"
94+
parameterFilters := []types.ParameterStringFilter{
95+
{
96+
Key: &filterKey,
97+
Values: []string{productTag},
98+
},
99+
}
100+
describeInput := &ssm.DescribeParametersInput{
101+
MaxResults: int32(maxResults),
102+
ParameterFilters: parameterFilters,
103+
}
104+
105+
var parameterKeys []string
106+
107+
n := 0
108+
for {
109+
// Fetch list of parameter keys
110+
results, err := FindParameterKeys(context.TODO(), client, describeInput)
111+
if err != nil {
112+
log.Fatal(err)
113+
}
114+
for _, p := range results.Parameters {
115+
parameterKeys = append(parameterKeys, *p.Name)
116+
}
117+
118+
// If there are no more parameters break
119+
if *&results.NextToken == nil {
120+
break
121+
}
122+
describeInput.NextToken = *&results.NextToken
123+
124+
n++
125+
if n >= 10 {
126+
log.Fatal("To many iterations over DescribeParameters loop")
127+
}
128+
}
129+
130+
var parameters []Parameter
131+
132+
// Split slice of parameter keys to chunks by 10 (max len allowed by AWS)
133+
// and fetch values for required parameters
134+
parameterKeyChunks := generateChunks(parameterKeys, 10)
135+
for _, chunk := range parameterKeyChunks {
136+
getInput := &ssm.GetParametersInput{
137+
Names: chunk,
138+
}
139+
results, err := FindParameters(context.TODO(), client, getInput)
140+
if err != nil {
141+
log.Fatal(err)
142+
}
143+
144+
for _, p := range results.Parameters {
145+
parameters = append(parameters, Parameter{Name: *p.Name, Value: *p.Value})
146+
}
147+
}
148+
fmt.Println(parameters)
149+
}

0 commit comments

Comments
 (0)