<commit_before>from core.region_connection import IAMConnection

IAM_USER = 'low_privileged_user'
IAM_GROUP = 'low_privileged_group'
GROUP_PRIVILEGES = '''\
{
  "Statement": [
    {
      "Sid": "Stmt1377108914887",
      "Action": "rds:*",
      "Effect": "Allow",
      "Resource": "*"
    },
    {
      "Sid": "Stmt1377108934836",
      "Action": "iam:*",
      "Effect": "Allow",
      "Resource": "*"
    },
    {
      "Sid": "Stmt1377109045369",
      "Action": "sqs:*",
      "Effect": "Allow",
      "Resource": "*"
    }
  ]
}
'''



def spawn_iam_user():
    '''
    Create an IAM user and return the API key and secret.
    '''
    conn = IAMConnection()
    conn.create_user(IAM_USER)
    credentials = conn.create_access_key(user_name=IAM_USER)
    
    key = credentials['create_access_key_response']['create_access_key_result']['access_key']
    api_key = key['access_key_id']
    api_secret = key['secret_access_key']

    conn.create_group(IAM_GROUP)
    conn.add_user_to_group(IAM_GROUP, IAM_USER)

    conn.put_group_policy(IAM_GROUP, 'LowPrivsPolicy', GROUP_PRIVILEGES)
    
    return api_key, api_secret

def teardown_iam_user():
    '''
    Remove the IAM user
    '''
    raise NotImplementedError<commit_msg>A working teardown<commit_after>from core.region_connection import IAMConnection

IAM_USER = 'low_privileged_user'
IAM_GROUP = 'low_privileged_group'
GROUP_PRIVILEGES = '''\
{
  "Statement": [
    {
      "Sid": "Stmt1377108914887",
      "Action": "rds:*",
      "Effect": "Allow",
      "Resource": "*"
    },
    {
      "Sid": "Stmt1377108934836",
      "Action": "iam:*",
      "Effect": "Allow",
      "Resource": "*"
    },
    {
      "Sid": "Stmt1377109045369",
      "Action": "sqs:*",
      "Effect": "Allow",
      "Resource": "*"
    }
  ]
}
'''



def spawn_iam_user():
    '''
    Create an IAM user and return the API key and secret.
    '''
    teardown_iam_user()
    
    conn = IAMConnection()
    conn.create_user(IAM_USER)
    credentials = conn.create_access_key(user_name=IAM_USER)
    
    key = credentials['create_access_key_response']['create_access_key_result']['access_key']
    api_key = key['access_key_id']
    api_secret = key['secret_access_key']

    conn.create_group(IAM_GROUP)
    conn.add_user_to_group(IAM_GROUP, IAM_USER)

    conn.put_group_policy(IAM_GROUP, 'LowPrivsPolicy', GROUP_PRIVILEGES)
    
    return api_key, api_secret

def teardown_iam_user():
    '''
    Remove the IAM user
    '''
    conn = IAMConnection()
    conn.remove_user_from_group(IAM_GROUP, IAM_USER)
    
    access_keys_response = conn.get_all_access_keys(IAM_USER)
    access_keys = access_keys_response['list_access_keys_response']['list_access_keys_result']['access_key_metadata']
    
    for access_key in access_keys:
        conn.delete_access_key(access_key['access_key_id'], user_name=IAM_USER)
        
    conn.delete_user(IAM_USER)
    conn.delete_group_policy(IAM_GROUP, 'LowPrivsPolicy')
    conn.delete_group(IAM_GROUP)
{'fraction_non_alphanumeric': 0.13578595317725753, 'fraction_numerical': 0.02608695652173913, 'mean_word_length': 3.431111111111111, 'pattern_counts': {'":': 26, '<': 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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6902221', 'n_tokens_mistral': 1134, 'n_tokens_neox': 1071, 'n_words': 162}