2021-02-03 10:10:39 +03:00
|
|
|
import http
|
2022-09-15 00:41:28 +03:00
|
|
|
import http.server
|
|
|
|
import json
|
|
|
|
import pytest
|
2021-02-03 10:10:39 +03:00
|
|
|
import queue
|
|
|
|
import threading
|
2022-09-15 00:41:28 +03:00
|
|
|
|
2022-10-27 14:47:48 +03:00
|
|
|
from conftest import extract_server_address_from
|
|
|
|
|
2021-02-03 10:10:39 +03:00
|
|
|
|
2022-09-15 00:41:28 +03:00
|
|
|
class QueryEchoWebhookServer(http.server.HTTPServer):
|
|
|
|
def __init__(self, server_address):
|
|
|
|
# TODO why maxsize=1
|
|
|
|
self.resp_queue = queue.Queue(maxsize=1)
|
|
|
|
super().__init__(server_address, QueryEchoWebhookHandler)
|
|
|
|
|
|
|
|
def get_event(self, timeout):
|
|
|
|
return self.resp_queue.get(timeout=timeout)
|
|
|
|
|
|
|
|
|
2021-02-03 10:10:39 +03:00
|
|
|
class QueryEchoWebhookHandler(http.server.BaseHTTPRequestHandler):
|
2022-09-15 00:41:28 +03:00
|
|
|
server: QueryEchoWebhookServer
|
|
|
|
|
2021-02-03 10:10:39 +03:00
|
|
|
def do_GET(self):
|
|
|
|
self.log_message("get")
|
|
|
|
self.send_response(http.HTTPStatus.OK)
|
|
|
|
self.end_headers()
|
|
|
|
|
|
|
|
def do_POST(self):
|
|
|
|
self.log_message("post")
|
|
|
|
content_len = self.headers.get("Content-Length")
|
|
|
|
req_body = self.rfile.read(int(content_len)).decode("utf-8")
|
|
|
|
req_json = json.loads(req_body)
|
|
|
|
print(json.dumps(req_json))
|
|
|
|
user_id_header = req_json["headers"]["auth-user-id"]
|
2021-10-28 21:42:50 +03:00
|
|
|
user_vars = {
|
2021-02-03 10:10:39 +03:00
|
|
|
"x-hasura-role":"user",
|
|
|
|
"x-hasura-user-id": user_id_header
|
|
|
|
}
|
|
|
|
self.send_response(http.HTTPStatus.OK)
|
|
|
|
self.send_header('Content-Type', 'application/json')
|
|
|
|
self.end_headers()
|
2021-10-28 21:42:50 +03:00
|
|
|
self.server.resp_queue.put({
|
|
|
|
"request": req_json["request"],
|
|
|
|
"headers": user_vars,
|
|
|
|
})
|
|
|
|
self.wfile.write(json.dumps(user_vars).encode('utf-8'))
|
2021-02-03 10:10:39 +03:00
|
|
|
|
|
|
|
|
server/tests-py: Start webhook.py inside the test harness.
We use a helper service to start a webhook-based authentication service for some tests. This moves the initialization of the service out of _test-server.sh_ and into the Python test harness, as a fixture.
In order to do this, I had to make a few changes. The main deviation is that we no longer run _all_ tests against an HGE with this authentication service, just a few (those in _test_webhook.py_). Because this reduced coverage, I have added some more tests there, which actually cover some areas not exacerbated elsewhere (mainly trying to use webhook credentials to talk to an admin-only endpoint).
The webhook service can run both with and without TLS, and decide whether it's necessary to skip one of these based on the arguments passed and how HGE is started, according to the following logic:
* If a TLS CA certificate is passed in, it will run with TLS, otherwise it will skip it.
* If HGE was started externally and a TLS certificate is provided, it will skip running without TLS, as it will assume that HGE was configured to talk to a webhook over HTTPS.
* Some tests should only be run with TLS; this is marked with a `tls_webhook_server` marker.
* Some tests should only be run _without_ TLS; this is marked with a `no_tls_webhook_server` marker.
The actual parameterization of the webhook service configuration is done through test subclasses, because normal pytest parameterization doesn't work with the `hge_fixture_env` hack that we use. Because `hge_fixture_env` is not a sanctioned way of conveying data between fixtures (and, unfortunately, there isn't a sanctioned way of doing this when the fixtures in question may not know about each other directly), parameterizing the `webhook_server` fixture doesn't actually parameterize `hge_server` properly. Subclassing forces this to work correctly.
The certificate generation is moved to a Python fixture, so that we don't have to revoke the CA certificate for _test_webhook_insecure.py_; we can just generate a bogus certificate instead. The CA certificate is still generated in the _test-server.sh_ script, as it needs to be installed into the OS certificate store.
Interestingly, the CA certificate installation wasn't actually working, because the certificates were written to the wrong location. This didn't cause any failures, as we weren't actually testing this behavior. This is now fixed with the other changes.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/6363
GitOrigin-RevId: 0f277d374daa64f657257ed2a4c2057c74b911db
2022-10-20 21:58:36 +03:00
|
|
|
@pytest.fixture(scope='class')
|
|
|
|
@pytest.mark.early
|
|
|
|
def query_echo_webhook(hge_fixture_env: dict[str, str]):
|
2022-10-27 14:47:48 +03:00
|
|
|
server_address = extract_server_address_from('HASURA_GRAPHQL_AUTH_HOOK')
|
|
|
|
server = QueryEchoWebhookServer(server_address)
|
|
|
|
thread = threading.Thread(target=server.serve_forever)
|
|
|
|
thread.start()
|
|
|
|
url = f'http://{server.server_address[0]}:{server.server_address[1]}'
|
|
|
|
print(f'{query_echo_webhook.__name__} server started on {url}')
|
|
|
|
hge_fixture_env['HASURA_GRAPHQL_AUTH_HOOK'] = url + '/'
|
server/tests-py: Start webhook.py inside the test harness.
We use a helper service to start a webhook-based authentication service for some tests. This moves the initialization of the service out of _test-server.sh_ and into the Python test harness, as a fixture.
In order to do this, I had to make a few changes. The main deviation is that we no longer run _all_ tests against an HGE with this authentication service, just a few (those in _test_webhook.py_). Because this reduced coverage, I have added some more tests there, which actually cover some areas not exacerbated elsewhere (mainly trying to use webhook credentials to talk to an admin-only endpoint).
The webhook service can run both with and without TLS, and decide whether it's necessary to skip one of these based on the arguments passed and how HGE is started, according to the following logic:
* If a TLS CA certificate is passed in, it will run with TLS, otherwise it will skip it.
* If HGE was started externally and a TLS certificate is provided, it will skip running without TLS, as it will assume that HGE was configured to talk to a webhook over HTTPS.
* Some tests should only be run with TLS; this is marked with a `tls_webhook_server` marker.
* Some tests should only be run _without_ TLS; this is marked with a `no_tls_webhook_server` marker.
The actual parameterization of the webhook service configuration is done through test subclasses, because normal pytest parameterization doesn't work with the `hge_fixture_env` hack that we use. Because `hge_fixture_env` is not a sanctioned way of conveying data between fixtures (and, unfortunately, there isn't a sanctioned way of doing this when the fixtures in question may not know about each other directly), parameterizing the `webhook_server` fixture doesn't actually parameterize `hge_server` properly. Subclassing forces this to work correctly.
The certificate generation is moved to a Python fixture, so that we don't have to revoke the CA certificate for _test_webhook_insecure.py_; we can just generate a bogus certificate instead. The CA certificate is still generated in the _test-server.sh_ script, as it needs to be installed into the OS certificate store.
Interestingly, the CA certificate installation wasn't actually working, because the certificates were written to the wrong location. This didn't cause any failures, as we weren't actually testing this behavior. This is now fixed with the other changes.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/6363
GitOrigin-RevId: 0f277d374daa64f657257ed2a4c2057c74b911db
2022-10-20 21:58:36 +03:00
|
|
|
hge_fixture_env['HASURA_GRAPHQL_AUTH_HOOK_MODE'] = 'POST'
|
2022-10-27 14:47:48 +03:00
|
|
|
yield server
|
|
|
|
server.shutdown()
|
|
|
|
server.server_close()
|
|
|
|
thread.join()
|
2021-02-03 10:10:39 +03:00
|
|
|
|
|
|
|
|
server/tests-py: Start webhook.py inside the test harness.
We use a helper service to start a webhook-based authentication service for some tests. This moves the initialization of the service out of _test-server.sh_ and into the Python test harness, as a fixture.
In order to do this, I had to make a few changes. The main deviation is that we no longer run _all_ tests against an HGE with this authentication service, just a few (those in _test_webhook.py_). Because this reduced coverage, I have added some more tests there, which actually cover some areas not exacerbated elsewhere (mainly trying to use webhook credentials to talk to an admin-only endpoint).
The webhook service can run both with and without TLS, and decide whether it's necessary to skip one of these based on the arguments passed and how HGE is started, according to the following logic:
* If a TLS CA certificate is passed in, it will run with TLS, otherwise it will skip it.
* If HGE was started externally and a TLS certificate is provided, it will skip running without TLS, as it will assume that HGE was configured to talk to a webhook over HTTPS.
* Some tests should only be run with TLS; this is marked with a `tls_webhook_server` marker.
* Some tests should only be run _without_ TLS; this is marked with a `no_tls_webhook_server` marker.
The actual parameterization of the webhook service configuration is done through test subclasses, because normal pytest parameterization doesn't work with the `hge_fixture_env` hack that we use. Because `hge_fixture_env` is not a sanctioned way of conveying data between fixtures (and, unfortunately, there isn't a sanctioned way of doing this when the fixtures in question may not know about each other directly), parameterizing the `webhook_server` fixture doesn't actually parameterize `hge_server` properly. Subclassing forces this to work correctly.
The certificate generation is moved to a Python fixture, so that we don't have to revoke the CA certificate for _test_webhook_insecure.py_; we can just generate a bogus certificate instead. The CA certificate is still generated in the _test-server.sh_ script, as it needs to be installed into the OS certificate store.
Interestingly, the CA certificate installation wasn't actually working, because the certificates were written to the wrong location. This didn't cause any failures, as we weren't actually testing this behavior. This is now fixed with the other changes.
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/6363
GitOrigin-RevId: 0f277d374daa64f657257ed2a4c2057c74b911db
2022-10-20 21:58:36 +03:00
|
|
|
@pytest.mark.usefixtures('per_method_tests_db_state')
|
|
|
|
@pytest.mark.admin_secret
|
2021-02-03 10:10:39 +03:00
|
|
|
class TestWebhookRequestContext(object):
|
|
|
|
@classmethod
|
|
|
|
def dir(cls):
|
|
|
|
return "queries/webhooks/request_context"
|
|
|
|
|
|
|
|
def test_query(self, hge_ctx, query_echo_webhook):
|
|
|
|
query = """
|
|
|
|
query allUsers {
|
|
|
|
users {
|
|
|
|
id
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
query_obj = {
|
|
|
|
"query": query,
|
|
|
|
"operationName": "allUsers"
|
|
|
|
}
|
|
|
|
headers = dict()
|
|
|
|
headers['auth-user-id'] = '1'
|
|
|
|
code, resp, _ = hge_ctx.anyq('/v1/graphql', query_obj, headers)
|
|
|
|
assert code == 200, resp
|
|
|
|
|
|
|
|
ev_full = query_echo_webhook.get_event(3)
|
|
|
|
assert ev_full['request'] == query_obj
|
|
|
|
|
2021-10-28 21:42:50 +03:00
|
|
|
def test_query_invalid(self, hge_ctx, query_echo_webhook):
|
|
|
|
"""
|
|
|
|
Even when an invalid query is sent, the webhook should still resolve
|
|
|
|
the user id header correctly
|
|
|
|
"""
|
|
|
|
query_obj = "invalid-query"
|
|
|
|
user_id_header = '1'
|
|
|
|
headers = dict()
|
|
|
|
headers['auth-user-id'] = user_id_header
|
|
|
|
code, resp, _ = hge_ctx.anyq('/v1/graphql', query_obj, headers)
|
|
|
|
assert code == 200, resp
|
|
|
|
|
|
|
|
ev_full = query_echo_webhook.get_event(3)
|
|
|
|
assert ev_full['headers']['x-hasura-user-id'] == user_id_header
|
|
|
|
|
2021-02-03 10:10:39 +03:00
|
|
|
def test_mutation_with_vars(self, hge_ctx, query_echo_webhook):
|
|
|
|
query = """
|
|
|
|
mutation insert_single_user($id: Int!, $name: String!) {
|
|
|
|
insert_users_one(
|
|
|
|
object: {
|
|
|
|
id: $id,
|
|
|
|
name: $name
|
|
|
|
}
|
|
|
|
) {
|
|
|
|
id
|
|
|
|
name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
variables = {"id": 4, "name": "danish"}
|
|
|
|
query_obj = {"query": query, "variables": variables, "operationName": "insert_single_user"}
|
|
|
|
headers = dict()
|
|
|
|
headers['auth-user-id'] = '4'
|
|
|
|
code, resp, _ = hge_ctx.anyq('/v1/graphql', query_obj, headers)
|
|
|
|
assert code == 200, resp
|
|
|
|
|
|
|
|
ev_full = query_echo_webhook.get_event(3)
|
|
|
|
exp_result = {"request": query_obj}
|
|
|
|
assert ev_full['request'] == query_obj
|