Add default User-Agent header for Action requests

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/3505
GitOrigin-RevId: fe74349d1ca699dc0edca1e5a829fd658226a5ee
This commit is contained in:
Daniel Chambers 2022-01-31 17:49:11 +11:00 committed by hasura-bot
parent e5f35c8051
commit 3c74f91a1c
7 changed files with 64 additions and 1 deletions

View File

@ -38,6 +38,7 @@ count (
- server: fix REST endpoints with path segments not showing correctly in the OpenAPI spec
- server: fix aliases used in GraphQL queries in REST endpoints not being reflected in the OpenAPI spec
- server: refresh JWKs a maximum of once per second (fix #5781)
- server: action webhook requests now include a User-Agent header (fix #8070)
- console: action/event trigger transforms are now called REST connectors
- console: fix list of tables (and schemas) being unsorted when creating a new trigger event (fix #6391)
- console: fix custom field names breaking browse table sorting and the pre-populating of the edit row form

View File

@ -498,7 +498,7 @@ callWebhook
let clientHeaders = if forwardClientHeaders then mkClientHeadersForward reqHeaders else mempty
-- Using HashMap to avoid duplicate headers between configuration headers
-- and client headers where configuration headers are preferred
hdrs = ("Content-Type", "application/json") : (Map.toList . Map.fromList) (resolvedConfHeaders <> clientHeaders)
hdrs = (Map.toList . Map.fromList) (resolvedConfHeaders <> defaultHeaders <> clientHeaders)
postPayload = J.toJSON actionWebhookPayload
requestBody = J.encode postPayload
requestBodySize = BL.length requestBody

View File

@ -3,6 +3,7 @@ module Hasura.HTTP
HttpException (..),
hdrsToText,
addDefaultHeaders,
defaultHeaders,
HttpResponse (..),
addHttpResponseHeaders,
getHTTPExceptionStatus,

View File

@ -345,6 +345,10 @@ class ActionsWebhookHandler(http.server.BaseHTTPRequestHandler):
resp, status = self.mirror_action()
self._send_response(status, resp)
elif req_path == "/mirror-headers":
resp, status = self.mirror_headers()
self._send_response(status, resp)
elif req_path == "/get-user-by-email":
resp, status = self.get_users_by_email(True)
self._send_response(status, resp)
@ -501,6 +505,12 @@ class ActionsWebhookHandler(http.server.BaseHTTPRequestHandler):
response = self.req_json['input']['arg']
return response, HTTPStatus.OK
def mirror_headers(self):
response = {
'headers': list(map(lambda header: { 'name': header[0], 'value': header[1] }, self.headers.items()))
}
return response, HTTPStatus.OK
def get_users_by_email(self, singleUser = False):
email = self.req_json['input']['email']
if not self.check_email(email):

View File

@ -90,6 +90,18 @@ args:
- name: name
type: String
- name: Headers
fields:
- name: name
type: String!
- name: value
type: String!
- name: OutHeaders
fields:
- name: headers
type: '[Headers]'
- name: OutObjectTransformed
fields:
- name: foo
@ -275,6 +287,14 @@ args:
"bar": {{ $body.name }}
}
- type: create_action
args:
name: mirror_headers
definition:
type: query
output_type: OutHeaders
handler: http://127.0.0.1:5593/mirror-headers
- type: create_action
args:
name: get_user_by_email

View File

@ -20,6 +20,10 @@ args:
args:
name: mirror_transformed_output
clear_data: true
- type: drop_action
args:
name: mirror_headers
clear_data: true
- type: drop_action
args:
name: get_user_by_email

View File

@ -98,6 +98,33 @@ class TestActionsSync:
def test_mirror_action_transformed_output_success(self, hge_ctx):
check_query_f(hge_ctx, self.dir() + '/mirror_action_transformed_output_success.yaml')
def test_mirror_headers(self, hge_ctx):
query = """
query {
mirror_headers {
headers {
name
value
}
}
}
"""
query_obj = {
"query": query
}
headers = {}
admin_secret = hge_ctx.hge_key
if admin_secret is not None:
headers['X-Hasura-Admin-Secret'] = admin_secret
code, resp, _ = hge_ctx.anyq('/v1/graphql', query_obj, headers)
assert code == 200, resp
resp_headers = resp['data']['mirror_headers']['headers']
user_agent_header = next((h['value'] for h in resp_headers if h['name'] == 'User-Agent'), None)
assert user_agent_header is not None
assert user_agent_header.startswith("hasura-graphql-engine/")
def test_results_list_transformed_output_success(self, hge_ctx):
check_query_f(hge_ctx, self.dir() + '/results_list_transformed_output_success.yaml')