-
Notifications
You must be signed in to change notification settings - Fork 18
/
awsiamrole_controller.go
443 lines (394 loc) · 13.3 KB
/
awsiamrole_controller.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
package main
import (
"context"
"encoding/json"
"fmt"
"strconv"
"time"
log "github.com/sirupsen/logrus"
av1 "github.com/zalando-incubator/kube-aws-iam-controller/pkg/apis/zalando.org/v1"
"github.com/zalando-incubator/kube-aws-iam-controller/pkg/clientset"
"github.com/zalando-incubator/kube-aws-iam-controller/pkg/recorder"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/tools/record"
)
const (
awsIAMRoleGenerationKey = "awsiamrole-generation"
)
var (
awsIAMRoleOwnerLabels = map[string]string{
heritageLabelKey: awsIAMControllerLabelValue,
"type": "awsiamrole",
}
)
// AWSIAMRoleController is a controller which lists AWSIAMRole resources and
// create/update matching secrets with AWS IAM role credentials.
type AWSIAMRoleController struct {
client clientset.Interface
recorder record.EventRecorder
interval time.Duration
refreshLimit time.Duration
creds CredentialsGetter
namespace string
}
// NewSecretsController initializes a new AWSIAMRoleController.
func NewAWSIAMRoleController(client clientset.Interface, interval, refreshLimit time.Duration, creds CredentialsGetter, namespace string) *AWSIAMRoleController {
return &AWSIAMRoleController{
client: client,
recorder: recorder.CreateEventRecorder(client),
interval: interval,
refreshLimit: refreshLimit,
creds: creds,
namespace: namespace,
}
}
// getCreds gets new credentials from the CredentialsGetter and converts them
// to a secret data map.
func (c *AWSIAMRoleController) getCreds(ctx context.Context, role string, sessionDuration time.Duration) (*Credentials, map[string][]byte, error) {
creds, err := c.creds.Get(ctx, role, sessionDuration)
if err != nil {
return nil, nil, err
}
credsFile := fmt.Sprintf(
credentialsFileTemplate,
creds.AccessKeyID,
creds.SecretAccessKey,
creds.SessionToken,
creds.Expiration.Format(time.RFC3339),
)
processCreds := ProcessCredentials{
Version: 1,
AccessKeyID: creds.AccessKeyID,
SecretAccessKey: creds.SecretAccessKey,
SessionToken: creds.SessionToken,
Expiration: creds.Expiration,
}
processCredsData, err := json.Marshal(&processCreds)
if err != nil {
return nil, nil, err
}
return creds, map[string][]byte{
roleARNKey: []byte(creds.RoleARN),
expireKey: []byte(creds.Expiration.Format(time.RFC3339)),
credentialsFileKey: []byte(credsFile),
credentialsProcessFileKey: []byte(credentialsProcessFileContent),
credentialsJSONFileKey: processCredsData,
}, nil
}
// Run runs the secret controller loop. This will refresh secrets with AWS IAM
// roles.
func (c *AWSIAMRoleController) Run(ctx context.Context) {
for {
err := c.refresh(ctx)
if err != nil {
log.Error(err)
}
select {
case <-time.After(c.interval):
case <-ctx.Done():
log.Info("Terminating AWSIAMRole controller loop.")
return
}
}
}
// refresh checks for soon to expire secrets and requests new credentials. It
// also looks for AWSIAMRole resources where secrets are missing and creates
// the secrets for the designated namespace.
func (c *AWSIAMRoleController) refresh(ctx context.Context) error {
opts := metav1.ListOptions{
LabelSelector: labels.Set(awsIAMRoleOwnerLabels).AsSelector().String(),
}
secrets, err := c.client.CoreV1().Secrets(c.namespace).List(ctx, opts)
if err != nil {
return err
}
awsIAMRoles, err := c.client.ZalandoV1().AWSIAMRoles(c.namespace).List(ctx, metav1.ListOptions{})
if err != nil {
return err
}
secretsMap := make(map[string]v1.Secret, len(secrets.Items))
for _, secret := range secrets.Items {
secretsMap[secret.Namespace+"/"+secret.Name] = secret
}
awsIAMRolesMap := make(map[string]av1.AWSIAMRole, len(awsIAMRoles.Items))
for _, role := range awsIAMRoles.Items {
awsIAMRolesMap[role.Namespace+"/"+role.Name] = role
}
orphanSecrets := make([]v1.Secret, 0, len(secrets.Items))
for _, secret := range secrets.Items {
awsIAMRole, ok := awsIAMRolesMap[secret.Namespace+"/"+secret.Name]
if !ok || !isOwnedReference(awsIAMRole.TypeMeta, awsIAMRole.ObjectMeta, secret.ObjectMeta) {
orphanSecrets = append(orphanSecrets, secret)
continue
}
role := awsIAMRole.Spec.RoleReference
roleSessionDuration := 3600 * time.Second
if awsIAMRole.Spec.RoleSessionDuration > 0 {
roleSessionDuration = time.Duration(awsIAMRole.Spec.RoleSessionDuration) * time.Second
}
// TODO: move to function
refreshCreds := false
expirary, ok := secret.Data[expireKey]
if !ok {
refreshCreds = true
} else {
expire, err := time.Parse(time.RFC3339, string(expirary))
if err != nil {
log.Debugf("Failed to parse expirary time %s: %v", expirary, err)
refreshCreds = true
} else if time.Now().UTC().Add(c.refreshLimit).After(expire) {
refreshCreds = true
}
}
if refreshCreds {
var creds *Credentials
creds, secret.Data, err = c.getCreds(ctx, role, roleSessionDuration)
if err != nil {
c.recorder.Event(&awsIAMRole,
v1.EventTypeWarning,
"GetCredentialsFailed",
fmt.Sprintf("Failed to get credentials for role '%s': %v", role, err),
)
continue
}
// update secret labels
secret.Labels = mergeLabels(awsIAMRole.Labels, awsIAMRoleOwnerLabels)
secret.Data[awsIAMRoleGenerationKey] = []byte(fmt.Sprintf("%d", awsIAMRole.Generation))
// update secret with refreshed credentials
_, err := c.client.CoreV1().Secrets(secret.Namespace).Update(ctx, &secret, metav1.UpdateOptions{})
if err != nil {
log.Errorf("Failed to update secret %s/%s: %v", secret.Namespace, secret.Name, err)
continue
}
log.WithFields(log.Fields{
"action": "update",
"role-arn": creds.RoleARN,
"secret": secret.Name,
"namespace": secret.Namespace,
"expire": creds.Expiration.String(),
"type": "awsiamrole",
}).Info()
c.recorder.Event(&awsIAMRole,
v1.EventTypeNormal,
"UpdateCredentials",
fmt.Sprintf("Updated credentials for role '%s', expiry time: %s", creds.RoleARN, creds.Expiration.String()),
)
// update AWSIAMRole status
expiryTime := metav1.NewTime(creds.Expiration)
awsIAMRole.Status = av1.AWSIAMRoleStatus{
ObservedGeneration: &awsIAMRole.Generation,
RoleARN: creds.RoleARN,
Expiration: &expiryTime,
}
_, err = c.client.ZalandoV1().AWSIAMRoles(awsIAMRole.Namespace).UpdateStatus(ctx, &awsIAMRole, metav1.UpdateOptions{})
if err != nil {
log.Errorf("Failed to update status for AWSIAMRole %s/%s: %v", awsIAMRole.Namespace, awsIAMRole.Name, err)
continue
}
}
}
// clean up orphaned secrets
for _, secret := range orphanSecrets {
err := c.client.CoreV1().Secrets(secret.Namespace).Delete(ctx, secret.Name, metav1.DeleteOptions{})
if err != nil {
log.Errorf("Failed to delete secret %s/%s: %v", secret.Namespace, secret.Name, err)
continue
}
log.WithFields(log.Fields{
"action": "delete",
"role-arn": string(secret.Data[roleARNKey]),
"secret": secret.Name,
"namespace": secret.Namespace,
}).Info("Removing unused credentials")
continue
}
// create secrets for new AWSIAMRoles without a secret
for _, awsIAMRole := range awsIAMRoles.Items {
role := awsIAMRole.Spec.RoleReference
roleSessionDuration := 3600 * time.Second
if awsIAMRole.Spec.RoleSessionDuration > 0 {
roleSessionDuration = time.Duration(awsIAMRole.Spec.RoleSessionDuration) * time.Second
}
if secret, ok := secretsMap[awsIAMRole.Namespace+"/"+awsIAMRole.Name]; ok {
// update secret if out of date
generation, err := getGeneration(secret.Data)
if err != nil {
c.recorder.Event(&awsIAMRole,
v1.EventTypeWarning,
"ReadSecretFailed",
fmt.Sprintf("Failed to parse AWSIAMRole generation from secret %s/%s: %v",
secret.Namespace,
secret.Name,
err),
)
continue
}
if awsIAMRole.Generation != generation {
var creds *Credentials
creds, secret.Data, err = c.getCreds(ctx, role, roleSessionDuration)
if err != nil {
c.recorder.Event(&awsIAMRole,
v1.EventTypeWarning,
"GetCredentialsFailed",
fmt.Sprintf("Failed to get credentials for role '%s': %v", role, err),
)
continue
}
// update secret labels
secret.Labels = mergeLabels(awsIAMRole.Labels, awsIAMRoleOwnerLabels)
secret.Data[awsIAMRoleGenerationKey] = []byte(fmt.Sprintf("%d", awsIAMRole.Generation))
// update secret with refreshed credentials
_, err := c.client.CoreV1().Secrets(secret.Namespace).Update(ctx, &secret, metav1.UpdateOptions{})
if err != nil {
c.recorder.Event(&awsIAMRole,
v1.EventTypeWarning,
"UpdateSecretFailed",
fmt.Sprintf("Failed to update secret %s/%s with credentials: %v", secret.Namespace, secret.Name, err),
)
continue
}
log.WithFields(log.Fields{
"action": "update",
"role-arn": creds.RoleARN,
"secret": secret.Name,
"namespace": secret.Namespace,
"expire": creds.Expiration.String(),
"type": "awsiamrole",
}).Info()
c.recorder.Event(&awsIAMRole,
v1.EventTypeNormal,
"UpdateCredentials",
fmt.Sprintf("Updated credentials for role '%s', expiry time: %s", creds.RoleARN, creds.Expiration.String()),
)
}
// update AWSIAMRole status if not up to date
if awsIAMRole.Status.ObservedGeneration == nil || *awsIAMRole.Status.ObservedGeneration != awsIAMRole.Generation {
expiration, err := time.Parse(time.RFC3339, string(secret.Data[expireKey]))
if err != nil {
c.recorder.Event(&awsIAMRole,
v1.EventTypeWarning,
"ReadSecretFailed",
fmt.Sprintf("Failed to parse expirary time '%s' from secret %s/%s: %v",
string(secret.Data[expireKey]),
secret.Namespace,
secret.Name,
err),
)
continue
}
expiryTime := metav1.NewTime(expiration)
awsIAMRole.Status = av1.AWSIAMRoleStatus{
ObservedGeneration: &awsIAMRole.Generation,
RoleARN: string(secret.Data[roleARNKey]),
Expiration: &expiryTime,
}
// update AWSIAMRole status
_, err = c.client.ZalandoV1().AWSIAMRoles(awsIAMRole.Namespace).UpdateStatus(ctx, &awsIAMRole, metav1.UpdateOptions{})
if err != nil {
log.Errorf("Failed to update status of AWSIAMRole %s/%s: %v", awsIAMRole.Namespace, awsIAMRole.Name, err)
continue
}
}
continue
}
creds, secretData, err := c.getCreds(ctx, role, roleSessionDuration)
if err != nil {
c.recorder.Event(&awsIAMRole,
v1.EventTypeWarning,
"GetCredentialsFailed",
fmt.Sprintf("Failed to get credentials for role '%s': %v", role, err),
)
continue
}
secretData[awsIAMRoleGenerationKey] = []byte(fmt.Sprintf("%d", awsIAMRole.Generation))
// create secret
secret := &v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: awsIAMRole.Name,
Namespace: awsIAMRole.Namespace,
Labels: mergeLabels(awsIAMRole.Labels, awsIAMRoleOwnerLabels),
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: awsIAMRole.APIVersion,
Kind: awsIAMRole.Kind,
Name: awsIAMRole.Name,
UID: awsIAMRole.UID,
},
},
},
Data: secretData,
}
_, err = c.client.CoreV1().Secrets(awsIAMRole.Namespace).Create(ctx, secret, metav1.CreateOptions{})
if err != nil {
c.recorder.Event(&awsIAMRole,
v1.EventTypeWarning,
"CreateSecretFailed",
fmt.Sprintf("Failed to create secret %s/%s with credentials: %v", awsIAMRole.Namespace, awsIAMRole.Name, err),
)
continue
}
log.WithFields(log.Fields{
"action": "create",
"role-arn": creds.RoleARN,
"secret": awsIAMRole.Name,
"namespace": awsIAMRole.Namespace,
"expire": creds.Expiration.String(),
"type": "awsiamrole",
}).Info()
c.recorder.Event(&awsIAMRole,
v1.EventTypeNormal,
"CreateCredentials",
fmt.Sprintf("Created credentials for role '%s', expiry time: %s", creds.RoleARN, creds.Expiration.String()),
)
// update AWSIAMRole status
expiryTime := metav1.NewTime(creds.Expiration)
awsIAMRole.Status = av1.AWSIAMRoleStatus{
ObservedGeneration: &awsIAMRole.Generation,
RoleARN: creds.RoleARN,
Expiration: &expiryTime,
}
_, err = c.client.ZalandoV1().AWSIAMRoles(awsIAMRole.Namespace).UpdateStatus(ctx, &awsIAMRole, metav1.UpdateOptions{})
if err != nil {
log.Errorf("Failed to update status of AWSIAMRole %s/%s: %v", awsIAMRole.Namespace, awsIAMRole.Name, err)
continue
}
}
return nil
}
// isOwnedReference returns true of the dependent object is owned by the owner
// object.
func isOwnedReference(ownerTypeMeta metav1.TypeMeta, ownerObjectMeta, dependent metav1.ObjectMeta) bool {
for _, ref := range dependent.OwnerReferences {
if ref.APIVersion == ownerTypeMeta.APIVersion &&
ref.Kind == ownerTypeMeta.Kind &&
ref.UID == ownerObjectMeta.UID &&
ref.Name == ownerObjectMeta.Name {
return true
}
}
return false
}
func mergeLabels(base, additional map[string]string) map[string]string {
labels := make(map[string]string, len(base)+len(additional))
for k, v := range base {
labels[k] = v
}
for k, v := range additional {
labels[k] = v
}
return labels
}
func getGeneration(secretData map[string][]byte) (int64, error) {
generation := int64(0)
gen, ok := secretData[awsIAMRoleGenerationKey]
if ok {
i, err := strconv.ParseInt(string(gen), 10, 64)
if err != nil {
return 0, err
}
generation = i
}
return generation, nil
}