| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
import awscrt.auth |
| 5 |
import awscrt.io |
| 6 |
import datetime |
| 7 |
from io import BytesIO |
| 8 |
import os |
| 9 |
import sys |
| 10 |
from test import NativeResourceTest, TIMEOUT |
| 11 |
import time |
| 12 |
import unittest |
| 13 |
|
| 14 |
EXAMPLE_ACCESS_KEY_ID = 'example_access_key_id' |
| 15 |
EXAMPLE_SECRET_ACCESS_KEY = 'example_secret_access_key' |
| 16 |
EXAMPLE_SESSION_TOKEN = 'example_session_token' |
| 17 |
EXAMPLE_SESSION_EXPIRATION = datetime.datetime.fromtimestamp(1609911816, tz=datetime.timezone.utc) |
| 18 |
|
| 19 |
|
| 20 |
class ScopedEnvironmentVariable: |
| 21 |
""" |
| 22 |
Set environment variable for lifetime of this object. |
| 23 |
""" |
| 24 |
|
| 25 |
def __init__(self, key, value): |
| 26 |
self.key = key |
| 27 |
self.value = value |
| 28 |
self.prev_value = os.environ.get(key) |
| 29 |
|
| 30 |
def __enter__(self): |
| 31 |
if self.value is None: |
| 32 |
os.environ.pop(self.key, None) |
| 33 |
else: |
| 34 |
os.environ[self.key] = self.value |
| 35 |
|
| 36 |
def __exit__(self, type, value, tb): |
| 37 |
if self.prev_value is None: |
| 38 |
os.environ.pop(self.key, None) |
| 39 |
else: |
| 40 |
os.environ[self.key] = self.prev_value |
| 41 |
|
| 42 |
|
| 43 |
class TestCredentials(NativeResourceTest): |
| 44 |
def test_create(self): |
| 45 |
credentials = awscrt.auth.AwsCredentials( |
| 46 |
EXAMPLE_ACCESS_KEY_ID, |
| 47 |
EXAMPLE_SECRET_ACCESS_KEY, |
| 48 |
EXAMPLE_SESSION_TOKEN, |
| 49 |
EXAMPLE_SESSION_EXPIRATION) |
| 50 |
|
| 51 |
|
| 52 |
self.assertTrue(EXAMPLE_ACCESS_KEY_ID == credentials.access_key_id) |
| 53 |
self.assertTrue(EXAMPLE_SECRET_ACCESS_KEY == credentials.secret_access_key) |
| 54 |
self.assertTrue(EXAMPLE_SESSION_TOKEN == credentials.session_token) |
| 55 |
self.assertTrue(EXAMPLE_SESSION_EXPIRATION == credentials.expiration) |
| 56 |
|
| 57 |
def test_create_no_session_token(self): |
| 58 |
credentials = awscrt.auth.AwsCredentials(EXAMPLE_ACCESS_KEY_ID, EXAMPLE_SECRET_ACCESS_KEY) |
| 59 |
|
| 60 |
|
| 61 |
self.assertTrue(EXAMPLE_ACCESS_KEY_ID == credentials.access_key_id) |
| 62 |
self.assertTrue(EXAMPLE_SECRET_ACCESS_KEY == credentials.secret_access_key) |
| 63 |
self.assertTrue(credentials.session_token is None) |
| 64 |
|
| 65 |
def test_create_no_expiration(self): |
| 66 |
credentials = awscrt.auth.AwsCredentials(EXAMPLE_ACCESS_KEY_ID, EXAMPLE_SECRET_ACCESS_KEY) |
| 67 |
|
| 68 |
|
| 69 |
self.assertTrue(EXAMPLE_ACCESS_KEY_ID == credentials.access_key_id) |
| 70 |
self.assertTrue(EXAMPLE_SECRET_ACCESS_KEY == credentials.secret_access_key) |
| 71 |
self.assertTrue(credentials.expiration is None) |
| 72 |
|
| 73 |
|
| 74 |
class TestProvider(NativeResourceTest): |
| 75 |
def test_static_provider(self): |
| 76 |
provider = awscrt.auth.AwsCredentialsProvider.new_static( |
| 77 |
EXAMPLE_ACCESS_KEY_ID, |
| 78 |
EXAMPLE_SECRET_ACCESS_KEY, |
| 79 |
EXAMPLE_SESSION_TOKEN) |
| 80 |
|
| 81 |
future = provider.get_credentials() |
| 82 |
credentials = future.result(TIMEOUT) |
| 83 |
|
| 84 |
|
| 85 |
self.assertTrue(EXAMPLE_ACCESS_KEY_ID == credentials.access_key_id) |
| 86 |
self.assertTrue(EXAMPLE_SECRET_ACCESS_KEY == credentials.secret_access_key) |
| 87 |
self.assertTrue(EXAMPLE_SESSION_TOKEN == credentials.session_token) |
| 88 |
|
| 89 |
def test_static_provider_no_session_token(self): |
| 90 |
provider = awscrt.auth.AwsCredentialsProvider.new_static( |
| 91 |
EXAMPLE_ACCESS_KEY_ID, |
| 92 |
EXAMPLE_SECRET_ACCESS_KEY) |
| 93 |
|
| 94 |
future = provider.get_credentials() |
| 95 |
credentials = future.result(TIMEOUT) |
| 96 |
|
| 97 |
|
| 98 |
self.assertTrue(EXAMPLE_ACCESS_KEY_ID == credentials.access_key_id) |
| 99 |
self.assertTrue(EXAMPLE_SECRET_ACCESS_KEY == credentials.secret_access_key) |
| 100 |
self.assertTrue(credentials.session_token is None) |
| 101 |
|
| 102 |
def test_default_provider(self): |
| 103 |
|
| 104 |
with ScopedEnvironmentVariable('AWS_ACCESS_KEY_ID', EXAMPLE_ACCESS_KEY_ID), \ |
| 105 |
ScopedEnvironmentVariable('AWS_SECRET_ACCESS_KEY', EXAMPLE_SECRET_ACCESS_KEY), \ |
| 106 |
ScopedEnvironmentVariable('AWS_SESSION_TOKEN', None): |
| 107 |
|
| 108 |
event_loop_group = awscrt.io.EventLoopGroup() |
| 109 |
host_resolver = awscrt.io.DefaultHostResolver(event_loop_group) |
| 110 |
bootstrap = awscrt.io.ClientBootstrap(event_loop_group, host_resolver) |
| 111 |
provider = awscrt.auth.AwsCredentialsProvider.new_default_chain(bootstrap) |
| 112 |
|
| 113 |
future = provider.get_credentials() |
| 114 |
credentials = future.result(TIMEOUT) |
| 115 |
|
| 116 |
|
| 117 |
self.assertTrue(EXAMPLE_ACCESS_KEY_ID == credentials.access_key_id) |
| 118 |
self.assertTrue(EXAMPLE_SECRET_ACCESS_KEY == credentials.secret_access_key) |
| 119 |
self.assertTrue(credentials.session_token is None) |
| 120 |
|
| 121 |
def test_profile_provider(self): |
| 122 |
|
| 123 |
profile_name = "crt_user" |
| 124 |
credentials_filepath = "test/resources/example_profile" |
| 125 |
event_loop_group = awscrt.io.EventLoopGroup() |
| 126 |
host_resolver = awscrt.io.DefaultHostResolver(event_loop_group) |
| 127 |
bootstrap = awscrt.io.ClientBootstrap(event_loop_group, host_resolver) |
| 128 |
provider = awscrt.auth.AwsCredentialsProvider.new_profile( |
| 129 |
bootstrap, profile_name=profile_name, credentials_filepath=credentials_filepath) |
| 130 |
|
| 131 |
future = provider.get_credentials() |
| 132 |
credentials = future.result(TIMEOUT) |
| 133 |
|
| 134 |
|
| 135 |
self.assertTrue(EXAMPLE_ACCESS_KEY_ID == credentials.access_key_id) |
| 136 |
self.assertTrue(EXAMPLE_SECRET_ACCESS_KEY == credentials.secret_access_key) |
| 137 |
self.assertTrue(credentials.session_token is None) |
| 138 |
|
| 139 |
def test_environment_provider(self): |
| 140 |
with ScopedEnvironmentVariable('AWS_ACCESS_KEY_ID', EXAMPLE_ACCESS_KEY_ID), \ |
| 141 |
ScopedEnvironmentVariable('AWS_SECRET_ACCESS_KEY', EXAMPLE_SECRET_ACCESS_KEY), \ |
| 142 |
ScopedEnvironmentVariable('AWS_SESSION_TOKEN', EXAMPLE_SESSION_TOKEN): |
| 143 |
|
| 144 |
provider = awscrt.auth.AwsCredentialsProvider.new_environment() |
| 145 |
credentials = provider.get_credentials().result(TIMEOUT) |
| 146 |
|
| 147 |
|
| 148 |
self.assertTrue(EXAMPLE_ACCESS_KEY_ID == credentials.access_key_id) |
| 149 |
self.assertTrue(EXAMPLE_SECRET_ACCESS_KEY == credentials.secret_access_key) |
| 150 |
self.assertTrue(EXAMPLE_SESSION_TOKEN == credentials.session_token) |
| 151 |
|
| 152 |
def test_chain_provider(self): |
| 153 |
provider = awscrt.auth.AwsCredentialsProvider.new_chain([ |
| 154 |
awscrt.auth.AwsCredentialsProvider.new_static('id_a', 'secret_a'), |
| 155 |
awscrt.auth.AwsCredentialsProvider.new_static('id_b', 'secret_b'), |
| 156 |
]) |
| 157 |
credentials = provider.get_credentials().result(TIMEOUT) |
| 158 |
self.assertTrue('id_a' == credentials.access_key_id) |
| 159 |
self.assertTrue('secret_a' == credentials.secret_access_key) |
| 160 |
self.assertTrue(credentials.session_token is None) |
| 161 |
|
| 162 |
def test_chain_provider_bad_args(self): |
| 163 |
with self.assertRaises(TypeError): |
| 164 |
awscrt.auth.AwsCredentialsProvider.new_chain(None) |
| 165 |
|
| 166 |
with self.assertRaises(ValueError): |
| 167 |
awscrt.auth.AwsCredentialsProvider.new_chain([]) |
| 168 |
|
| 169 |
with self.assertRaises(TypeError): |
| 170 |
provider = awscrt.auth.AwsCredentialsProvider.new_chain([ |
| 171 |
awscrt.auth.AwsCredentialsProvider.new_static('id_a', 'secret_a'), |
| 172 |
"I am not an AwsCredentialsProvider", |
| 173 |
]) |
| 174 |
|
| 175 |
def test_process_provider(self): |
| 176 |
with ScopedEnvironmentVariable("AWS_CONFIG_FILE", "test/resources/example_config"): |
| 177 |
if sys.platform == 'win32': |
| 178 |
profile = 'test_process_provider_win' |
| 179 |
else: |
| 180 |
profile = 'test_process_provider' |
| 181 |
provider = awscrt.auth.AwsCredentialsProvider.new_process(profile) |
| 182 |
credentials = provider.get_credentials().result(TIMEOUT) |
| 183 |
|
| 184 |
|
| 185 |
self.assertTrue('process_access_key_id' == credentials.access_key_id) |
| 186 |
self.assertTrue('process_secret_access_key' == credentials.secret_access_key) |
| 187 |
self.assertTrue(credentials.session_token is None) |
| 188 |
|
| 189 |
def test_delegate_provider(self): |
| 190 |
def delegate_get_credentials(): |
| 191 |
return awscrt.auth.AwsCredentials("accesskey", "secretAccessKey", "sessionToken") |
| 192 |
|
| 193 |
provider = awscrt.auth.AwsCredentialsProvider.new_delegate(delegate_get_credentials) |
| 194 |
credentials = provider.get_credentials().result(TIMEOUT) |
| 195 |
|
| 196 |
|
| 197 |
self.assertTrue('accesskey' == credentials.access_key_id) |
| 198 |
self.assertTrue('secretAccessKey' == credentials.secret_access_key) |
| 199 |
self.assertTrue('sessionToken' == credentials.session_token) |
| 200 |
|
| 201 |
def test_delegate_provider_exception(self): |
| 202 |
|
| 203 |
def delegate_get_credentials(): |
| 204 |
raise Exception("purposefully thrown exception") |
| 205 |
|
| 206 |
provider = awscrt.auth.AwsCredentialsProvider.new_delegate(delegate_get_credentials) |
| 207 |
|
| 208 |
with self.assertRaises(Exception): |
| 209 |
credentials_future = provider.get_credentials() |
| 210 |
credentials = credentials_future.result(TIMEOUT) |
| 211 |
|
| 212 |
def test_delegate_provider_exception_from_bad_return_type(self): |
| 213 |
|
| 214 |
def delegate_get_credentials(): |
| 215 |
return "purposefully return wrong type" |
| 216 |
|
| 217 |
provider = awscrt.auth.AwsCredentialsProvider.new_delegate(delegate_get_credentials) |
| 218 |
|
| 219 |
with self.assertRaises(Exception): |
| 220 |
credentials_future = provider.get_credentials() |
| 221 |
credentials = credentials_future.result(TIMEOUT) |
| 222 |
|
| 223 |
|
| 224 |
@unittest.skipUnless(os.environ.get('AWS_TEST_MQTT311_COGNITO_IDENTITY') and os.environ.get('AWS_TEST_MQTT311_COGNITO_ENDPOINT'), |
| 225 |
'set env var to run test: AWS_TEST_MQTT311_COGNITO_IDENTITY and AWS_TEST_MQTT311_COGNITO_ENDPOINT') |
| 226 |
class CognitoCredentialsProviderTest(NativeResourceTest): |
| 227 |
|
| 228 |
def test_unauthenticated(self): |
| 229 |
identity = os.environ.get('AWS_TEST_MQTT311_COGNITO_IDENTITY') |
| 230 |
identity_endpoint = os.environ.get('AWS_TEST_MQTT311_COGNITO_ENDPOINT') |
| 231 |
tls_opts = awscrt.io.TlsContextOptions() |
| 232 |
tls_context = awscrt.io.ClientTlsContext(tls_opts) |
| 233 |
|
| 234 |
provider = awscrt.auth.AwsCredentialsProvider.new_cognito( |
| 235 |
endpoint=identity_endpoint, |
| 236 |
identity=identity, |
| 237 |
tls_ctx=tls_context |
| 238 |
) |
| 239 |
|
| 240 |
self.assertIsNotNone(provider) |
| 241 |
credentials = provider.get_credentials().result(TIMEOUT) |
| 242 |
self.assertIsNotNone(credentials) |
| 243 |
|
| 244 |
def test_cognito_provider_create_exception_bad_login(self): |
| 245 |
identity = os.environ.get('AWS_TEST_MQTT311_COGNITO_IDENTITY') |
| 246 |
identity_endpoint = os.environ.get('AWS_TEST_MQTT311_COGNITO_ENDPOINT') |
| 247 |
tls_opts = awscrt.io.TlsContextOptions() |
| 248 |
tls_context = awscrt.io.ClientTlsContext(tls_opts) |
| 249 |
|
| 250 |
with self.assertRaises(Exception): |
| 251 |
provider = awscrt.auth.AwsCredentialsProvider.new_cognito( |
| 252 |
endpoint=identity_endpoint, |
| 253 |
identity=identity, |
| 254 |
tls_ctx=tls_context, |
| 255 |
logins=[('provider1', 5), ('provider2', ['List not string'])] |
| 256 |
) |
| 257 |
|
| 258 |
self.assertIsNone(provider) |
| 259 |
|
| 260 |
def test_maximal_create(self): |
| 261 |
identity = os.environ.get('AWS_TEST_MQTT311_COGNITO_IDENTITY') |
| 262 |
identity_endpoint = os.environ.get('AWS_TEST_MQTT311_COGNITO_ENDPOINT') |
| 263 |
tls_opts = awscrt.io.TlsContextOptions() |
| 264 |
tls_context = awscrt.io.ClientTlsContext(tls_opts) |
| 265 |
|
| 266 |
provider = awscrt.auth.AwsCredentialsProvider.new_cognito( |
| 267 |
endpoint=identity_endpoint, |
| 268 |
identity=identity, |
| 269 |
tls_ctx=tls_context, |
| 270 |
logins=[('provider1', 'token1'), ('provide2', 'token2')], |
| 271 |
custom_role_arn='not-a-real-arn' |
| 272 |
) |
| 273 |
|
| 274 |
self.assertIsNotNone(provider) |
| 275 |
|
| 276 |
|
| 277 |
@unittest.skipUnless(os.environ.get('AWS_TESTING_COGNITO_IDENTITY') and os.environ.get('AWS_TEST_MQTT311_COGNITO_ENDPOINT') |
| 278 |
and os.environ.get('AWS_TEST_HTTP_PROXY_HOST') and os.environ.get('AWS_TEST_HTTP_PROXY_PORT'), |
| 279 |
'set env var to run test: AWS_TESTING_COGNITO_IDENTITY, AWS_TEST_MQTT311_COGNITO_ENDPOINT, ' |
| 280 |
'AWS_TEST_HTTP_PROXY_HOST, and AWS_TEST_HTTP_PROXY_PORT') |
| 281 |
class CognitoCredentialsProviderProxyTest(NativeResourceTest): |
| 282 |
|
| 283 |
def test_unauthenticated(self): |
| 284 |
identity = os.environ.get('AWS_TESTING_COGNITO_IDENTITY') |
| 285 |
identity_endpoint = os.environ.get('AWS_TEST_MQTT311_COGNITO_ENDPOINT') |
| 286 |
tls_opts = awscrt.io.TlsContextOptions() |
| 287 |
tls_context = awscrt.io.ClientTlsContext(tls_opts) |
| 288 |
http_proxy_options = awscrt.http.HttpProxyOptions( |
| 289 |
os.environ.get('AWS_TEST_HTTP_PROXY_HOST'), |
| 290 |
int(os.environ.get('AWS_TEST_HTTP_PROXY_PORT', '0'))) |
| 291 |
|
| 292 |
provider = awscrt.auth.AwsCredentialsProvider.new_cognito( |
| 293 |
endpoint=identity_endpoint, |
| 294 |
identity=identity, |
| 295 |
tls_ctx=tls_context, |
| 296 |
http_proxy_options=http_proxy_options |
| 297 |
) |
| 298 |
|
| 299 |
self.assertIsNotNone(provider) |
| 300 |
credentials = provider.get_credentials().result(TIMEOUT) |
| 301 |
self.assertIsNotNone(credentials) |
| 302 |
|
| 303 |
|
| 304 |
class TestSigningConfig(NativeResourceTest): |
| 305 |
def test_create(self): |
| 306 |
algorithm = awscrt.auth.AwsSigningAlgorithm.V4 |
| 307 |
signature_type = awscrt.auth.AwsSignatureType.HTTP_REQUEST_QUERY_PARAMS |
| 308 |
credentials_provider = awscrt.auth.AwsCredentialsProvider.new_static( |
| 309 |
EXAMPLE_ACCESS_KEY_ID, EXAMPLE_SECRET_ACCESS_KEY) |
| 310 |
region = 'us-west-2' |
| 311 |
service = 'aws-suborbital-ion-cannon' |
| 312 |
date = datetime.datetime(year=2000, month=1, day=1) |
| 313 |
|
| 314 |
def should_sign_header(name): |
| 315 |
return not name.tolower().startswith('x-do-not-sign') |
| 316 |
|
| 317 |
use_double_uri_encode = False |
| 318 |
should_normalize_uri_path = False |
| 319 |
signed_body_value = awscrt.auth.AwsSignedBodyValue.EMPTY_SHA256 |
| 320 |
signed_body_header_type = awscrt.auth.AwsSignedBodyHeaderType.X_AMZ_CONTENT_SHA_256 |
| 321 |
expiration_in_seconds = 123 |
| 322 |
omit_session_token = True |
| 323 |
|
| 324 |
cfg = awscrt.auth.AwsSigningConfig(algorithm=algorithm, |
| 325 |
signature_type=signature_type, |
| 326 |
credentials_provider=credentials_provider, |
| 327 |
region=region, |
| 328 |
service=service, |
| 329 |
date=date, |
| 330 |
should_sign_header=should_sign_header, |
| 331 |
use_double_uri_encode=use_double_uri_encode, |
| 332 |
should_normalize_uri_path=should_normalize_uri_path, |
| 333 |
signed_body_value=signed_body_value, |
| 334 |
signed_body_header_type=signed_body_header_type, |
| 335 |
expiration_in_seconds=expiration_in_seconds, |
| 336 |
omit_session_token=omit_session_token) |
| 337 |
|
| 338 |
self.assertIs(algorithm, cfg.algorithm) |
| 339 |
self.assertIs(signature_type, cfg.signature_type) |
| 340 |
self.assertIs(credentials_provider, cfg.credentials_provider) |
| 341 |
self.assertEqual(region, cfg.region) |
| 342 |
self.assertEqual(service, cfg.service) |
| 343 |
self.assertEqual(date, cfg.date) |
| 344 |
self.assertIs(should_sign_header, cfg.should_sign_header) |
| 345 |
self.assertEqual(use_double_uri_encode, cfg.use_double_uri_encode) |
| 346 |
self.assertEqual(should_normalize_uri_path, cfg.should_normalize_uri_path) |
| 347 |
self.assertEqual(signed_body_value, cfg.signed_body_value) |
| 348 |
self.assertIs(signed_body_header_type, cfg.signed_body_header_type) |
| 349 |
self.assertEqual(expiration_in_seconds, cfg.expiration_in_seconds) |
| 350 |
self.assertEqual(omit_session_token, cfg.omit_session_token) |
| 351 |
|
| 352 |
def test_replace(self): |
| 353 |
credentials_provider = awscrt.auth.AwsCredentialsProvider.new_static( |
| 354 |
EXAMPLE_ACCESS_KEY_ID, EXAMPLE_SECRET_ACCESS_KEY) |
| 355 |
|
| 356 |
|
| 357 |
orig_cfg = awscrt.auth.AwsSigningConfig( |
| 358 |
algorithm=awscrt.auth.AwsSigningAlgorithm.V4, |
| 359 |
signature_type=awscrt.auth.AwsSignatureType.HTTP_REQUEST_QUERY_PARAMS, |
| 360 |
credentials_provider=credentials_provider, |
| 361 |
region='us-west-1', |
| 362 |
service='aws-suborbital-ion-cannon', |
| 363 |
date=datetime.datetime( |
| 364 |
year=2000, |
| 365 |
month=1, |
| 366 |
day=1), |
| 367 |
should_sign_header=lambda x: False, |
| 368 |
use_double_uri_encode=False, |
| 369 |
should_normalize_uri_path=False, |
| 370 |
signed_body_value=awscrt.auth.AwsSignedBodyValue.EMPTY_SHA256, |
| 371 |
signed_body_header_type=awscrt.auth.AwsSignedBodyHeaderType.X_AMZ_CONTENT_SHA_256, |
| 372 |
expiration_in_seconds=123, |
| 373 |
omit_session_token=True) |
| 374 |
|
| 375 |
|
| 376 |
def _replace_attr(name, value): |
| 377 |
new_cfg = orig_cfg.replace(**{name: value}) |
| 378 |
self.assertIsNot(orig_cfg, new_cfg) |
| 379 |
|
| 380 |
self.assertEqual(value, getattr(new_cfg, name)) |
| 381 |
|
| 382 |
|
| 383 |
for attr in awscrt.auth.AwsSigningConfig._attributes: |
| 384 |
if attr == name: |
| 385 |
self.assertNotEqual(getattr(orig_cfg, attr), getattr(new_cfg, attr), |
| 386 |
"replaced value should not match original") |
| 387 |
else: |
| 388 |
self.assertEqual(getattr(orig_cfg, attr), getattr(new_cfg, attr), |
| 389 |
"value should match original") |
| 390 |
|
| 391 |
_replace_attr('signature_type', awscrt.auth.AwsSignatureType.HTTP_REQUEST_HEADERS) |
| 392 |
_replace_attr('credentials_provider', |
| 393 |
awscrt.auth.AwsCredentialsProvider.new_static(EXAMPLE_ACCESS_KEY_ID, EXAMPLE_SECRET_ACCESS_KEY)) |
| 394 |
_replace_attr('region', 'us-west-2') |
| 395 |
_replace_attr('service', 'aws-nothing-but-bees') |
| 396 |
_replace_attr('date', datetime.datetime(year=2001, month=1, day=1)) |
| 397 |
_replace_attr('should_sign_header', lambda x: True) |
| 398 |
_replace_attr('use_double_uri_encode', True) |
| 399 |
_replace_attr('should_normalize_uri_path', True) |
| 400 |
_replace_attr('signed_body_value', awscrt.auth.AwsSignedBodyValue.UNSIGNED_PAYLOAD) |
| 401 |
_replace_attr('signed_body_header_type', awscrt.auth.AwsSignedBodyHeaderType.NONE) |
| 402 |
_replace_attr('expiration_in_seconds', 987) |
| 403 |
_replace_attr('omit_session_token', False) |
| 404 |
|
| 405 |
|
| 406 |
new_cfg = orig_cfg.replace(region='us-west-3', service='aws-slow-blinking') |
| 407 |
self.assertEqual('us-west-3', new_cfg.region) |
| 408 |
self.assertEqual('aws-slow-blinking', new_cfg.service) |
| 409 |
|
| 410 |
self.assertEqual(orig_cfg.should_sign_header, new_cfg.should_sign_header) |
| 411 |
|
| 412 |
def test_special_defaults(self): |
| 413 |
credentials_provider = awscrt.auth.AwsCredentialsProvider.new_static( |
| 414 |
EXAMPLE_ACCESS_KEY_ID, EXAMPLE_SECRET_ACCESS_KEY) |
| 415 |
|
| 416 |
config = awscrt.auth.AwsSigningConfig( |
| 417 |
algorithm=awscrt.auth.AwsSigningAlgorithm.V4, |
| 418 |
signature_type=awscrt.auth.AwsSignatureType.HTTP_REQUEST_QUERY_PARAMS, |
| 419 |
credentials_provider=credentials_provider, |
| 420 |
region='us-west-1', |
| 421 |
service='aws-suborbital-ion-cannon') |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
self.assertIsNone(config.signed_body_value) |
| 427 |
self.assertIsNone(config.expiration_in_seconds) |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
self.assertAlmostEqual(time.time(), config.date.timestamp(), delta=2.0) |
| 432 |
|
| 433 |
|
| 434 |
SIGV4TEST_ACCESS_KEY_ID = 'AKIDEXAMPLE' |
| 435 |
SIGV4TEST_SECRET_ACCESS_KEY = 'wJalrXUtnFEMI/K7MDENG+bPxRfiCYEXAMPLEKEY' |
| 436 |
SIGV4TEST_SESSION_TOKEN = None |
| 437 |
SIGV4TEST_SERVICE = 'service' |
| 438 |
SIGV4TEST_REGION = 'us-east-1' |
| 439 |
SIGV4TEST_DATE = datetime.datetime( |
| 440 |
year=2015, |
| 441 |
month=8, |
| 442 |
day=30, |
| 443 |
hour=12, |
| 444 |
minute=36, |
| 445 |
second=0, |
| 446 |
tzinfo=datetime.timezone.utc) |
| 447 |
|
| 448 |
|
| 449 |
class TestSigner(NativeResourceTest): |
| 450 |
|
| 451 |
def test_signing_sigv4_headers(self): |
| 452 |
|
| 453 |
self._test_signing_sigv4_headers( |
| 454 |
method='GET', |
| 455 |
path='/', |
| 456 |
unsigned_headers=[('Host', 'example.amazonaws.com')], |
| 457 |
signed_headers=[ |
| 458 |
('Host', |
| 459 |
'example.amazonaws.com'), |
| 460 |
('X-Amz-Date', |
| 461 |
'20150830T123600Z'), |
| 462 |
('Authorization', |
| 463 |
'AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20150830/us-east-1/service/aws4_request, SignedHeaders=host;x-amz-date, Signature=5fa00fa31553b73ebf1942676e86291e8372ff2a2260956d9b8aae1d763fbf31')]) |
| 464 |
|
| 465 |
def _test_signing_sigv4_headers(self, method, path, unsigned_headers, signed_headers): |
| 466 |
credentials_provider = awscrt.auth.AwsCredentialsProvider.new_static( |
| 467 |
SIGV4TEST_ACCESS_KEY_ID, SIGV4TEST_SECRET_ACCESS_KEY, SIGV4TEST_SESSION_TOKEN) |
| 468 |
|
| 469 |
signing_config = awscrt.auth.AwsSigningConfig( |
| 470 |
algorithm=awscrt.auth.AwsSigningAlgorithm.V4, |
| 471 |
signature_type=awscrt.auth.AwsSignatureType.HTTP_REQUEST_HEADERS, |
| 472 |
credentials_provider=credentials_provider, |
| 473 |
region=SIGV4TEST_REGION, |
| 474 |
service=SIGV4TEST_SERVICE, |
| 475 |
date=SIGV4TEST_DATE) |
| 476 |
|
| 477 |
http_request = awscrt.http.HttpRequest( |
| 478 |
method=method, |
| 479 |
path=path, |
| 480 |
headers=awscrt.http.HttpHeaders(unsigned_headers)) |
| 481 |
|
| 482 |
signing_future = awscrt.auth.aws_sign_request(http_request, signing_config) |
| 483 |
|
| 484 |
signing_result = signing_future.result(TIMEOUT) |
| 485 |
|
| 486 |
self.assertIs(http_request, signing_result) |
| 487 |
|
| 488 |
self.assertEqual(method, http_request.method) |
| 489 |
self.assertEqual(path, http_request.path) |
| 490 |
|
| 491 |
|
| 492 |
for prev_header in unsigned_headers: |
| 493 |
self.assertIn(prev_header, http_request.headers) |
| 494 |
|
| 495 |
|
| 496 |
for signed_header in signed_headers: |
| 497 |
self.assertIn(signed_header, http_request.headers) |
| 498 |
|
| 499 |
def test_signing_sigv4_body(self): |
| 500 |
credentials_provider = awscrt.auth.AwsCredentialsProvider.new_static( |
| 501 |
SIGV4TEST_ACCESS_KEY_ID, SIGV4TEST_SECRET_ACCESS_KEY, SIGV4TEST_SESSION_TOKEN) |
| 502 |
|
| 503 |
signing_config = awscrt.auth.AwsSigningConfig( |
| 504 |
algorithm=awscrt.auth.AwsSigningAlgorithm.V4, |
| 505 |
signature_type=awscrt.auth.AwsSignatureType.HTTP_REQUEST_HEADERS, |
| 506 |
credentials_provider=credentials_provider, |
| 507 |
region=SIGV4TEST_REGION, |
| 508 |
service=SIGV4TEST_SERVICE, |
| 509 |
date=SIGV4TEST_DATE, |
| 510 |
signed_body_header_type=awscrt.auth.AwsSignedBodyHeaderType.X_AMZ_CONTENT_SHA_256) |
| 511 |
|
| 512 |
body_stream = BytesIO(b'hello') |
| 513 |
|
| 514 |
http_request = awscrt.http.HttpRequest( |
| 515 |
method='POST', |
| 516 |
path='/', |
| 517 |
headers=awscrt.http.HttpHeaders([('Host', 'example.amazonaws.com'), ('Content-Length', '5')]), |
| 518 |
body_stream=body_stream) |
| 519 |
|
| 520 |
signing_future = awscrt.auth.aws_sign_request(http_request, signing_config) |
| 521 |
|
| 522 |
signing_result = signing_future.result(TIMEOUT) |
| 523 |
|
| 524 |
self.assertIs(http_request, signing_result) |
| 525 |
|
| 526 |
self.assertEqual('2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824', |
| 527 |
signing_result.headers.get('x-amz-content-sha256')) |
| 528 |
self.assertEqual( |
| 529 |
'AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20150830/us-east-1/service/aws4_request, SignedHeaders=content-length;host;x-amz-content-sha256;x-amz-date, Signature=8e17c5b22b7bb28da47f44b08691c087a0993d0965bfab053376360790d44d6c', |
| 530 |
signing_result.headers.get('Authorization')) |
| 531 |
|
| 532 |
|
| 533 |
self.assertEqual(0, body_stream.tell()) |
| 534 |
|
| 535 |
def test_signing_sigv4_utf8(self): |
| 536 |
|
| 537 |
self._test_signing_sigv4_headers( |
| 538 |
method='GET', |
| 539 |
path=u'/\u1234', ሴ" |
| 540 |
unsigned_headers=[('Host', 'example.amazonaws.com')], |
| 541 |
signed_headers=[ |
| 542 |
('Host', |
| 543 |
'example.amazonaws.com'), |
| 544 |
('X-Amz-Date', |
| 545 |
'20150830T123600Z'), |
| 546 |
('Authorization', |
| 547 |
'AWS4-HMAC-SHA256 Credential=AKIDEXAMPLE/20150830/us-east-1/service/aws4_request, SignedHeaders=host;x-amz-date, Signature=8318018e0b0f223aa2bbf98705b62bb787dc9c0e678f255a891fd03141be5d85')]) |
| 548 |
|
| 549 |
def test_signing_sigv4a_headers(self): |
| 550 |
credentials_provider = awscrt.auth.AwsCredentialsProvider.new_static( |
| 551 |
SIGV4TEST_ACCESS_KEY_ID, SIGV4TEST_SECRET_ACCESS_KEY, SIGV4TEST_SESSION_TOKEN) |
| 552 |
|
| 553 |
signing_config = awscrt.auth.AwsSigningConfig( |
| 554 |
algorithm=awscrt.auth.AwsSigningAlgorithm.V4_ASYMMETRIC, |
| 555 |
signature_type=awscrt.auth.AwsSignatureType.HTTP_REQUEST_HEADERS, |
| 556 |
credentials_provider=credentials_provider, |
| 557 |
region=SIGV4TEST_REGION, |
| 558 |
service=SIGV4TEST_SERVICE, |
| 559 |
date=SIGV4TEST_DATE) |
| 560 |
|
| 561 |
http_request = awscrt.http.HttpRequest('GET', '/') |
| 562 |
http_request.headers.add('Host', 'example.amazonaws.com') |
| 563 |
|
| 564 |
signing_future = awscrt.auth.aws_sign_request(http_request, signing_config) |
| 565 |
signed_request = signing_future.result(TIMEOUT) |
| 566 |
|
| 567 |
auth_header_value = signed_request.headers.get('Authorization') |
| 568 |
self.assertIsNotNone(auth_header_value) |
| 569 |
self.assertTrue(auth_header_value.startswith( |
| 570 |
'AWS4-ECDSA-P256-SHA256 Credential=AKIDEXAMPLE/20150830/service/aws4_request, SignedHeaders=host;x-amz-date;x-amz-region-set, Signature=')) |