Skip to content
This repository has been archived by the owner on Mar 26, 2024. It is now read-only.

replace the security token hack with a context manager #14

Merged
merged 1 commit into from
Sep 20, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions mkwheelhouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,22 @@ def spawn(args, capture_output=False):
return subprocess.check_call(args)


class UrlSafeS3Connection(object):
# Hack to work around Boto bug that generates invalid URLs when
# query_auth=False and an IAM role is in use.
# See: https://github.com/boto/boto/issues/2043
# See: https://github.com/WhoopInc/mkwheelhouse/issues/11
def __init__(self, s3_connection):
self.connection = s3_connection
self.security_token = s3_connection.provider.security_token

def __enter__(self):
self.connection.provider.security_token = ''

def __exit__(self, type, value, traceback):
self.connection.provider.security_token = self.security_token


class Bucket(object):
def __init__(self, url):
if not re.match(r'^(s3:)?//', url):
Expand All @@ -41,11 +57,6 @@ def __init__(self, url):
self.s3 = boto.s3.connect_to_region(
region_name=self.region,
calling_format=boto.s3.connection.OrdinaryCallingFormat())
# Hack to work around Boto bug that generates invalid URLs when
# query_auth=False and an IAM role is in use.
# See: https://github.com/boto/boto/issues/2043
# See: https://github.com/WhoopInc/mkwheelhouse/issues/11
self.s3.provider.security_token = ''
self.bucket = self.s3.get_bucket(self.name)

def _get_region(self):
Expand Down Expand Up @@ -80,7 +91,8 @@ def has_key(self, key):

def generate_url(self, key):
key = self.get_key(key)
return key.generate_url(expires_in=0, query_auth=False)
with UrlSafeS3Connection(self.s3):
return key.generate_url(expires_in=0, query_auth=False)

def list(self):
return self.bucket.list(prefix=self.prefix)
Expand Down