Exemplo n.º 1
0
 def assertEqualUrl(self, url1, url2):
     # We compare long urls by their dictionary parts
     parts1 = self._urlparse(url1)
     parts2 = self._urlparse(url2)
     self.assertEqual(
         parse_qs(parts1.pop('query')), parse_qs(parts2.pop('query')))
     self.assertEqual(parts1, parts2)
Exemplo n.º 2
0
 def test_copy_snapshot_injects_presigned_url(self):
     self.add_copy_snapshot_response(self.snapshot_id)
     with self.http_stubber:
         result = self.client.copy_snapshot(
             SourceRegion='us-west-2',
             SourceSnapshotId=self.snapshot_id,
         )
     self.assertEqual(result['SnapshotId'], self.snapshot_id)
     self.assertEqual(len(self.http_stubber.requests), 1)
     snapshot_request = self.http_stubber.requests[0]
     body = parse_qs(snapshot_request.body)
     self.assertIn('PresignedUrl', body)
     presigned_url = urlparse(body['PresignedUrl'][0])
     self.assertEqual(presigned_url.scheme, 'https')
     self.assertEqual(presigned_url.netloc, 'ec2.us-west-2.amazonaws.com')
     query_args = parse_qs(presigned_url.query)
     self.assertEqual(query_args['Action'], ['CopySnapshot'])
     self.assertEqual(query_args['Version'], ['2016-11-15'])
     self.assertEqual(query_args['SourceRegion'], ['us-west-2'])
     self.assertEqual(query_args['DestinationRegion'], ['us-east-1'])
     self.assertEqual(query_args['SourceSnapshotId'], [self.snapshot_id])
     self.assertEqual(query_args['X-Amz-Algorithm'], ['AWS4-HMAC-SHA256'])
     expected_credential = 'access_key/20110909/us-west-2/ec2/aws4_request'
     self.assertEqual(query_args['X-Amz-Credential'], [expected_credential])
     self.assertEqual(query_args['X-Amz-Date'], ['20110909T233600Z'])
     self.assertEqual(query_args['X-Amz-Expires'], ['3600'])
     self.assertEqual(query_args['X-Amz-SignedHeaders'], ['host'])
     expected_signature = (
         'a94a6b52afdf3daa34c2e2a38a62b72c8dac129c9904c61aa1a5d86e38628537')
     self.assertEqual(query_args['X-Amz-Signature'], [expected_signature])
Exemplo n.º 3
0
 def assertEqualUrl(self, url1, url2):
     # We compare long urls by their dictionary parts
     parts1 = self._urlparse(url1)
     parts2 = self._urlparse(url2)
     self.assertEqual(parse_qs(parts1.pop('query')),
                      parse_qs(parts2.pop('query')))
     self.assertEqual(parts1, parts2)
Exemplo n.º 4
0
def assert_url_equal(url1, url2):
    parts1 = _urlparse(url1)
    parts2 = _urlparse(url2)

    # Because the query string ordering isn't relevant, we have to parse
    # every single part manually and then handle the query string.
    assert parts1.scheme == parts2.scheme
    assert parts1.netloc == parts2.netloc
    assert parts1.path == parts2.path
    assert parts1.params == parts2.params
    assert parts1.fragment == parts2.fragment
    assert parts1.username == parts2.username
    assert parts1.password == parts2.password
    assert parts1.hostname == parts2.hostname
    assert parts1.port == parts2.port
    assert parse_qs(parts1.query) == parse_qs(parts2.query)
    def assert_url_equal(self, url1, url2):
        parts1 = self._urlparse(url1)
        parts2 = self._urlparse(url2)

        # Because the query string ordering isn't relevant, we have to parse
        # every single part manually and then handle the query string.
        self.assertEqual(parts1.scheme, parts2.scheme)
        self.assertEqual(parts1.netloc, parts2.netloc)
        self.assertEqual(parts1.path, parts2.path)
        self.assertEqual(parts1.params, parts2.params)
        self.assertEqual(parts1.fragment, parts2.fragment)
        self.assertEqual(parts1.username, parts2.username)
        self.assertEqual(parts1.password, parts2.password)
        self.assertEqual(parts1.hostname, parts2.hostname)
        self.assertEqual(parts1.port, parts2.port)
        self.assertEqual(parse_qs(parts1.query), parse_qs(parts2.query))
Exemplo n.º 6
0
def assert_url_equal(url1, url2):
    parts1 = _urlparse(url1)
    parts2 = _urlparse(url2)

    # Because the query string ordering isn't relevant, we have to parse
    # every single part manually and then handle the query string.
    assert_equal(parts1.scheme, parts2.scheme)
    assert_equal(parts1.netloc, parts2.netloc)
    assert_equal(parts1.path, parts2.path)
    assert_equal(parts1.params, parts2.params)
    assert_equal(parts1.fragment, parts2.fragment)
    assert_equal(parts1.username, parts2.username)
    assert_equal(parts1.password, parts2.password)
    assert_equal(parts1.hostname, parts2.hostname)
    assert_equal(parts1.port, parts2.port)
    assert_equal(parse_qs(parts1.query), parse_qs(parts2.query))
Exemplo n.º 7
0
    def _modify_request_before_signing(self, request):
        # We automatically set this header, so if it's the auto-set value we
        # want to get rid of it since it doesn't make sense for presigned urls.
        content_type = request.headers.get('content-type')
        blacklisted_content_type = (
            'application/x-www-form-urlencoded; charset=utf-8'
        )
        if content_type == blacklisted_content_type:
            del request.headers['content-type']

        # Note that we're not including X-Amz-Signature.
        # From the docs: "The Canonical Query String must include all the query
        # parameters from the preceding table except for X-Amz-Signature.
        signed_headers = self.signed_headers(self.headers_to_sign(request))

        auth_params = {
            'X-Amz-Algorithm': 'AWS4-HMAC-SHA256',
            'X-Amz-Credential': self.scope(request),
            'X-Amz-Date': request.context['timestamp'],
            'X-Amz-Expires': self._expires,
            'X-Amz-SignedHeaders': signed_headers,
        }
        if self.credentials.token is not None:
            auth_params['X-Amz-Security-Token'] = self.credentials.token
        # Now parse the original query string to a dict, inject our new query
        # params, and serialize back to a query string.
        url_parts = urlsplit(request.url)
        # parse_qs makes each value a list, but in our case we know we won't
        # have repeated keys so we know we have single element lists which we
        # can convert back to scalar values.
        query_dict = dict(
            [(k, v[0]) for k, v in
             parse_qs(url_parts.query, keep_blank_values=True).items()])
        # The spec is particular about this.  It *has* to be:
        # https://<endpoint>?<operation params>&<auth params>
        # You can't mix the two types of params together, i.e just keep doing
        # new_query_params.update(op_params)
        # new_query_params.update(auth_params)
        # percent_encode_sequence(new_query_params)
        operation_params = ''
        if request.data:
            # We also need to move the body params into the query string. To
            # do this, we first have to convert it to a dict.
            query_dict.update(self._get_body_as_dict(request))
            request.data = ''
        if query_dict:
            operation_params = percent_encode_sequence(query_dict) + '&'
        new_query_string = (operation_params +
                            percent_encode_sequence(auth_params))
        # url_parts is a tuple (and therefore immutable) so we need to create
        # a new url_parts with the new query string.
        # <part>   - <index>
        # scheme   - 0
        # netloc   - 1
        # path     - 2
        # query    - 3  <-- we're replacing this.
        # fragment - 4
        p = url_parts
        new_url_parts = (p[0], p[1], p[2], new_query_string, p[4])
        request.url = urlunsplit(new_url_parts)
Exemplo n.º 8
0
    def _modify_request_before_signing(self, request):
        # We automatically set this header, so if it's the auto-set value we
        # want to get rid of it since it doesn't make sense for presigned urls.
        content_type = request.headers.get('content-type')
        blacklisted_content_type = (
            'application/x-www-form-urlencoded; charset=utf-8'
        )
        if content_type == blacklisted_content_type:
            del request.headers['content-type']

        # Note that we're not including X-Amz-Signature.
        # From the docs: "The Canonical Query String must include all the query
        # parameters from the preceding table except for X-Amz-Signature.
        signed_headers = self.signed_headers(self.headers_to_sign(request))

        auth_params = {
            'X-Amz-Algorithm': 'AWS4-HMAC-SHA256',
            'X-Amz-Credential': self.scope(request),
            'X-Amz-Date': request.context['timestamp'],
            'X-Amz-Expires': self._expires,
            'X-Amz-SignedHeaders': signed_headers,
        }
        if self.credentials.token is not None:
            auth_params['X-Amz-Security-Token'] = self.credentials.token
        # Now parse the original query string to a dict, inject our new query
        # params, and serialize back to a query string.
        url_parts = urlsplit(request.url)
        # parse_qs makes each value a list, but in our case we know we won't
        # have repeated keys so we know we have single element lists which we
        # can convert back to scalar values.
        query_dict = dict(
            [(k, v[0]) for k, v in
             parse_qs(url_parts.query, keep_blank_values=True).items()])
        # The spec is particular about this.  It *has* to be:
        # https://<endpoint>?<operation params>&<auth params>
        # You can't mix the two types of params together, i.e just keep doing
        # new_query_params.update(op_params)
        # new_query_params.update(auth_params)
        # percent_encode_sequence(new_query_params)
        operation_params = ''
        if request.data:
            # We also need to move the body params into the query string. To
            # do this, we first have to convert it to a dict.
            query_dict.update(self._get_body_as_dict(request))
            request.data = ''
        if query_dict:
            operation_params = percent_encode_sequence(query_dict) + '&'
        new_query_string = (operation_params +
                            percent_encode_sequence(auth_params))
        # url_parts is a tuple (and therefore immutable) so we need to create
        # a new url_parts with the new query string.
        # <part>   - <index>
        # scheme   - 0
        # netloc   - 1
        # path     - 2
        # query    - 3  <-- we're replacing this.
        # fragment - 4
        p = url_parts
        new_url_parts = (p[0], p[1], p[2], new_query_string, p[4])
        request.url = urlunsplit(new_url_parts)
