Skip to content

Commit 43d0ea8

Browse files
authored
Fix bug in updating group.spec.inlinePolicies (#84)
The bug occurs when updating an existing item in `spec.inlinePolicies`. The code incorrectly includes both the old(to remove) and new(to add)values when using `lo.Difference`. This causes the code to first attempt to replace the xisting policies with a call to `iam::PutGroupPolicy` and then mistakenly "delete" them right after. This patch fixes this bug by calling `lo.Find` to double check whether we need to keep an inline policy (if it only needs to be updated) By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 5e71c09 commit 43d0ea8

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

pkg/resource/group/hooks.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,11 @@ func (rm *resourceManager) syncInlinePolicies(
177177
}
178178
}
179179
for _, pair := range toDelete {
180+
// do not remove elements we just updated with `addInlinePolicy`
181+
if _, ok := lo.Find(toAdd, func(entry lo.Entry[string, string]) bool { return entry.Key == pair.Key }); ok {
182+
continue
183+
}
184+
180185
polName := pair.Key
181186
rlog.Debug(
182187
"removing inline policy from group",

test/e2e/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
acktest @ git+https://github.com/aws-controllers-k8s/test-infra.git@75752b2
1+
acktest @ git+https://github.com/aws-controllers-k8s/test-infra.git@371852014efcb8c26c454f861eb546c93a48f205

test/e2e/tests/test_group.py

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,20 @@ def test_crud(self, simple_group):
109109
"Action": ["ec2:Get*"],
110110
"Resource": ["*"]
111111
}]
112+
}'''
113+
inline_doc_2 = '''{
114+
"Version": "2012-10-17",
115+
"Statement": [{
116+
"Effect": "Allow",
117+
"Action": ["s3:Get*"],
118+
"Resource": ["*"]
119+
}]
112120
}'''
113121
updates = {
114122
"spec": {
115123
"inlinePolicies": {
116124
"ec2get": inline_doc,
125+
"s3get": inline_doc_2,
117126
},
118127
},
119128
}
@@ -122,21 +131,58 @@ def test_crud(self, simple_group):
122131

123132
expect_inline_policies = {
124133
'ec2get': inline_doc,
134+
's3get': inline_doc_2,
125135
}
126136
cr = k8s.get_resource(ref)
127137
assert cr is not None
128138
assert 'spec' in cr
129139
assert 'inlinePolicies' in cr['spec']
130-
assert len(cr['spec']['inlinePolicies']) == 1
140+
assert len(cr['spec']['inlinePolicies']) == 2
131141
assert expect_inline_policies == cr['spec']['inlinePolicies']
132142

133143
latest_inline_policies = group.get_inline_policies(group_name)
134-
assert len(latest_inline_policies) == 1
144+
assert len(latest_inline_policies) == 2
135145
assert 'ec2get' in latest_inline_policies
146+
136147
got_pol_doc = latest_inline_policies['ec2get']
137148
nospace_got_doc = "".join(c for c in got_pol_doc if not c.isspace())
138149
nospace_exp_doc = "".join(c for c in inline_doc if not c.isspace())
150+
assert nospace_exp_doc == nospace_got_doc
139151

152+
got_pol_doc = latest_inline_policies['s3get']
153+
nospace_got_doc = "".join(c for c in got_pol_doc if not c.isspace())
154+
nospace_exp_doc = "".join(c for c in inline_doc_2 if not c.isspace())
155+
assert nospace_exp_doc == nospace_got_doc
156+
157+
inline_doc_s3_get_object = '''{
158+
"Version": "2012-10-17",
159+
"Statement": [{
160+
"Effect": "Allow",
161+
"Action": ["s3:GetObject"],
162+
"Resource": ["*"]
163+
}]
164+
}'''
165+
# update s3get policy document
166+
updates = {
167+
"spec": {
168+
"inlinePolicies": {
169+
"ec2get": inline_doc,
170+
"s3get": inline_doc_s3_get_object,
171+
},
172+
},
173+
}
174+
k8s.patch_custom_resource(ref, updates)
175+
time.sleep(MODIFY_WAIT_AFTER_SECONDS)
176+
177+
latest_inline_policies = group.get_inline_policies(group_name)
178+
assert len(latest_inline_policies) == 2
179+
assert 's3get' in latest_inline_policies
180+
assert 'ec2get' in latest_inline_policies
181+
182+
# expect s3get policy document to change into inlinde_doc_s3_get_object
183+
got_pol_doc = latest_inline_policies['s3get']
184+
nospace_got_doc = "".join(c for c in got_pol_doc if not c.isspace())
185+
nospace_exp_doc = "".join(c for c in inline_doc_s3_get_object if not c.isspace())
140186
assert nospace_exp_doc == nospace_got_doc
141187

142188
# Remove the inline policy we just added and check the updates are

0 commit comments

Comments
 (0)