-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathhandler_test.go
152 lines (134 loc) · 4.07 KB
/
handler_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
142
143
144
145
146
147
148
149
150
151
152
package handler_test
import (
"errors"
"io/ioutil"
"strings"
"testing"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/secretsmanager"
"github.com/aws/aws-sdk-go/service/sts"
"github.com/golang/mock/gomock"
logrus "github.com/sirupsen/logrus/hooks/test"
"github.com/telia-oss/concourse-sts-lambda"
"github.com/telia-oss/concourse-sts-lambda/mocks"
)
func TestHandler(t *testing.T) {
config := handler.Configuration{
Bucket: "bucket",
Key: "key",
}
team := strings.TrimSpace(`
{
"name": "test-team",
"accounts": [{
"name": "test-account",
"roleArn": "test-account-arn"
}]
}
`)
creds := &sts.AssumeRoleOutput{
Credentials: &sts.Credentials{
AccessKeyId: aws.String("access-key"),
SecretAccessKey: aws.String("secret-key"),
SessionToken: aws.String("token"),
Expiration: aws.Time(time.Now()),
},
}
tests := []struct {
description string
path string
config handler.Configuration
team string
stsOutput *sts.AssumeRoleOutput
stsError error
putSecretError error
createSecretError error
}{
{
description: "assumes a role and writes the secrets",
path: "/concourse/{{.Team}}/{{.Account}}",
config: config,
team: team,
stsOutput: creds,
stsError: nil,
createSecretError: nil,
putSecretError: nil,
},
{
description: "continues if it fails to assume role",
path: "/concourse/{{.Team}}/{{.Account}}",
config: config,
team: team,
stsOutput: nil,
stsError: errors.New("test-error"),
createSecretError: nil,
putSecretError: nil,
},
{
description: "continues if it fails create secret",
path: "/concourse/{{.Team}}/{{.Account}}",
config: config,
team: team,
stsOutput: creds,
stsError: nil,
createSecretError: errors.New("test-error"),
putSecretError: nil,
},
{
description: "continues if it fails write secret",
path: "/concourse/{{.Team}}/{{.Account}}",
config: config,
team: team,
stsOutput: creds,
stsError: nil,
createSecretError: nil,
putSecretError: errors.New("test-error"),
},
{
description: "does not error if the secret already exists",
path: "/concourse/{{.Team}}/{{.Account}}",
config: config,
team: team,
stsOutput: creds,
stsError: nil,
createSecretError: awserr.New(secretsmanager.ErrCodeResourceExistsException, "", errors.New("test-error")),
putSecretError: nil,
},
}
for _, tc := range tests {
t.Run(tc.description, func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
s3Object := &s3.GetObjectOutput{
Body: ioutil.NopCloser(strings.NewReader(tc.team)),
}
s3 := mocks.NewMockS3Client(ctrl)
s3.EXPECT().GetObject(gomock.Any()).Times(1).Return(s3Object, nil)
sts := mocks.NewMockSTSClient(ctrl)
sts.EXPECT().AssumeRole(gomock.Any()).Times(1).Return(tc.stsOutput, tc.stsError)
secrets := mocks.NewMockSecretsClient(ctrl)
if tc.stsError == nil {
secrets.EXPECT().CreateSecret(gomock.Any()).MinTimes(1).Return(nil, tc.createSecretError)
}
if tc.stsError == nil {
if tc.createSecretError != nil {
if e, ok := tc.createSecretError.(awserr.Error); ok {
if e.Code() == secretsmanager.ErrCodeResourceExistsException {
secrets.EXPECT().UpdateSecret(gomock.Any()).MinTimes(1).Return(nil, tc.putSecretError)
}
}
} else {
secrets.EXPECT().UpdateSecret(gomock.Any()).MinTimes(1).Return(nil, tc.putSecretError)
}
}
logger, _ := logrus.NewNullLogger()
handle := handler.New(handler.NewTestManager(secrets, sts, s3), tc.path, logger)
if err := handle(tc.config); err != nil {
t.Fatalf("unexpected error: %s", err)
}
})
}
}