|
4 | 4 | from pytest_mock import mocker
|
5 | 5 |
|
6 | 6 | from redshift_connector import InterfaceError, RedshiftProperty, set_iam_properties
|
| 7 | +from redshift_connector.auth import AWSCredentialsProvider |
7 | 8 | from redshift_connector.config import ClientProtocolVersion
|
8 | 9 | from redshift_connector.iam_helper import set_iam_credentials
|
9 | 10 | from redshift_connector.plugin import (
|
@@ -40,7 +41,7 @@ def mock_all_provider_get_credentials(mocker):
|
40 | 41 | mocker.patch("redshift_connector.plugin.{}.get_credentials".format(provider), return_value=None)
|
41 | 42 |
|
42 | 43 |
|
43 |
| -def get_set_iam_properties_args(**kwargs): |
| 44 | +def get_set_iam_properties_args(**kwargs) -> typing.Dict[str, typing.Any]: |
44 | 45 | return {
|
45 | 46 | "info": RedshiftProperty(),
|
46 | 47 | "user": "awsuser",
|
@@ -80,6 +81,10 @@ def get_set_iam_properties_args(**kwargs):
|
80 | 81 | "allow_db_user_override": True,
|
81 | 82 | "client_protocol_version": ClientProtocolVersion.BASE_SERVER,
|
82 | 83 | "database_metadata_current_db_only": True,
|
| 84 | + "access_key_id": None, |
| 85 | + "secret_access_key": None, |
| 86 | + "session_token": None, |
| 87 | + "profile": None, |
83 | 88 | "ssl_insecure": None,
|
84 | 89 | **kwargs,
|
85 | 90 | }
|
@@ -138,19 +143,75 @@ def test_set_iam_properties_enforce_client_protocol_version(_input):
|
138 | 143 | ({"ssl": False, "iam": True}, "Invalid connection property setting. SSL must be enabled when using IAM"),
|
139 | 144 | (
|
140 | 145 | {"iam": False, "credentials_provider": "anything"},
|
141 |
| - "Invalid connection property setting. IAM must be enabled when using credentials via identity provider", |
| 146 | + "Invalid connection property setting", |
| 147 | + ), |
| 148 | + ( |
| 149 | + {"iam": False, "profile": "default"}, |
| 150 | + "Invalid connection property setting", |
| 151 | + ), |
| 152 | + ( |
| 153 | + {"iam": False, "access_key_id": "my_key"}, |
| 154 | + "Invalid connection property setting", |
| 155 | + ), |
| 156 | + ( |
| 157 | + {"iam": False, "secret_access_key": "shh it's a secret"}, |
| 158 | + "Invalid connection property setting", |
| 159 | + ), |
| 160 | + ( |
| 161 | + {"iam": False, "session_token": "my_session"}, |
| 162 | + "Invalid connection property setting", |
142 | 163 | ),
|
143 | 164 | (
|
144 | 165 | {"iam": True, "ssl": True},
|
145 |
| - "Invalid connection property setting. Credentials provider cannot be None when IAM is enabled", |
| 166 | + "Invalid connection property setting", |
| 167 | + ), |
| 168 | + ( |
| 169 | + {"iam": True, "ssl": True, "access_key_id": "my_key", "credentials_provider": "OktaCredentialsProvider"}, |
| 170 | + "Invalid connection property setting", |
| 171 | + ), |
| 172 | + ( |
| 173 | + {"iam": True, "ssl": True, "secret_access_key": "my_secret", "credentials_provider": "OktaCredentialsProvider"}, |
| 174 | + "Invalid connection property setting", |
| 175 | + ), |
| 176 | + ( |
| 177 | + {"iam": True, "ssl": True, "session_token": "token", "credentials_provider": "OktaCredentialsProvider"}, |
| 178 | + "Invalid connection property setting", |
| 179 | + ), |
| 180 | + ( |
| 181 | + {"iam": True, "ssl": True, "profile": "default", "credentials_provider": "OktaCredentialsProvider"}, |
| 182 | + "Invalid connection property setting", |
| 183 | + ), |
| 184 | + ( |
| 185 | + {"iam": True, "ssl": True, "profile": "default", "access_key_id": "my_key"}, |
| 186 | + "Invalid connection property setting", |
| 187 | + ), |
| 188 | + ( |
| 189 | + {"iam": True, "ssl": True, "profile": "default", "secret_access_key": "my_secret"}, |
| 190 | + "Invalid connection property setting", |
| 191 | + ), |
| 192 | + ( |
| 193 | + {"iam": True, "ssl": True, "profile": "default", "session_token": "token"}, |
| 194 | + "Invalid connection property setting", |
| 195 | + ), |
| 196 | + ( |
| 197 | + {"iam": True, "ssl": True, "secret_access_key": "my_secret"}, |
| 198 | + "Invalid connection property setting", |
| 199 | + ), |
| 200 | + ( |
| 201 | + {"iam": True, "ssl": True, "session_token": "token"}, |
| 202 | + "Invalid connection property setting", |
| 203 | + ), |
| 204 | + ( |
| 205 | + {"iam": True, "ssl": True, "access_key_id": "my_key", "password": ""}, |
| 206 | + "Invalid connection property setting", |
146 | 207 | ),
|
147 | 208 | (
|
148 | 209 | {"iam": False, "ssl_insecure": False},
|
149 |
| - "Invalid connection property setting. IAM must be enabled when using ssl_insecure", |
| 210 | + "Invalid connection property setting", |
150 | 211 | ),
|
151 | 212 | (
|
152 | 213 | {"iam": False, "ssl_insecure": True},
|
153 |
| - "Invalid connection property setting. IAM must be enabled when using ssl_insecure", |
| 214 | + "Invalid connection property setting", |
154 | 215 | ),
|
155 | 216 | ]
|
156 | 217 |
|
@@ -213,3 +274,50 @@ def test_set_iam_properties_provider_assigned(mocker, provider):
|
213 | 274 | assert spy.call_count == 1
|
214 | 275 | # ensure call to add_Parameter was made on the expected Provider class
|
215 | 276 | assert isinstance(spy.call_args[0][0], expectedProvider) is True
|
| 277 | + |
| 278 | + |
| 279 | +valid_aws_credential_args: typing.List[typing.Dict[str, str]] = [ |
| 280 | + {"profile": "default"}, |
| 281 | + {"access_key_id": "myAccessKey", "secret_access_key": "mySecret"}, |
| 282 | + {"access_key_id": "myAccessKey", "password": "myHiddenSecret"}, |
| 283 | + {"access_key_id": "myAccessKey", "secret_access_key": "mySecret", "session_token": "mySession"}, |
| 284 | +] |
| 285 | + |
| 286 | + |
| 287 | +@pytest.mark.parametrize("test_input", valid_aws_credential_args) |
| 288 | +def test_set_iam_properties_via_aws_credentials(mocker, test_input): |
| 289 | + # spy = mocker.spy("redshift_connector", "set_iam_credentials") |
| 290 | + info_obj: typing.Dict[str, typing.Any] = get_set_iam_properties_args(**test_input) |
| 291 | + info_obj["ssl"] = True |
| 292 | + info_obj["iam"] = True |
| 293 | + |
| 294 | + mocker.patch("redshift_connector.iam_helper.set_iam_credentials", return_value=None) |
| 295 | + set_iam_properties(**info_obj) |
| 296 | + |
| 297 | + for aws_cred_key, aws_cred_val in enumerate(test_input): |
| 298 | + if aws_cred_key == "profile": |
| 299 | + assert info_obj["info"].profile == aws_cred_val |
| 300 | + if aws_cred_key == "access_key_id": |
| 301 | + assert info_obj["info"].access_key_id == aws_cred_val |
| 302 | + if aws_cred_key == "secret_access_key": |
| 303 | + assert info_obj["info"].secret_access_key == aws_cred_val |
| 304 | + if aws_cred_key == "password": |
| 305 | + assert info_obj["info"].password == aws_cred_val |
| 306 | + if aws_cred_key == "session_token": |
| 307 | + assert info_obj["info"].session_token == aws_cred_val |
| 308 | + |
| 309 | + |
| 310 | +def test_set_iam_credentials_via_aws_credentials(mocker): |
| 311 | + redshift_property: RedshiftProperty = RedshiftProperty() |
| 312 | + redshift_property.profile = "profile_val" |
| 313 | + redshift_property.access_key_id = "access_val" |
| 314 | + redshift_property.secret_access_key = "secret_val" |
| 315 | + redshift_property.session_token = "session_val" |
| 316 | + |
| 317 | + mocker.patch("redshift_connector.iam_helper.set_cluster_credentials", return_value=None) |
| 318 | + spy = mocker.spy(AWSCredentialsProvider, "add_parameter") |
| 319 | + |
| 320 | + set_iam_credentials(redshift_property) |
| 321 | + assert spy.called is True |
| 322 | + assert spy.call_count == 1 |
| 323 | + assert spy.call_args[0][1] == redshift_property |
0 commit comments