From 4220199a86a05066752eeae4f2adc7337fefdb77 Mon Sep 17 00:00:00 2001 From: Joey Orlando Date: Thu, 25 Jan 2024 14:41:35 -0500 Subject: [PATCH] cast X-Realms header to jsonified string --- engine/common/cloud_auth_api/client.py | 16 ++++++++++------ .../common/cloud_auth_api/tests/test_client.py | 15 +++++++++------ 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/engine/common/cloud_auth_api/client.py b/engine/common/cloud_auth_api/client.py index 193051a8..12b86961 100644 --- a/engine/common/cloud_auth_api/client.py +++ b/engine/common/cloud_auth_api/client.py @@ -1,4 +1,5 @@ import enum +import json import typing from urllib.parse import urljoin @@ -43,17 +44,20 @@ class CloudAuthApiClient: org_id = org.org_id stack_id = org.stack_id + # NOTE: header values must always be strings headers = { "Authorization": f"Bearer {self.api_token}", # need to cast to str otherwise - requests.exceptions.InvalidHeader: Header part ... from ('X-Org-ID', 5000) # must be of type str or bytes, not "X-Org-ID": str(org_id), - "X-Realms": [ - { - "type": "stack", - "identifier": str(stack_id), - }, - ], + "X-Realms": json.dumps( + [ + { + "type": "stack", + "identifier": str(stack_id), + }, + ] + ), } url = urljoin(self.api_base_url, "v1/sign") diff --git a/engine/common/cloud_auth_api/tests/test_client.py b/engine/common/cloud_auth_api/tests/test_client.py index 8c0a9bd1..2a10626c 100644 --- a/engine/common/cloud_auth_api/tests/test_client.py +++ b/engine/common/cloud_auth_api/tests/test_client.py @@ -1,3 +1,4 @@ +import json from unittest.mock import patch import pytest @@ -66,12 +67,14 @@ def test_request_signed_token(mock_requests, make_organization, response_status_ headers={ "Authorization": f"Bearer {GRAFANA_CLOUD_AUTH_API_SYSTEM_TOKEN}", "X-Org-ID": str(org_id), - "X-Realms": [ - { - "type": "stack", - "identifier": str(stack_id), - }, - ], + "X-Realms": json.dumps( + [ + { + "type": "stack", + "identifier": str(stack_id), + }, + ] + ), }, json={ "claims": claims,