Path: blob/develop/tests/unit/customizations/emrcontainers/test_update_assume_role_policy.py
1569 views
# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.1#2# Licensed under the Apache License, Version 2.0 (the "License"). You3# may not use this file except in compliance with the License. A copy of4# the License is located at5#6# http://aws.amazon.com/apache2.0/7#8# or in the "license" file accompanying this file. This file is9# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF10# ANY KIND, either express or implied. See the License for the specific11# language governing permissions and limitations under the License.1213import copy14import json1516from awscli.testutils import BaseAWSCommandParamsTest, mock, unittest17from awscli.customizations.emrcontainers.base36 import Base3618from awscli.customizations.emrcontainers.constants \19import TRUST_POLICY_STATEMENT_FORMAT, \20TRUST_POLICY_STATEMENT_ALREADY_EXISTS, \21TRUST_POLICY_UPDATE_SUCCESSFUL222324def json_matches(first, second):25return json.dumps(first, sort_keys=True) == json.dumps(second,26sort_keys=True)272829class TestUpdateAssumeRolePolicy(BaseAWSCommandParamsTest):30cluster_name = 'test-cluster'31namespace = 'test'32role_name = 'myrole'33account_id = '123456789012'34oidc_provider = 'oidc-provider/id/test'35aws_partition = 'aws'36aws_cn_partition = 'aws-cn'3738base36_encoded_role_name = Base36().encode(role_name)39expected_statement = TRUST_POLICY_STATEMENT_FORMAT % {40"AWS_ACCOUNT_ID": account_id,41"OIDC_PROVIDER": oidc_provider,42"NAMESPACE": namespace,43"BASE36_ENCODED_ROLE_NAME": base36_encoded_role_name,44"AWS_PARTITION": aws_partition45}4647expected_statement_cn = TRUST_POLICY_STATEMENT_FORMAT % {48"AWS_ACCOUNT_ID": account_id,49"OIDC_PROVIDER": oidc_provider,50"NAMESPACE": namespace,51"BASE36_ENCODED_ROLE_NAME": base36_encoded_role_name,52"AWS_PARTITION": aws_cn_partition53}5455def setUp(self):56super(TestUpdateAssumeRolePolicy, self).setUp()5758self.command = (59'emr-containers update-role-trust-policy --cluster-name=%s '60'--namespace=%s --role-name=%s' % (61self.cluster_name, self.namespace, self.role_name)62)63self.policy_document = {64"Version": "2012-10-17",65"Statement": [66{67"Effect": "Allow",68"Principal": {69"AWS": "arn:aws:iam::123456789012:root"70},71"Action": "sts:AssumeRole"72}73]74}7576self.expected_policy_document = copy.deepcopy(self.policy_document)77self.expected_policy_document.get("Statement").append(78json.loads(self.expected_statement))7980self.expected_policy_document_cn = copy.deepcopy(self.policy_document)81self.expected_policy_document_cn.get("Statement").append(82json.loads(self.expected_statement_cn))8384# Assert the call to update trust policy of the role85def assert_trust_policy_updated(self, cmd_output,86expected_policy_document=None):87if expected_policy_document is None:88expected_policy_document = self.expected_policy_document8990self.assertTrue(TRUST_POLICY_UPDATE_SUCCESSFUL % self.role_name91in cmd_output)9293# Check if UpdateAssumeRolePolicy was invoked94self.assertEqual(len(self.operations_called), 1)95self.assertEqual(self.operations_called[0][0].name,96'UpdateAssumeRolePolicy')97self.assertEqual(self.operations_called[0][1]['RoleName'],98self.role_name)99100self.assertTrue(json_matches(json.loads(101self.operations_called[0][1]['PolicyDocument']),102expected_policy_document))103104# Use case: Expected trust policy does not exist105# Expected results: Operation is performed by client106# to update the trust policy in expected format107@mock.patch('awscli.customizations.emrcontainers.'108'iam.IAM.get_assume_role_policy')109@mock.patch('awscli.customizations.emrcontainers.'110'eks.EKS.get_oidc_issuer_id')111@mock.patch('awscli.customizations.emrcontainers.'112'eks.EKS.get_account_id')113def test_trust_policy_does_not_exist(self, get_account_id_patch,114get_oidc_issuer_id_patch,115get_assume_role_policy_patch):116get_assume_role_policy_patch.return_value = self.policy_document117get_oidc_issuer_id_patch.return_value = self.oidc_provider118get_account_id_patch.return_value = self.account_id119120output = self.run_cmd(self.command, expected_rc=0)121self.assert_trust_policy_updated(output[0])122123# Use case: Expected trust policy exists but the condition section124# has an additional condition125# Expected results: Operation is performed by client to update126# the trust policy in expected format127@mock.patch('awscli.customizations.emrcontainers.'128'iam.IAM.get_assume_role_policy')129@mock.patch('awscli.customizations.emrcontainers.'130'eks.EKS.get_oidc_issuer_id')131@mock.patch('awscli.customizations.emrcontainers.'132'eks.EKS.get_account_id')133def test_trust_policy_exists_with_more_keys(self, get_account_id_patch,134get_oidc_issuer_id_patch,135get_assume_role_policy_patch):136statement_with_additional_condition_key = json.loads(137self.expected_statement)138statement_with_additional_condition_key.get("Condition").get(139"StringLike")["test:key"] = "value"140self.policy_document.get("Statement").append(141statement_with_additional_condition_key)142143self.expected_policy_document = copy.deepcopy(self.policy_document)144self.expected_policy_document.get("Statement").append(json.loads(145self.expected_statement))146147get_assume_role_policy_patch.return_value = self.policy_document148get_oidc_issuer_id_patch.return_value = self.oidc_provider149get_account_id_patch.return_value = self.account_id150151output = self.run_cmd(self.command, expected_rc=0)152self.assert_trust_policy_updated(output[0])153154# Use case: Initial trust policy document does not have Statements section155# Expected results: Operation is performed by client to update156# the trust policy in expected format157@mock.patch('awscli.customizations.emrcontainers.'158'iam.IAM.get_assume_role_policy')159@mock.patch('awscli.customizations.emrcontainers.'160'eks.EKS.get_oidc_issuer_id')161@mock.patch('awscli.customizations.emrcontainers.'162'eks.EKS.get_account_id')163def test_policy_document_has_missing_key(self,164get_account_id_patch,165get_oidc_issuer_id_patch,166get_assume_role_policy_patch):167del self.policy_document["Statement"]168169self.expected_policy_document = copy.deepcopy(self.policy_document)170self.expected_policy_document["Statement"] = [json.loads(171self.expected_statement)]172173get_assume_role_policy_patch.return_value = self.policy_document174get_oidc_issuer_id_patch.return_value = self.oidc_provider175get_account_id_patch.return_value = self.account_id176177output = self.run_cmd(self.command, expected_rc=0)178self.assert_trust_policy_updated(output[0])179180# Use case: Initial trust policy document has empty Statements section181# Expected results: Operation is performed by client to update182# the trust policy in expected format183@mock.patch('awscli.customizations.emrcontainers.'184'iam.IAM.get_assume_role_policy')185@mock.patch('awscli.customizations.emrcontainers.'186'eks.EKS.get_oidc_issuer_id')187@mock.patch('awscli.customizations.emrcontainers.'188'eks.EKS.get_account_id')189def test_policy_document_has_empty_statements(self,190get_account_id_patch,191get_oidc_issuer_id_patch,192get_assume_role_policy_patch):193del self.policy_document.get("Statement")[:]194195self.expected_policy_document = copy.deepcopy(self.policy_document)196self.expected_policy_document.get("Statement").append(json.loads(197self.expected_statement))198199get_assume_role_policy_patch.return_value = self.policy_document200get_oidc_issuer_id_patch.return_value = self.oidc_provider201get_account_id_patch.return_value = self.account_id202203output = self.run_cmd(self.command, expected_rc=0)204self.assert_trust_policy_updated(output[0])205206# Use case: Expected trust policy does not exist and user performs a dry run207# Expected results: No operation is performed by client208# The command should print the expected policy document to stdout209@mock.patch('awscli.customizations.emrcontainers.'210'iam.IAM.get_assume_role_policy')211@mock.patch('awscli.customizations.emrcontainers.'212'eks.EKS.get_oidc_issuer_id')213@mock.patch('awscli.customizations.emrcontainers.'214'eks.EKS.get_account_id')215def test_trust_policy_does_not_exist_dry_run(self, get_account_id_patch,216get_oidc_issuer_id_patch,217get_assume_role_policy_patch):218get_assume_role_policy_patch.return_value = self.policy_document219get_oidc_issuer_id_patch.return_value = self.oidc_provider220get_account_id_patch.return_value = self.account_id221222output = self.run_cmd(self.command + " --dry-run", expected_rc=0)223self.assertEqual(len(self.operations_called), 0)224self.assertTrue(json_matches(json.loads(output[0]),225self.expected_policy_document))226227# Use case: Expected trust policy already exists228# Expected results: No operation is performed by client229# The command should print that the trust policy statement already exists230@mock.patch('awscli.customizations.emrcontainers.'231'iam.IAM.get_assume_role_policy')232@mock.patch('awscli.customizations.emrcontainers.'233'eks.EKS.get_oidc_issuer_id')234@mock.patch('awscli.customizations.emrcontainers.'235'eks.EKS.get_account_id')236def test_trust_policy_exists(self, get_account_id_patch,237get_oidc_issuer_id_patch,238get_assume_role_policy_patch):239self.policy_document = self.expected_policy_document240241get_assume_role_policy_patch.return_value = self.policy_document242get_oidc_issuer_id_patch.return_value = self.oidc_provider243get_account_id_patch.return_value = self.account_id244245output = self.run_cmd(self.command, expected_rc=0)246self.assertEqual(len(self.operations_called), 0)247self.assertTrue(TRUST_POLICY_STATEMENT_ALREADY_EXISTS % self.role_name248in output[0])249250# Use case: Expected trust policy does not exist in cn-north-1251# Expected results: Operation is performed by client in cn-north-1252# to update the trust policy in expected format253@mock.patch('awscli.customizations.emrcontainers.'254'iam.IAM.get_assume_role_policy')255@mock.patch('awscli.customizations.emrcontainers.'256'eks.EKS.get_oidc_issuer_id')257@mock.patch('awscli.customizations.emrcontainers.'258'eks.EKS.get_account_id')259def test_trust_policy_does_not_exist_in_cn(self,260get_account_id_patch,261get_oidc_issuer_id_patch,262get_assume_role_policy_patch):263get_assume_role_policy_patch.return_value = self.policy_document264get_oidc_issuer_id_patch.return_value = self.oidc_provider265get_account_id_patch.return_value = self.account_id266self.command += ' --region cn-north-1'267268output = self.run_cmd(self.command, expected_rc=0)269self.assert_trust_policy_updated(output[0],270self.expected_policy_document_cn)271272273if __name__ == "__main__":274unittest.main()275276277