Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __get_boto3_session(self, config=None):
if config:
# If an API access key pair is provided in the 'aws' section, that
# has priority
if 'access_key' in config:
session = boto3.session.Session(
aws_access_key_id=config.get('access_key'),
aws_secret_access_key=config.get('secret_key'),
region_name=config.get('region', None)
)
# If an AWS profile in the 'aws' section, that comes next
elif 'profile' in config:
profile = config.get('profile')
if profile not in boto3.session.Session().available_profiles:
raise self.NoSuchAWSProfile("AWS profile '{}' does not exist in your ~/.aws/config".format(profile))
session = boto3.session.Session(
profile_name=config.get('profile'),
region_name=config.get('region', None)
)
else:
# We have an 'aws' section, but it has neither credentials nor
# a profile, so possibly it just has a region.
session = boto3.session.Session(
region_name=config.get('region', None)
)
else:
# There was no 'aws' section in our config, so just leave it up to
# the normal AWS credentials resolution
session = boto3.session.Session()
return session