Exemplo n.º 9
0
 def get_parsed_query_string(self, request):
     query_string_dict = parse_qs(urlsplit(request.url).query)
     # Also, parse_qs sets each value in the dict to be a list, but
     # because we know that we won't have repeated keys, we simplify
     # the dict and convert it back to a single value.
     for key in query_string_dict:
         query_string_dict[key] = query_string_dict[key][0]
     return query_string_dict
Exemplo n.º 10
0
 def get_parsed_query_string(self, request):
     query_string_dict = parse_qs(urlsplit(request.url).query)
     # Also, parse_qs sets each value in the dict to be a list, but
     # because we know that we won't have repeated keys, we simplify
     # the dict and convert it back to a single value.
     for key in query_string_dict:
         query_string_dict[key] = query_string_dict[key][0]
     return query_string_dict
Exemplo n.º 11
0
 def _modify_request_before_signing(self, request):
     # This is our chance to add additional query params we need
     # before we go about calculating the signature.
     request.headers = {}
     request.method = 'GET'
     # Note that we're not including X-Amz-Signature.
     # From the docs: "The Canonical Query String must include all the query
     # parameters from the preceding table except for X-Amz-Signature.
     auth_params = {
         'X-Amz-Algorithm': 'AWS4-HMAC-SHA256',
         'X-Amz-Credential': self.scope(request),
         'X-Amz-Date': request.context['timestamp'],
         'X-Amz-Expires': self._expires,
         'X-Amz-SignedHeaders': 'host',
     }
     if self.credentials.token is not None:
         auth_params['X-Amz-Security-Token'] = self.credentials.token
     # Now parse the original query string to a dict, inject our new query
     # params, and serialize back to a query string.
     url_parts = urlsplit(request.url)
     # parse_qs makes each value a list, but in our case we know we won't
     # have repeated keys so we know we have single element lists which we
     # can convert back to scalar values.
     query_dict = dict([(k, v[0])
                        for k, v in parse_qs(url_parts.query).items()])
     # The spec is particular about this.  It *has* to be:
     # https://<endpoint>?<operation params>&<auth params>
     # You can't mix the two types of params together, i.e just keep doing
     # new_query_params.update(op_params)
     # new_query_params.update(auth_params)
     # percent_encode_sequence(new_query_params)
     operation_params = ''
     if request.data:
         # We also need to move the body params into the query string.
         # request.data will be populated, for example, with query services
         # which normally form encode the params into the body.
         # This means that request.data is a dict() of the operation params.
         query_dict.update(request.data)
         request.data = ''
     if query_dict:
         operation_params = percent_encode_sequence(query_dict) + '&'
     new_query_string = (operation_params +
                         percent_encode_sequence(auth_params))
     # url_parts is a tuple (and therefore immutable) so we need to create
     # a new url_parts with the new query string.
     # <part>   - <index>
     # scheme   - 0
     # netloc   - 1
     # path     - 2
     # query    - 3  <-- we're replacing this.
     # fragment - 4
     p = url_parts
     new_url_parts = (p[0], p[1], p[2], new_query_string, p[4])
     request.url = urlunsplit(new_url_parts)
