1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
# Copyright 2014 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
from botocore.credentials import (
AssumeRoleProvider,
AssumeRoleWithWebIdentityProvider,
CredentialResolver,
)
from botocore.exceptions import ProfileNotFound
from botocore.hooks import HierarchicalEmitter
from botocore.session import Session
from awscli.customizations import assumerole
from awscli.testutils import mock, unittest
class TestAssumeRolePlugin(unittest.TestCase):
def test_assume_role_provider_injected(self):
mock_assume_role = mock.Mock(spec=AssumeRoleProvider)
mock_web_identity = mock.Mock(spec=AssumeRoleWithWebIdentityProvider)
providers = {
'assume-role': mock_assume_role,
'assume-role-with-web-identity': mock_web_identity,
}
mock_resolver = mock.Mock(spec=CredentialResolver)
mock_resolver.get_provider = providers.get
session = mock.Mock(spec=Session)
session.get_component.return_value = mock_resolver
assumerole.inject_assume_role_provider_cache(
session, event_name='building-command-table.foo'
)
session.get_component.assert_called_with('credential_provider')
self.assertIsInstance(mock_assume_role.cache, assumerole.JSONFileCache)
self.assertIsInstance(
mock_web_identity.cache,
assumerole.JSONFileCache,
)
def test_assume_role_provider_registration(self):
event_handlers = HierarchicalEmitter()
assumerole.register_assume_role_provider(event_handlers)
session = mock.Mock(spec=Session)
event_handlers.emit('session-initialized', session=session)
# Just verifying that anything on the session was called ensures
# that our handler was called, as it's the only thing that should
# be registered.
session.get_component.assert_called_with('credential_provider')
def test_no_registration_if_profile_does_not_exist(self):
session = mock.Mock(spec=Session)
session.get_component.side_effect = ProfileNotFound(profile='unknown')
assumerole.inject_assume_role_provider_cache(
session, event_name='building-command-table.foo'
)
credential_provider = session.get_component.return_value
self.assertFalse(credential_provider.get_provider.called)
|