-
Notifications
You must be signed in to change notification settings - Fork 415
/
configmap_test.go
287 lines (245 loc) · 7.44 KB
/
configmap_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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package configmap
import (
"reflect"
"testing"
"time"
core_v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/watch"
v1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/kubernetes/typed/core/v1/fake"
k8stesting "k8s.io/client-go/testing"
"sigs.k8s.io/aws-iam-authenticator/pkg/config"
)
var testUser = config.UserMapping{Username: "matlan", Groups: []string{"system:master", "dev"}}
var testRole = config.RoleMapping{Username: "computer", Groups: []string{"system:nodes"}}
func makeStore() MapStore {
ms := MapStore{
users: make(map[string]config.UserMapping),
roles: make(map[string]config.RoleMapping),
awsAccounts: make(map[string]interface{}),
}
ms.users["matt"] = testUser
ms.roles["instance"] = testRole
ms.awsAccounts["123"] = nil
return ms
}
func makeStoreWClient() (MapStore, *fake.FakeConfigMaps) {
fakeConfigMaps := &fake.FakeConfigMaps{}
fakeConfigMaps.Fake = &fake.FakeCoreV1{}
fakeConfigMaps.Fake.Fake = &k8stesting.Fake{}
ms := MapStore{
users: make(map[string]config.UserMapping),
roles: make(map[string]config.RoleMapping),
configMap: v1.ConfigMapInterface(fakeConfigMaps),
}
return ms, fakeConfigMaps
}
func TestUserMapping(t *testing.T) {
ms := makeStore()
user, err := ms.UserMapping("matt")
if err != nil {
t.Errorf("Could not find user 'matt' in map")
}
if !reflect.DeepEqual(user, testUser) {
t.Errorf("User for 'matt' does not match expected values. (Actual: %+v, Expected: %+v", user, testUser)
}
user, err = ms.UserMapping("nic")
if err != UserNotFound {
t.Errorf("UserNotFound error was not returned for user 'nic'")
}
if !reflect.DeepEqual(user, config.UserMapping{}) {
t.Errorf("User value returned when user is not in the map was not empty: %+v", user)
}
}
func TestRoleMapping(t *testing.T) {
ms := makeStore()
role, err := ms.RoleMapping("instance")
if err != nil {
t.Errorf("Could not find user 'instance in map")
}
if !reflect.DeepEqual(role, testRole) {
t.Errorf("Role for 'instance' does not match expected value. (Acutal: %+v, Expected: %+v", role, testRole)
}
role, err = ms.RoleMapping("borg")
if err != RoleNotFound {
t.Errorf("RoleNotFound error was not returend for role 'borg'")
}
if !reflect.DeepEqual(role, config.RoleMapping{}) {
t.Errorf("Role value returend when role is not in map was not empty: %+v", role)
}
}
func TestAWSAccount(t *testing.T) {
ms := makeStore()
if !ms.AWSAccount("123") {
t.Errorf("Expected aws account '123' to be in accounts list: %v", ms.awsAccounts)
}
if ms.AWSAccount("345") {
t.Errorf("Did not expect account '345' to be in accounts list: %v", ms.awsAccounts)
}
}
var userMapping = `
-
userarn: "arn:iam:matlan"
username: matlan
groups:
- loadedfromconfigmap
- "system:master"
-
groups:
- "system:master"
userarn: "arn:iam:NIC"
username: nic
`
var roleMapping = `
- rolearn: "arn:iam:123:role/me"
username: "{{Session}}"
groups:
- system:nodes
`
var updatedUserMapping = `
-
groups:
- "system:master"
- "test"
userarn: "arn:iam:NIC"
username: nic
- userarn: "arn:iam:beswar"
username: beswar
groups:
- "system:master"
- userarn: "arn:iam:nogroups"
username: nogroups
`
var updatedRoleMapping = `
- rolearn: "arn:iam:123:role/me"
username: "{{Session}}"
groups:
- system:nodes
- rolearn: "arn:iam:123:role/you"
username: "test"
groups:
- system:nodes
`
var autoMappedAWSAccountsYAML = `
- 123
- 345
`
var updatedAWSAccountsYAML = `
- 567
`
func TestLoadConfigMap(t *testing.T) {
ms, fakeConfigMaps := makeStoreWClient()
watcher := watch.NewFake()
fakeConfigMaps.Fake.Fake.AddWatchReactor("configmaps",
func(action k8stesting.Action) (handled bool, ret watch.Interface, err error) {
return true, watcher, nil
})
stopCh := make(chan struct{})
ms.startLoadConfigMap(stopCh)
defer close(stopCh)
time.Sleep(2 * time.Millisecond)
meta := metav1.ObjectMeta{Name: "aws-auth"}
data := make(map[string]string)
data["mapUsers"] = userMapping
data["mapRoles"] = roleMapping
data["mapAccounts"] = autoMappedAWSAccountsYAML
watcher.Add(&core_v1.ConfigMap{ObjectMeta: meta, Data: data})
time.Sleep(2 * time.Millisecond)
if !ms.AWSAccount("123") {
t.Errorf("AWS Account '123' not in allowed accounts")
}
if !ms.AWSAccount("345") {
t.Errorf("AWS Account '345' not in allowed accounts")
}
expectedUser := config.UserMapping{
UserARN: "arn:iam:NIC",
Username: "nic",
Groups: []string{"system:master"},
}
user, err := ms.UserMapping("arn:iam:nic")
if err != nil {
t.Errorf("Expected to find user 'nic' but got error: %v", err)
}
if !reflect.DeepEqual(user, expectedUser) {
t.Errorf("User returned from mapping does not match expected user. (Actual: %+v, Expected: %+v", user, expectedUser)
}
updateData := make(map[string]string)
updateData["mapUsers"] = updatedUserMapping
updateData["mapRoles"] = updatedRoleMapping
updateData["mapAccounts"] = updatedAWSAccountsYAML
watcher.Modify(&core_v1.ConfigMap{ObjectMeta: meta, Data: updateData})
//TODO: Sync without using sleep
time.Sleep(10 * time.Millisecond)
if ms.AWSAccount("345") {
t.Errorf("AWS Account '345' is in map after update")
}
if !ms.AWSAccount("567") {
t.Errorf("AWS Account '567' is not in map after update")
}
expectedUser.Groups = append(expectedUser.Groups, "test")
user, err = ms.UserMapping("arn:iam:nic")
if !reflect.DeepEqual(user, expectedUser) {
t.Errorf("Updated returned from mapping does not match expected user. (Actual: %+v, Expected: %+v", user, expectedUser)
}
expectedUser = config.UserMapping{
UserARN: "arn:iam:beswar",
Username: "beswar",
Groups: []string{"system:master"},
}
user, err = ms.UserMapping("arn:iam:beswar")
if !reflect.DeepEqual(user, expectedUser) {
t.Errorf("Updated did not return new user 'arn:iam:beswar', matching expected value. (Actual: %+v, Expected: %+v", user, expectedUser)
}
user, err = ms.UserMapping("arn:iam:matlan")
if err != UserNotFound {
t.Errorf("Expected updated mapping not to contain user 'arn:iam:matlan', got err: %v", err)
}
}
func TestParseMap(t *testing.T) {
m1 := map[string]string{
"mapRoles": `- rolearn: arn:aws:iam::123456789101:role/test-NodeInstanceRole-1VWRHZ3GKZ1T4
username: system:node:{{EC2PrivateDNSName}}
groups:
- system:bootstrappers
- system:nodes
`,
"mapUsers": `- userarn: arn:aws:iam::123456789101:user/Hello
username: Hello
groups:
- system:masters
- userarn: arn:aws:iam::123456789101:user/World
username: World
groups:
- system:masters
`,
}
userMappings := []config.UserMapping{
{UserARN: "arn:aws:iam::123456789101:user/Hello", Username: "Hello", Groups: []string{"system:masters"}},
{UserARN: "arn:aws:iam::123456789101:user/World", Username: "World", Groups: []string{"system:masters"}},
}
roleMappings := []config.RoleMapping{
{RoleARN: "arn:aws:iam::123456789101:role/test-NodeInstanceRole-1VWRHZ3GKZ1T4", Username: "system:node:{{EC2PrivateDNSName}}", Groups: []string{"system:bootstrappers", "system:nodes"}},
}
accounts := []string{}
u, r, a, err := ParseMap(m1)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(u, userMappings) {
t.Fatalf("unexpected userMappings %+v", u)
}
if !reflect.DeepEqual(r, roleMappings) {
t.Fatalf("unexpected roleMappings %+v", r)
}
if !reflect.DeepEqual(a, accounts) {
t.Fatalf("unexpected accounts %+v", a)
}
m2, err := EncodeMap(u, r, a)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(m1, m2) {
t.Fatalf("unexpected %v != %v", m1, m2)
}
}