-
Notifications
You must be signed in to change notification settings - Fork 14.3k
/
test_systems_manager.py
385 lines (335 loc) · 14.5 KB
/
test_systems_manager.py
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
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License 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.
from __future__ import annotations
import json
from unittest import mock
import pytest
from moto import mock_aws
from airflow.configuration import initialize_secrets_backends
from airflow.providers.amazon.aws.secrets.systems_manager import SystemsManagerParameterStoreBackend
from tests_common.test_utils.config import conf_vars
URI_CONNECTION = pytest.param(
"postgres://my-login:my-pass@my-host:5432/my-schema?param1=val1¶m2=val2", id="uri-connection"
)
JSON_CONNECTION = pytest.param(
json.dumps(
{
"conn_type": "postgres",
"login": "my-login",
"password": "my-pass",
"host": "my-host",
"port": 5432,
"schema": "my-schema",
"extra": {"param1": "val1", "param2": "val2"},
}
),
id="json-connection",
)
class TestSsmSecrets:
@mock.patch(
"airflow.providers.amazon.aws.secrets.systems_manager."
"SystemsManagerParameterStoreBackend.get_conn_value"
)
def test_aws_ssm_get_connection(self, mock_get_value):
mock_get_value.return_value = "scheme://user:pass@host:100"
conn = SystemsManagerParameterStoreBackend().get_connection("fake_conn")
assert conn.host == "host"
@mock_aws
@pytest.mark.parametrize("ssm_value", [JSON_CONNECTION, URI_CONNECTION])
def test_get_conn_value(self, ssm_value):
param = {
"Name": "/airflow/connections/test_postgres",
"Type": "String",
"Value": ssm_value,
}
ssm_backend = SystemsManagerParameterStoreBackend()
ssm_backend.client.put_parameter(**param)
returned_conn_value = ssm_backend.get_conn_value(conn_id="test_postgres")
assert ssm_value == returned_conn_value
test_conn = ssm_backend.get_connection(conn_id="test_postgres")
assert test_conn.conn_id == "test_postgres"
assert test_conn.conn_type == "postgres"
assert test_conn.login == "my-login"
assert test_conn.password == "my-pass"
assert test_conn.host == "my-host"
assert test_conn.port == 5432
assert test_conn.schema == "my-schema"
assert test_conn.extra_dejson == {"param1": "val1", "param2": "val2"}
@mock_aws
def test_get_conn_value_non_existent_key(self):
"""
Test that if the key with connection ID is not present in SSM,
SystemsManagerParameterStoreBackend.get_connection should return None
"""
conn_id = "test_mysql"
param = {
"Name": "/airflow/connections/test_postgres",
"Type": "String",
"Value": "postgresql://airflow:airflow@host:5432/airflow",
}
ssm_backend = SystemsManagerParameterStoreBackend()
ssm_backend.client.put_parameter(**param)
assert ssm_backend.get_conn_value(conn_id=conn_id) is None
assert ssm_backend.get_connection(conn_id=conn_id) is None
@mock_aws
def test_get_variable(self):
param = {"Name": "/airflow/variables/hello", "Type": "String", "Value": "world"}
ssm_backend = SystemsManagerParameterStoreBackend()
ssm_backend.client.put_parameter(**param)
returned_uri = ssm_backend.get_variable("hello")
assert "world" == returned_uri
@mock_aws
def test_get_config(self):
param = {
"Name": "/airflow/config/sql_alchemy_conn",
"Type": "String",
"Value": "sqlite:///Users/test_user/airflow.db",
}
ssm_backend = SystemsManagerParameterStoreBackend()
ssm_backend.client.put_parameter(**param)
returned_uri = ssm_backend.get_config("sql_alchemy_conn")
assert "sqlite:///Users/test_user/airflow.db" == returned_uri
@mock_aws
def test_get_variable_secret_string(self):
param = {"Name": "/airflow/variables/hello", "Type": "SecureString", "Value": "world"}
ssm_backend = SystemsManagerParameterStoreBackend()
ssm_backend.client.put_parameter(**param)
returned_uri = ssm_backend.get_variable("hello")
assert "world" == returned_uri
@mock_aws
def test_get_variable_non_existent_key(self):
"""
Test that if Variable key is not present in SSM,
SystemsManagerParameterStoreBackend.get_variables should return None
"""
param = {"Name": "/airflow/variables/hello", "Type": "String", "Value": "world"}
ssm_backend = SystemsManagerParameterStoreBackend()
ssm_backend.client.put_parameter(**param)
assert ssm_backend.get_variable("test_mysql") is None
@conf_vars(
{
("secrets", "backend"): "airflow.providers.amazon.aws.secrets.systems_manager."
"SystemsManagerParameterStoreBackend",
(
"secrets",
"backend_kwargs",
): '{"use_ssl": false, "role_arn": "arn:aws:iam::222222222222:role/awesome-role"}',
}
)
@mock.patch("airflow.providers.amazon.aws.hooks.base_aws.SessionFactory")
def test_passing_client_kwargs(self, mock_session_factory):
backends = initialize_secrets_backends()
systems_manager = next(
backend
for backend in backends
if backend.__class__.__name__ == "SystemsManagerParameterStoreBackend"
)
# Mock SessionFactory, session and client
mock_session_factory_instance = mock_session_factory.return_value
mock_ssm_client = mock.MagicMock(return_value="mock-ssm-client")
mock_session = mock.MagicMock()
mock_session.client = mock_ssm_client
mock_create_session = mock.MagicMock(return_value=mock_session)
mock_session_factory_instance.create_session = mock_create_session
systems_manager.client
assert mock_session_factory.call_count == 1
mock_session_factory_call_kwargs = mock_session_factory.call_args.kwargs
assert "conn" in mock_session_factory_call_kwargs
conn_wrapper = mock_session_factory_call_kwargs["conn"]
assert conn_wrapper.conn_id == "SystemsManagerParameterStoreBackend__connection"
assert conn_wrapper.role_arn == "arn:aws:iam::222222222222:role/awesome-role"
mock_ssm_client.assert_called_once_with(service_name="ssm", use_ssl=False)
@mock.patch(
"airflow.providers.amazon.aws.secrets.systems_manager."
"SystemsManagerParameterStoreBackend._get_secret"
)
def test_connection_prefix_none_value(self, mock_get_secret):
"""
Test that if Variable key is not present in SSM,
SystemsManagerParameterStoreBackend.get_conn_value should return None,
SystemsManagerParameterStoreBackend._get_secret should not be called
"""
kwargs = {"connections_prefix": None}
ssm_backend = SystemsManagerParameterStoreBackend(**kwargs)
assert ssm_backend.get_conn_value("test_mysql") is None
mock_get_secret.assert_not_called()
@mock.patch(
"airflow.providers.amazon.aws.secrets.systems_manager."
"SystemsManagerParameterStoreBackend._get_secret"
)
def test_variable_prefix_none_value(self, mock_get_secret):
"""
Test that if Variable key is not present in SSM,
SystemsManagerParameterStoreBackend.get_variables should return None,
SystemsManagerParameterStoreBackend._get_secret should not be called
"""
kwargs = {"variables_prefix": None}
ssm_backend = SystemsManagerParameterStoreBackend(**kwargs)
assert ssm_backend.get_variable("hello") is None
mock_get_secret.assert_not_called()
@mock.patch(
"airflow.providers.amazon.aws.secrets.systems_manager."
"SystemsManagerParameterStoreBackend._get_secret"
)
def test_config_prefix_none_value(self, mock_get_secret):
"""
Test that if Variable key is not present in SSM,
SystemsManagerParameterStoreBackend.get_config should return None,
SystemsManagerParameterStoreBackend._get_secret should not be called
"""
kwargs = {"config_prefix": None}
ssm_backend = SystemsManagerParameterStoreBackend(**kwargs)
assert ssm_backend.get_config("config") is None
mock_get_secret.assert_not_called()
@mock.patch(
"airflow.providers.amazon.aws.secrets.systems_manager.SystemsManagerParameterStoreBackend.client",
new_callable=mock.PropertyMock,
)
@pytest.mark.parametrize(
"connection_id, connections_lookup_pattern, num_client_calls",
[
("test", "test", 1),
("test", ".*", 1),
("test", "T.*", 1),
("test", "dummy-pattern", 0),
("test", None, 1),
],
)
def test_connection_lookup_pattern(
self, mock_client, connection_id, connections_lookup_pattern, num_client_calls
):
"""
Test that if Connection ID is looked up in AWS Parameter Store
"""
mock_client().get_parameter.return_value = {
"Parameter": {
"Value": None,
},
}
kwargs = {"connections_lookup_pattern": connections_lookup_pattern}
secrets_manager_backend = SystemsManagerParameterStoreBackend(**kwargs)
secrets_manager_backend.get_conn_value(connection_id)
assert mock_client().get_parameter.call_count == num_client_calls
@mock.patch(
"airflow.providers.amazon.aws.secrets.systems_manager.SystemsManagerParameterStoreBackend.client",
new_callable=mock.PropertyMock,
)
@pytest.mark.parametrize(
"variable_key, variables_lookup_pattern, num_client_calls",
[
("test", "test", 1),
("test", ".*", 1),
("test", "T.*", 1),
("test", "dummy-pattern", 0),
("test", None, 1),
],
)
def test_variable_lookup_pattern(
self, mock_client, variable_key, variables_lookup_pattern, num_client_calls
):
"""
Test that if Variable key is looked up in AWS Parameter Store
"""
mock_client().get_parameter.return_value = {
"Parameter": {
"Value": None,
},
}
kwargs = {"variables_lookup_pattern": variables_lookup_pattern}
secrets_manager_backend = SystemsManagerParameterStoreBackend(**kwargs)
secrets_manager_backend.get_variable(variable_key)
assert mock_client().get_parameter.call_count == num_client_calls
@mock.patch(
"airflow.providers.amazon.aws.secrets.systems_manager.SystemsManagerParameterStoreBackend.client",
new_callable=mock.PropertyMock,
)
@pytest.mark.parametrize(
"config_key, config_lookup_pattern, num_client_calls",
[
("test", "test", 1),
("test", ".*", 1),
("test", "T.*", 1),
("test", "dummy-pattern", 0),
("test", None, 1),
],
)
def test_config_lookup_pattern(self, mock_client, config_key, config_lookup_pattern, num_client_calls):
"""
Test that if Variable key is looked up in AWS Parameter Store
"""
mock_client().get_parameter.return_value = {
"Parameter": {
"Value": None,
},
}
kwargs = {"config_lookup_pattern": config_lookup_pattern}
secrets_manager_backend = SystemsManagerParameterStoreBackend(**kwargs)
secrets_manager_backend.get_config(config_key)
assert mock_client().get_parameter.call_count == num_client_calls
@mock.patch(
"airflow.providers.amazon.aws.secrets.systems_manager.SystemsManagerParameterStoreBackend.client",
new_callable=mock.PropertyMock,
)
def test_connection_prefix_with_no_leading_slash(self, mock_client):
"""
Test that if Connection ID is looked up in AWS Parameter Store with the added leading "/"
"""
mock_client().get_parameter.return_value = {
"Parameter": {
"Value": None,
},
}
kwargs = {"connections_prefix": "airflow/connections"}
secrets_manager_backend = SystemsManagerParameterStoreBackend(**kwargs)
secrets_manager_backend.get_conn_value("test_mysql")
mock_client().get_parameter.assert_called_with(
Name="/airflow/connections/test_mysql", WithDecryption=True
)
@mock.patch(
"airflow.providers.amazon.aws.secrets.systems_manager.SystemsManagerParameterStoreBackend.client",
new_callable=mock.PropertyMock,
)
def test_variable_prefix_with_no_leading_slash(self, mock_client):
"""
Test that if Variable key is looked up in AWS Parameter Store with the added leading "/"
"""
mock_client().get_parameter.return_value = {
"Parameter": {
"Value": None,
},
}
kwargs = {"variables_prefix": "airflow/variables"}
secrets_manager_backend = SystemsManagerParameterStoreBackend(**kwargs)
secrets_manager_backend.get_variable("hello")
mock_client().get_parameter.assert_called_with(Name="/airflow/variables/hello", WithDecryption=True)
@mock.patch(
"airflow.providers.amazon.aws.secrets.systems_manager.SystemsManagerParameterStoreBackend.client",
new_callable=mock.PropertyMock,
)
def test_config_prefix_with_no_leading_slash(self, mock_client):
"""
Test that if Config key is looked up in AWS Parameter Store with the added leading "/"
"""
mock_client().get_parameter.return_value = {
"Parameter": {
"Value": None,
},
}
kwargs = {"config_prefix": "airflow/config"}
secrets_manager_backend = SystemsManagerParameterStoreBackend(**kwargs)
secrets_manager_backend.get_config("config")
mock_client().get_parameter.assert_called_with(Name="/airflow/config/config", WithDecryption=True)