Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use boto3 to manage credentials #78

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
80 changes: 12 additions & 68 deletions s3iam.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import os
import re

import boto3
import yum
import yum.config
import yum.Errors
Expand Down Expand Up @@ -174,7 +175,6 @@ def __init__(self, repoid, repo):
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)

self.iamrole = None
self.grabber = None
self.enable()

Expand All @@ -191,11 +191,9 @@ def grab(self):
elif self.delegated_role:
self.grabber.get_delegated_role_credentials(self.delegated_role)
else:
self.grabber.get_role()
self.grabber.get_credentials()
return self.grabber


class S3Grabber(object):

def __init__(self, repo):
Expand All @@ -222,59 +220,20 @@ def __init__(self, repo):
# Ensure urljoin doesn't ignore base path:
if not self.baseurl.endswith('/'):
self.baseurl += '/'
self.session = boto3.Session()
self.access_key = None
self.secret_key = None
self.token = None

def get_role(self):
"""Read IAM role from AWS metadata store."""
request = urllib2.Request(
urlparse.urljoin(
"http://169.254.169.254",
"/latest/meta-data/iam/security-credentials/"
))

try:
response = urllib2.urlopen(request)
self.iamrole = (response.read())
except Exception:
response = None
self.iamrole = ""
finally:
if response:
response.close()

def get_credentials(self):
"""Read IAM credentials from AWS metadata store.
Note: This method should be explicitly called after constructing new
object, as in 'explicit is better than implicit'.
"""
request = urllib2.Request(
urlparse.urljoin(
urlparse.urljoin(
"http://169.254.169.254/",
"latest/meta-data/iam/security-credentials/",
), self.iamrole))

try:
response = urllib2.urlopen(request)
data = json.loads(response.read())
self.access_key = data['AccessKeyId']
self.secret_key = data['SecretAccessKey']
self.token = data['Token']
except Exception:
response = None
finally:
if response:
response.close()

if self.access_key is None and self.secret_key is None:
if "AWS_ACCESS_KEY_ID" in os.environ:
self.access_key = os.environ['AWS_ACCESS_KEY_ID']
if "AWS_SECRET_ACCESS_KEY" in os.environ:
self.secret_key = os.environ['AWS_SECRET_ACCESS_KEY']
if "AWS_SESSION_TOKEN" in os.environ:
self.token = os.environ['AWS_SESSION_TOKEN']
credentials = self.session.get_credentials()
self.access_key = credentials.access_key
self.secret_key = credentials.secret_key
self.token = credentials.token

if self.access_key is None and self.secret_key is None:
if hasattr(self, 'name'):
Expand All @@ -296,31 +255,16 @@ def get_delegated_role_credentials(self, delegated_role):
Note: This method should be explicitly called after constructing new
object, as in 'explicit is better than implicit'.
"""
import boto.sts
sts_client = self.session.client('sts', region_name=self.get_instance_region())
assumed_role = sts_client.assume_role(RoleArn=delegated_role, RoleSessionName='yum')

sts_conn = boto.sts.connect_to_region(self.get_instance_region())
assumed_role = sts_conn.assume_role(delegated_role, 'yum')

self.access_key = assumed_role.credentials.access_key
self.secret_key = assumed_role.credentials.secret_key
self.token = assumed_role.credentials.session_token
self.access_key = assumed_role['Credentials']['AccessKeyId']
self.secret_key = assumed_role['Credentials']['SecretAccessKey']
self.token = assumed_role['Credentials']['SessionToken']

def get_instance_region(self):
"""Read region from AWS metadata store."""
request = urllib2.Request(
urlparse.urljoin(
"http://169.254.169.254",
"/latest/meta-data/placement/availability-zone"
))

response = None
try:
response = urllib2.urlopen(request)
data = response.read()
finally:
if response:
response.close()
return data[:-1]
return self.session.region_name

def _request(self, path, timeval=None):
url = urlparse.urljoin(self.baseurl, urllib2.quote(path))
Expand Down
2 changes: 1 addition & 1 deletion yum-plugin-s3-iam.spec
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Source0: %{name}-%{version}.tar.gz
BuildRoot: %(mktemp -ud %{_tmppath}/%{name}-%{version}-%{release}-XXXXXX)
BuildArch: noarch

Requires: yum
Requires: yum, python2-boto3

%description
Yum package manager plugin for private S3 repositories.
Expand Down