graphql-engine/server/tests-py/webhook.py
nizar-m 0ffb0478b9 Tests for server with access control, and some more tests (#710)
* 1) Tests for creating permissions
2) Test for constraint_on with GraphQL insert on_conflict

* Run tests with access key and webhook

* Tests for GraphQL query with quoted columns

* Rewrite test-server.sh so that it can be run locally

* JWT based tests

* Tests with various postgres types

* For tests on select queries, run setup only once per class

* Tests for v1 count queries

* Skip teardown for tests that does not modify data

* Workaround for hpc 'parse error when reading .tix file'

* Move GeoJson tests to the new structure

* Basic tests for v1 queries

* Tests for column, table or operator not found error cases on GraphQL queries

* Skip test teardown for mutation tests which does not change database state, even when it returns 200.
2018-10-28 23:57:49 +05:30

65 lines
2.2 KiB
Python

#!/usr/bin/env python
"""
Webhook for GraphQL engine
For successful authentication
1) Add header X-Hasura-Auth-From: webhook to the list of headers
2) Base64 encode the required headers (including X-Hasura-Auth-From)
3) Pass it as the bearer token with the Authorization header
"""
import base64
import json
import ssl
import http.server
import traceback
import sys
class S(http.server.BaseHTTPRequestHandler):
def do_GET(self):
if 'Authorization' in self.headers:
auth = self.headers['Authorization']
h = dict()
if auth.startswith("Bearer "):
try:
h = json.loads(base64.b64decode(auth[7:]).decode("utf-8"))
if h.get('X-Hasura-Auth-Mode') == 'webhook':
print (h)
self.send_response(200)
self.send_header('Content-Type', 'application/json')
self.end_headers()
self.wfile.write(json.dumps(h).encode('utf-8'))
else:
print ('forbidden')
self.send_response(401)
self.end_headers()
self.wfile.write('{}')
except Exception as e:
print ('forbidden')
self.send_response(401)
self.end_headers()
print("type error: " + str(e))
print(traceback.format_exc())
else:
self.send_response(401)
self.end_headers()
print ('forbidden')
def run(keyfile, certfile, server_class=http.server.HTTPServer, handler_class=S, port=9090):
server_address = ('', port)
httpd = server_class(server_address, handler_class)
httpd.socket = ssl.wrap_socket (
httpd.socket,
certfile=certfile,
keyfile=keyfile,
server_side=True,
ssl_version=ssl.PROTOCOL_SSLv23)
print('Starting httpd...')
httpd.serve_forever()
if __name__ == "__main__":
if len(sys.argv) != 4:
print("Usage: python webhook.py port keyfile certfile")
sys.exit(1)
run(keyfile=sys.argv[2],certfile=sys.argv[3], port=int(sys.argv[1]))