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