OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2012 Mitch Garnaat http://garnaat.org/ |
| 2 # Copyright (c) 2012 Amazon.com, Inc. or its affiliates. |
| 3 # All Rights Reserved |
| 4 # |
| 5 # Permission is hereby granted, free of charge, to any person obtaining a |
| 6 # copy of this software and associated documentation files (the |
| 7 # "Software"), to deal in the Software without restriction, including |
| 8 # without limitation the rights to use, copy, modify, merge, publish, dis- |
| 9 # tribute, sublicense, and/or sell copies of the Software, and to permit |
| 10 # persons to whom the Software is furnished to do so, subject to the fol- |
| 11 # lowing conditions: |
| 12 # |
| 13 # The above copyright notice and this permission notice shall be included |
| 14 # in all copies or substantial portions of the Software. |
| 15 # |
| 16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 17 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- |
| 18 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
| 19 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 20 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 22 # IN THE SOFTWARE. |
| 23 # |
| 24 import requests.packages.urllib3 |
| 25 import hmac |
| 26 import base64 |
| 27 from hashlib import sha256 |
| 28 import sys |
| 29 import datetime |
| 30 |
| 31 try: |
| 32 from urllib.parse import quote |
| 33 except ImportError: |
| 34 from urllib import quote |
| 35 |
| 36 |
| 37 class SigV2Auth(object): |
| 38 """ |
| 39 Sign an Query Signature V2 request. |
| 40 """ |
| 41 def __init__(self, credentials, api_version=''): |
| 42 self.credentials = credentials |
| 43 self.api_version = api_version |
| 44 self.hmac = hmac.new(self.credentials.secret_key.encode('utf-8'), |
| 45 digestmod=sha256) |
| 46 |
| 47 def calc_signature(self, args): |
| 48 scheme, host, port = requests.packages.urllib3.get_host(args['url']) |
| 49 string_to_sign = '%s\n%s\n%s\n' % (args['method'], host, '/') |
| 50 hmac = self.hmac.copy() |
| 51 args['params']['SignatureMethod'] = 'HmacSHA256' |
| 52 if self.credentials.token: |
| 53 args['params']['SecurityToken'] = self.credentials.token |
| 54 sorted_params = sorted(args['params']) |
| 55 pairs = [] |
| 56 for key in sorted_params: |
| 57 value = args['params'][key] |
| 58 pairs.append(quote(key, safe='') + '=' + |
| 59 quote(value, safe='-_~')) |
| 60 qs = '&'.join(pairs) |
| 61 string_to_sign += qs |
| 62 print('string_to_sign') |
| 63 print(string_to_sign) |
| 64 hmac.update(string_to_sign.encode('utf-8')) |
| 65 b64 = base64.b64encode(hmac.digest()).strip().decode('utf-8') |
| 66 return (qs, b64) |
| 67 |
| 68 def add_auth(self, args): |
| 69 args['params']['Action'] = 'DescribeInstances' |
| 70 args['params']['AWSAccessKeyId'] = self.credentials.access_key |
| 71 args['params']['SignatureVersion'] = '2' |
| 72 args['params']['Timestamp'] = datetime.datetime.utcnow().isoformat() |
| 73 args['params']['Version'] = self.api_version |
| 74 qs, signature = self.calc_signature(args) |
| 75 args['params']['Signature'] = signature |
| 76 if args['method'] == 'POST': |
| 77 args['data'] = args['params'] |
| 78 args['params'] = {} |
OLD | NEW |