forked from MayaraCloud/apt-transport-s3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths3
executable file
·643 lines (541 loc) · 22.4 KB
/
s3
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
#!/usr/bin/python3 -u
# Copyright (C) 2018- Mayara Cloud Ltd.
# Copyright (C) 2016-2018 Claranet Ltd.
# Copyright (C) 2014-2016 Bashton Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# https://github.com/MayaraCloud/apt-transport-s3
import urllib.request
import urllib.error
import urllib.parse
import time
import hashlib
import hmac
import json
import sys
import os
import datetime
import xml.etree.ElementTree as ET
import socket
import ssl
from configobj import ConfigObj
RETRIES = 5
SPECIAL_REGION_ENDPOINTS = {
'cn-north-1': 's3.cn-north-1.amazonaws.com.cn',
'cn-northwest-1': 's3.cn-northwest-1.amazonaws.com.cn'
}
TOKEN_TTL_SECONDS = 21600
TOKEN_HEADER = "X-aws-ec2-metadata-token"
TOKEN_HEADER_TTL = "X-aws-ec2-metadata-token-ttl-seconds"
def wait_time(c):
return pow(2, c) - 1
class AWSCredentials(object):
"""
Class for dealing with IAM role credentials from meta-data server and later
on to deal with boto/aws config provided keys
"""
def __init__(self, config_file=None):
self.conf_file = config_file
host = 'http://169.254.169.254'
path_session_token = 'latest/api/token'
path_sec_cred = '/latest/meta-data/iam/security-credentials/'
path_instance_metadata = '/latest/dynamic/instance-identity/document'
self.session_token_metadata = urllib.parse.urljoin(host, path_session_token)
self.credentials_metadata = urllib.parse.urljoin(host, path_sec_cred)
self.instance_metadata = urllib.parse.urljoin(
host, path_instance_metadata)
self.session_token = None
def __imdsv2_ensure_token(self):
# Get IMDSv2 session token
request = urllib.request.Request(self.session_token_metadata, method='PUT', headers={TOKEN_HEADER_TTL: TOKEN_TTL_SECONDS})
response = None
for i in range(0, RETRIES):
try:
response = urllib.request.urlopen(request, None, 10)
self.session_token = response.read()
break
except ssl.SSLError as e:
if 'IMDSv2 timed out' in e.message:
time.sleep(wait_time(i + 1))
else:
raise e
except socket.timeout:
time.sleep(wait_time(i + 1))
except urllib.error.URLError as e:
if hasattr(e, 'reason'):
raise Exception("IMDSv2 URL error reason: {}, probable cause is \
that you don't have IAM role on this machine".format(e.reason))
elif hasattr(e, 'code'):
raise Exception("Server error code: {}".format(e.code))
finally:
if response:
response.close()
else:
raise Exception("IMDSv2 session token request timed out")
def __get_role(self):
if not self.session_token:
self.__imdsv2_ensure_token()
# Read IAM role from AWS metadata store
request = urllib.request.Request(self.credentials_metadata, headers={TOKEN_HEADER: self.session_token})
response = None
for i in range(0, RETRIES):
try:
response = urllib.request.urlopen(request, None, 10)
self.iamrole = str(response.read(), "utf-8")
break
except ssl.SSLError as e:
if 'timed out' in e.message:
time.sleep(wait_time(i + 1))
else:
raise e
except socket.timeout:
time.sleep(wait_time(i + 1))
except urllib.error.URLError as e:
if hasattr(e, 'reason'):
raise Exception("URL error reason: {}, probable cause is \
that you don't have IAM role on this machine".format(e.reason))
elif hasattr(e, 'code'):
raise Exception("Server error code: {}".format(e.code))
finally:
if response:
response.close()
else:
raise Exception("GetRole request timed out")
def __load_config(self):
"""
Loading config file from predefined location.
Example config file content:
AccessKeyId = mykey
SecretAccessKey = mysecretkey
Region = eu-west-2 # this is obligatory if instance is in different
region then apt repo
"""
# Checking if 'file' exists, if it does read it
if os.path.isfile(os.path.expanduser(self.conf_file)):
config = ConfigObj(os.path.expanduser(self.conf_file))
return config
else:
raise Exception("Config file: {} doesn't exist".format(self.conf_file))
def __request_json(self, url):
if not self.session_token:
self.__imdsv2_ensure_token()
request = urllib.request.Request(url, headers={TOKEN_HEADER: self.session_token})
response = None
for i in range(0, RETRIES):
try:
response = urllib.request.urlopen(request, None, 30)
return json.loads(response.read())
except ssl.SSLError as e:
if 'timed out' in e.message:
time.sleep(wait_time(i + 1))
else:
raise e
except socket.timeout:
time.sleep(wait_time(i + 1))
except urllib.error.URLError as e:
if hasattr(e, 'reason'):
raise Exception("URL error reason: {}".format(e.reason))
elif hasattr(e, 'code'):
raise Exception("Server error code: {}".format(e.code))
finally:
if response:
response.close()
raise Exception("request for {} timed out".format(url))
def __get_region(self, config):
# try to load region from config file
region = config.get('Region')
# if region is not defied load it from instance metadata
if region is None or region == '':
region = self.__request_json(self.instance_metadata)['region']
return region
def __get_endpoint(self, config):
""" We assume that if endpoint attribute exists in the config this
takes priority over Region and bucket URL construction. If it doesn't
exist checking Region and comparing it to the dict containing special
regions (slightly different fqdn's). If it's there use it, if not
construct host for bucket url using Region from the config or from the
instance metadata (if bucket and host are in the same region it will
work, if not error will be thrown)
"""
if config.get('Endpoint') is not None:
host = config['Endpoint']
else:
if self.region in list(SPECIAL_REGION_ENDPOINTS.keys()):
host = SPECIAL_REGION_ENDPOINTS[self.region]
else:
host = 's3.{}.amazonaws.com'.format(self.region)
return host
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'.
"""
data = {}
try:
data = self.__load_config()
except:
pass
self.region = self.__get_region(data)
self.host = self.__get_endpoint(data)
# Setting up AWS creds based on environment variables if env vars
# doesn't exist setting var value to None
if data.get("AccessKeyId") is None:
data['AccessKeyId'] = os.environ.get("AWS_ACCESS_KEY_ID", None)
data['SecretAccessKey'] = os.environ.get(
"AWS_SECRET_ACCESS_KEY", None)
data['Token'] = os.environ.get("AWS_SESSION_TOKEN", None)
if data.get("AccessKeyId") is None:
self.__get_role()
data = self.__request_json(urllib.parse.urljoin(self.credentials_metadata,
self.iamrole))
self.access_key = data['AccessKeyId']
if self.access_key is None or self.access_key == '':
raise Exception("AccessKeyId required")
self.secret_key = data['SecretAccessKey']
if self.secret_key is None or self.secret_key == '':
raise Exception("SecretAccessKey required")
self.token = data.get('Token')
def v4Sign(self, key, msg):
return hmac.new(key, msg.encode('utf-8'), hashlib.sha256).digest()
def getSignatureKey(self, dateStamp, serviceName):
kDate = self.v4Sign(
('AWS4' + self.secret_key).encode('utf-8'), dateStamp)
kRegion = self.v4Sign(kDate, self.region)
kService = self.v4Sign(kRegion, serviceName)
kSigning = self.v4Sign(kService, 'aws4_request')
return kSigning
def uriopen(self, uri):
"""uriopen(uri) open the remote file and return a file object."""
try:
return urllib.request.urlopen(self._request(uri), None, 30)
except urllib.error.HTTPError as e:
# HTTPError is a "file like object" similar to what
# urllib.request.urlopen returns, so return it and let caller
# deal with the error code
if e.code == 400:
# token errors are buried in 400 messages so expose
xmlResponse = ET.fromstring(e.read())
if xmlResponse is not None:
e.msg = "{} - {}".format(e,
xmlResponse.find("Message").text)
if e.code == 301:
e.msg = "{} - Set s3auth.conf region to match bucket 'Region':\
bucket may not be in {}".format(e, self.region)
return e
# For other errors, throw an exception directly
except urllib.error.URLError as e:
if hasattr(e, 'reason'):
raise Exception("URL error reason: gc" % e.reason)
elif hasattr(e, 'code'):
raise Exception("Server error code: gc" % e.code)
except socket.timeout:
raise Exception("Socket timeout")
def _request(self, uri):
uri_parsed = urllib.parse.urlparse(uri)
# Below if statement is to accommodate AWS guidance in regards buckets
# naming convention presented in
# https://docs.aws.amazon.com/AmazonS3/latest/dev/BucketRestrictions.html
# S3 bucket names for apt repo can NOT contain '.' in the name
# otherwise TLS certs are broken
if '.' in uri_parsed.netloc:
raise Exception("uri should not include fully qualified domain \
name for bucket")
scheme = 'https'
bucket = uri_parsed.netloc
host = '{}.{}'.format(bucket, self.host)
path = '{}'.format(urllib.parse.quote(uri_parsed.path, safe='-._~/'))
s3url = urllib.parse.urlunparse(
(
scheme,
host,
path,
'',
'',
''
)
)
request = urllib.request.Request(s3url)
request.add_header('x-amz-content-sha256', self._payload_hash(request))
# Create a date for headers and the credential string
amzdate = datetime.datetime.utcnow().strftime('%Y%m%dT%H%M%SZ')
request.add_header('x-amz-date', amzdate)
if self.token is not None and self.token != '':
request.add_header('x-amz-security-token', self.token)
canonical_request = self._canonical_request(request, host, amzdate)
authorization_header = self._authorization_header(
canonical_request, amzdate)
request.add_header('Authorization', authorization_header)
return request
def _authorization_header(self, canonical_request, amzdate):
"""
AWS documentation for signing requests with v4Sign:
https://docs.aws.amazon.com/general/latest/gr/sigv4_signing.html
"""
datestamp = amzdate.split('T')[0]
algorithm = 'AWS4-HMAC-SHA256'
credential_scope = datestamp + '/' + self.region + '/s3/aws4_request'
string_to_sign = algorithm + '\n' \
+ amzdate + '\n' \
+ credential_scope + '\n' \
+ canonical_request
signing_key = self.getSignatureKey(datestamp, 's3')
signature = hmac.new(signing_key, string_to_sign.encode(
'utf-8'), hashlib.sha256).hexdigest()
authorization_header = "{} Credential={}/{}, SignedHeaders={}, Signature={}".format(
algorithm,
self.access_key,
credential_scope,
self._signed_headers(),
signature
)
return authorization_header
def _canonical_request(self, request, host, amzdate):
canonical_uri = request.selector
canonical_querystring = ''
canonical_headers = 'host:' + host + '\n' \
+ 'x-amz-content-sha256:' \
+ self._payload_hash(request) + '\n' \
+ 'x-amz-date:' + amzdate + '\n'
if self.token is not None and self.token != '':
canonical_headers += 'x-amz-security-token:' + self.token + '\n'
canonical_request = request.get_method() + '\n' \
+ canonical_uri + '\n' \
+ canonical_querystring + '\n' \
+ canonical_headers + '\n' \
+ self._signed_headers() + '\n' \
+ self._payload_hash(request)
return hashlib.sha256(canonical_request.encode('utf-8')).hexdigest()
def _signed_headers(self):
signed_headers = 'host;x-amz-content-sha256;x-amz-date'
if self.token is not None and self.token != '':
signed_headers += ';x-amz-security-token'
return signed_headers
def _payload_hash(self, request):
payload = request.data
if payload is None:
payload = ''.encode('utf-8')
return hashlib.sha256(payload).hexdigest()
class APTMessage(object):
MESSAGE_CODES = {
100: 'Capabilities',
102: 'Status',
200: 'URI Start',
201: 'URI Done',
400: 'URI Failure',
600: 'URI Acquire',
601: 'Configuration',
}
def __init__(self, code, headers):
self.code = code
self.headers = headers
def encode(self):
result = '{0} {1}\n'.format(self.code, self.MESSAGE_CODES[self.code])
for item in list(self.headers.keys()):
if self.headers[item] is not None:
result += '{0}: {1}\n'.format(item, self.headers[item])
return result + '\n'
class S3_method(object):
__eof = False
def __init__(self, config_file='/etc/apt/s3auth.conf'):
self.iam = AWSCredentials(config_file)
self.iam.get_credentials()
self.send_capabilities()
def fail(self, message='Failed'):
self.send_uri_failure({'URI': self.uri, 'Message': message})
def _read_message(self):
"""
Apt uses for communication with its methods the text protocol similar
to http. This function parses the protocol messages from stdin.
"""
if self.__eof:
return None
result = {}
line = sys.stdin.readline()
while line == '\n':
line = sys.stdin.readline()
if not line:
self.__eof = True
return None
s = line.split(" ", 1)
result['_number'] = int(s[0])
result['_text'] = s[1].strip()
while not self.__eof:
line = sys.stdin.readline()
if not line:
self.__eof = True
return result
if line == '\n':
return result
(item, value) = line.split(":", 1)
if not result.get(item):
result[item] = []
result[item].append(value.strip())
return result
def send(self, code, headers):
message = APTMessage(code, headers)
sys.stdout.write(message.encode())
def send_capabilities(self):
self.send(100, {
'Version': '1.1',
'Single-Instance': 'true',
'Send-Config': 'true'})
def send_status(self, headers):
self.send(102, headers)
def send_uri_start(self, headers):
self.send(200, headers)
def send_uri_done(self, headers):
self.send(201, headers)
def send_uri_failure(self, headers):
self.send(400, headers)
def run(self):
"""Loop through requests on stdin"""
while True:
message = self._read_message()
if message is None:
return 0
if message['_number'] == 601:
try:
self.configure(message)
except Exception as e:
self.fail(e.__class__.__name__ + ": " + str(e))
elif message['_number'] == 600:
try:
self.fetch(message)
except Exception as e:
self.fail(e.__class__.__name__ + ": " + str(e))
else:
self.fail(str(message['_text']))
def configure(self, message):
"""
Reads APT configuration (dict) and checks for proxy settings by reading
all available options with in 'Config-Item' and lookinup
'Acquire::http::Proxy'
"""
for item in message['Config-Item']:
if item.startswith('Acquire::http::Proxy'):
(key, value) = item.split('=', 1)
if key == 'Acquire::http::Proxy':
os.environ['http_proxy'] = value
os.environ['https_proxy'] = value
def format_error_response(self, response):
"""Extracts Info from AWS Error Repsonse and returns a formatted string
When the S3 API returns an error the body of the response also contains
information about the error. This function reads the Response object
for the body and returns a string of the form "[{Code}] {Message}"
will return an empty string when the response can not be parsed
https://docs.aws.amazon.com/AmazonS3/latest/API/ErrorResponses.html#RESTErrorResponses
"""
try:
error = ET.fromstring(response.read())
except xml.etree.ElementTree.ParseError:
return ''
code = error.find('Code')
message = error.find('Message')
if code is not None and message is not None:
return '[{}] {}'.format(code.text, message.text)
# fallback to returning an empty string
return ''
def fetch(self, msg):
self.uri = msg['URI'][0]
self.filename = msg['Filename'][0]
self.send_status({'URI': self.uri, 'Message': 'Waiting for headers'})
for i in range(0, RETRIES):
try:
response = self.iam.uriopen(self.uri)
except ssl.SSLError as e:
if 'timed out' in e.message:
time.sleep(wait_time(i + 1))
continue
else:
raise e
except socket.timeout:
time.sleep(wait_time(i + 1))
continue
self.send_status({
'URI': self.uri,
'Message': 'Waiting for headers'})
if response.code != 200:
error_detail = self.format_error_response(response)
self.send_uri_failure({
'URI': self.uri,
'Message': '{} {} {}'.format(response.code,
response.msg,
error_detail),
'FailReason': 'HttpError' + str(response.code)})
try:
while True:
data = response.read(4096)
if not len(data):
break
except ssl.SSLError as e:
if 'timed out' in e.message:
pass
else:
raise e
except socket.timeout:
pass
finally:
response.close()
return
self.send_uri_start({
'URI': self.uri,
'Size': response.headers.get('content-length'),
'Last-Modified': response.headers.get('last-modified')})
f = open(self.filename, "wb")
hash_sha256 = hashlib.sha256()
hash_sha512 = hashlib.sha512()
hash_md5 = hashlib.md5()
try:
while True:
data = response.read(4096)
if not len(data):
break
hash_sha256.update(data)
hash_sha512.update(data)
hash_md5.update(data)
f.write(data)
break
except ssl.SSLError as e:
if 'timed out' in e.message:
time.sleep(wait_time(i + 1))
else:
raise e
except socket.timeout:
time.sleep(wait_time(i + 1))
finally:
response.close()
f.close()
else:
raise Exception("Fetch request timed out")
self.send_uri_done({
'URI': self.uri,
'Filename': self.filename,
'Size': response.headers.get('content-length'),
'Last-Modified': response.headers.get('last-modified'),
'MD5-Hash': hash_md5.hexdigest(),
'MD5Sum-Hash': hash_md5.hexdigest(),
'SHA256-Hash': hash_sha256.hexdigest(),
'SHA512-Hash': hash_sha512.hexdigest()})
if __name__ == '__main__':
try:
config = '/etc/apt/s3auth.conf'
if len(sys.argv) == 2 and os.path.isfile(sys.argv[1]):
config = sys.argv[1]
method = S3_method(config)
ret = method.run()
sys.exit(ret)
except KeyboardInterrupt:
pass