Skip to content

Commit d9825ef

Browse files
committed
Integrated "aws_ssm" plugin for checkenv
1 parent c5d434f commit d9825ef

4 files changed

Lines changed: 46 additions & 29 deletions

File tree

aws_ssm.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// checkenv plugin that provides the environment variables defined in the checkenv process.
2+
3+
package main
4+
5+
import (
6+
"context"
7+
8+
"github.com/bugout-dev/checkenv/aws_ssm"
9+
)
10+
11+
func AWSSystemsManagerParameterStoreProvider(filter string) (map[string]string, error) {
12+
environment := make(map[string]string)
13+
14+
AWSSystemsManagerFlags := aws_ssm.Flags{
15+
Export: false,
16+
MaxResults: 10,
17+
Outfile: "",
18+
Update: false,
19+
}
20+
AWSSystemsManagerFlags.FilterTags = aws_ssm.ParseFilterTags(filter)
21+
22+
ctx := context.Background()
23+
api := aws_ssm.InitAWSClient(ctx)
24+
keys := aws_ssm.FetchKeysOfParameters(ctx, api, AWSSystemsManagerFlags)
25+
keyChunks := aws_ssm.GenerateChunks(keys, 10)
26+
parameters := aws_ssm.FetchParameters(ctx, api, keyChunks, AWSSystemsManagerFlags)
27+
for _, parameter := range parameters {
28+
environment[parameter.Name] = parameter.Value
29+
}
30+
31+
return environment, nil
32+
}
33+
34+
func init() {
35+
helpString := "Provides environment variables defined in AWS Systems Manager Parameter Store."
36+
RegisterPlugin("aws_ssm", helpString, noop, AWSSystemsManagerParameterStoreProvider)
37+
}

aws_ssm/aws_ssm.go

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,6 @@ import (
1111
"github.com/aws/aws-sdk-go-v2/service/ssm"
1212
)
1313

14-
// SSMGetParametersAPI and SSMDescribeParametersAPI defines the interface
15-
// for the GetParameters and DescribeParameters function.
16-
// We use this interface to test the function using a mocked service
17-
type SSMGetParametersAPI interface {
18-
GetParameters(
19-
ctx context.Context,
20-
params *ssm.GetParametersInput,
21-
optFns ...func(*ssm.Options),
22-
) (*ssm.GetParametersOutput, error)
23-
}
24-
25-
type SSMDescribeParametersAPI interface {
26-
DescribeParameters(
27-
ctx context.Context,
28-
params *ssm.DescribeParametersInput,
29-
optFns ...func(*ssm.Options),
30-
) (*ssm.DescribeParametersOutput, error)
31-
}
32-
3314
type AWSSystemsManagerParameterStore interface {
3415
GetParameters(
3516
ctx context.Context,
@@ -52,11 +33,11 @@ type AWSSystemsManagerParameterStore interface {
5233
// Output:
5334
// If success, a GetParametersOutput object containing the result of the service call and nil
5435
// Otherwise, nil and an error from the call to GetParameters
55-
func ExecGetParameters(c context.Context, api SSMGetParametersAPI, input *ssm.GetParametersInput) (*ssm.GetParametersOutput, error) {
36+
func ExecGetParameters(c context.Context, api AWSSystemsManagerParameterStore, input *ssm.GetParametersInput) (*ssm.GetParametersOutput, error) {
5637
return api.GetParameters(c, input)
5738
}
5839

59-
func ExecDescribeParameters(c context.Context, api SSMDescribeParametersAPI, input *ssm.DescribeParametersInput) (*ssm.DescribeParametersOutput, error) {
40+
func ExecDescribeParameters(c context.Context, api AWSSystemsManagerParameterStore, input *ssm.DescribeParametersInput) (*ssm.DescribeParametersOutput, error) {
6041
return api.DescribeParameters(c, input)
6142
}
6243

aws_ssm/aws_ssm_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ import (
1313
)
1414

1515
// Fill fake output data
16-
type SSMGetParametersImpl struct{}
17-
type SSMDescribeParametersImpl struct{}
16+
type MockAWSSystemManagerParameterStore struct{}
1817

19-
func (dt SSMGetParametersImpl) GetParameters(
18+
func (dt MockAWSSystemManagerParameterStore) GetParameters(
2019
ctx context.Context,
2120
params *ssm.GetParametersInput,
2221
optFns ...func(*ssm.Options),
@@ -34,7 +33,7 @@ func (dt SSMGetParametersImpl) GetParameters(
3433

3534
return output, nil
3635
}
37-
func (dt SSMDescribeParametersImpl) DescribeParameters(
36+
func (dt MockAWSSystemManagerParameterStore) DescribeParameters(
3837
ctx context.Context,
3938
params *ssm.DescribeParametersInput,
4039
optFns ...func(*ssm.Options),
@@ -87,7 +86,7 @@ func TestDescribeParameters(t *testing.T) {
8786
t.Fatal("Failed to populate data")
8887
}
8988

90-
api := &SSMDescribeParametersImpl{}
89+
api := &MockAWSSystemManagerParameterStore{}
9190

9291
flags := Flags{FilterTags: []FilterTag{{Name: "Product", Value: "test"}}}
9392

@@ -111,7 +110,7 @@ func TestDescribeParameters(t *testing.T) {
111110
func TestGetParameters(t *testing.T) {
112111
parameterKeyChunks := GenerateChunks(globalParameterKeys, 10)
113112

114-
api := &SSMGetParametersImpl{}
113+
api := &MockAWSSystemManagerParameterStore{}
115114

116115
flags := Flags{Export: false}
117116

aws_ssm/parameters.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
// Fetch values for parameters
1717
// Inputs:
1818
// chunks: list of lists with parameter key values
19-
func FetchParameters(ctx context.Context, api SSMGetParametersAPI, chunks [][]string, flags Flags) []Parameter {
19+
func FetchParameters(ctx context.Context, api AWSSystemsManagerParameterStore, chunks [][]string, flags Flags) []Parameter {
2020
var parameters []Parameter
2121

2222
for _, chunk := range chunks {
@@ -46,7 +46,7 @@ func FetchParameters(ctx context.Context, api SSMGetParametersAPI, chunks [][]st
4646
// Fetch list of parameter keys from AWS with defined filters
4747
func FetchKeysOfParameters(
4848
ctx context.Context,
49-
api SSMDescribeParametersAPI,
49+
api AWSSystemsManagerParameterStore,
5050
flags Flags,
5151
) []string {
5252
var parameters []string

0 commit comments

Comments
 (0)