Exemple #1
0
def update_credentials_file(filename, target_profile, source_profile,
                            credentials, new_access_key):
    if target_profile != source_profile:
        credentials.remove_section(target_profile)
        # Hack: Python 2's implementation of ConfigParser rejects new sections
        # named 'default'.
        if PY2 and target_profile == 'default':
            # noinspection PyProtectedMember
            credentials._sections[
                target_profile] = configparser._default_dict()
        else:
            credentials.add_section(target_profile)

        for k, v in credentials.items(source_profile):
            credentials.set(target_profile, k, v)

    credentials.set(target_profile, 'aws_access_key_id',
                    new_access_key['AccessKeyId'])
    credentials.set(target_profile, 'aws_secret_access_key',
                    new_access_key['SecretAccessKey'])
    if 'SessionToken' in new_access_key:
        credentials.set(target_profile, 'aws_session_token',
                        new_access_key['SessionToken'])
        credentials.set(target_profile, 'awsmfa_expiration',
                        new_access_key['Expiration'].isoformat())
    else:
        credentials.remove_option(target_profile, 'aws_session_token')
        credentials.remove_option(target_profile, 'awsmfa_expiration')

    temp_credentials_file = filename + ".tmp"
    with open(temp_credentials_file, "w") as out:
        credentials.write(out)
    os.rename(temp_credentials_file, filename)
Exemple #2
0
def update_credentials_file(filename, target_profile, source_profile,
                            credentials, new_access_key):
    # reload credentials before writing to avoid conflicts with other processes
    credentials.read(filename)
    if target_profile != source_profile:
        credentials.remove_section(target_profile)
        # Hack: Python 2's implementation of ConfigParser rejects new sections
        # named 'default'.
        if PY2 and target_profile == 'default':
            # noinspection PyProtectedMember
            credentials._sections[target_profile] = configparser._default_dict(
            )
        else:
            credentials.add_section(target_profile)

        for k, v in credentials.items(source_profile):
            credentials.set(target_profile, k, v)

    credentials.set(target_profile, 'aws_access_key_id',
                    new_access_key['AccessKeyId'])
    credentials.set(target_profile, 'aws_secret_access_key',
                    new_access_key['SecretAccessKey'])
    if 'SessionToken' in new_access_key:
        credentials.set(target_profile, 'aws_session_token',
                        new_access_key['SessionToken'])
        credentials.set(target_profile, 'awsmfa_expiration',
                        new_access_key['Expiration'].isoformat())
    else:
        credentials.remove_option(target_profile, 'aws_session_token')
        credentials.remove_option(target_profile, 'awsmfa_expiration')

    temp_credentials_file = filename + ".tmp"
    with open(temp_credentials_file, "w") as out:
        credentials.write(out)
    try:
        os.rename(temp_credentials_file, filename)
    except WindowsError as E:
        os.remove(filename)
        os.rename(temp_credentials_file, filename)