def calc_signature(self, request, params): logger.debug("Calculating signature using v2 auth.") split = urlsplit(request.url) path = split.path if len(path) == 0: path = '/' string_to_sign = '%s\n%s\n%s\n' % (request.method, split.netloc, path) lhmac = hmac.new(self.credentials.secret_key.encode('utf-8'), digestmod=sha256) pairs = [] for key in sorted(params): # Any previous signature should not be a part of this # one, so we skip that particular key. This prevents # issues during retries. if key == 'Signature': continue value = six.text_type(params[key]) pairs.append(quote(key.encode('utf-8'), safe='') + '=' + quote(value.encode('utf-8'), safe='-_~')) qs = '&'.join(pairs) string_to_sign += qs logger.debug('String to sign: %s', string_to_sign) lhmac.update(string_to_sign.encode('utf-8')) b64 = base64.b64encode(lhmac.digest()).strip().decode('utf-8') return (qs, b64)
def _canonical_query_string_params(self, params): l = [] for param in sorted(params): value = str(params[param]) l.append('%s=%s' % (quote(param, safe='-_.~'), quote(value, safe='-_.~'))) cqs = '&'.join(l) return cqs
def _canonical_query_string_params(self, params): l = [] for param in params: value = str(params[param]) l.append("%s=%s" % (quote(param, safe="-_.~"), quote(value, safe="-_.~"))) l = sorted(l) cqs = "&".join(l) return cqs
def canonical_query_string(self, request): cqs = "" if request.params: params = request.params l = [] for param in params: value = str(params[param]) l.append("%s=%s" % (quote(param, safe="-_.~"), quote(value, safe="-_.~"))) l = sorted(l) cqs = "&".join(l) return cqs
def canonical_query_string(self, request): cqs = '' if request.params: params = request.params l = [] for param in params: value = str(params[param]) l.append('%s=%s' % (quote(param, safe='-_.~'), quote(value, safe='-_.~'))) l = sorted(l) cqs = '&'.join(l) return cqs
def _canonical_query_string_url(self, parts): buf = "" if parts.query: qsa = parts.query.split("&") qsa = [a.split("=", 1) for a in qsa] quoted_qsa = [] for q in qsa: if len(q) == 2: quoted_qsa.append("%s=%s" % (quote(q[0], safe="-_.~"), quote(unquote(q[1]), safe="-_.~"))) elif len(q) == 1: quoted_qsa.append("%s=" % quote(q[0], safe="-_.~")) if len(quoted_qsa) > 0: quoted_qsa.sort(key=itemgetter(0)) buf += "&".join(quoted_qsa) return buf
def build_uri(self, operation, params): logger.debug("Building URI for rest endpoint.") uri = operation.http["uri"] if "?" in uri: path, query_params = uri.split("?") else: path = uri query_params = "" logger.debug("Templated URI path: %s", path) logger.debug("Templated URI query_params: %s", query_params) path_components = [] for pc in path.split("/"): if pc: pc = six.text_type(pc).format(**params["uri_params"]) path_components.append(pc) path = quote("/".join(path_components).encode("utf-8"), safe="/~") query_param_components = [] for qpc in query_params.split("&"): if qpc: if "=" in qpc: key_name, value_name = qpc.split("=") else: key_name = qpc value_name = None if value_name: value_name = value_name.strip("{}") if value_name in params["uri_params"]: value = params["uri_params"][value_name] query_param_components.append("%s=%s" % (key_name, value)) else: query_param_components.append(key_name) query_params = "&".join(query_param_components) logger.debug("Rendered path: %s", path) logger.debug("Rendered query_params: %s", query_params) return path + "?" + query_params
def test_decode_quoted_jsondoc(self): event = self.session.create_event( "after-parsed", "iam", "GetUserPolicy", "policyDocumentType", "PolicyDocument" ) value = quote('{"foo":"bar"}') rv = self.session.emit(event, shape={}, value=value) converted_value = first_non_none_response(rv) self.assertEqual(converted_value, {"foo": "bar"})
def _canonical_query_string_url(self, parts): buf = '' if parts.query: qsa = parts.query.split('&') qsa = [a.split('=', 1) for a in qsa] quoted_qsa = [] for q in qsa: if len(q) == 2: quoted_qsa.append( '%s=%s' % (quote(q[0], safe='-_.~'), quote(unquote(q[1]), safe='-_.~'))) elif len(q) == 1: quoted_qsa.append('%s=' % quote(q[0], safe='-_.~')) if len(quoted_qsa) > 0: quoted_qsa.sort() buf += '&'.join(quoted_qsa) return buf
def calc_signature(self, request, params): logger.debug("Calculating signature using v2 auth.") split = urlsplit(request.url) path = split.path if len(path) == 0: path = "/" string_to_sign = "%s\n%s\n%s\n" % (request.method, split.netloc, path) lhmac = hmac.new(self.credentials.secret_key.encode("utf-8"), digestmod=sha256) pairs = [] for key in sorted(params): value = params[key] pairs.append(quote(key.encode("utf-8"), safe="") + "=" + quote(value.encode("utf-8"), safe="-_~")) qs = "&".join(pairs) string_to_sign += qs logger.debug("String to sign: %s", string_to_sign) lhmac.update(string_to_sign.encode("utf-8")) b64 = base64.b64encode(lhmac.digest()).strip().decode("utf-8") return (qs, b64)
def canonical_query_string(self, request): split = urlsplit(request.url) buf = '' if split.query: qsa = split.query.split('&') qsa = [a.split('=', 1) for a in qsa] quoted_qsa = [] for q in qsa: if len(q) == 2: quoted_qsa.append( '%s=%s' % (quote(q[0], safe='-_.~'), quote(unquote(q[1]), safe='-_.~'))) elif len(q) == 1: quoted_qsa.append('%s=' % quote(q[0], safe='-_.~')) if len(quoted_qsa) > 0: quoted_qsa.sort(key=itemgetter(0)) buf += '&'.join(quoted_qsa) return buf
def test_decode_quoted_jsondoc(self): event = self.session.create_event('after-parsed', 'iam', 'GetUserPolicy', 'policyDocumentType', 'PolicyDocument') value = quote('{"foo":"bar"}') rv = self.session.emit(event, shape={}, value=value) converted_value = first_non_none_response(rv) self.assertEqual(converted_value, {'foo': 'bar'})
def quote_source_header(params, **kwargs): if params['headers'] and 'x-amz-copy-source' in params['headers']: value = params['headers']['x-amz-copy-source'] p = urlsplit(value) # We only want to quote the path. If the user specified # extra parts, say '?versionId=myversionid' then that part # should not be quoted. quoted = quote(p[2].encode('utf-8'), '/~') final_source = urlunsplit((p[0], p[1], quoted, p[3], p[4])) params['headers']['x-amz-copy-source'] = final_source
def calc_signature(self, request, params): logger.debug("Calculating signature using v2 auth.") split = urlsplit(request.url) path = split.path if len(path) == 0: path = '/' string_to_sign = '%s\n%s\n%s\n' % (request.method, split.netloc, path) lhmac = hmac.new(self.credentials.secret_key.encode('utf-8'), digestmod=sha256) pairs = [] for key in sorted(params): value = params[key] pairs.append(quote(key, safe='') + '=' + quote(value, safe='-_~')) qs = '&'.join(pairs) string_to_sign += qs logger.debug('String to sign: %s', string_to_sign) lhmac.update(string_to_sign.encode('utf-8')) b64 = base64.b64encode(lhmac.digest()).strip().decode('utf-8') return (qs, b64)
def copy(self): """ Copies a object in s3 to another location in s3. """ copy_source = quote(self.src.encode('utf-8'), safe='/~') bucket, key = find_bucket_key(self.dest) params = {'endpoint': self.endpoint, 'bucket': bucket, 'copy_source': copy_source, 'key': key} self._handle_object_params(params) response_data, http = operate(self.service, 'CopyObject', params)
def percent_encode(input_str, safe=SAFE_CHARS): """Urlencodes a string. Whereas percent_encode_sequence handles taking a dict/sequence and producing a percent encoded string, this function deals only with taking a string (not a dict/sequence) and percent encoding it. """ if not isinstance(input_str, string_types): input_str = text_type(input_str) return quote(text_type(input_str).encode('utf-8'), safe=safe)
def copy(self): """ Copies a object in s3 to another location in s3. """ copy_source = quote(self.src) bucket, key = find_bucket_key(self.dest) params = {'endpoint': self.endpoint, 'bucket': bucket, 'copy_source': copy_source, 'key': key} if self.parameters['acl']: params['acl'] = self.parameters['acl'][0] response_data, http = operate(self.service, 'CopyObject', params)
def calc_signature(self, request, params): logger.debug("Calculating signature using v2 auth.") split = urlsplit(request.url) path = split.path if len(path) == 0: path = '/' string_to_sign = '%s\n%s\n%s\n' % (request.method, split.netloc, path) lhmac = hmac.new(self.credentials.secret_key.encode('utf-8'), digestmod=sha256) pairs = [] for key in sorted(params): value = six.text_type(params[key]) pairs.append(quote(key.encode('utf-8'), safe='') + '=' + quote(value.encode('utf-8'), safe='-_~')) qs = '&'.join(pairs) string_to_sign += qs logger.debug('String to sign: %s', string_to_sign) lhmac.update(string_to_sign.encode('utf-8')) b64 = base64.b64encode(lhmac.digest()).strip().decode('utf-8') return (qs, b64)
def build_uri(self, operation, params): logger.debug('Building URI for rest endpoint.') uri = operation.http['uri'] if '?' in uri: path, query_params = uri.split('?') else: path = uri query_params = '' logger.debug('Templated URI path: %s', path) logger.debug('Templated URI query_params: %s', query_params) path_components = [] for pc in path.split('/'): if pc: pc = six.text_type(pc).format(**params['uri_params']) path_components.append(pc) path = quote('/'.join(path_components).encode('utf-8'), safe='/~') query_param_components = [] for qpc in query_params.split('&'): if qpc: if '=' in qpc: key_name, value_name = qpc.split('=') else: key_name = qpc value_name = None if value_name: value_name = value_name.strip('{}') if value_name in params['uri_params']: value = params['uri_params'][value_name] if isinstance(value, six.string_types): value = quote(value.encode('utf-8'), safe='/~') query_param_components.append('%s=%s' % ( key_name, value)) else: query_param_components.append(key_name) query_params = '&'.join(query_param_components) logger.debug('Rendered path: %s', path) logger.debug('Rendered query_params: %s', query_params) return path + '?' + query_params
def copy(self): """ Copies a object in s3 to another location in s3. """ copy_source = quote(self.src) bucket, key = find_bucket_key(self.dest) params = { 'endpoint': self.endpoint, 'bucket': bucket, 'copy_source': copy_source, 'key': key } if self.parameters['acl']: params['acl'] = self.parameters['acl'][0] response_data, http = operate(self.service, 'CopyObject', params)
def percent_encode(input_str, safe=SAFE_CHARS): """Urlencodes a string. Whereas percent_encode_sequence handles taking a dict/sequence and producing a percent encoded string, this function deals only with taking a string (not a dict/sequence) and percent encoding it. If given the binary type, will simply URL encode it. If given the text type, will produce the binary type by UTF-8 encoding the text. If given something else, will convert it to the text type first. """ # If its not a binary or text string, make it a text string. if not isinstance(input_str, (six.binary_type, six.text_type)): input_str = six.text_type(input_str) # If it's not bytes, make it bytes by UTF-8 encoding it. if not isinstance(input_str, six.binary_type): input_str = input_str.encode('utf-8') return quote(input_str, safe=safe)
def _normalize_url_path(self, path): normalized_path = quote(normalize_url_path(path), safe='/~') return normalized_path
def test_cant_decode_quoted_jsondoc(self): value = quote('{"foo": "missing end quote}') converted_value = handlers.decode_quoted_jsondoc(value) self.assertEqual(converted_value, value)
def test_decode_quoted_jsondoc(self): value = quote('{"foo":"bar"}') converted_value = handlers.decode_quoted_jsondoc(value) self.assertEqual(converted_value, {'foo': 'bar'})
def quote_source_header(params, **kwargs): if params['headers'] and 'x-amz-copy-source' in params['headers']: value = params['headers']['x-amz-copy-source'] params['headers']['x-amz-copy-source'] = quote(value.encode('utf-8'), '/~')
def quote_source_header(params, **kwargs): if params['headers'] and 'x-amz-copy-source' in params['headers']: value = params['headers']['x-amz-copy-source'] params['headers']['x-amz-copy-source'] = quote( value.encode('utf-8'), '/~')