"""Fixtures for lambda testing.""" import os import boto3 import pytest from moto import mock_aws @pytest.fixture() def aws_credentials(): """Mock AWS Credentials for moto.""" os.environ['AWS_ACCESS_KEY_ID'] = 'testing' os.environ['AWS_SECRET_ACCESS_KEY'] = 'testing' os.environ['AWS_SECURITY_TOKEN'] = 'testing' os.environ['AWS_SESSION_TOKEN'] = 'testing' os.environ['AWS_DEFAULT_REGION'] = 'us-east-1' @pytest.fixture() def mock_account_id(): """Return a test account.""" return '123456789012' @pytest.fixture() def event_json(): """ Provide sample event json. Data is a base64-encoded string with test_iam_user@theorchard.com as the 'preferred_username' key to match the fixture iam user """ event = { 'headers': { 'x-amzn-oidc-data': 'eyJ0eXAiOiJKV1QiLCJraWQiOiJiOTVjNzg5Yi1iMTM2LTQxM2ItOGEzYy1jZmVkMmQ5YjBmN2MiLCJhbGciOiJFUzI1NiIsImlzcyI6Imh0dHBzOi8vdGhlb3JjaGFyZC5va3RhLmNvbSIsImNsaWVudCI6IjBvYXZxNG0zZ2x0VzdqOGVVMHg3Iiwic2lnbmVyIjoiYXJuOmF3czplbGFzdGljbG9hZGJhbGFuY2luZzp1cy1lYXN0LTE6MTAzMjMzOTMyMDg5OmxvYWRiYWxhbmNlci9hcHAvZGV2LWJyZWFrLWdsYXNzL2FmNWFlZDk4MzQ3MmQ3NjEiLCJleHAiOjE2NTcxMjI0NjV9.eyJlbWFpbCI6InRlc3RfaWFtX3VzZXJAdGhlb3JjaGFyZC5jb20ifQ==.DbkzuTfk-FLW4Er48i2V4hGQqr9vMCu91Hdx-2l7eFeQ4Foo_8v0Bq7MFqCmj3VfuQ82bcEmtdImwvbJilzdGQ==', # noqa } } return event @pytest.fixture() def iam_client(aws_credentials): """Return a client.""" with mock_aws(): yield boto3.client('iam') @pytest.fixture() def sts_client(aws_credentials): """Return an STS client.""" with mock_aws(): yield boto3.client('sts') @pytest.fixture() def break_glass_role_name(): """Return a test role name.""" return 'dev-break-glass-access-role' @pytest.fixture() def vefiry_role_name(): """Return a test role name.""" return 'dev-break-glass-verify-role' @pytest.fixture() def break_glass_role_arn(iam_client, break_glass_role_name): """Mock AWS break glass role.""" role = iam_client.create_role( RoleName=break_glass_role_name, AssumeRolePolicyDocument='junk') role_arn = role['Role']['Arn'] return role_arn @pytest.fixture() def verify_role_arn(iam_client, vefiry_role_name): """Mock AWS break glass role.""" role = iam_client.create_role( RoleName=vefiry_role_name, AssumeRolePolicyDocument='junk') role_arn = role['Role']['Arn'] return role_arn @pytest.fixture() def create_mfa_device(iam_client, iam_user): """Create a virtual MFA device.""" mfa_device = iam_client.create_virtual_mfa_device( VirtualMFADeviceName='mfa-device') mfa_serial_number = mfa_device['VirtualMFADevice']['SerialNumber'] iam_client.enable_mfa_device( UserName=iam_user, SerialNumber=mfa_serial_number, AuthenticationCode1='234567', AuthenticationCode2='987654') return mfa_serial_number @pytest.fixture() def policy_json(): """Return an IAM policy.""" policy = """ { "Version": "2012-10-17", "Statement": { "Effect": "Allow", "Action": "s3:ListBucket", "Resource": "arn:aws:s3:::example_orcd_bucket" } } """ return policy @pytest.fixture def iam_user(iam_client): """Create an IAM user with tags. Returns the username.""" user = { "UserName": "test_user_1", "Tags": [ {"Key": "email", "Value": "test_iam_user@theorchard.com"}, ], } iam_client.create_user(UserName=user["UserName"], Tags=user["Tags"]) # Handy mapping for tests return user["UserName"] @pytest.fixture def iam_users(iam_client): """Create a few IAM users with tags. Returns a list of usernames.""" users = [ { "UserName": "test_user_1", "Tags": [ {"Key": "email", "Value": "test_iam_user@theorchard.com"}, ], }, { "UserName": "test_user_2", "Tags": [ {"Key": "email", "Value": "test_iam_user@theorchard.com"}, ], }, ] for u in users: iam_client.create_user(UserName=u["UserName"], Tags=u["Tags"]) return [u["UserName"] for u in users]