|
1 | 1 | import os
|
2 | 2 | import logging
|
| 3 | +import json |
| 4 | +import unittest |
| 5 | + |
| 6 | +from localstack.constants import TEST_AWS_ACCOUNT_ID |
| 7 | +from localstack.utils.aws import aws_stack |
3 | 8 | from localstack.utils.kinesis import kinesis_connector
|
4 | 9 |
|
5 | 10 |
|
6 |
| -def run_kcl_with_iam_assume_role(): |
7 |
| - env_vars = {} |
8 |
| - if os.environ.get('AWS_ASSUME_ROLE_ARN'): |
9 |
| - env_vars['AWS_ASSUME_ROLE_ARN'] = os.environ.get('AWS_ASSUME_ROLE_ARN') |
10 |
| - env_vars['AWS_ASSUME_ROLE_SESSION_NAME'] = os.environ.get('AWS_ASSUME_ROLE_SESSION_NAME') |
11 |
| - env_vars['ENV'] = os.environ.get('ENV') or 'main' |
12 |
| - |
13 |
| - def process_records(records): |
14 |
| - print(records) |
15 |
| - |
16 |
| - # start Kinesis client |
17 |
| - stream_name = 'test-foobar' |
18 |
| - kinesis_connector.listen_to_kinesis( |
19 |
| - stream_name=stream_name, |
20 |
| - listener_func=process_records, |
21 |
| - env_vars=env_vars, |
22 |
| - kcl_log_level=logging.INFO, |
23 |
| - wait_until_started=True) |
| 11 | +class TestIAMIntegrations(unittest.TestCase): |
| 12 | + |
| 13 | + def test_run_kcl_with_iam_assume_role(self): |
| 14 | + env_vars = {} |
| 15 | + if os.environ.get('AWS_ASSUME_ROLE_ARN'): |
| 16 | + env_vars['AWS_ASSUME_ROLE_ARN'] = os.environ.get('AWS_ASSUME_ROLE_ARN') |
| 17 | + env_vars['AWS_ASSUME_ROLE_SESSION_NAME'] = os.environ.get('AWS_ASSUME_ROLE_SESSION_NAME') |
| 18 | + env_vars['ENV'] = os.environ.get('ENV') or 'main' |
| 19 | + |
| 20 | + def process_records(records): |
| 21 | + print(records) |
| 22 | + |
| 23 | + # start Kinesis client |
| 24 | + stream_name = 'test-foobar' |
| 25 | + kinesis_connector.listen_to_kinesis( |
| 26 | + stream_name=stream_name, |
| 27 | + listener_func=process_records, |
| 28 | + env_vars=env_vars, |
| 29 | + kcl_log_level=logging.INFO, |
| 30 | + wait_until_started=True) |
| 31 | + |
| 32 | + def test_attach_iam_role_to_new_iam_user(self): |
| 33 | + test_policy_document = { |
| 34 | + 'Version': '2012-10-17', |
| 35 | + 'Statement': { |
| 36 | + 'Effect': 'Allow', |
| 37 | + 'Action': 's3:ListBucket', |
| 38 | + 'Resource': 'arn:aws:s3:::example_bucket' |
| 39 | + } |
| 40 | + } |
| 41 | + test_user_name = 'test-user' |
| 42 | + |
| 43 | + iam_client = aws_stack.connect_to_service('iam') |
| 44 | + |
| 45 | + iam_client.create_user(UserName=test_user_name) |
| 46 | + response = iam_client.create_policy(PolicyName='test-policy', |
| 47 | + PolicyDocument=json.dumps(test_policy_document)) |
| 48 | + test_policy_arn = response['Policy']['Arn'] |
| 49 | + self.assertIn(TEST_AWS_ACCOUNT_ID, test_policy_arn) |
| 50 | + iam_client.attach_user_policy(UserName=test_user_name, PolicyArn=test_policy_arn) |
| 51 | + attached_user_policies = iam_client.list_attached_user_policies(UserName=test_user_name) |
| 52 | + self.assertEqual(len(attached_user_policies['AttachedPolicies']), 1) |
| 53 | + self.assertEqual(attached_user_policies['AttachedPolicies'][0]['PolicyArn'], test_policy_arn) |
0 commit comments