Beispiel #1
0
    def test_role_policies(self):
        policies = self.iam.role_policies()
        assert len(set(policies)) == 0

        iam = IAM(role_name='AWSServiceRoleForAPIGateway')
        assert iam.role_policies() == {'APIGatewayServiceRolePolicy': 'arn:aws:iam::aws:policy/aws-service-role/APIGatewayServiceRolePolicy'}
        assert len(iam.role_policies_statements().get('APIGatewayServiceRolePolicy')[0].get('Action')) > 10
Beispiel #2
0
 def test_role_policies(self):
     iam = IAM(role_name='AWSBatchServiceRole')
     assert iam.role_policies() == {
         'AWSBatchServiceRole':
         'arn:aws:iam::aws:policy/service-role/AWSBatchServiceRole'
     }
     assert len(iam.role_policies_statements().get('AWSBatchServiceRole')
                [0].get('Action')) == 59
Beispiel #3
0
class test_Temp_Aws_Roles(TestCase):
    def setUp(self):
        self.temp_aws_roles = Temp_Aws_Roles()
        self.account_id = '244560807427'

    def test_for_lambda_invocation(self):
        role_arn = self.temp_aws_roles.for_lambda_invocation()
        assert role_arn == 'arn:aws:iam::244560807427:role/temp_role_for_lambda_invocation'

    @unittest.skip(
        'causes side effect with other tests, due to the fact that AWS takes a while to sync the privileges internally'
    )
    def create_test_for_lambda_invocation(self):
        role_name = self.temp_aws_roles.role_name__for_lambda_invocation
        role_arn = 'arn:aws:iam::{0}:role/{1}'.format(self.account_id,
                                                      role_name)
        assert role_arn == self.temp_aws_roles.for_lambda_invocation(
            delete_existing=True)
        self.iam = IAM().set_role_name(role_name)
        policies_statements = self.iam.role_policies_statements(
            just_statements=True)
        resource = policies_statements[0].get('Resource')[0]
        assert self.account_id in resource  # confirm account_id value is in there (regression test for bug)
Beispiel #4
0
class IAM_Role:
    def __init__(self, role_name=None):
        self.role_name = role_name or f"osbot_temp_role_{random_string()}"
        self.iam = IAM(role_name=self.role_name)

    def add_policy_for__lambda(self):
        temp_policy_name = 'policy_{0}'.format(self.role_name)
        cloud_watch_arn = f'arn:aws:logs:{AWS_Config().aws_session_region_name()}:{AWS_Config().aws_session_account_id()}:log-group:/aws/lambda/*'
        iam_policy = IAM_Policy(temp_policy_name)
        policy_arn = iam_policy.add_cloud_watch(cloud_watch_arn).create().get(
            'policy_arn')
        self.iam.role_policy_attach(policy_arn)
        return policy_arn

    def arn(self):
        return self.iam.role_arn()

    def attach_policy(self, policy_name, policy_document):
        self.delete_policy(policy_name=policy_name)
        result_create = self.iam.policy_create(policy_name=policy_name,
                                               policy_document=policy_document)
        policy_arn = result_create.get('policy_arn')
        self.iam.role_policy_attach(policy_arn=policy_arn)
        return policy_arn

    def create(self, policy_document, skip_if_exists=True):
        self.iam.role_create(policy_document=policy_document,
                             skip_if_exists=skip_if_exists)
        return self.exists()

    def create_for__lambda(self):
        result = self.create_for_service__assume_role('lambda.amazonaws.com')
        if result.get('status') == 'ok':
            self.add_policy_for__lambda()
        return result

    def create_for__code_build(self):
        return self.create_for_service__assume_role('codebuild.amazonaws.com')

    def create_for_service__assume_role(self, service):
        statement = {
            'Action': 'sts:AssumeRole',
            'Effect': 'Allow',
            'Principal': {
                'Service': service
            }
        }
        return self.create_from_statement(statement)

    def create_for_service(self, service, statement):
        statement['Principal'] = {'Service': service}
        return self.create_from_statement(statement)

    def create_for_service_with_policies(self,
                                         service,
                                         policies,
                                         project_name,
                                         recreate_policy=False):
        role = self.create_for_service__assume_role(service)
        role_arn = role.get('role_arn')
        policies_arns = self.iam.policies_create(policies, project_name,
                                                 recreate_policy)
        self.iam.role_policies_attach(policies_arns)
        return {"role_arn": role_arn, "policies_arns": policies_arns}

    def create_from_statement(self, statement):
        return self.create_from_statements([statement])

    def create_from_statements(self, statement):
        role_arn = self.iam.role_arn()
        if role_arn:
            return {
                'status': 'warning',
                'data': 'role already exists',
                'role_name': self.iam.role_name,
                'role_arn': role_arn
            }
        else:
            policy_document = {'Statement': statement}
            data = self.iam.role_create(policy_document)
            return {
                'status': 'ok',
                'data': data,
                'role_name': self.iam.role_name,
                'role_arn': data.get('Arn')
            }

    def delete(self):
        return self.iam.role_delete()

    def delete_policy(self, policy_arn=None, policy_name=None):
        return self.iam.policy_delete(policy_arn=policy_arn,
                                      policy_name=policy_name)

    def exists(self):
        return self.iam.role_exists()

    def info(self):
        return self.iam.role_info()

    def not_exists(self):
        return self.iam.role_not_exists()

    def policies_statements(self):
        return self.iam.role_policies_statements()