Пример #1
0
    def test_simple_mfa_in_cache_but_expired(self):
        response = {
            'Credentials': {
                'AccessKeyId': 'foo',
                'SecretAccessKey': 'bar',
                'SessionToken': 'baz',
                'Expiration': some_future_time().isoformat(),
            },
        }
        client_creator = create_client_creator(with_response=response)
        cache_key = ('ab1a4e3a5b3a855357eaf66f645e85c0f1147764')
        cache = {
            cache_key: {
                'Credentials': {
                    'AccessKeyId': 'foo-cached',
                    'SecretAccessKey': 'bar-cached',
                    'SessionToken': 'baz-cached',
                    'Expiration': datetime.now(tzlocal()),
                }
            }
        }

        refresher = credentials.SimpleMFACredentialFetcher(
            client_creator,
            self.source_creds,
            self.mfa_serial,
            mfa_prompter=self.prompter,
            cache=cache)
        expected = get_expected_creds_from_response(response)
        response = refresher.fetch_credentials()

        self.assertEqual(response, expected)
Пример #2
0
    def test_simple_mfa_with_tmp_config(self):
        response = {
            'Credentials': {
                'AccessKeyId': 'foo',
                'SecretAccessKey': 'bar',
                'SessionToken': 'baz',
                'Expiration': some_future_time().isoformat()
            },
        }
        profile_name = 'development'
        client_creator = create_client_creator(with_response=response)
        p = os.path.join(self.tempdir, "tmp_config_file")
        cache = credentials.SimpleMFACache(
            credentials.TempConfigWriter(p, profile_name, 'us-west-2'), {})
        provider = credentials.SimpleMFAProvider(self.create_config_loader(),
                                                 client_creator,
                                                 cache=cache,
                                                 profile_name=profile_name,
                                                 prompter=self.prompter)

        creds = provider.load()

        self.assertEqual(creds.access_key, 'foo')
        self.assertEqual(creds.secret_key, 'bar')
        self.assertEqual(creds.token, 'baz')
        self.assertEqual(response['Credentials'],
                         get_expected_creds_from_config(profile_name, p))
Пример #3
0
 def test_write_with_non_exist_file(self):
     response = {
         'Credentials': {
             'AccessKeyId': 'foo',
             'SecretAccessKey': 'bar',
             'SessionToken': 'baz',
             'Expiration': some_future_time().isoformat(),
         },
     }
     p = os.path.join(self.tempdir, "non_exist_config_file")
     writer = credentials.TempConfigWriter(p, self.profile_name,
                                           "us-west-2")
     writer.update(response)
     self.assertEqual(response['Credentials'],
                      get_expected_creds_from_config(self.profile_name, p))
Пример #4
0
    def test_no_cache(self):
        response = {
            'Credentials': {
                'AccessKeyId': 'foo',
                'SecretAccessKey': 'bar',
                'SessionToken': 'baz',
                'Expiration': some_future_time().isoformat()
            },
        }
        client_creator = create_client_creator(with_response=response)
        refresher = credentials.SimpleMFACredentialFetcher(
            client_creator,
            self.source_creds,
            self.mfa_serial,
            mfa_prompter=self.prompter,
        )

        expected_response = get_expected_creds_from_response(response)
        response = refresher.fetch_credentials()

        self.assertEqual(response, expected_response)
Пример #5
0
 def test_write_with_exist_file(self):
     response = {
         'Credentials': {
             'AccessKeyId': 'foo',
             'SecretAccessKey': 'bar',
             'SessionToken': 'baz',
             'Expiration': some_future_time().isoformat(),
         },
     }
     p = os.path.join(self.tempdir, "exist_config_file")
     config = ConfigParser()
     config.add_section("test")
     config.set("test", "foo", "bar")
     writer = credentials.TempConfigWriter(p, self.profile_name,
                                           "us-west-2")
     writer.update(response)
     self.assertEqual(response['Credentials'],
                      get_expected_creds_from_config(self.profile_name, p))
     config2 = ConfigParser()
     config2.readfp(open(p))
     self.assertEqual(config.get("test", "foo"), "bar")
Пример #6
0
    def test_simple_mfa_with_no_cache(self):
        response = {
            'Credentials': {
                'AccessKeyId': 'foo',
                'SecretAccessKey': 'bar',
                'SessionToken': 'baz',
                'Expiration': some_future_time().isoformat()
            },
        }
        client_creator = create_client_creator(with_response=response)
        provider = credentials.SimpleMFAProvider(self.create_config_loader(),
                                                 client_creator,
                                                 cache={},
                                                 profile_name='development',
                                                 prompter=self.prompter)

        creds = provider.load()

        self.assertEqual(creds.access_key, 'foo')
        self.assertEqual(creds.secret_key, 'bar')
        self.assertEqual(creds.token, 'baz')