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)
try:
os.rename(temp_credentials_file, filename)
except WindowsError as E:
os.remove(filename)
os.rename(temp_credentials_file, filename)
评论列表
文章目录