How to use policy_arn method in localstack

Best Python code snippet using localstack_python

test_policy_wrapper.py

Source:test_policy_wrapper.py Github

copy

Full Screen

1# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.2# SPDX-License-Identifier: Apache-2.03"""4Unit tests for policy_wrapper.py functions.5"""6import datetime7import json8import pytest9from botocore.exceptions import ClientError10import policy_wrapper11@pytest.mark.parametrize("error_code", [None, "MalformedPolicyDocument"])12def test_create_policy(make_stubber, make_unique_name, error_code):13 iam_stubber = make_stubber(policy_wrapper.iam.meta.client)14 policy_name = make_unique_name('policy-')15 policy_description = 'Just a test.'16 actions = ['test:JustTest', 'test:AlsoTest']17 resource_arn = 'arn:aws:test:::test/resource'18 policy_arn = 'arn:aws:iam:::test/policy'19 policy_doc = json.dumps({20 "Version": "2012-10-17",21 "Statement": [22 {23 "Effect": "Allow",24 "Action": actions,25 "Resource": resource_arn26 }27 ]28 })29 iam_stubber.stub_create_policy(30 policy_name, policy_arn, policy_doc, description=policy_description,31 error_code=error_code)32 if error_code is None:33 policy = policy_wrapper.create_policy(34 policy_name, policy_description, actions, resource_arn)35 assert policy.arn == policy_arn36 else:37 with pytest.raises(ClientError) as exc_info:38 policy_wrapper.create_policy(39 policy_name, policy_description, actions, resource_arn)40 assert exc_info.value.response['Error']['Code'] == error_code41@pytest.mark.parametrize("error_code", [None, "DeleteConflict"])42def test_delete_policy(make_stubber, make_unique_name, error_code):43 iam_stubber = make_stubber(policy_wrapper.iam.meta.client)44 policy_arn = 'arn:aws:test:::test-policy'45 iam_stubber.stub_delete_policy(policy_arn, error_code)46 if error_code is None:47 policy_wrapper.delete_policy(policy_arn)48 else:49 with pytest.raises(ClientError) as exc_info:50 policy_wrapper.delete_policy(policy_arn)51 assert exc_info.value.response['Error']['Code'] == error_code52@pytest.mark.parametrize("error_code", [None, "MalformedPolicyDocument"])53def test_create_policy_version(make_stubber, make_unique_name, error_code):54 iam_stubber = make_stubber(policy_wrapper.iam.meta.client)55 policy_arn = 'arn:aws:iam:::test/policy'56 actions = ['test:JustTest', 'test:AlsoTest']57 resource_arn = 'arn:aws:test:::test/resource'58 policy_version_id = 'test-policy-version'59 set_as_default = True60 policy_doc = json.dumps({61 "Version": "2012-10-17",62 "Statement": [63 {64 "Effect": "Allow",65 "Action": actions,66 "Resource": resource_arn67 }68 ]69 })70 iam_stubber.stub_create_policy_version(71 policy_arn, policy_version_id, policy_doc=policy_doc,72 set_as_default=set_as_default, error_code=error_code)73 if error_code is None:74 policy_version = policy_wrapper.create_policy_version(75 policy_arn, actions, resource_arn, True)76 assert policy_version.version_id == policy_version_id77 else:78 with pytest.raises(ClientError) as exc_info:79 policy_wrapper.create_policy_version(80 policy_arn, actions, resource_arn, True)81 assert exc_info.value.response['Error']['Code'] == error_code82@pytest.mark.parametrize("error_code", [None, "TestException"])83def test_list_policies(make_stubber, error_code):84 iam_stubber = make_stubber(policy_wrapper.iam.meta.client)85 scope = 'Local'86 policies = {87 f'test-policy-{index}': f'arn:aws:iam:::test/policy-{index}'88 for index in range(1, 5)89 }90 iam_stubber.stub_list_policies(scope, policies, error_code=error_code)91 if error_code is None:92 got_policies = policy_wrapper.list_policies(scope)93 assert len(got_policies) == len(policies)94 else:95 with pytest.raises(ClientError) as exc_info:96 policy_wrapper.list_policies(scope)97 assert exc_info.value.response['Error']['Code'] == error_code98@pytest.mark.parametrize("error_code", [None, "NoSuchEntity"])99def test_get_default_version_statement(make_stubber, error_code):100 iam_stubber = make_stubber(policy_wrapper.iam.meta.client)101 policy_name = 'test-policy'102 policy_arn = f'arn:aws:iam:::{policy_name}'103 policy_version_id = 'test-version-id'104 policy_doc = {105 'Version': '2012-10-17',106 'Statement': [107 {108 'Effect': 'Allow',109 'Action': ['test:DoSomething', 'test:DoSomethingElse'],110 'Resource': 'arn:aws:test:::test-resource'111 }112 ]113 }114 iam_stubber.stub_get_policy(115 policy_arn, policy_version_id, error_code=error_code)116 if error_code is None:117 iam_stubber.stub_get_policy_version(118 policy_arn, policy_version_id, json.dumps(policy_doc), error_code=error_code)119 if error_code is None:120 got_statement = policy_wrapper.get_default_policy_statement(policy_arn)121 assert got_statement == policy_doc['Statement']122 else:123 with pytest.raises(ClientError) as exc_info:124 policy_wrapper.get_default_policy_statement(policy_arn)125 assert exc_info.value.response['Error']['Code'] == error_code126@pytest.mark.parametrize(127 "version_count,default_index,list_error_code,default_error_code",128 [(4, 3, None, None), (0, None, None, None), (3, 0, None, None),129 (2, None, None, None), (4, 3, "NoSuchEntity", None),130 (4, 3, None, "NoSuchEntity")])131def test_rollback_policy_version(132 make_stubber, version_count, default_index, list_error_code,133 default_error_code):134 iam_stubber = make_stubber(policy_wrapper.iam.meta.client)135 policy_arn = 'arn:aws:iam:::test-policy'136 policy_versions = [{137 'document': f'test-doc-{index+1}',138 'id': f'v{index+1}',139 'is_default': index == default_index,140 'create_date': datetime.datetime.now() + datetime.timedelta(days=index)141 } for index in range(version_count)]142 iam_stubber.stub_list_policy_versions(143 policy_arn, policy_versions, error_code=list_error_code)144 if list_error_code is None\145 and version_count > 0\146 and default_index is not None\147 and default_index > 0:148 iam_stubber.stub_set_default_policy_version(149 policy_arn, policy_versions[default_index-1]['id'],150 error_code=default_error_code)151 if default_error_code is None:152 iam_stubber.stub_delete_policy_version(153 policy_arn, policy_versions[default_index]['id'])154 if list_error_code is None and default_error_code is None:155 got_version = policy_wrapper.rollback_policy_version(policy_arn)156 if version_count == 0 or default_index is None \157 or default_index == 0:158 assert got_version is None159 else:160 assert got_version.version_id == policy_versions[default_index-1]['id']161 else:162 with pytest.raises(ClientError) as exc_info:163 policy_wrapper.rollback_policy_version(policy_arn)164 if list_error_code is not None:165 assert exc_info.value.response['Error']['Code'] == list_error_code166 else:167 assert exc_info.value.response['Error']['Code'] == default_error_code168@pytest.mark.parametrize("error_code", [None, "NoSuchEntity"])169def test_attach_to_role(make_stubber, error_code):170 iam_stubber = make_stubber(policy_wrapper.iam.meta.client)171 role_name = 'test-role'172 policy_arn = 'arn:aws:iam:::test-policy'173 iam_stubber.stub_attach_role_policy(role_name, policy_arn, error_code)174 if error_code is None:175 policy_wrapper.attach_to_role(role_name, policy_arn)176 else:177 with pytest.raises(ClientError) as exc_info:178 policy_wrapper.attach_to_role(role_name, policy_arn)179 assert exc_info.value.response['Error']['Code'] == error_code180@pytest.mark.parametrize("error_code", [None, "NoSuchEntity"])181def test_detach_from_role(make_stubber, error_code):182 iam_stubber = make_stubber(policy_wrapper.iam.meta.client)183 role_name = 'test-role'184 policy_arn = 'arn:aws:iam:::test-policy'185 iam_stubber.stub_detach_role_policy(role_name, policy_arn, error_code)186 if error_code is None:187 policy_wrapper.detach_from_role(role_name, policy_arn)188 else:189 with pytest.raises(ClientError) as exc_info:190 policy_wrapper.detach_from_role(role_name, policy_arn)...

