Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
700 changes: 6 additions & 694 deletions UnleashClient/__init__.py

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions UnleashClient/clients/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# ruff: noqa: F401
from .unleash_client import (
INSTANCES,
ExperimentalMode,
UnleashClient,
build_ready_callback,
)
704 changes: 704 additions & 0 deletions UnleashClient/clients/unleash_client.py

Large diffs are not rendered by default.

26 changes: 0 additions & 26 deletions UnleashClient/environment_resolver.py

This file was deleted.

27 changes: 26 additions & 1 deletion UnleashClient/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
from enum import Enum
from threading import RLock
from typing import Any
from typing import Any, Dict, Optional

import mmh3 # pylint: disable=import-error
from requests import Response
Expand Down Expand Up @@ -67,3 +67,28 @@ def log_resp_info(resp: Response) -> None:
LOGGER.debug("HTTP status code: %s", resp.status_code)
LOGGER.debug("HTTP headers: %s", resp.headers)
LOGGER.debug("HTTP content: %s", resp.text)


def extract_environment_from_headers(
headers: Optional[Dict[str, str]],
) -> Optional[str]:
if not headers:
return None

auth_key = next(
(key for key in headers if key.lower() == "authorization"),
None,
)
if not auth_key:
return None

auth_value = headers.get(auth_key)
if not auth_value:
return None

_, sep, after_colon = auth_value.partition(":")
if not sep:
return None

environment, _, _ = after_colon.partition(".")
return environment or None
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,7 @@ def test_uc_cache_bootstrap_file(cache):
test_file = Path(
Path(__file__).parent.resolve(),
"..",
"..",
"utilities",
"mocks",
"mock_bootstrap.json",
Expand Down Expand Up @@ -1041,6 +1042,34 @@ def test_multiple_instances_are_unique_on_api_key(caplog):
client2.destroy()


def test_redact_to_print_safely_truncates_middle():
api_key = "abcdef1234567890ghijklmnop"
redacted = UnleashClient._redact_to_print_safely(api_key)
assert redacted == "abcdef...nop"
assert api_key not in redacted


def test_api_key_is_not_logged_in_plain_text_in_multiple_instances_warning(caplog):
api_key = "abcdef1234567890ghijklmnop"
client1 = UnleashClient(
URL,
APP_NAME,
custom_headers={"Authorization": api_key},
)
client2 = UnleashClient(
URL,
APP_NAME,
custom_headers={"Authorization": api_key},
)

log_text = " ".join(str(r.msg) for r in caplog.records)
assert api_key not in log_text
assert "abcdef...nop" in log_text

client1.destroy()
client2.destroy()


@responses.activate
def test_signals_feature_flag(cache):
# Set up API
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from UnleashClient.environment_resolver import extract_environment_from_headers
from UnleashClient.utils import extract_environment_from_headers


def test_valid_headers():
Expand Down
Loading