This repository was archived by the owner on Aug 22, 2019. It is now read-only.
forked from JeremyLoy/config
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaws_test.go
141 lines (114 loc) · 3.89 KB
/
aws_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package config
import (
"context"
"encoding/base64"
"net/http"
"testing"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/secretsmanager"
"github.com/aws/aws-sdk-go-v2/service/secretsmanager/secretsmanageriface"
"github.com/aws/aws-sdk-go-v2/service/ssm"
"github.com/aws/aws-sdk-go-v2/service/ssm/ssmiface"
"github.com/stretchr/testify/assert"
)
type mockSecretManagerClient struct {
secretsmanageriface.ClientAPI
checkInput func(*secretsmanager.GetSecretValueInput)
stringValue *string
binaryValue []byte
}
func (m *mockSecretManagerClient) GetSecretValueRequest(in *secretsmanager.GetSecretValueInput) secretsmanager.GetSecretValueRequest {
if m.checkInput != nil {
m.checkInput(in)
}
req := &aws.Request{
Data: &secretsmanager.GetSecretValueOutput{
SecretString: m.stringValue,
SecretBinary: m.binaryValue,
},
HTTPRequest: new(http.Request),
}
return secretsmanager.GetSecretValueRequest{Request: req, Input: in, Copy: m.GetSecretValueRequest}
}
type mockParameterStoreClient struct {
ssmiface.ClientAPI
checkInput func(*ssm.GetParameterInput)
stringValue *string
binaryValue []byte
}
func (m *mockParameterStoreClient) GetParameterRequest(in *ssm.GetParameterInput) ssm.GetParameterRequest {
if m.checkInput != nil {
m.checkInput(in)
}
var value *string
if m.stringValue != nil {
value = m.stringValue
} else if m.binaryValue != nil {
value = aws.String(base64.StdEncoding.EncodeToString(m.binaryValue))
}
req := &aws.Request{
Data: &ssm.GetParameterOutput{
Parameter: &ssm.Parameter{
Value: value,
},
},
HTTPRequest: new(http.Request),
}
return ssm.GetParameterRequest{Request: req, Input: in, Copy: m.GetParameterRequest}
}
func TestAWSSecretManagerValuePreProcessor_PreProcessValue(t *testing.T) {
ctx := context.Background()
t.Run("NonPrefixedValues", func(t *testing.T) {
p := AWSSecretManagerValuePreProcessor{}
assert.Equal(t, "bar", p.PreProcessValue("FOO_1", "bar"))
assert.Equal(t, "test", p.PreProcessValue("FOO_BAR_BAZ", "test"))
})
t.Run("SecretsManager", func(t *testing.T) {
manager := &mockSecretManagerClient{}
p := &AWSSecretManagerValuePreProcessor{
decryptParameterStoreValues: true,
secretsManager: manager,
ctx: ctx,
}
t.Run("Simple", func(t *testing.T) {
manager.checkInput = func(input *secretsmanager.GetSecretValueInput) {
assert.Equal(t, "foo_bar", *input.SecretId)
}
manager.stringValue = aws.String("baz")
assert.Equal(t, "baz", p.PreProcessValue("FOO", "sm://foo_bar"))
})
// "complex" in the sense that this would break using strings.TrimPrefix(...)
t.Run("Complex", func(t *testing.T) {
manager.checkInput = func(input *secretsmanager.GetSecretValueInput) {
assert.Equal(t, "small_foo_bar", *input.SecretId)
}
manager.stringValue = aws.String("baz")
assert.Equal(t, "baz", p.PreProcessValue("FOO", "sm://small_foo_bar"))
})
})
t.Run("ParameterStore", func(t *testing.T) {
storeClient := &mockParameterStoreClient{}
p := &AWSSecretManagerValuePreProcessor{
decryptParameterStoreValues: true,
parameterStore: storeClient,
ctx: ctx,
}
t.Run("Simple", func(t *testing.T) {
storeClient.checkInput = func(input *ssm.GetParameterInput) {
assert.Equal(t, "foo_bar", *input.Name)
assert.True(t, *input.WithDecryption)
}
storeClient.stringValue = aws.String("baz")
assert.Equal(t, "baz", p.PreProcessValue("FOO", "ssm://foo_bar"))
})
// "complex" in the sense that this would break using strings.TrimPrefix(...)
t.Run("Complex", func(t *testing.T) {
storeClient.checkInput = func(input *ssm.GetParameterInput) {
assert.Equal(t, "ssmall_foo_bar", *input.Name)
assert.True(t, *input.WithDecryption)
}
storeClient.stringValue = aws.String("baz")
assert.Equal(t, "baz", p.PreProcessValue("FOO", "ssm://ssmall_foo_bar"))
})
})
}