-
Notifications
You must be signed in to change notification settings - Fork 417
/
yaml_test.go
170 lines (155 loc) · 4.75 KB
/
yaml_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
package configmap
import (
"context"
"io/ioutil"
"path"
"reflect"
"strings"
"testing"
"time"
"github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
utilyaml "k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/aws-iam-authenticator/pkg/config"
)
var log = logrus.New()
func TestConfigMap(t *testing.T) {
log.Level = logrus.DebugLevel
nodeRoleARN := "arn:aws:iam::555555555555:role/devel-worker-nodes-NodeInstanceRole-74RF4UBDUKL6"
adminUserARN := "arn:aws:iam::555555555555:user/admin"
validRoleMappings := []config.RoleMapping{
{
RoleARN: nodeRoleARN,
Username: "system:node:{{EC2PrivateDNSName}}",
Groups: []string{"system:bootstrappers", "system:nodes"},
},
}
validUserMappings := []config.UserMapping{
{
UserARN: adminUserARN,
Username: "admin",
Groups: []string{"system:masters"},
},
}
validAWSAccounts := map[string]bool{
"555555555555": true,
}
tests := []struct {
configMapYaml string
expectedRoleMappings []config.RoleMapping
expectedUserMappings []config.UserMapping
expectedAWSAccounts map[string]bool
expectCreateError bool
}{
// Success cases
{
// Valid aws-auth.yaml based on one in EKS documentation.
"aws-auth.yaml", validRoleMappings, validUserMappings, validAWSAccounts, false,
},
// Fail cases -- ideally, validation should reject these before they reach us
{
// mapusers instead of mapUsers
// parsing fails, top-level keys are case-sensitive
"aws-auth-lower-case-top-keys.yaml", nil, nil, nil, false,
},
{
// MaPuSeRs instead of mapUsers
// parsing fails, top-level keys are case-sensitive
"aws-auth-crazy-case-top-keys.yaml", nil, nil, nil, false,
},
{
// an extra space ' ' before group '- system:nodes'
// parsing succeeds, but groups is invalid 'system:bootstrappers - system:nodes'
"aws-auth-space-out-of-place.yaml", []config.RoleMapping{
{
RoleARN: nodeRoleARN,
Username: "system:node:{{EC2PrivateDNSName}}",
Groups: []string{"system:bootstrappers - system:nodes"},
},
}, validUserMappings, validAWSAccounts, false,
},
{
// a missing bar '|' after 'mapRoles:'
// create fails, mapRoles is a top-level configMap key unlike upstream
"aws-auth-missing-bar.yaml", nil, nil, nil, true,
},
}
for _, tt := range tests {
t.Run(tt.configMapYaml, func(t *testing.T) {
cm, err := configMapFromYaml(tt.configMapYaml)
if err != nil && !tt.expectCreateError {
t.Errorf("error creating configMap from yaml: %v", err)
} else if err == nil && tt.expectCreateError {
t.Errorf("expected error creating configMap from yaml, got nothing")
}
cs := fake.NewSimpleClientset()
ms := MapStore{}
ms.configMap = cs.CoreV1().ConfigMaps("kube-system")
stopCh := make(chan struct{})
ms.startLoadConfigMap(stopCh)
defer close(stopCh)
time.Sleep(2 * time.Millisecond)
_, _ = cs.CoreV1().ConfigMaps("kube-system").Create(context.TODO(), cm, metav1.CreateOptions{})
time.Sleep(2 * time.Millisecond)
for _, em := range tt.expectedRoleMappings {
m, err := ms.RoleMapping(strings.ToLower(em.RoleARN))
if err != nil {
t.Errorf("%v", err)
}
if !reflect.DeepEqual(em, m) {
t.Errorf("expected role mapping %v, got %v", em, m)
}
}
ms.mutex.Lock()
if len(tt.expectedRoleMappings) != len(ms.roles) {
t.Errorf("expected role mappings %v, got %v", tt.expectedRoleMappings, ms.roles)
}
ms.mutex.Unlock()
for _, em := range tt.expectedUserMappings {
m, err := ms.UserMapping(strings.ToLower(em.UserARN))
if err != nil {
t.Errorf("%v", err)
}
if !reflect.DeepEqual(em, m) {
t.Errorf("expected user mapping %v, got %v", em, m)
}
}
ms.mutex.Lock()
if len(tt.expectedUserMappings) != len(ms.users) {
t.Errorf("expected user mappings %v, got %v", tt.expectedUserMappings, ms.users)
}
ms.mutex.Unlock()
for accountID, eok := range tt.expectedAWSAccounts {
ok := ms.AWSAccount(strings.ToLower(accountID))
if eok != ok {
t.Errorf("expected account %s %v, got %v", accountID, eok, ok)
}
}
ms.mutex.Lock()
if len(tt.expectedAWSAccounts) != len(ms.awsAccounts) {
t.Errorf("expected accounts %v, got %v", tt.expectedAWSAccounts, ms.awsAccounts)
}
ms.mutex.Unlock()
})
}
}
func configMapFromYaml(fileName string) (*v1.ConfigMap, error) {
var cm v1.ConfigMap
data, err := ioutil.ReadFile(path.Join("./yaml/", fileName))
if err != nil {
return nil, err
}
json, err := utilyaml.ToJSON(data)
if err != nil {
return nil, err
}
err = runtime.DecodeInto(scheme.Codecs.UniversalDecoder(), json, &cm)
if err != nil {
return nil, err
}
return &cm, nil
}