<commit_before>#!/usr/bin/env python

import boto3

# The calls to AWS STS AssumeRole must be signed with the access key ID
# and secret access key of an existing IAM user or using existing temporary 
# credentials. (You cannot call AssumeRole with the access key for the root 
# account.) The credentials can be in environment variables or in a 
# configuration file and will be discovered automatically by the 
# boto3.client() function. For more information, see the Python SDK 
# documentation: http://boto3.readthedocs.org/en/latest/guide/sqs.html

# create an STS client object that represents a live connection to the 
# STS service
sts_client = boto3.client('sts')

task_executor = "arn:aws:iam::968994658855:role/task_executor"

roleArn = "arn:aws:iam::968994658855:role/task_executor"
# Call the assume_role method of the STSConnection object and pass the role
# ARN and a role session name.
assumedRoleObject = sts_client.assume_role(
    RoleArn="arn:aws:iam::968994658855:role/task_executor",
    RoleSessionName="AssumeRoleSessionasd1"
)

# From the response that contains the assumed role, get the temporary 
# credentials that can be used to make subsequent API calls
credentials = assumedRoleObject['Credentials']


def print_creds(creds):
    print "Expiration : ", creds["Expiration"]
    print "Key Id     : ", creds["AccessKeyId"]
    print "Secret Key : ", creds["SecretAccessKey"]
    print "Token      : ", creds["SessionToken"]


print_creds(credentials)


# Use the temporary credentials that AssumeRole returns to make a 
# connection to Amazon S3  
s3_resource = boto3.resource(
    's3',
    aws_access_key_id = credentials['AccessKeyId'],
    aws_secret_access_key = credentials['SecretAccessKey'],
    aws_session_token = credentials['SessionToken'],
)

# Use the Amazon S3 resource object that is now configured with the 
# credentials to access your S3 buckets. 
for bucket in s3_resource.buckets.all():
    print(bucket.name)<commit_msg>Adding STS tests<commit_after>#!/usr/bin/env python

import time
import boto3

# The calls to AWS STS AssumeRole must be signed with the access key ID
# and secret access key of an existing IAM user or using existing temporary 
# credentials. (You cannot call AssumeRole with the access key for the root 
# account.) The credentials can be in environment variables or in a 
# configuration file and will be discovered automatically by the 
# boto3.client() function. For more information, see the Python SDK 
# documentation: http://boto3.readthedocs.org/en/latest/guide/sqs.html


def get_temp_creds(role_arn):
    # create an STS client object that represents a live connection to the 
    # STS service
    sts_client = boto3.client('sts')

    session_name = "Temp_session_{0}".format(int(time.time()))
    
    # Call the assume_role method of the STSConnection object and pass the role
    # ARN and a role session name.
    assumedRoleObject = sts_client.assume_role(RoleArn=role_arn,
                                               RoleSessionName=session_name)
                                           

    credentials = assumedRoleObject['Credentials']
    return credentials

def print_creds(creds):
    print "Expiration : ", creds["Expiration"]
    print "Key Id     : ", creds["AccessKeyId"]
    print "Secret Key : ", creds["SecretAccessKey"]
    print "Token      : ", creds["SessionToken"]


if __name__ == "__main__" :

    print "Running STS tests:"
    # From the response that contains the assumed role, get the temporary 
    # credentials that can be used to make subsequent API calls

    role_pfx = "arn:aws:iam::968994658855:role/"
    roles    = ["klab_public", "wos_read_access", "jstor_access", "god_mode"]
    
    for role in roles:
        credentials = get_temp_creds(role_pfx+role)
        print_creds(credentials)


        # Use the temporary credentials that AssumeRole returns to make a 
        # connection to Amazon S3  
        s3_resource = boto3.resource(
            's3',
            aws_access_key_id = credentials['AccessKeyId'],
            aws_secret_access_key = credentials['SecretAccessKey'],
            aws_session_token = credentials['SessionToken'],
        )

        # Use the Amazon S3 resource object that is now configured with the 
        # credentials to access your S3 buckets. 
        for bucket in s3_resource.buckets.all():
            print(bucket.name)
{'fraction_non_alphanumeric': 0.08005458267000228, 'fraction_numerical': 0.016374801000682282, 'mean_word_length': 3.478615071283096, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6145264', 'n_tokens_mistral': 1295, 'n_tokens_neox': 1238, 'n_words': 430}