Full Screen

Full Screen

bot.py

Source:bot.py Github

copy

Full Screen

...72 iam_client.attach_role_policy(RoleName=identity_arn.name, PolicyArn=str(policy_arn))73 else:74 raise Exception('Expected identity to be of resource type (user/role/group): {}'.format(identity_arn))75def create_policy(iam_client, policy_arn, policy_document):76 policy_arn = _new_policy_arn(policy_arn, policy_document)77 logging.info("New policy: {}".format(policy_arn))78 logging.info("[{}] Creating policy".format(policy_arn))79 try:80 iam_client.create_policy(PolicyName=policy_arn.name, PolicyDocument=policy_document)81 except botocore.exceptions.ClientError as error:82 if not error.response['Error']['Code'] == 'EntityAlreadyExists':83 raise84 logging.info("[{}] Policy already exists".format(policy_arn))85 return policy_arn86def _new_policy_arn(policy_arn, policy_document):87 policy_name = policy_arn.name88 m = _SONRAI_POLICY_NAME_PATTERN.match(policy_name)89 if m:90 base = m.group(1)91 else:92 base = "Sonrai-" + policy_name93 policy_name = base + "-" + hashlib.md5(policy_document.encode('utf-8')).hexdigest()94 return sonrai.platform.aws.arn.parse('arn:{}:{}:{}:{}:{}/{}'.format(95 policy_arn.partition or '', policy_arn.service or '', policy_arn.region or '',96 policy_arn.account_id or '', policy_arn.resource_type or '', policy_name))...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run localstack automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful