Example #1
0
    def test_three_max_limits(self):
        add_max_limit("foo", uri_hash_func, "/foo", 4)
        add_max_limit("bar", uri_hash_func, "/bar", 3)
        add_max_limit("post", method_hash_func, "POST", 2)

        set_counter('foo', 3)
        message = HTTPServerRequest("GET", "/foo")
        conditions = Limits.conditions(message)
        flag, counters = conditional_incr_counters(conditions)
        self.assertTrue(flag)
        self.assertEqual(counters, ["foo"])
        set_counter('foo', 3)

        set_counter('post', 1)
        message = HTTPServerRequest("POST", "/foo")
        conditions = Limits.conditions(message)
        flag, counters = conditional_incr_counters(conditions)
        self.assertTrue(flag)
        assertCountEqual(self, counters, ["foo", "post"])
        set_counter('foo', 3)
        set_counter('post', 1)

        set_counter('bar', 4)
        message = HTTPServerRequest("GET", "/bar")
        conditions = Limits.conditions(message)
        flag, counters = conditional_incr_counters(conditions)
        self.assertFalse(flag)

        del_counter('foo')
        del_counter('post')
        del_counter('bar')
    def test_400_parse_error(self):
        proxy = mock.Mock()
        plugin = JSONRPC(proxy, {})

        connection = mock.Mock(spec=[])
        connection.write_headers = mock.MagicMock()
        connection.finish = mock.MagicMock()
        request = HTTPServerRequest(method='PUT',
                                    uri='/',
                                    version='HTTP/1.1',
                                    headers={
                                        'X-Cocaine-JSON-RPC': 'Enable',
                                    },
                                    connection=connection,
                                    body='{',
                                    host='localhost')
        request.logger = NULLLOGGER

        yield plugin.process(request)

        connection.write_headers.assert_called_with(
            ResponseStartLine(version='HTTP/1.1',
                              code=400,
                              reason='Bad JSON-RPC request'), mock.ANY,
            '{"message": "Parse error: Invalid JSON was received by the server.", "code": -32700}'
        )
def tornado_request_handler(request, data):
    unpacked_data = msgpack_unpackb(data)
    method, uri, version, headers, body = unpacked_data
    version = format_http_version(version)
    tornado_request = HTTPServerRequest(method, uri, version, HTTPHeaders(headers), body)
    tornado_request.hpack_headers = lambda: request.headers
    return tornado_request
Example #4
0
 def test_mds_process(self):
     mdsplugin = MDSExec(
         CocaineProxy(),
         {"srw_host": "http://localhost:%d" % self.get_http_port()})
     conn = _FakeConnection()
     req = HTTPServerRequest(
         method="PUT",
         uri="/blabla",
         version="HTTP/1.1",
         headers={
             "X-Cocaine-Service": "application",
             "X-Cocaine-Event": "event",
             "X-Srw-Key": "320.namespace:301123837.E150591:1046883323",
             "X-Srw-Namespace": "namespace",
             "X-Srw-Key-Type": "mds",
             "Authorization": "Basic aaabbb",
         },
         connection=conn,
         body="body",
         host="localhost")
     req.logger = NULLLOGGER
     yield mdsplugin.process(req)
     self.assertEqual(conn.start_line.code, 202)
     self.assertEqual(conn.start_line.version, "HTTP/1.1")
     self.assertEqual(len(conn.chunks), 1)
     self.assertEqual(''.join(conn.chunks), "CHUNK1CHUNK2CHUNK3")
     self.assertEqual(conn.headers["A"], "B")
    def test_400_invalid_request_error(self):
        proxy = mock.Mock()
        plugin = JSONRPC(proxy, {})

        connection = mock.Mock(spec=[])
        connection.write_headers = mock.MagicMock()
        connection.finish = mock.MagicMock()
        request = HTTPServerRequest(
            method='PUT',
            uri='/',
            version='HTTP/1.1',
            headers={
                'X-Cocaine-JSON-RPC': 'Enable',
            },
            connection=connection,
            body='{"jsonrpc": 2.0, "method": "method", "params_": [], "id": 1}',
            host='localhost')
        request.logger = NULLLOGGER

        yield plugin.process(request)

        connection.write_headers.assert_called_with(
            ResponseStartLine(version='HTTP/1.1',
                              code=400,
                              reason='Bad JSON-RPC request'), mock.ANY,
            '{"message": "The JSON sent is not a valid Request object.", "code": -32600}'
        )
Example #6
0
 def test_serialize_query_string(self):
     uri = "/foo/bar"
     req = HTTPServerRequest(method='GET', uri=uri)
     utf = u"éééééé"
     bs = utf.encode('utf-8')
     req.query_arguments = {
         "foo1": [b"bar1", b"bar2"],
         "foo2": [bs],
         "foo3": [b"bar3"]
     }
     msg = serialize_http_request(req)
     (hreq, body_link, extra_dict) = \
         unserialize_request_message(msg)
     o = urlparse(hreq.url)
     if six.PY3:
         parsed = sorted(parse_qsl(o.query), key=lambda x: x[0])
     else:
         parsed = sorted(parse_qsl(o.query.encode('ASCII')))
     self.assertEquals(parsed[0][0], 'foo1')
     self.assertEquals(parsed[0][1], 'bar1')
     self.assertEquals(parsed[1][0], 'foo1')
     self.assertEquals(parsed[1][1], 'bar2')
     self.assertEquals(parsed[2][0], 'foo2')
     if six.PY3:
         self.assertEquals(parsed[2][1], utf)
     else:
         self.assertEquals(parsed[2][1], bs)
     self.assertEquals(parsed[3][0], 'foo3')
     self.assertEquals(parsed[3][1], 'bar3')
Example #7
0
    def test_remote_ip_does_not_match_function(self):
        def criterion_function(arg):
            return False

        request = HTTPServerRequest(uri='/')
        request.remote_ip = '10.0.0.1'
        criteria = Criteria(remote_ip=criterion_function)
        result = criteria.match(HTTPExchange(request))
        self.assertFalse(result)
Example #8
0
    def get_handler(self, protocol=None, host=None, prefix=None):
        req = HTTPServerRequest(method='GET',
                                uri='/',
                                headers=HTTPHeaders(
                                    {'X-Path-Prefix': prefix or self.prefix}))
        req.protocol = protocol or self.protocol
        req.host = host or self.host
        req.connection = self.conn

        return BaseRequestHandler(self.app, req)
def http_request():
    mocked_http_request = HTTPServerRequest(
        uri='/v1/resources/test/providers/test/path/mock',
        method='GET'
    )
    mocked_http_request.headers['User-Agent'] = 'test'
    mocked_http_request.connection = HTTP1ConnectionParameters()
    mocked_http_request.connection.set_close_callback = mock.Mock()
    mocked_http_request.request_time = mock.Mock(return_value=10)
    mocked_http_request.body = MockRequestBody()
    return mocked_http_request
Example #10
0
 def normalize_request(self, request: HTTPServerRequest) -> HTTPServerRequest:
     path, sep, query = request.uri.partition('?') # type: str, str, str
     _new_path = '/'.join([''] + [_part for _part in path.split('/') if _part])
     if (path.endswith('/')):
         _new_path += '/'
     if (_new_path != path):
         _new_uri = _new_path + sep + query
         self.logger.trace(f"Rewriting uri: '{path}' => '{_new_path}'")
         request.uri = _new_uri
         request.path = _new_path
     return request
Example #11
0
 def _create_handler(self, logged_in=True):
     app = self.get_app()
     request = HTTPServerRequest(method='GET', uri='/subscriptions')
     request.connection = MagicMock()
     handler = SubscriptionHandler(application=app, request=request,
                                   sub_server=None, resolvers=None)
     if logged_in:
         handler.get_current_user = lambda: {'name': getuser()}
     else:
         handler.get_current_user = lambda: None
     return handler
Example #12
0
 def test_serialize_basic(self):
     uri = "/foo/bar"
     req = HTTPServerRequest(method='GET', uri=uri)
     req.remote_ip = "127.0.0.1"
     msg = serialize_http_request(req)
     (hreq, body_link, extra_dict) = \
         unserialize_request_message(msg)
     self.assertEquals(hreq.method, 'GET')
     self.assertTrue(body_link is None)
     self.assertEquals(len(extra_dict), 0)
     self.assertEquals(hreq.url, "http://127.0.0.1" + uri)
     self.assertEquals(hreq.body, None)
     self.assertEquals(body_link, None)
Example #13
0
 def test_serialize_headers(self):
     uri = "/foo/bar"
     req = HTTPServerRequest(method='GET', uri=uri)
     headers = HTTPHeaders()
     headers.add("Foo", "bar")
     headers.add("Foo", "bar2")
     headers.add("Foo2", "bar3")
     req.headers = headers
     msg = serialize_http_request(req)
     (hreq, body_link, extra_dict) = \
         unserialize_request_message(msg)
     self.assertEquals(len(list(hreq.headers.get_all())), 5)
     self.assertEquals(hreq.headers['Foo2'], "bar3")
     self.assertEquals(hreq.headers['Foo'], "bar,bar2")
Example #14
0
async def test_nothing_whitelisted(mock_getenv, mock_session):
    # Given
    upstream_request = HTTPServerRequest(
        method="HEAD",
        uri="/awsproxy/bucket-name-1",
        headers=HTTPHeaders({
            "Authorization":
            "AWS4-HMAC-SHA256 "
            "Credential=AKIDEXAMPLE/20190828/us-west-2/s3/aws4_request, "
            "SignedHeaders=host;x-amz-content-sha256;x-amz-date;x-amz-user-agent, "
            "Signature=0d02795c4feed38e5a4cd80aec3a2c67886b11797a23c307e4f52c2cfe0c137e",
            "Host":
            "localhost:8888",
            "X-Amz-User-Agent":
            "aws-sdk-js/2.507.0 promise",
            "X-Amz-Content-Sha256":
            "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
            "X-Amz-Date":
            "20190828T173626Z",
        }),
        body=None,
        host="localhost:8888",
    )
    mock_getenv.return_value = ""

    # When
    with pytest.raises(HTTPError) as e:
        await AwsProxyRequest(upstream_request, create_endpoint_resolver(),
                              mock_session).execute_downstream()

        # Then
        assert 403 == e.value.code
        assert "Service s3 is not whitelisted for proxying requests" == e.value.message
Example #15
0
 def test_set_input_header(self):
     request = HTTPServerRequest(method='GET', uri='/')
     exchange = HTTPExchange(request)
     actions = Actions(set_input_header=('Header-Name', 'header value'))
     actions.execute_input_actions(exchange)
     self.assertEqual(exchange.request.headers['Header-Name'],
                      'header value')
async def test_authenticator_uses_lti_grades_sender_control_file_when_student(tmp_path):
    """
    Is the LTIGradesSenderControlFile class register_data method called when setting the user_role with the
    Student string?
    """

    def _change_flag():
        LTIGradesSenderControlFile.FILE_LOADED = True

    with patch.object(LTI11LaunchValidator, 'validate_launch_request', return_value=True):
        with patch.object(LTIGradesSenderControlFile, 'register_data', return_value=None) as mock_register_data:
            with patch.object(
                LTIGradesSenderControlFile, '_loadFromFile', return_value=None
            ) as mock_loadFromFileMethod:
                mock_loadFromFileMethod.side_effect = _change_flag
                sender_controlfile = LTIGradesSenderControlFile(tmp_path)
                authenticator = LTI11Authenticator()
                handler = Mock(spec=RequestHandler)
                request = HTTPServerRequest(method='POST', connection=Mock(),)
                handler.request = request
                handler.request.arguments = factory_lti11_complete_launch_args(lms_vendor='edx', role='Student')
                handler.request.get_argument = lambda x, strip=True: factory_lti11_complete_launch_args('Student')[x][
                    0
                ].decode()

                _ = await authenticator.authenticate(handler, None)
                assert mock_register_data.called
Example #17
0
    def post(self, **kw):
        paths = self.get_arguments('paths[]')

        if len(paths) > MultiHandler.paths_limit:
            self.set_status(400)
            super(MultiHandler, self).write('Too many paths: %d max' %
                                            MultiHandler.paths_limit)
            super(MultiHandler, self).finish()
            return

        for path in paths:
            request = HTTPServerRequest(method='GET',
                                        uri=path,
                                        host=self.request.host,
                                        connection=self.request.connection)

            handler = MultiHandler(self.application,
                                   request,
                                   context=self.context)

            # Copy over the storage as-is, which allows those requests to
            # share storage if needed (useful for request-storage)
            handler.context.modules.storage = self.context.modules.storage

            m = re.match(Url.regex(), path)
            yield handler.check_image(m.groupdict())

        # Close the request ASAP, the work is to be done async
        self.set_status(200)
        super(MultiHandler, self).finish()
Example #18
0
def mock_handler(Handler, uri='https://hub.example.com', method='GET', **settings):
    """Instantiate a Handler in a mock application"""
    application = web.Application(
        hub=Mock(
            base_url='/hub/',
            server=Mock(
                base_url='/hub/'
            ),
        ),
        cookie_secret=os.urandom(32),
        db=Mock(
            rollback=Mock(return_value=None)
        ),
        **settings
    )
    request = HTTPServerRequest(
        method=method,
        uri=uri,
        connection=Mock(),
    )
    handler = Handler(
        application=application,
        request=request,
    )
    handler._transforms = []
    return handler
Example #19
0
 def _make_lti11_mock_request_handler(
     handler: RequestHandler,
     uri: str = "https://hub.example.com",
     method: str = "POST",
     **settings: dict,
 ) -> RequestHandler:
     """Instantiate a Handler in a mock application"""
     application = Application(
         hub=Mock(
             base_url="/hub/",
             server=Mock(base_url="/hub/"),
         ),
         cookie_secret=os.urandom(32),
         db=Mock(rollback=Mock(return_value=None)),
         **settings,
     )
     request = HTTPServerRequest(
         method=method,
         uri=uri,
         connection=Mock(),
     )
     handler = RequestHandler(
         application=application,
         request=request,
     )
     handler._transforms = []
     return handler
Example #20
0
async def test_authenticator_uses_lti_grades_sender_control_file_with_instructor_role(
    mock_mkdir, tmp_path, make_lti11_success_authentication_request_args, mock_nbhelper
):
    """
    Is the LTIGradesSenderControlFile class register_data method called when setting the user_role with the
    Instructor string?
    """

    def _change_flag():
        LTIGradesSenderControlFile.FILE_LOADED = True

    with patch.object(LTI11LaunchValidator, 'validate_launch_request', return_value=True):
        with patch.object(LTIGradesSenderControlFile, 'register_data', return_value=None) as mock_register_data:
            with patch.object(
                LTIGradesSenderControlFile, '_loadFromFile', return_value=None
            ) as mock_loadFromFileMethod:
                mock_loadFromFileMethod.side_effect = _change_flag
                authenticator = LTI11Authenticator()
                handler = Mock(spec=RequestHandler)
                request = HTTPServerRequest(
                    method='POST',
                    connection=Mock(),
                )
                handler.request = request
                handler.request.arguments = make_lti11_success_authentication_request_args(
                    lms_vendor='canvas', role='Instructor'
                )
                handler.request.get_argument = lambda x, strip=True: make_lti11_success_authentication_request_args(
                    'Instructor'
                )[x][0].decode()

                _ = await authenticator.authenticate(handler, None)
                assert mock_register_data.called
Example #21
0
async def test_authenticator_invokes_validator_with_decoded_dict(
    make_lti11_success_authentication_request_args, mock_nbhelper, gradesender_controlfile_mock
):
    """
    Does the authentictor call the validator?
    """
    with patch.object(LTI11LaunchValidator, 'validate_launch_request', return_value=True) as mock_validator:
        authenticator = LTI11Authenticator()
        handler = Mock(spec=RequestHandler)
        request = HTTPServerRequest(method='POST', uri='/hub', host='example.com')
        handler.request = request
        handler.request.protocol = 'https'
        handler.request.arguments = make_lti11_success_authentication_request_args('canvas')
        handler.request.get_argument = lambda x, strip=True: make_lti11_success_authentication_request_args('canvas')[
            x
        ][0].decode()

        _ = await authenticator.authenticate(handler, None)
        # check our validator was called
        assert mock_validator.called
        decoded_args = {
            k: handler.request.get_argument(k, strip=False)
            for k, v in make_lti11_success_authentication_request_args('canvas').items()
        }
        # check validator was called with correct dict params (decoded)
        mock_validator.assert_called_with('https://example.com/hub', {}, decoded_args)
Example #22
0
def create_http_request():
    return HTTPServerRequest(
        method="POST",
        uri="/pcsd/uri",
        headers={"Cookie": "cookie1=first;cookie2=second"},
        body=str.encode(urlencode({"post-key": "post-value"})),
        host="pcsd-host:2224")
Example #23
0
async def test_errors_passed_through(mock_fetch, mock_session):
    # Given
    upstream_request = HTTPServerRequest(
        method="GET",
        uri="/awsproxy/clusters/myname",
        headers=HTTPHeaders({
            "Authorization":
            "AWS4-HMAC-SHA256 "
            "Credential=AKIDEXAMPLE/20190816/us-east-2/eks/aws4_request, "
            "SignedHeaders=host;x-amz-content-sha256;x-amz-date;x-amz-user-agent, "
            "Signature=f176ff82da6efc539bb8a2860be6ea19a99adf93d87be8ba96f25f1d29c91ba9",
            "Host":
            "localhost:8888",
            "X-Amz-User-Agent":
            "aws-sdk-js/2.507.0 promise",
            "X-Amz-Content-Sha256":
            "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
            "X-Amz-Date":
            "20190816T224244Z",
        }),
        body=b"",
        host="localhost:8888",
    )
    mock_fetch.side_effect = HTTPClientError(code=500, message="Something bad")

    # When
    with pytest.raises(HTTPClientError) as e:
        await AwsProxyRequest(upstream_request, create_endpoint_resolver(),
                              mock_session).execute_downstream()

        assert 500 == e.value.code
        assert "Something bad" == e.value.message
async def test_authenticator_uses_lti11validator(
    make_lti11_success_authentication_request_args, ):
    """
    Ensure that we call the LTI11Validator from the LTI11Authenticator.
    """
    with patch.object(LTI11LaunchValidator,
                      "validate_launch_request",
                      return_value=True) as mock_validator:

        authenticator = LTI11Authenticator()
        handler = Mock(spec=RequestHandler)
        request = HTTPServerRequest(
            method="POST",
            connection=Mock(),
        )
        handler.request = request

        handler.request.arguments = make_lti11_success_authentication_request_args(
            "lmsvendor")
        handler.request.get_argument = (
            lambda x, strip=True:
            make_lti11_success_authentication_request_args("lmsvendor")[x][
                0].decode())

        _ = await authenticator.authenticate(handler, None)
        assert mock_validator.called
Example #25
0
async def test_post_with_query_params_no_body(mock_fetch, mock_session):
    # Given
    upstream_request = HTTPServerRequest(
        method="POST",
        uri="/awsproxy/bucket-name-1/Multipart-0.16441670919496487.txt?uploads",
        headers=HTTPHeaders({
            "Authorization":
            "AWS4-HMAC-SHA256 "
            "Credential=AKIDEXAMPLE/20190828/us-west-2/s3/aws4_request, "
            "SignedHeaders=host;x-amz-content-sha256;x-amz-date;x-amz-user-agent, "
            "Signature=451d7037903cee381c6a5d2c61c6ee2b5d36f35650e95abcf5e8af11b57c0cf8",
            "Host":
            "localhost:8888",
            "X-Amz-User-Agent":
            "aws-sdk-js/2.507.0 promise",
            "X-Amz-Content-Sha256":
            "a08b81ef3b4ec5e1f65ca97d00928105c3d7eb6d50ae59fc15f0d14b64c9ec3b",
            "X-Amz-Date":
            "20190828T173626Z",
        }),
        body=b"",
        host="localhost:8888",
    )

    # When
    await AwsProxyRequest(upstream_request, create_endpoint_resolver(),
                          mock_session).execute_downstream()

    # Then
    expected = HTTPRequest(
        url=
        "https://s3.us-west-2.amazonaws.com/bucket-name-1/Multipart-0.16441670919496487.txt"
        "?uploads",
        method=upstream_request.method,
        body=None,
        headers={
            "Authorization":
            "AWS4-HMAC-SHA256 "
            "Credential=access_key/20190828/us-west-2/s3/aws4_request, "
            "SignedHeaders=host;x-amz-content-sha256;x-amz-date;"
            "x-amz-security-token;x-amz-user-agent, "
            "Signature="
            "ba2062818cb4cd80a73dd43d006f141ede069b8ccc2ece16c20504587bd5045b",
            "X-Amz-User-Agent":
            upstream_request.headers["X-Amz-User-Agent"],
            "X-Amz-Content-Sha256":
            upstream_request.headers["X-Amz-Content-Sha256"],
            "X-Amz-Date":
            upstream_request.headers["X-Amz-Date"],
            "X-Amz-Security-Token":
            "session_token",
            "Host":
            "s3.us-west-2.amazonaws.com",
        },
        follow_redirects=False,
        allow_nonstandard_methods=True,
    )

    assert_http_response(mock_fetch, expected)
Example #26
0
 def test_stop_option(self):
     add_rule(Criteria(path='/foo'),
              Actions(set_input_header=('Header-Name', 'FOO')), stop=True)
     add_rule(Criteria(path='/foo'),
              Actions(set_input_header=('Header-Name', 'BAR')))
     request = HTTPServerRequest(method='GET', uri='/foo')
     Rules.execute_input_actions(HTTPExchange(request))
     self.assertEqual(request.headers['Header-Name'], 'FOO')
Example #27
0
 def test_serialize_body_link(self):
     uri = "/foo/bar"
     req = HTTPServerRequest(method='PUT', uri=uri)
     msg = serialize_http_request(req, body_link="http://foo.com/bar")
     (hreq, body_link, extra_dict) = \
         unserialize_request_message(msg)
     self.assertEquals(hreq.body, None)
     self.assertEquals(body_link, "http://foo.com/bar")
Example #28
0
    def test_get_real_client_addr_with_correct_nginx_config(self):
        handler = MixinHandler()
        handler.request = HTTPServerRequest(uri='/')

        ip = '127.0.0.1'
        handler.request.headers.add('X-Real-Ip', ip)
        handler.request.headers.add('X-Real-Port', '12345')
        self.assertEqual(handler.get_real_client_addr(), (ip, 12345))
Example #29
0
    def test_does_not_match_function(self):
        def criterion_function(path):
            return False

        request = HTTPServerRequest(uri='/foo/bar')
        criteria = Criteria(custom=criterion_function)
        result = criteria.match(HTTPExchange(request))
        self.assertFalse(result)
Example #30
0
async def test_get_with_encoded_uri(mock_fetch, mock_session):
    # Given
    upstream_request = HTTPServerRequest(
        method="GET",
        uri="/awsproxy/bucket-name-1/ll%3A%3Askeleton%201.png",
        headers=HTTPHeaders({
            "Authorization":
            "AWS4-HMAC-SHA256 "
            "Credential=AKIDEXAMPLE/20190816/us-west-2/s3/aws4_request, "
            "SignedHeaders=host;x-amz-content-sha256;x-amz-date;x-amz-user-agent, "
            "Signature=822f116d22d577aa2dc1033f354fa2a6fd3a2b6a0fd51885472b57daf45d605e",
            "Host":
            "localhost:8888",
            "X-Amz-User-Agent":
            "aws-sdk-js/2.507.0 promise",
            "X-Amz-Content-Sha256":
            "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
            "X-Amz-Date":
            "20190816T224244Z",
        }),
        body=b"",
        host="localhost:8888",
    )

    # When
    await AwsProxyRequest(upstream_request, create_endpoint_resolver(),
                          mock_session).execute_downstream()

    # Then
    expected = HTTPRequest(
        url=
        "https://s3.us-west-2.amazonaws.com/bucket-name-1/ll%3A%3Askeleton%201.png",
        method=upstream_request.method,
        body=None,
        headers={
            "Authorization":
            "AWS4-HMAC-SHA256 "
            "Credential=access_key/20190816/us-west-2/s3/aws4_request, "
            "SignedHeaders=host;x-amz-content-sha256;x-amz-date;"
            "x-amz-security-token;x-amz-user-agent, "
            "Signature="
            "4715991ba2461bfda29bda8a53747a13448c1303c2e03d8ee8a4992df08f5551",
            "X-Amz-User-Agent":
            upstream_request.headers["X-Amz-User-Agent"],
            "X-Amz-Content-Sha256":
            upstream_request.headers["X-Amz-Content-Sha256"],
            "X-Amz-Date":
            upstream_request.headers["X-Amz-Date"],
            "X-Amz-Security-Token":
            "session_token",
            "Host":
            "s3.us-west-2.amazonaws.com",
        },
        follow_redirects=False,
        allow_nonstandard_methods=True,
    )

    assert_http_response(mock_fetch, expected)
Example #31
0
async def test_request_with_base_url(mock_getenv, mock_fetch, mock_session):
    # Given
    upstream_request = HTTPServerRequest(
        method="HEAD",
        uri="base-url/awsproxy/bucket-name-1",
        headers=HTTPHeaders({
            "Authorization":
            "AWS4-HMAC-SHA256 "
            "Credential=AKIDEXAMPLE/20190828/us-west-2/s3/aws4_request, "
            "SignedHeaders=host;x-amz-content-sha256;x-amz-date;x-amz-user-agent, "
            "Signature=0d02795c4feed38e5a4cd80aec3a2c67886b11797a23c307e4f52c2cfe0c137e",
            "Host":
            "localhost:8888",
            "X-Amz-User-Agent":
            "aws-sdk-js/2.507.0 promise",
            "X-Amz-Content-Sha256":
            "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
            "X-Amz-Date":
            "20190828T173626Z",
        }),
        body=None,
        host="localhost:8888",
    )
    mock_getenv.return_value = "s3,"

    # When
    await AwsProxyRequest(upstream_request, create_endpoint_resolver(),
                          mock_session).execute_downstream()

    # Then
    expected = HTTPRequest(
        url="https://s3.us-west-2.amazonaws.com/bucket-name-1",
        method=upstream_request.method,
        body=None,
        headers={
            "Authorization":
            "AWS4-HMAC-SHA256 "
            "Credential=access_key/20190828/us-west-2/s3/aws4_request, "
            "SignedHeaders=host;x-amz-content-sha256;x-amz-date;"
            "x-amz-security-token;x-amz-user-agent, "
            "Signature="
            "6d724e3bd64390d5d84010d6fc0f8147b3e3917c5befa3f8d1efb691b408e821",
            "X-Amz-User-Agent":
            upstream_request.headers["X-Amz-User-Agent"],
            "X-Amz-Content-Sha256":
            upstream_request.headers["X-Amz-Content-Sha256"],
            "X-Amz-Date":
            upstream_request.headers["X-Amz-Date"],
            "X-Amz-Security-Token":
            "session_token",
            "Host":
            "s3.us-west-2.amazonaws.com",
        },
        follow_redirects=False,
        allow_nonstandard_methods=True,
    )

    assert_http_response(mock_fetch, expected)
