diff options
author | Mitch Garnaat <mitch@garnaat.com> | 2012-03-29 06:25:29 -0700 |
---|---|---|
committer | Mitch Garnaat <mitch@garnaat.com> | 2012-03-29 06:25:29 -0700 |
commit | 07e870487136af00e1d580d281dcd29ef1dd07af (patch) | |
tree | 98dab0826b69bb863172e14493b7a04a70635f94 | |
parent | ac5f6ada5ea399ec0a53b73f051772847fddd350 (diff) | |
download | boto-07e870487136af00e1d580d281dcd29ef1dd07af.tar.gz |
More pylint cleanup.
-rw-r--r-- | boto/auth.py | 71 | ||||
-rw-r--r-- | boto/auth_handler.py | 1 | ||||
-rw-r--r-- | boto/handler.py | 2 | ||||
-rw-r--r-- | boto/plugin.py | 12 |
4 files changed, 49 insertions, 37 deletions
diff --git a/boto/auth.py b/boto/auth.py index 55e45287..e236234a 100644 --- a/boto/auth.py +++ b/boto/auth.py @@ -47,32 +47,45 @@ class HmacKeys(object): if provider.access_key is None or provider.secret_key is None: raise boto.auth_handler.NotReadyToAuthenticate() self.host = host + self.config = config self.update_provider(provider) def update_provider(self, provider): + """ + Update the HMAC's for the authenticator based on new + credentials in the provider. + """ self._provider = provider - sk = self._provider.secret_key.encode('utf-8') - self._hmac = hmac.new(sk, digestmod=compat.sha) + sec_key = self._provider.secret_key.encode('utf-8') + self._hmac = hmac.new(sec_key, digestmod=compat.sha) if compat.sha256: - self._hmac_256 = hmac.new(sk, digestmod=compat.sha256) + self._hmac_256 = hmac.new(sec_key, digestmod=compat.sha256) else: self._hmac_256 = None def algorithm(self): + """ + Determine which HMAC algorithm we are using and return + an appropriate string. + """ if self._hmac_256: return 'HmacSHA256' else: return 'HmacSHA1' def sign_string(self, string_to_sign): + """ + Sign the passed-in string using HMAC and return a base64-encoded + version. + """ if self._hmac_256: - hmac = self._hmac_256.copy() + local_hmac = self._hmac_256.copy() else: - hmac = self._hmac.copy() + local_hmac = self._hmac.copy() if not isinstance(string_to_sign, compat.binary_type): string_to_sign = string_to_sign.encode('utf-8') - hmac.update(string_to_sign) - return base64.b64encode(hmac.digest()).strip().decode('utf-8') + local_hmac.update(string_to_sign) + return base64.b64encode(local_hmac.digest()).strip().decode('utf-8') class AnonAuthHandler(AuthHandler, HmacKeys): @@ -160,9 +173,9 @@ class HmacAuthV3Handler(AuthHandler, HmacKeys): headers['Date'] = formatdate(usegmt=True) b64_hmac = self.sign_string(headers['Date']) - s = "AWS3-HTTPS AWSAccessKeyId=%s," % self._provider.access_key - s += "Algorithm=%s,Signature=%s" % (self.algorithm(), b64_hmac) - headers['X-Amzn-Authorization'] = s + val = "AWS3-HTTPS AWSAccessKeyId=%s," % self._provider.access_key + val += "Algorithm=%s,Signature=%s" % (self.algorithm(), b64_hmac) + headers['X-Amzn-Authorization'] = val class HmacAuthV3HTTPHandler(AuthHandler, HmacKeys): @@ -196,10 +209,10 @@ class HmacAuthV3HTTPHandler(AuthHandler, HmacKeys): case, sorting them in alphabetical order and then joining them into a string, separated by newlines. """ - l = ['%s:%s' % (n.lower().strip(), + head_lst = ['%s:%s' % (n.lower().strip(), headers_to_sign[n].strip()) for n in headers_to_sign] - l.sort() - return '\n'.join(l) + head_lst.sort() + return '\n'.join(head_lst) def string_to_sign(self, http_request): """ @@ -236,11 +249,11 @@ class HmacAuthV3HTTPHandler(AuthHandler, HmacKeys): string_to_sign = string_to_sign.encode('utf-8') hash_value = compat.sha256(string_to_sign).digest() b64_hmac = self.sign_string(hash_value) - s = "AWS3 AWSAccessKeyId=%s," % self._provider.access_key - s += "Algorithm=%s," % self.algorithm() - s += "SignedHeaders=%s," % ';'.join(headers_to_sign) - s += "Signature=%s" % b64_hmac - req.headers['X-Amzn-Authorization'] = s + sig_hdr = "AWS3 AWSAccessKeyId=%s," % self._provider.access_key + sig_hdr += "Algorithm=%s," % self.algorithm() + sig_hdr += "SignedHeaders=%s," % ';'.join(headers_to_sign) + sig_hdr += "Signature=%s" % b64_hmac + req.headers['X-Amzn-Authorization'] = sig_hdr class QuerySignatureHelper(HmacKeys): @@ -284,17 +297,17 @@ class QuerySignatureV0AuthHandler(QuerySignatureHelper, AuthHandler): def _calc_signature(self, params, *args): boto.log.debug('using _calc_signature_0') - hmac = self._hmac.copy() + local_hmac = self._hmac.copy() s = params['Action'] + params['Timestamp'] s = s.encode('utf-8') - hmac.update(s) + local_hmac.update(s) keys = sorted(params, key=str.lower) pairs = [] for key in keys: val = boto.utils.get_utf8_value(params[key]) pairs.append(key + '=' + compat.quote(val)) qs = '&'.join(pairs) - return (qs, base64.b64encode(hmac.digest())) + return (qs, base64.b64encode(local_hmac.digest())) class QuerySignatureV1AuthHandler(QuerySignatureHelper, AuthHandler): @@ -307,18 +320,18 @@ class QuerySignatureV1AuthHandler(QuerySignatureHelper, AuthHandler): def _calc_signature(self, params, *args): boto.log.debug('using _calc_signature_1') - hmac = self._hmac.copy() + local_hmac = self._hmac.copy() keys = sorted(params, key=str.lower) pairs = [] for key in keys: key = key.encode('utf-8') - hmac.update(key) + local_hmac.update(key) val = boto.utils.get_utf8_value(params[key]) val = val.encode('utf-8') - hmac.update(val) + local_hmac.update(val) pairs.append(key + '=' + compat.quote(val)) qs = '&'.join(pairs) - return (qs, base64.b64encode(hmac.digest())) + return (qs, base64.b64encode(local_hmac.digest())) class QuerySignatureV2AuthHandler(QuerySignatureHelper, AuthHandler): @@ -332,10 +345,10 @@ class QuerySignatureV2AuthHandler(QuerySignatureHelper, AuthHandler): boto.log.debug('using _calc_signature_2') string_to_sign = '%s\n%s\n%s\n' % (verb, server_name.lower(), path) if self._hmac_256: - hmac = self._hmac_256.copy() + local_hmac = self._hmac_256.copy() params['SignatureMethod'] = 'HmacSHA256' else: - hmac = self._hmac.copy() + local_hmac = self._hmac.copy() params['SignatureMethod'] = 'HmacSHA1' if self._provider.security_token: params['SecurityToken'] = self._provider.security_token @@ -350,8 +363,8 @@ class QuerySignatureV2AuthHandler(QuerySignatureHelper, AuthHandler): string_to_sign += qs boto.log.debug('string_to_sign: %s' % string_to_sign) string_to_sign = string_to_sign.encode('utf-8') - hmac.update(string_to_sign) - b64 = base64.b64encode(hmac.digest()) + local_hmac.update(string_to_sign) + b64 = base64.b64encode(local_hmac.digest()) boto.log.debug('len(b64)=%d' % len(b64)) boto.log.debug('base64 encoded digest: %s' % b64) return (qs, b64) diff --git a/boto/auth_handler.py b/boto/auth_handler.py index 6dd16256..c42be8ad 100644 --- a/boto/auth_handler.py +++ b/boto/auth_handler.py @@ -52,7 +52,6 @@ class AuthHandler(Plugin): NotReadyToAuthenticate: if this handler is not willing to authenticate for the given provider and config. """ - pass def add_auth(self, http_request): """ diff --git a/boto/handler.py b/boto/handler.py index 6cfa78ec..b44b2d94 100644 --- a/boto/handler.py +++ b/boto/handler.py @@ -32,7 +32,7 @@ class XmlHandler(xml.sax.ContentHandler): def startElement(self, name, attrs): self.current_text = '' new_node = self.nodes[-1][1].startElement(name, attrs, self.connection) - if new_node != None: + if new_node is not None: self.nodes.append((name, new_node)) def endElement(self, name): diff --git a/boto/plugin.py b/boto/plugin.py index 54bba490..906a66b6 100644 --- a/boto/plugin.py +++ b/boto/plugin.py @@ -70,12 +70,12 @@ def _import_module(filename): (path, name) = os.path.split(filename) (name, ext) = os.path.splitext(name) - (file, filename, data) = imp.find_module(name, [path]) + (fp, filename, data) = imp.find_module(name, [path]) try: - return imp.load_module(name, file, filename, data) + return imp.load_module(name, fp, filename, data) finally: - if file: - file.close() + if fp: + fp.close() _plugin_loaded = False @@ -89,5 +89,5 @@ def load_plugins(config): if not config.has_option('Plugin', 'plugin_directory'): return directory = config.get('Plugin', 'plugin_directory') - for file in glob.glob(os.path.join(directory, '*.py')): - _import_module(file) + for file_name in glob.glob(os.path.join(directory, '*.py')): + _import_module(file_name) |