-
Notifications
You must be signed in to change notification settings - Fork 13.7k
/
test_secrets.py
191 lines (167 loc) · 7.63 KB
/
test_secrets.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
#
# 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
from unittest import mock
from airflow.configuration import ensure_secrets_loaded, initialize_secrets_backends
from airflow.models import Connection, Variable
from tests.test_utils.config import conf_vars
from tests.test_utils.db import clear_db_variables
class TestConnectionsFromSecrets:
@mock.patch("airflow.secrets.metastore.MetastoreBackend.get_connection")
@mock.patch("airflow.secrets.environment_variables.EnvironmentVariablesBackend.get_connection")
def test_get_connection_second_try(self, mock_env_get, mock_meta_get):
mock_env_get.side_effect = [None] # return None
Connection.get_connection_from_secrets("fake_conn_id")
mock_meta_get.assert_called_once_with(conn_id="fake_conn_id")
mock_env_get.assert_called_once_with(conn_id="fake_conn_id")
@mock.patch("airflow.secrets.metastore.MetastoreBackend.get_connection")
@mock.patch("airflow.secrets.environment_variables.EnvironmentVariablesBackend.get_connection")
def test_get_connection_first_try(self, mock_env_get, mock_meta_get):
mock_env_get.side_effect = ["something"] # returns something
Connection.get_connection_from_secrets("fake_conn_id")
mock_env_get.assert_called_once_with(conn_id="fake_conn_id")
mock_meta_get.not_called()
@conf_vars(
{
(
"secrets",
"backend",
): "airflow.providers.amazon.aws.secrets.systems_manager.SystemsManagerParameterStoreBackend",
("secrets", "backend_kwargs"): '{"connections_prefix": "/airflow", "profile_name": null}',
}
)
def test_initialize_secrets_backends(self):
backends = initialize_secrets_backends()
backend_classes = [backend.__class__.__name__ for backend in backends]
assert 3 == len(backends)
assert 'SystemsManagerParameterStoreBackend' in backend_classes
@conf_vars(
{
(
"secrets",
"backend",
): "airflow.providers.amazon.aws.secrets.systems_manager.SystemsManagerParameterStoreBackend",
("secrets", "backend_kwargs"): '{"use_ssl": false}',
}
)
def test_backends_kwargs(self):
backends = initialize_secrets_backends()
systems_manager = next(
backend
for backend in backends
if backend.__class__.__name__ == 'SystemsManagerParameterStoreBackend'
)
assert systems_manager.kwargs == {}
assert systems_manager.use_ssl is False
@conf_vars(
{
(
"secrets",
"backend",
): "airflow.providers.amazon.aws.secrets.systems_manager.SystemsManagerParameterStoreBackend",
("secrets", "backend_kwargs"): '{"connections_prefix": "/airflow", "profile_name": null}',
}
)
@mock.patch.dict(
'os.environ',
{
'AIRFLOW_CONN_TEST_MYSQL': 'mysql://airflow:airflow@host:5432/airflow',
},
)
@mock.patch(
"airflow.providers.amazon.aws.secrets.systems_manager."
"SystemsManagerParameterStoreBackend.get_connection"
)
def test_backend_fallback_to_env_var(self, mock_get_connection):
mock_get_connection.return_value = None
backends = ensure_secrets_loaded()
backend_classes = [backend.__class__.__name__ for backend in backends]
assert 'SystemsManagerParameterStoreBackend' in backend_classes
conn = Connection.get_connection_from_secrets(conn_id="test_mysql")
# Assert that SystemsManagerParameterStoreBackend.get_conn_uri was called
mock_get_connection.assert_called_once_with(conn_id='test_mysql')
assert 'mysql://airflow:airflow@host:5432/airflow' == conn.get_uri()
class TestVariableFromSecrets:
def setup_method(self) -> None:
clear_db_variables()
def teardown_method(self) -> None:
clear_db_variables()
@mock.patch("airflow.secrets.metastore.MetastoreBackend.get_variable")
@mock.patch("airflow.secrets.environment_variables.EnvironmentVariablesBackend.get_variable")
def test_get_variable_second_try(self, mock_env_get, mock_meta_get):
"""
Test if Variable is not present in Environment Variable, it then looks for it in
Metastore DB
"""
mock_env_get.return_value = None
Variable.get_variable_from_secrets("fake_var_key")
mock_meta_get.assert_called_once_with(key="fake_var_key")
mock_env_get.assert_called_once_with(key="fake_var_key")
@mock.patch("airflow.secrets.metastore.MetastoreBackend.get_variable")
@mock.patch("airflow.secrets.environment_variables.EnvironmentVariablesBackend.get_variable")
def test_get_variable_first_try(self, mock_env_get, mock_meta_get):
"""
Test if Variable is present in Environment Variable, it does not look for it in
Metastore DB
"""
mock_env_get.return_value = "something"
Variable.get_variable_from_secrets("fake_var_key")
mock_env_get.assert_called_once_with(key="fake_var_key")
mock_meta_get.assert_not_called()
def test_backend_fallback_to_default_var(self):
"""
Test if a default_var is defined and no backend has the Variable,
the value returned is default_var
"""
variable_value = Variable.get(key="test_var", default_var="new")
assert "new" == variable_value
@conf_vars(
{
(
"secrets",
"backend",
): "airflow.providers.amazon.aws.secrets.systems_manager.SystemsManagerParameterStoreBackend",
("secrets", "backend_kwargs"): '{"variables_prefix": "/airflow", "profile_name": null}',
}
)
@mock.patch.dict(
'os.environ',
{
'AIRFLOW_VAR_MYVAR': 'a_venv_value',
},
)
@mock.patch("airflow.secrets.metastore.MetastoreBackend.get_variable")
@mock.patch(
"airflow.providers.amazon.aws.secrets.systems_manager."
"SystemsManagerParameterStoreBackend.get_variable"
)
def test_backend_variable_order(self, mock_secret_get, mock_meta_get):
backends = ensure_secrets_loaded()
backend_classes = [backend.__class__.__name__ for backend in backends]
assert 'SystemsManagerParameterStoreBackend' in backend_classes
mock_secret_get.return_value = None
mock_meta_get.return_value = None
assert "a_venv_value" == Variable.get(key="MYVAR")
mock_secret_get.assert_called_with(key="MYVAR")
mock_meta_get.assert_not_called()
mock_secret_get.return_value = None
mock_meta_get.return_value = "a_metastore_value"
assert "a_metastore_value" == Variable.get(key="not_myvar")
mock_meta_get.assert_called_once_with(key="not_myvar")
mock_secret_get.return_value = "a_secret_value"
assert "a_secret_value" == Variable.get(key="not_myvar")