Debian Codegraph @

ansible_collections/amazon/aws/tests/unit/plugins/module_utils/elbv2/test_prune.py

raw
1 #
2 # (c) 2021 Red Hat Inc.
3 #
4 # This file is part of Ansible
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 import pytest
8
9 from ansible_collections.amazon.aws.plugins.module_utils import elbv2
10
11 example_arn = "arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/nlb-123456789abc/abcdef0123456789"
12 example_arn2 = "arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/nlb-0123456789ab/0123456789abcdef"
13
14 one_action = [
15 dict(
16 ForwardConfig=dict(
17 TargetGroupStickinessConfig=dict(Enabled=False),
18 TargetGroups=[
19 dict(TargetGroupArn=example_arn, Weight=1),
20 ],
21 ),
22 TargetGroupArn=example_arn,
23 Type="forward",
24 )
25 ]
26
27 one_action_two_tg = [
28 dict(
29 ForwardConfig=dict(
30 TargetGroupStickinessConfig=dict(Enabled=False),
31 TargetGroups=[
32 dict(TargetGroupArn=example_arn, Weight=1),
33 dict(TargetGroupArn=example_arn2, Weight=1),
34 ],
35 ),
36 TargetGroupArn=example_arn,
37 Type="forward",
38 )
39 ]
40
41 simplified_action = dict(Type="forward", TargetGroupArn=example_arn)
42 # Examples of various minimalistic actions which are all the same
43 simple_actions = [
44 dict(Type="forward", TargetGroupArn=example_arn),
45 dict(
46 Type="forward", TargetGroupArn=example_arn, ForwardConfig=dict(TargetGroups=[dict(TargetGroupArn=example_arn)])
47 ),
48 dict(Type="forward", ForwardConfig=dict(TargetGroups=[dict(TargetGroupArn=example_arn)])),
49 dict(
50 Type="forward",
51 TargetGroupArn=example_arn,
52 ForwardConfig=dict(TargetGroups=[dict(TargetGroupArn=example_arn, Weight=1)]),
53 ),
54 dict(Type="forward", ForwardConfig=dict(TargetGroups=[dict(TargetGroupArn=example_arn, Weight=1)])),
55 dict(
56 Type="forward",
57 TargetGroupArn=example_arn,
58 ForwardConfig=dict(TargetGroups=[dict(TargetGroupArn=example_arn, Weight=42)]),
59 ),
60 dict(Type="forward", ForwardConfig=dict(TargetGroups=[dict(TargetGroupArn=example_arn, Weight=42)])),
61 dict(
62 Type="forward",
63 TargetGroupArn=example_arn,
64 ForwardConfig=dict(
65 TargetGroupStickinessConfig=dict(Enabled=False), TargetGroups=[dict(TargetGroupArn=example_arn)]
66 ),
67 ),
68 dict(
69 Type="forward",
70 ForwardConfig=dict(
71 TargetGroupStickinessConfig=dict(Enabled=False), TargetGroups=[dict(TargetGroupArn=example_arn)]
72 ),
73 ),
74 dict(
75 Type="forward",
76 TargetGroupArn=example_arn,
77 ForwardConfig=dict(
78 TargetGroupStickinessConfig=dict(Enabled=False), TargetGroups=[dict(TargetGroupArn=example_arn, Weight=1)]
79 ),
80 ),
81 dict(
82 Type="forward",
83 ForwardConfig=dict(
84 TargetGroupStickinessConfig=dict(Enabled=False), TargetGroups=[dict(TargetGroupArn=example_arn, Weight=1)]
85 ),
86 ),
87 dict(
88 Type="forward",
89 TargetGroupArn=example_arn,
90 ForwardConfig=dict(
91 TargetGroupStickinessConfig=dict(Enabled=False), TargetGroups=[dict(TargetGroupArn=example_arn, Weight=42)]
92 ),
93 ),
94 dict(
95 Type="forward",
96 ForwardConfig=dict(
97 TargetGroupStickinessConfig=dict(Enabled=False), TargetGroups=[dict(TargetGroupArn=example_arn, Weight=42)]
98 ),
99 ),
100 ]
101
102 # Test that _prune_ForwardConfig() doesn't mangle things we don't expect
103 complex_actions = [
104 # Non-Forwarding
105 dict(
106 Type="authenticate-oidc",
107 TargetGroupArn=example_arn,
108 AuthenticateOidcConfig=dict(
109 Issuer="https://idp.ansible.test/oidc-config",
110 AuthorizationEndpoint="https://idp.ansible.test/authz",
111 TokenEndpoint="https://idp.ansible.test/token",
112 UserInfoEndpoint="https://idp.ansible.test/user",
113 ClientId="ExampleClient",
114 UseExistingClientSecret=False,
115 ),
116 ),
117 dict(
118 Type="redirect",
119 RedirectConfig=dict(Protocol="HTTPS", Port=443, Host="redirect.ansible.test", Path="/", StatusCode="HTTP_302"),
120 ),
121 # Multiple TGs
122 dict(
123 TargetGroupArn=example_arn,
124 Type="forward",
125 ForwardConfig=dict(
126 TargetGroupStickinessConfig=dict(Enabled=False),
127 TargetGroups=[
128 dict(TargetGroupArn=example_arn, Weight=1),
129 dict(TargetGroupArn=example_arn2, Weight=1),
130 ],
131 ),
132 ),
133 # Sticky-Sessions
134 dict(
135 Type="forward",
136 TargetGroupArn=example_arn,
137 ForwardConfig=dict(
138 TargetGroupStickinessConfig=dict(Enabled=True, DurationSeconds=3600),
139 TargetGroups=[dict(TargetGroupArn=example_arn)],
140 ),
141 ),
142 ]
143
144 simplified_oidc_action = dict(
145 Type="authenticate-oidc",
146 TargetGroupArn=example_arn,
147 AuthenticateOidcConfig=dict(
148 Issuer="https://idp.ansible.test/oidc-config",
149 AuthorizationEndpoint="https://idp.ansible.test/authz",
150 TokenEndpoint="https://idp.ansible.test/token",
151 UserInfoEndpoint="https://idp.ansible.test/user",
152 ClientId="ExampleClient",
153 Scope="openid",
154 SessionTimeout=604800,
155 UseExistingClientSecret=True,
156 OnUnauthenticatedRequest="authenticate",
157 SessionCookieName="AWSELBAuthSessionCookie",
158 ),
159 )
160 oidc_actions = [
161 dict(
162 Type="authenticate-oidc",
163 TargetGroupArn=example_arn,
164 AuthenticateOidcConfig=dict(
165 Issuer="https://idp.ansible.test/oidc-config",
166 AuthorizationEndpoint="https://idp.ansible.test/authz",
167 TokenEndpoint="https://idp.ansible.test/token",
168 UserInfoEndpoint="https://idp.ansible.test/user",
169 ClientId="ExampleClient",
170 UseExistingClientSecret=True,
171 Scope="openid",
172 SessionTimeout=604800,
173 ),
174 ),
175 dict(
176 Type="authenticate-oidc",
177 TargetGroupArn=example_arn,
178 AuthenticateOidcConfig=dict(
179 Issuer="https://idp.ansible.test/oidc-config",
180 AuthorizationEndpoint="https://idp.ansible.test/authz",
181 TokenEndpoint="https://idp.ansible.test/token",
182 UserInfoEndpoint="https://idp.ansible.test/user",
183 ClientId="ExampleClient",
184 ClientSecret="MyVerySecretString",
185 UseExistingClientSecret=True,
186 ),
187 ),
188 ]
189
190
191 ####
192
193
194 # Original tests
195 def test__prune_secret():
196 assert elbv2._prune_secret(one_action[0]) == one_action[0]
197
198
199 def test__prune_ForwardConfig():
200 expectation = {"TargetGroupArn": example_arn, "Type": "forward"}
201 pruned_config = elbv2._prune_ForwardConfig(one_action[0])
202 assert pruned_config == expectation
203
204 # https://github.com/ansible-collections/community.aws/issues/1089
205 pruned_config = elbv2._prune_ForwardConfig(one_action_two_tg[0])
206 assert pruned_config == one_action_two_tg[0]
207
208
209 ####
210
211
212 @pytest.mark.parametrize("action", simple_actions)
213 def test__prune_ForwardConfig_simplifiable_actions(action):
214 pruned_config = elbv2._prune_ForwardConfig(action)
215 assert pruned_config == simplified_action
216
217
218 @pytest.mark.parametrize("action", complex_actions)
219 def test__prune_ForwardConfig_non_simplifiable_actions(action):
220 pruned_config = elbv2._prune_ForwardConfig(action)
221 assert pruned_config == action
222
223
224 @pytest.mark.parametrize("action", oidc_actions)
225 def test__prune_secret_simplifiable_actions(action):
226 pruned_config = elbv2._prune_secret(action)
227 assert pruned_config == simplified_oidc_action
228
229
230 @pytest.mark.parametrize("action", complex_actions)
231 def test__prune_secret_non_simplifiable_actions(action):
232 pruned_config = elbv2._prune_secret(action)
233 assert pruned_config == action
234
235
236 # Test _simple_forward_config_arn
237 @pytest.mark.parametrize(
238 "config,parent_arn,expected",
239 [
240 ({}, None, None),
241 ({"TargetGroupStickinessConfig": {"Enabled": True}}, None, None),
242 ({"TargetGroupStickinessConfig": {"Enabled": False}}, None, None),
243 ({"Some": "info"}, None, None),
244 ({"TargetGroups": ["group1", "group2"]}, None, None),
245 ({"TargetGroupStickinessConfig": {"Enabled": False}}, "parent_arn", "parent_arn"),
246 ({"TargetGroups": [{"Weight": 2, "TargetGroupArn": "group_arn", "other": "key"}]}, None, None),
247 ({"TargetGroups": [{"Weight": 2, "other": "key"}]}, None, None),
248 ({"TargetGroups": [{"Weight": 2, "TargetGroupArn": "group_arn", "other": "key"}]}, None, None),
249 ({"TargetGroups": [{"Weight": 2}]}, "parent_arn", "parent_arn"),
250 (
251 {
252 "TargetGroupStickinessConfig": {"Enabled": False},
253 "TargetGroups": [{"Weight": 2, "TargetGroupArn": "group_arn"}],
254 },
255 "parent_arn",
256 None,
257 ),
258 (
259 {
260 "TargetGroupStickinessConfig": {"Enabled": False},
261 "TargetGroups": [{"Weight": 2, "TargetGroupArn": "group_arn"}],
262 },
263 "group_arn",
264 "group_arn",
265 ),
266 ],
267 )
268 def test__simple_forward_config_arn(config, parent_arn, expected):
269 assert elbv2._simple_forward_config_arn(config, parent_arn) == expected
270
271
272 @pytest.mark.parametrize(
273 "action,expected",
274 [
275 ({"Type": "logger"}, {"Type": "logger"}),
276 (
277 {"Type": "authenticate-oidc", "AuthenticateOidcConfig": {}},
278 {"Type": "authenticate-oidc", "AuthenticateOidcConfig": {"UseExistingClientSecret": True}},
279 ),
280 ],
281 )
282 def test__append_use_existing_client_secretn(action, expected):
283 assert elbv2._append_use_existing_client_secretn(action) == expected