Пример #1
0
    def test_get_assume_role_credentials(self):
        roleArn = 'role-arn'
        roleSessionName = 'role-session-name'
        mock_assume_role_client = mock.Mock()
        mock_assume_role_function = mock.Mock()
        mock_assume_role_client.assume_role = mock_assume_role_function

        awsumepy.get_assume_role_credentials(mock_assume_role_client, roleArn,
                                             roleSessionName)
        mock_assume_role_function.assert_called_once_with(
            RoleArn='role-arn', RoleSessionName='role-session-name')

        mock_assume_role_function.side_effect = Exception
        with self.assertRaises(SystemExit):
            awsumepy.get_assume_role_credentials(mock_assume_role_client,
                                                 roleArn, roleSessionName)
Пример #2
0
def refresh_session(oldSession, roleArn, sessionName):
    """
    oldSession - the session to refresh;
    roleArn - the role_arn used to make the assume_role call;
    sessionName - what to name the assumed role session;
    refresh the `oldSession` role credentials and return them
    """
    #create the client to make aws calls
    refreshClient = awsumepy.create_boto_sts_client(
        None, oldSession['SecretAccessKey'], oldSession['AccessKeyId'],
        oldSession['SessionToken'])
    #call assume_role
    roleCredentials = awsumepy.get_assume_role_credentials(
        refreshClient, roleArn, sessionName)
    #format the credentials for awsume
    newRoleSession = awsumepy.create_awsume_session(roleCredentials,
                                                    oldSession)
    #localize the expiration
    newRoleSession['Expiration'] = newRoleSession['Expiration'].replace(
        tzinfo=None)
    return newRoleSession