OLD | NEW |
(Empty) | |
| 1 import os |
| 2 import urlparse |
| 3 import boto |
| 4 import boto.connection |
| 5 import boto.jsonresponse |
| 6 import boto.exception |
| 7 import awsqueryrequest |
| 8 |
| 9 class NoCredentialsError(boto.exception.BotoClientError): |
| 10 |
| 11 def __init__(self): |
| 12 s = 'Unable to find credentials' |
| 13 boto.exception.BotoClientError.__init__(self, s) |
| 14 |
| 15 class AWSQueryService(boto.connection.AWSQueryConnection): |
| 16 |
| 17 Name = '' |
| 18 Description = '' |
| 19 APIVersion = '' |
| 20 Authentication = 'sign-v2' |
| 21 Path = '/' |
| 22 Port = 443 |
| 23 Provider = 'aws' |
| 24 EnvURL = 'AWS_URL' |
| 25 |
| 26 Regions = [] |
| 27 |
| 28 def __init__(self, **args): |
| 29 self.args = args |
| 30 self.check_for_credential_file() |
| 31 self.check_for_env_url() |
| 32 if 'host' not in self.args: |
| 33 if self.Regions: |
| 34 region_name = self.args.get('region_name', |
| 35 self.Regions[0]['name']) |
| 36 for region in self.Regions: |
| 37 if region['name'] == region_name: |
| 38 self.args['host'] = region['endpoint'] |
| 39 if 'path' not in self.args: |
| 40 self.args['path'] = self.Path |
| 41 if 'port' not in self.args: |
| 42 self.args['port'] = self.Port |
| 43 try: |
| 44 boto.connection.AWSQueryConnection.__init__(self, **self.args) |
| 45 self.aws_response = None |
| 46 except boto.exception.NoAuthHandlerFound: |
| 47 raise NoCredentialsError() |
| 48 |
| 49 def check_for_credential_file(self): |
| 50 """ |
| 51 Checks for the existance of an AWS credential file. |
| 52 If the environment variable AWS_CREDENTIAL_FILE is |
| 53 set and points to a file, that file will be read and |
| 54 will be searched credentials. |
| 55 Note that if credentials have been explicitelypassed |
| 56 into the class constructor, those values always take |
| 57 precedence. |
| 58 """ |
| 59 if 'AWS_CREDENTIAL_FILE' in os.environ: |
| 60 path = os.environ['AWS_CREDENTIAL_FILE'] |
| 61 path = os.path.expanduser(path) |
| 62 path = os.path.expandvars(path) |
| 63 if os.path.isfile(path): |
| 64 fp = open(path) |
| 65 lines = fp.readlines() |
| 66 fp.close() |
| 67 for line in lines: |
| 68 if line[0] != '#': |
| 69 if '=' in line: |
| 70 name, value = line.split('=', 1) |
| 71 if name.strip() == 'AWSAccessKeyId': |
| 72 if 'aws_access_key_id' not in self.args: |
| 73 value = value.strip() |
| 74 self.args['aws_access_key_id'] = value |
| 75 elif name.strip() == 'AWSSecretKey': |
| 76 if 'aws_secret_access_key' not in self.args: |
| 77 value = value.strip() |
| 78 self.args['aws_secret_access_key'] = value |
| 79 else: |
| 80 print 'Warning: unable to read AWS_CREDENTIAL_FILE' |
| 81 |
| 82 def check_for_env_url(self): |
| 83 """ |
| 84 First checks to see if a url argument was explicitly passed |
| 85 in. If so, that will be used. If not, it checks for the |
| 86 existence of the environment variable specified in ENV_URL. |
| 87 If this is set, it should contain a fully qualified URL to the |
| 88 service you want to use. |
| 89 Note that any values passed explicitly to the class constructor |
| 90 will take precedence. |
| 91 """ |
| 92 url = self.args.get('url', None) |
| 93 if url: |
| 94 del self.args['url'] |
| 95 if not url and self.EnvURL in os.environ: |
| 96 url = os.environ[self.EnvURL] |
| 97 if url: |
| 98 rslt = urlparse.urlparse(url) |
| 99 if 'is_secure' not in self.args: |
| 100 if rslt.scheme == 'https': |
| 101 self.args['is_secure'] = True |
| 102 else: |
| 103 self.args['is_secure'] = False |
| 104 |
| 105 host = rslt.netloc |
| 106 port = None |
| 107 l = host.split(':') |
| 108 if len(l) > 1: |
| 109 host = l[0] |
| 110 port = int(l[1]) |
| 111 if 'host' not in self.args: |
| 112 self.args['host'] = host |
| 113 if port and 'port' not in self.args: |
| 114 self.args['port'] = port |
| 115 |
| 116 if rslt.path and 'path' not in self.args: |
| 117 self.args['path'] = rslt.path |
| 118 |
| 119 def _required_auth_capability(self): |
| 120 return [self.Authentication] |
| 121 |
OLD | NEW |