Last active
January 2, 2016 15:29
-
-
Save ivanlei/8323642 to your computer and use it in GitHub Desktop.
This is a script takes AWS credentials csv file containing an AWS keypair and:
* Uses keyring package to store the Secret Access Key
* Outputs the proper config lines for .boto file to use the keyring
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# | |
# This is a script takes AWS credentials csv file containing an AWS keypair and: | |
# * Uses keyring package to store the Secret Access Key | |
# * Outputs the proper config lines for .boto file to use the keyring | |
# | |
import sys | |
import keyring | |
import optparse | |
from csv import DictReader | |
def get_cmdline_options(): | |
"""Read commandline options | |
:return: an object with `.credentials_csv' and `.keyring_name` set | |
""" | |
parser = optparse.OptionParser() | |
parser.add_option('-i', '--credentials_csv', dest='credentials_csv', default='credentials.csv', | |
help='file path to credentials file') | |
parser.add_option('-r', '--keyring_name', dest='keyring_name', default='aws_private_key', | |
help='name of keyring to store AWS secret access key in') | |
options, _ = parser.parse_args() | |
return options | |
def read_credentials_csv(credentials_csv): | |
"""Read the credentials CSV file into a dict""" | |
reader = DictReader(open(credentials_csv, 'rU')) | |
for row in reader: | |
aws_access_key = row['Access Key Id'] | |
aws_secret_key = row['Secret Access Key'] | |
return aws_access_key, aws_secret_key | |
def main(): | |
options = get_cmdline_options() | |
aws_access_key, aws_secret_key = read_credentials_csv(options.credentials_csv) | |
keyring.set_password(options.keyring_name, aws_access_key, aws_secret_key) | |
print '[Credentials]' | |
print 'aws_access_key_id = {0}'.format(aws_access_key) | |
print 'keyring = {0}'.format(options.keyring_name) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment