/
hooks.go
363 lines (321 loc) · 11.5 KB
/
hooks.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
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
package open_id_connect_provider
import (
"context"
"fmt"
"strings"
ackcompare "github.com/aws-controllers-k8s/runtime/pkg/compare"
ackcondition "github.com/aws-controllers-k8s/runtime/pkg/condition"
ackerr "github.com/aws-controllers-k8s/runtime/pkg/errors"
ackrtlog "github.com/aws-controllers-k8s/runtime/pkg/runtime/log"
svcsdk "github.com/aws/aws-sdk-go/service/iam"
corev1 "k8s.io/api/core/v1"
svcapitypes "github.com/aws-controllers-k8s/iam-controller/apis/v1alpha1"
commonutil "github.com/aws-controllers-k8s/iam-controller/pkg/util"
)
func (rm *resourceManager) customUpdateOpenIDConnectProvider(
ctx context.Context,
desired *resource,
latest *resource,
delta *ackcompare.Delta,
) (updated *resource, err error) {
rlog := ackrtlog.FromContext(ctx)
exit := rlog.Trace("rm.customUpdateOpenIDConnectProvider")
defer func() { exit(err) }()
if immutableFieldChanges := rm.getImmutableFieldChanges(delta); len(immutableFieldChanges) > 0 {
msg := fmt.Sprintf("Immutable Spec fields have been modified: %s", strings.Join(immutableFieldChanges, ","))
return nil, ackerr.NewTerminalError(fmt.Errorf(msg))
}
if delta.DifferentAt("Spec.Thumbprints") {
// Update the thumbprint list
thumbprintInput, err := rm.newUpdateThumbprintRequestPayload(ctx, desired)
if err != nil {
return nil, err
}
var thumbprintResp *svcsdk.UpdateOpenIDConnectProviderThumbprintOutput
_ = thumbprintResp
thumbprintResp, err = rm.sdkapi.UpdateOpenIDConnectProviderThumbprintWithContext(ctx, thumbprintInput)
rm.metrics.RecordAPICall("UPDATE", "UpdateOpenIDConnectProviderThumbprint", err)
if err != nil {
return nil, err
}
}
if delta.DifferentAt("Spec.ClientIDs") {
// Update the client ID list
// here we only have an "add" and a "remove"
// https://docs.aws.amazon.com/sdk-for-go/api/service/iam/#IAM.AddClientIDToOpenIDConnectProvider and
// https://docs.aws.amazon.com/sdk-for-go/api/service/iam/#IAM.RemoveClientIDFromOpenIDConnectProvider
// so we have to compute the diff ourselves
desiredClientIDs := map[string]bool{}
latestClientIDs := map[string]bool{}
for _, desiredClientID := range desired.ko.Spec.ClientIDs {
desiredClientIDs[*desiredClientID] = true
}
for _, latestClientID := range latest.ko.Spec.ClientIDs {
latestClientIDs[*latestClientID] = true
}
for desiredClientID, _ := range desiredClientIDs {
_, hasLatest := latestClientIDs[desiredClientID]
if !hasLatest {
// clientID is to be added
addClientIDInput, err := rm.newAddClientIDRequestPayload(ctx, desired, &desiredClientID)
if err != nil {
return nil, err
}
var addClientIDResp *svcsdk.AddClientIDToOpenIDConnectProviderOutput
_ = addClientIDResp
addClientIDResp, err = rm.sdkapi.AddClientIDToOpenIDConnectProviderWithContext(ctx, addClientIDInput)
rm.metrics.RecordAPICall("UPDATE", "AddClientIDToOpenIDConnectProvider", err)
if err != nil {
return nil, err
}
} else {
delete(desiredClientIDs, desiredClientID)
delete(latestClientIDs, desiredClientID)
}
}
for latestClientID, _ := range latestClientIDs {
// clientID is to be removed
removeClientIDInput, err := rm.newRemoveClientIDRequestPayload(ctx, desired, &latestClientID)
if err != nil {
return nil, err
}
var removeClientIDResp *svcsdk.RemoveClientIDFromOpenIDConnectProviderOutput
_ = removeClientIDResp
removeClientIDResp, err = rm.sdkapi.RemoveClientIDFromOpenIDConnectProviderWithContext(ctx, removeClientIDInput)
rm.metrics.RecordAPICall("UPDATE", "RemoveClientIDFromOpenIDConnectProvider", err)
if err != nil {
return nil, err
}
}
}
if delta.DifferentAt("Spec.Tags") {
if err := rm.syncTags(ctx, desired); err != nil {
return nil, err
}
}
// There really isn't a status of a role... it either exists or doesn't. If
// we get here, that means the update was successful and the desired state
// of the role matches what we provided...
ackcondition.SetSynced(desired, corev1.ConditionTrue, nil, nil)
return desired, nil
}
// custom comparison function for comparing
// - lists of Tag structs where the order of the structs in the list is not important.
// - URLs where a prefix of https:// should be disregarded
func customPreCompare(
delta *ackcompare.Delta,
a *resource,
b *resource,
) {
if len(a.ko.Spec.Tags) != len(b.ko.Spec.Tags) {
delta.Add("Spec.Tags", a.ko.Spec.Tags, b.ko.Spec.Tags)
} else if len(a.ko.Spec.Tags) > 0 {
if !commonutil.EqualTags(a.ko.Spec.Tags, b.ko.Spec.Tags) {
delta.Add("Spec.Tags", a.ko.Spec.Tags, b.ko.Spec.Tags)
}
}
if ackcompare.HasNilDifference(a.ko.Spec.URL, b.ko.Spec.URL) {
delta.Add("Spec.URL", a.ko.Spec.URL, b.ko.Spec.URL)
} else if a.ko.Spec.URL != nil && b.ko.Spec.URL != nil {
// the URL field must begin with "https://"
// c.f. https://docs.aws.amazon.com/IAM/latest/APIReference/API_CreateOpenIDConnectProvider.html
// however, when stored in the IAM backend, the "https://" prefix is stripped out
// thus here we treat "https://" as a null token for the purposes of string comparison
if *a.ko.Spec.URL != *b.ko.Spec.URL {
aURL := strings.TrimPrefix(*a.ko.Spec.URL, "https://")
bURL := strings.TrimPrefix(*b.ko.Spec.URL, "https://")
if aURL != bURL {
delta.Add("Spec.URL", a.ko.Spec.URL, b.ko.Spec.URL)
}
}
}
}
// syncTags examines the Tags in the supplied OpenIDConnectProvider and calls the ListOpenIDConnectProviderTags,
// TagOpenIDConnectProvider and UntagOpenIDConnectProvider API endpoints to ensure that the set of associated Tags stays
// in sync with the OpenIDConnectProvider.Spec.Tags
func (rm *resourceManager) syncTags(
ctx context.Context,
r *resource,
) (err error) {
rlog := ackrtlog.FromContext(ctx)
exit := rlog.Trace("rm.syncTags")
defer func(err error) { exit(err) }(err)
toAdd := []*svcapitypes.Tag{}
toDelete := []*svcapitypes.Tag{}
existingTags, err := rm.getTags(ctx, r)
if err != nil {
return err
}
for _, t := range r.ko.Spec.Tags {
if !inTags(*t.Key, *t.Value, existingTags) {
toAdd = append(toAdd, t)
}
}
for _, t := range existingTags {
if !inTags(*t.Key, *t.Value, r.ko.Spec.Tags) {
toDelete = append(toDelete, t)
}
}
if len(toAdd) > 0 {
for _, t := range toAdd {
rlog.Debug("adding tag to OpenIDConnectProvider", "key", *t.Key, "value", *t.Value)
}
if err = rm.addTags(ctx, r, toAdd); err != nil {
return err
}
}
if len(toDelete) > 0 {
for _, t := range toDelete {
rlog.Debug("removing tag from OpenIDConnectProvider", "key", *t.Key, "value", *t.Value)
}
if err = rm.removeTags(ctx, r, toDelete); err != nil {
return err
}
}
return nil
}
// inTags returns true if the supplied key and value can be found in the
// supplied list of Tag structs.
//
// TODO(jaypipes): When we finally standardize Tag handling in ACK, move this
// to the ACK common runtime/ or pkg/ repos
func inTags(
key string,
value string,
tags []*svcapitypes.Tag,
) bool {
for _, t := range tags {
if *t.Key == key && t.Value != nil && *t.Value == value {
return true
}
}
return false
}
// getTags returns the list of tags attached to the OpenIDConnectProvider
func (rm *resourceManager) getTags(
ctx context.Context,
r *resource,
) ([]*svcapitypes.Tag, error) {
var err error
var resp *svcsdk.ListOpenIDConnectProviderTagsOutput
rlog := ackrtlog.FromContext(ctx)
exit := rlog.Trace("rm.getTags")
defer func() { exit(err) }()
input := &svcsdk.ListOpenIDConnectProviderTagsInput{}
input.OpenIDConnectProviderArn = (*string)(r.ko.Status.ACKResourceMetadata.ARN)
res := []*svcapitypes.Tag{}
for {
resp, err = rm.sdkapi.ListOpenIDConnectProviderTagsWithContext(ctx, input)
if err != nil || resp == nil {
break
}
for _, t := range resp.Tags {
res = append(res, &svcapitypes.Tag{Key: t.Key, Value: t.Value})
}
if resp.IsTruncated != nil && !*resp.IsTruncated {
break
}
input.SetMarker(*resp.Marker)
rm.metrics.RecordAPICall("READ_MANY", "ListOpenIDConnectProviderTags", err)
}
return res, err
}
// addTags adds the supplied Tags to the supplied OpenIDConnectProvider resource
func (rm *resourceManager) addTags(
ctx context.Context,
r *resource,
tags []*svcapitypes.Tag,
) (err error) {
rlog := ackrtlog.FromContext(ctx)
exit := rlog.Trace("rm.addTags")
defer func() { exit(err) }()
input := &svcsdk.TagOpenIDConnectProviderInput{}
input.OpenIDConnectProviderArn = (*string)(r.ko.Status.ACKResourceMetadata.ARN)
inTags := []*svcsdk.Tag{}
for _, t := range tags {
inTags = append(inTags, &svcsdk.Tag{Key: t.Key, Value: t.Value})
}
input.Tags = inTags
_, err = rm.sdkapi.TagOpenIDConnectProviderWithContext(ctx, input)
rm.metrics.RecordAPICall("UPDATE", "TagOpenIDConnectProvider", err)
return err
}
// removeTags removes the supplied Tags from the supplied OpenIDConnectProvider resource
func (rm *resourceManager) removeTags(
ctx context.Context,
r *resource,
tags []*svcapitypes.Tag,
) (err error) {
rlog := ackrtlog.FromContext(ctx)
exit := rlog.Trace("rm.removeTags")
defer func() { exit(err) }()
input := &svcsdk.UntagOpenIDConnectProviderInput{}
input.OpenIDConnectProviderArn = (*string)(r.ko.Status.ACKResourceMetadata.ARN)
inTagKeys := []*string{}
for _, t := range tags {
inTagKeys = append(inTagKeys, t.Key)
}
input.TagKeys = inTagKeys
_, err = rm.sdkapi.UntagOpenIDConnectProviderWithContext(ctx, input)
rm.metrics.RecordAPICall("UPDATE", "UntagOpenIDConnectProvider", err)
return err
}
// returns an SDK-specific struct for the HTTP request
// payload of the UpdateThumbprint API call for the resource
func (rm *resourceManager) newUpdateThumbprintRequestPayload(
ctx context.Context,
r *resource,
) (*svcsdk.UpdateOpenIDConnectProviderThumbprintInput, error) {
res := &svcsdk.UpdateOpenIDConnectProviderThumbprintInput{}
if r.ko.Spec.Thumbprints != nil {
res.SetThumbprintList(r.ko.Spec.Thumbprints)
}
if r.ko.Status.ACKResourceMetadata.ARN != nil {
res.SetOpenIDConnectProviderArn(string(*r.ko.Status.ACKResourceMetadata.ARN))
}
return res, nil
}
// returns an SDK-specific struct for the HTTP request
// payload of the AddClientIDToOpenIDConnectProvider API call for the resource
func (rm *resourceManager) newAddClientIDRequestPayload(
ctx context.Context,
r *resource,
clientId *string,
) (*svcsdk.AddClientIDToOpenIDConnectProviderInput, error) {
res := &svcsdk.AddClientIDToOpenIDConnectProviderInput{}
if clientId != nil {
res.SetClientID(*clientId)
}
if r.ko.Status.ACKResourceMetadata.ARN != nil {
res.SetOpenIDConnectProviderArn(string(*r.ko.Status.ACKResourceMetadata.ARN))
}
return res, nil
}
// returns an SDK-specific struct for the HTTP request
// payload of the RemoveClientIDFromOpenIDConnectProvider API call for the resource
func (rm *resourceManager) newRemoveClientIDRequestPayload(
ctx context.Context,
r *resource,
clientId *string,
) (*svcsdk.RemoveClientIDFromOpenIDConnectProviderInput, error) {
res := &svcsdk.RemoveClientIDFromOpenIDConnectProviderInput{}
if clientId != nil {
res.SetClientID(*clientId)
}
if r.ko.Status.ACKResourceMetadata.ARN != nil {
res.SetOpenIDConnectProviderArn(string(*r.ko.Status.ACKResourceMetadata.ARN))
}
return res, nil
}