Skip to content

Commit 2dad5fb

Browse files
committed
Only pass through the redirector headers when using the redirector service
1 parent 8df5efa commit 2dad5fb

File tree

3 files changed

+247
-464
lines changed

3 files changed

+247
-464
lines changed

domain/url/build_url.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from urllib.parse import urlparse, urlencode, urlunsplit
33

44
from domain.validation.argument_validation import ensure_string_not_empty
5+
from infrastructure.octopus import get_octopus_headers
56

67

78
def build_unredirected_url(base_url, path, query=None):
@@ -28,7 +29,7 @@ def build_unredirected_url(base_url, path, query=None):
2829
return urlunsplit((parsed.scheme, parsed.netloc, path, query, ""))
2930

3031

31-
def build_url(base_url, path, query=None):
32+
def build_url(base_url, api_key, path, query=None):
3233
"""
3334
Create a URL from the Octopus URL, additional path, and query params
3435
:param base_url: The Octopus URL
@@ -52,11 +53,24 @@ def build_url(base_url, path, query=None):
5253
os.environ.get("DISABLE_REDIRECTION", "false").lower() == "true"
5354
)
5455

56+
headers = get_octopus_headers(api_key) if api_key else {}
57+
58+
redirector_headers = {
59+
"X_REDIRECTION_SERVICE_API_KEY": os.environ.get(
60+
"REDIRECTION_SERVICE_APIKEY", "Unknown"
61+
),
62+
"X_REDIRECTION_UPSTREAM_HOST": parsed.hostname,
63+
}
64+
5565
if disable_redirector or is_octopus_cloud_local_or_example(parsed):
56-
return urlunsplit((parsed.scheme, parsed.netloc, path, query, ""))
66+
return urlunsplit((parsed.scheme, parsed.netloc, path, query, "")), headers
5767

5868
# For everyone else, we have to route requests through the redirection service
59-
return urlunsplit(("https", os.environ.get("REDIRECTION_HOST"), path, query, ""))
69+
headers.update(redirector_headers)
70+
return (
71+
urlunsplit(("https", os.environ.get("REDIRECTION_HOST"), path, query, "")),
72+
headers,
73+
)
6074

6175

6276
def is_octopus_cloud_local_or_example(url):

0 commit comments

Comments
 (0)