Exemplo n.º 12
0
 def _modify_request_before_signing(self, request):
     # This is our chance to add additional query params we need
     # before we go about calculating the signature.
     request.headers = {}
     request.method = 'GET'
     # Note that we're not including X-Amz-Signature.
     # From the docs: "The Canonical Query String must include all the query
     # parameters from the preceding table except for X-Amz-Signature.
     auth_params = {
         'X-Amz-Algorithm': 'AWS4-HMAC-SHA256',
         'X-Amz-Credential': self.scope(request),
         'X-Amz-Date': self.timestamp,
         'X-Amz-Expires': self._expires,
         'X-Amz-SignedHeaders': 'host',
     }
     if self.credentials.token is not None:
         auth_params['X-Amz-Security-Token'] = self.credentials.token
     # Now parse the original query string to a dict, inject our new query
     # params, and serialize back to a query string.
     url_parts = urlsplit(request.url)
     # parse_qs makes each value a list, but in our case we know we won't
     # have repeated keys so we know we have single element lists which we
     # can convert back to scalar values.
     query_dict = dict(
         [(k, v[0]) for k, v in parse_qs(url_parts.query).items()])
     # The spec is particular about this.  It *has* to be:
     # https://<endpoint>?<operation params>&<auth params>
     # You can't mix the two types of params together, i.e just keep doing
     # new_query_params.update(op_params)
     # new_query_params.update(auth_params)
     # percent_encode_sequence(new_query_params)
     operation_params = ''
     if request.data:
         # We also need to move the body params into the query string.
         # request.data will be populated, for example, with query services
         # which normally form encode the params into the body.
         # This means that request.data is a dict() of the operation params.
         query_dict.update(request.data)
         request.data = ''
     if query_dict:
         operation_params = percent_encode_sequence(query_dict) + '&'
     new_query_string = (operation_params +
                         percent_encode_sequence(auth_params))
     # url_parts is a tuple (and therefore immutable) so we need to create
     # a new url_parts with the new query string.
     # <part>   - <index>
     # scheme   - 0
     # netloc   - 1
     # path     - 2
     # query    - 3  <-- we're replacing this.
     # fragment - 4
     p = url_parts
     new_url_parts = (p[0], p[1], p[2], new_query_string, p[4])
     request.url = urlunsplit(new_url_parts)
    def _modify_request_before_signing(self, request):
        super()._modify_request_before_signing(request)

        # We automatically set this header, so if it's the auto-set value we
        # want to get rid of it since it doesn't make sense for presigned urls.
        content_type = request.headers.get('content-type')
        if content_type == 'application/x-www-form-urlencoded; charset=utf-8':
            del request.headers['content-type']

        # Now parse the original query string to a dict, inject our new query
        # params, and serialize back to a query string.
        url_parts = urlsplit(request.url)
        # parse_qs makes each value a list, but in our case we know we won't
        # have repeated keys so we know we have single element lists which we
        # can convert back to scalar values.
        query_dict = dict(
            [(k, v[0]) for k, v in
             parse_qs(url_parts.query, keep_blank_values=True).items()])
        if request.params:
            query_dict.update(request.params)
            request.params = {}
        # The spec is particular about this.  It *has* to be:
        # https://<endpoint>?<operation params>&<auth params>
        # You can't mix the two types of params together, i.e just keep doing
        # new_query_params.update(op_params)
        # new_query_params.update(auth_params)
        # percent_encode_sequence(new_query_params)
        if request.data:
            # We also need to move the body params into the query string. To
            # do this, we first have to convert it to a dict.
            query_dict.update(_get_body_as_dict(request))
            request.data = ''
        new_query_string = percent_encode_sequence(query_dict)
        # url_parts is a tuple (and therefore immutable) so we need to create
        # a new url_parts with the new query string.
        # <part>   - <index>
        # scheme   - 0
        # netloc   - 1
        # path     - 2
        # query    - 3  <-- we're replacing this.
        # fragment - 4
        p = url_parts
        new_url_parts = (p[0], p[1], p[2], new_query_string, p[4])
        request.url = urlunsplit(new_url_parts)
Exemplo n.º 14
0
 def assertDesiredUrl(self, url, base, params):
     self.assertEqual(len(url.splitlines()), 1, "Expects only 1 line")
     self.assertTrue(url.startswith(base), "URL mismatch")
     url = url.strip()  # Otherwise the last param contains a trailing CRLF
     self.assertEqual(parse_qs(urlparse(url).query), params)
Exemplo n.º 15
0
 def assertDesiredUrl(self, url, base, params):
     self.assertEqual(len(url.splitlines()), 1, "Expects only 1 line")
     self.assertTrue(url.startswith(base), "URL mismatch")
     url = url.strip()  # Otherwise the last param contains a trailing CRLF
     self.assertEqual(parse_qs(urlparse(url).query), params)