-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy paths3iam.py
executable file
·296 lines (254 loc) · 10.2 KB
/
s3iam.py
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!/usr/bin/env python
# Copyright 2012, Julius Seporaitis
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import urllib2
import urlparse
import time
import hashlib
import hmac
import json
import yum
import yum.config
import yum.Errors
import yum.plugins
from yum.yumRepo import YumRepository
__author__ = "Julius Seporaitis"
__email__ = "[email protected]"
__copyright__ = "Copyright 2012, Julius Seporaitis"
__license__ = "Apache 2.0"
__version__ = "1.0.3"
__all__ = ['requires_api_version', 'plugin_type', 'CONDUIT',
'config_hook', 'prereposetup_hook']
requires_api_version = '2.5'
plugin_type = yum.plugins.TYPE_CORE
CONDUIT = None
def config_hook(conduit):
yum.config.RepoConf.s3_enabled = yum.config.BoolOption(False)
yum.config.RepoConf.key_id = yum.config.Option()
yum.config.RepoConf.secret_key = yum.config.Option()
yum.config.RepoConf.delegated_role = yum.config.Option()
def prereposetup_hook(conduit):
"""Plugin initialization hook. Setup the S3 repositories."""
repos = conduit.getRepos()
for repo in repos.listEnabled():
if isinstance(repo, YumRepository) and repo.s3_enabled:
new_repo = S3Repository(repo.id, repo.baseurl)
new_repo.name = repo.name
# new_repo.baseurl = repo.baseurl
new_repo.mirrorlist = repo.mirrorlist
new_repo.basecachedir = repo.basecachedir
new_repo.gpgcheck = repo.gpgcheck
new_repo.gpgkey = repo.gpgkey
new_repo.key_id = repo.key_id
new_repo.secret_key = repo.secret_key
new_repo.proxy = repo.proxy
new_repo.enablegroups = repo.enablegroups
if hasattr(repo, 'priority'):
new_repo.priority = repo.priority
if hasattr(repo, 'base_persistdir'):
new_repo.base_persistdir = repo.base_persistdir
if hasattr(repo, 'metadata_expire'):
new_repo.metadata_expire = repo.metadata_expire
if hasattr(repo, 'skip_if_unavailable'):
new_repo.skip_if_unavailable = repo.skip_if_unavailable
repos.delete(repo.id)
repos.add(new_repo)
class S3Repository(YumRepository):
"""Repository object for Amazon S3, using IAM Roles."""
def __init__(self, repoid, baseurl):
super(S3Repository, self).__init__(repoid)
self.iamrole = None
self.baseurl = baseurl
self.grabber = None
self.enable()
@property
def grabfunc(self):
raise NotImplementedError("grabfunc called, when it shouldn't be!")
@property
def grab(self):
if not self.grabber:
self.grabber = S3Grabber(self)
if self.key_id and self.secret_key:
self.grabber.set_credentials(self.key_id, self.secret_key)
elif self.delegated_role:
self.grabber.get_instance_region()
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):
"""Initialize file grabber.
Note: currently supports only single repo.baseurl. So in case of a list
only the first item will be used.
"""
if isinstance(repo, basestring):
self.baseurl = repo
else:
if len(repo.baseurl) != 1:
raise yum.plugins.PluginYumExit("s3iam: repository '%s' "
"must have only one "
"'baseurl' value" % repo.id)
else:
self.baseurl = repo.baseurl[0]
# Ensure urljoin doesn't ignore base path:
if not self.baseurl.endswith('/'):
self.baseurl += '/'
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/"
))
response = None
try:
response = urllib2.urlopen(request)
self.iamrole = (response.read())
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))
response = None
try:
response = urllib2.urlopen(request)
data = json.loads(response.read())
finally:
if response:
response.close()
self.access_key = data['AccessKeyId']
self.secret_key = data['SecretAccessKey']
self.token = data['Token']
def set_credentials(self, access_key, secret_key):
self.access_key = access_key
self.secret_key = secret_key
self.token = None
def get_delegated_role_credentials(self, delegated_role):
"""Collect temporary credentials from AWS STS service. Uses
delegated_role value from configuration.
Note: This method should be explicitly called after constructing new
object, as in 'explicit is better than implicit'.
"""
import boto.sts
sts_conn = boto.sts.connect_to_region(self.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
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()
self.region = data[:-1]
def _request(self, path):
url = urlparse.urljoin(self.baseurl, urllib2.quote(path))
request = urllib2.Request(url)
if self.token:
request.add_header('x-amz-security-token', self.token)
signature = self.sign(request)
request.add_header('Authorization', "AWS {0}:{1}".format(
self.access_key,
signature))
return request
def urlgrab(self, url, filename=None, **kwargs):
"""urlgrab(url) copy the file to the local filesystem."""
request = self._request(url)
if filename is None:
filename = request.get_selector()
if filename.startswith('/'):
filename = filename[1:]
response = None
try:
out = open(filename, 'w+')
response = urllib2.urlopen(request)
buff = response.read(8192)
while buff:
out.write(buff)
buff = response.read(8192)
except urllib2.HTTPError, e:
# Wrap exception as URLGrabError so that YumRepository catches it
from urlgrabber.grabber import URLGrabError
new_e = URLGrabError(14, '%s on %s' % (e, url))
new_e.code = e.code
new_e.exception = e
new_e.url = url
raise new_e
finally:
if response:
response.close()
out.close()
return filename
def urlopen(self, url, **kwargs):
"""urlopen(url) open the remote file and return a file object."""
return urllib2.urlopen(self._request(url))
def urlread(self, url, limit=None, **kwargs):
"""urlread(url) return the contents of the file as a string."""
return urllib2.urlopen(self._request(url)).read()
def sign(self, request, timeval=None):
"""Attach a valid S3 signature to request.
request - instance of Request
"""
date = time.strftime("%a, %d %b %Y %H:%M:%S GMT", timeval or time.gmtime())
request.add_header('Date', date)
host = request.get_host()
# TODO: bucket name finding is ugly, I should find a way to support
# both naming conventions: http://bucket.s3.amazonaws.com/ and
# http://s3.amazonaws.com/bucket/
try:
pos = host.find(".s3")
assert pos != -1
bucket = host[:pos]
except AssertionError:
raise yum.plugins.PluginYumExit(
"s3iam: baseurl hostname should be in format: "
"'<bucket>.s3<aws-region>.amazonaws.com'; "
"found '%s'" % host)
resource = "/%s%s" % (bucket, request.get_selector(), )
if self.token:
amz_headers = 'x-amz-security-token:%s\n' % self.token
else:
amz_headers = ''
sigstring = ("%(method)s\n\n\n%(date)s\n"
"%(canon_amzn_headers)s%(canon_amzn_resource)s") % ({
'method': request.get_method(),
'date': request.headers.get('Date'),
'canon_amzn_headers': amz_headers,
'canon_amzn_resource': resource})
digest = hmac.new(
str(self.secret_key),
str(sigstring),
hashlib.sha1).digest()
signature = digest.encode('base64')
return signature.strip()