/usr/lib/python2.7/dist-packages/botocore/credentials.py is in python-botocore 0.29.0+repack-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | # Copyright (c) 2012-2013 Mitch Garnaat http://garnaat.org/
# Copyright 2012-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish, dis-
# tribute, sublicense, and/or sell copies of the Software, and to permit
# persons to whom the Software is furnished to do so, subject to the fol-
# lowing conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#
import os
import requests
import logging
from six.moves import configparser
from botocore.compat import json
logger = logging.getLogger(__name__)
class Credentials(object):
"""
Holds the credentials needed to authenticate requests. In addition
the Credential object knows how to search for credentials and how
to choose the right credentials when multiple credentials are found.
:ivar access_key: The access key part of the credentials.
:ivar secret_key: The secret key part of the credentials.
:ivar token: The security token, valid only for session credentials.
:ivar method: A string which identifies where the credentials
were found. Valid values are: iam_role|env|config|boto.
"""
def __init__(self, access_key=None, secret_key=None, token=None):
self.access_key = access_key
self.secret_key = secret_key
self.token = token
self.method = None
self.profiles = []
def _search_md(url='http://169.254.169.254/latest/meta-data/iam/security-credentials/'):
d = {}
try:
r = requests.get(url, timeout=.1)
if r.status_code == 200 and r.content:
fields = r.content.decode('utf-8').split('\n')
for field in fields:
if field.endswith('/'):
d[field[0:-1]] = _search_md(url + field)
else:
val = requests.get(url + field).content.decode('utf-8')
if val[0] == '{':
val = json.loads(val)
else:
p = val.find('\n')
if p > 0:
val = r.content.decode('utf-8').split('\n')
d[field] = val
except (requests.Timeout, requests.ConnectionError):
pass
return d
def search_iam_role(**kwargs):
credentials = None
metadata = kwargs.get('metadata', None)
if metadata is None:
metadata = _search_md()
if metadata:
for role_name in metadata:
credentials = Credentials(metadata[role_name]['AccessKeyId'],
metadata[role_name]['SecretAccessKey'],
metadata[role_name]['Token'])
credentials.method = 'iam-role'
logger.info('Found IAM Role: %s', role_name)
return credentials
def search_environment(**kwargs):
"""
Search for credentials in explicit environment variables.
"""
session = kwargs.get('session')
credentials = None
access_key = session.get_variable('access_key', ('env',))
secret_key = session.get_variable('secret_key', ('env',))
token = session.get_variable('token', ('env',))
if access_key and secret_key:
credentials = Credentials(access_key, secret_key, token)
credentials.method = 'env'
logger.info('Found credentials in Environment variables.')
return credentials
def search_credentials_file(**kwargs):
"""
Search for a credential file used by original EC2 CLI tools.
"""
credentials = None
if 'AWS_CREDENTIAL_FILE' in os.environ:
full_path = os.path.expanduser(os.environ['AWS_CREDENTIAL_FILE'])
try:
lines = map(str.strip, open(full_path).readlines())
except IOError:
logger.warn('Unable to load AWS_CREDENTIAL_FILE (%s).', full_path)
else:
config = dict(line.split('=', 1) for line in lines if '=' in line)
access_key = config.get('AWSAccessKeyId')
secret_key = config.get('AWSSecretKey')
if access_key and secret_key:
credentials = Credentials(access_key, secret_key)
credentials.method = 'credentials-file'
logger.info('Found credentials in AWS_CREDENTIAL_FILE.')
return credentials
def search_file(**kwargs):
"""
If there is are credentials in the configuration associated with
the session, use those.
"""
credentials = None
session = kwargs.get('session')
access_key = session.get_variable('access_key', methods=('config',))
secret_key = session.get_variable('secret_key', methods=('config',))
token = session.get_variable('token', ('config',))
if access_key and secret_key:
credentials = Credentials(access_key, secret_key, token)
credentials.method = 'config'
logger.info('Found credentials in config file.')
return credentials
def search_boto_config(**kwargs):
"""
Look for credentials in boto config file.
"""
credentials = access_key = secret_key = None
if 'BOTO_CONFIG' in os.environ:
paths = [os.environ['BOTO_CONFIG']]
else:
paths = ['/etc/boto.cfg', '~/.boto']
paths = [os.path.expandvars(p) for p in paths]
paths = [os.path.expanduser(p) for p in paths]
cp = configparser.RawConfigParser()
cp.read(paths)
if cp.has_section('Credentials'):
if cp.has_option('Credentials', 'aws_access_key_id'):
access_key = cp.get('Credentials', 'aws_access_key_id')
if cp.has_option('Credentials', 'aws_secret_access_key'):
secret_key = cp.get('Credentials', 'aws_secret_access_key')
if access_key and secret_key:
credentials = Credentials(access_key, secret_key)
credentials.method = 'boto'
logger.info('Found credentials in boto config file.')
return credentials
AllCredentialFunctions = [search_environment,
search_credentials_file,
search_file,
search_boto_config,
search_iam_role]
_credential_methods = (('env', search_environment),
('config', search_file),
('credentials-file', search_credentials_file),
('boto', search_boto_config),
('iam-role', search_iam_role))
def get_credentials(session, metadata=None):
credentials = None
for cred_method, cred_fn in _credential_methods:
credentials = cred_fn(session=session,
metadata=metadata)
if credentials:
break
return credentials
|