Skip to content

Commit 2fb6c79

Browse files
authored
ensure comparing maps of strings not pointers (#70)
For the inline policies, I used `github.com/samber/lo.Difference` function to compare two sets of k/v pairs. Unfortunately, I forgot to dereference the `*string` values of the inline policy `map[string]*string` into a `map[string]string` before doing so, which meant that the `lo.Difference` function was comparing against pointer values. Fixes issue aws-controllers-k8s/community#1735 By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 8af5bdf commit 2fb6c79

File tree

4 files changed

+49
-9
lines changed

4 files changed

+49
-9
lines changed

pkg/resource/group/hooks.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
ackutil "github.com/aws-controllers-k8s/runtime/pkg/util"
2222
svcsdk "github.com/aws/aws-sdk-go/service/iam"
2323
"github.com/samber/lo"
24+
25+
commonutil "github.com/aws-controllers-k8s/iam-controller/pkg/util"
2426
)
2527

2628
// syncManagedPolicies examines the managed PolicyARNs in the supplied Group
@@ -153,8 +155,12 @@ func (rm *resourceManager) syncInlinePolicies(
153155

154156
existingPolicies := latest.ko.Spec.InlinePolicies
155157

156-
existingPairs := lo.ToPairs(existingPolicies)
157-
desiredPairs := lo.ToPairs(desired.ko.Spec.InlinePolicies)
158+
existingPairs := lo.ToPairs(
159+
commonutil.MapStringFromMapStringPointers(existingPolicies),
160+
)
161+
desiredPairs := lo.ToPairs(
162+
commonutil.MapStringFromMapStringPointers(desired.ko.Spec.InlinePolicies),
163+
)
158164

159165
toDelete, toAdd := lo.Difference(existingPairs, desiredPairs)
160166

@@ -165,7 +171,7 @@ func (rm *resourceManager) syncInlinePolicies(
165171
"adding inline policy to group",
166172
"policy_name", polName,
167173
)
168-
err = rm.addInlinePolicy(ctx, desired, polName, polDoc)
174+
err = rm.addInlinePolicy(ctx, desired, polName, &polDoc)
169175
if err != nil {
170176
return err
171177
}

pkg/resource/role/hooks.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,12 @@ func (rm *resourceManager) syncInlinePolicies(
205205

206206
existingPolicies := latest.ko.Spec.InlinePolicies
207207

208-
existingPairs := lo.ToPairs(existingPolicies)
209-
desiredPairs := lo.ToPairs(desired.ko.Spec.InlinePolicies)
208+
existingPairs := lo.ToPairs(
209+
commonutil.MapStringFromMapStringPointers(existingPolicies),
210+
)
211+
desiredPairs := lo.ToPairs(
212+
commonutil.MapStringFromMapStringPointers(desired.ko.Spec.InlinePolicies),
213+
)
210214

211215
toDelete, toAdd := lo.Difference(existingPairs, desiredPairs)
212216

@@ -217,7 +221,7 @@ func (rm *resourceManager) syncInlinePolicies(
217221
"adding inline policy to role",
218222
"policy_name", polName,
219223
)
220-
err = rm.addInlinePolicy(ctx, desired, polName, polDoc)
224+
err = rm.addInlinePolicy(ctx, desired, polName, &polDoc)
221225
if err != nil {
222226
return err
223227
}

pkg/resource/user/hooks.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,12 @@ func (rm *resourceManager) syncInlinePolicies(
212212

213213
existingPolicies := latest.ko.Spec.InlinePolicies
214214

215-
existingPairs := lo.ToPairs(existingPolicies)
216-
desiredPairs := lo.ToPairs(desired.ko.Spec.InlinePolicies)
215+
existingPairs := lo.ToPairs(
216+
commonutil.MapStringFromMapStringPointers(existingPolicies),
217+
)
218+
desiredPairs := lo.ToPairs(
219+
commonutil.MapStringFromMapStringPointers(desired.ko.Spec.InlinePolicies),
220+
)
217221

218222
toDelete, toAdd := lo.Difference(existingPairs, desiredPairs)
219223

@@ -224,7 +228,7 @@ func (rm *resourceManager) syncInlinePolicies(
224228
"adding inline policy to user",
225229
"policy_name", polName,
226230
)
227-
err = rm.addInlinePolicy(ctx, desired, polName, polDoc)
231+
err = rm.addInlinePolicy(ctx, desired, polName, &polDoc)
228232
if err != nil {
229233
return err
230234
}

pkg/util/map.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"). You may
4+
// not use this file except in compliance with the License. A copy of the
5+
// License is located at
6+
//
7+
// http://aws.amazon.com/apache2.0/
8+
//
9+
// or in the "license" file accompanying this file. This file is distributed
10+
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
// express or implied. See the License for the specific language governing
12+
// permissions and limitations under the License.
13+
14+
package util
15+
16+
// MapStringFromMapStringPointers takes a map[string]*string and returns a
17+
// map[string]string
18+
func MapStringFromMapStringPointers(
19+
subject map[string]*string,
20+
) map[string]string {
21+
result := map[string]string{}
22+
for k, v := range subject {
23+
result[k] = *v
24+
}
25+
return result
26+
}

0 commit comments

Comments
 (0)