Example #32
0
async def test_put(mock_fetch, mock_session):
    # Given
    upstream_request = HTTPServerRequest(
        method="PUT",
        uri="/awsproxy/clusters/myname",
        headers=HTTPHeaders({
            "Authorization":
            "AWS4-HMAC-SHA256 "
            "Credential=AKIDEXAMPLE/20190816/us-east-2/eks/aws4_request, "
            "SignedHeaders=host;x-amz-content-sha256;x-amz-date;x-amz-user-agent, "
            "Signature=24203e07130d74b28845f756f5440603d24400d53d07ddda9d7add99d5ec7c8d",
            "Host":
            "localhost:8888",
            "X-Amz-User-Agent":
            "aws-sdk-js/2.507.0 promise",
            "X-Amz-Content-Sha256":
            "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
            "X-Amz-Date":
            "20190816T224244Z",
        }),
        body=b'{"Name":"Foo","Id":"Bar"}',
        host="localhost:8888",
    )

    # When
    await AwsProxyRequest(upstream_request, create_endpoint_resolver(),
                          mock_session).execute_downstream()

    # Then
    expected = HTTPRequest(
        url="https://eks.us-east-2.amazonaws.com/clusters/myname",
        method=upstream_request.method,
        body=upstream_request.body,
        headers={
            "Authorization":
            "AWS4-HMAC-SHA256 "
            "Credential=access_key/20190816/us-east-2/eks/aws4_request, "
            "SignedHeaders=host;x-amz-content-sha256;x-amz-date;"
            "x-amz-security-token;x-amz-user-agent, "
            "Signature="
            "b6d04b91fa9e1993657806821321eb10a665e89d6de2f390fa39d40d77015971",
            "X-Amz-User-Agent":
            upstream_request.headers["X-Amz-User-Agent"],
            "X-Amz-Content-Sha256":
            upstream_request.headers["X-Amz-Content-Sha256"],
            "X-Amz-Date":
            upstream_request.headers["X-Amz-Date"],
            "X-Amz-Security-Token":
            "session_token",
            "Host":
            "eks.us-east-2.amazonaws.com",
        },
        follow_redirects=False,
        allow_nonstandard_methods=True,
    )

    assert_http_response(mock_fetch, expected)
Example #33
0
 def _log(self, status_code: int, request: httputil.HTTPServerRequest) -> None:
     if status_code < 400:
         log_method = access_log.info
     elif status_code < 500:
         log_method = access_log.warning
     else:
         log_method = access_log.error
     request_time = 1000.0 * request.request_time()
     assert request.method is not None
     assert request.uri is not None
     summary = request.method + " " + request.uri + " (" + request.remote_ip + ")"
     log_method("%d %s %.2fms", status_code, summary, request_time)
Example #34
0
 def test_mds_process(self):
     mdsplugin = MDSExec(CocaineProxy(), {"srw_host": "http://localhost:%d" % self.get_http_port()})
     conn = _FakeConnection()
     req = HTTPServerRequest(method="PUT", uri="/blabla",
                             version="HTTP/1.1", headers={
                                 "X-Cocaine-Service": "application",
                                 "X-Cocaine-Event": "event",
                                 "X-Srw-Key": "320.namespace:301123837.E150591:1046883323",
                                 "X-Srw-Namespace": "namespace",
                                 "X-Srw-Key-Type": "mds",
                                 "Authorization": "Basic aaabbb",
                             },
                             connection=conn,
                             body="body", host="localhost")
     req.logger = NULLLOGGER
     yield mdsplugin.process(req)
     self.assertEqual(conn.start_line.code, 202)
     self.assertEqual(conn.start_line.version, "HTTP/1.1")
     self.assertEqual(len(conn.chunks), 1)
     self.assertEqual(''.join(conn.chunks), "CHUNK1CHUNK2CHUNK3")
     self.assertEqual(conn.headers["A"], "B")