Skip to content

Conversation

@rolodato
Copy link
Contributor

This is the behaviour when running the Edge Proxy with no configuration:

% docker run --rm flagsmith/edge-proxy

2025-03-14T18:43:17.793837Z [info     ] Started server process [1]     [uvicorn.error]
2025-03-14T18:43:17.793915Z [info     ] Waiting for application startup. [uvicorn.error]
2025-03-14T18:43:17.795896Z [info     ] Application startup complete.  [uvicorn.error]
2025-03-14T18:43:17.796444Z [info     ] Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit) [uvicorn.error]
2025-03-14T18:43:18.033553Z [info     ] HTTP Request: GET https://edge.api.flagsmith.com/api/v1/environment-document/ "HTTP/1.1 401 Unauthorized" [httpx]
2025-03-14T18:43:18.034012Z [error    ] error_fetching_document        [edge_proxy.environments] client_side_key=environment_key
Traceback (most recent call last):
  File "/app/src/edge_proxy/environments.py", line 61, in refresh_environment_caches
    environment_document = await self._fetch_document(
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/src/edge_proxy/environments.py", line 150, in _fetch_document
    response.raise_for_status()
  File "/opt/venv/lib/python3.12/site-packages/httpx/_models.py", line 761, in raise_for_status
    raise HTTPStatusError(message, request=request, response=self)
httpx.HTTPStatusError: Client error '401 Unauthorized' for url 'https://edge.api.flagsmith.com/api/v1/environment-document/'
For more information check: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/401

On startup, we're always making an invalid request to fetch the environment document with ID environment_key. This is confusing and unnecessary.

Instead, we now validate environment key pairs using Pydantic so that any configuration errors immediately exit with a descriptive error. For example:

% rye run edge-proxy-serve                                                                            96.498s (feat/logging) 15:46
Traceback (most recent call last):
  File "/Users/rolodato/source/flagsmith/edge-proxy/.venv/bin/edge-proxy-serve", line 8, in <module>
    sys.exit(serve())
             ^^^^^^^
  File "/Users/rolodato/source/flagsmith/edge-proxy/src/edge_proxy/main.py", line 7, in serve
    settings = get_settings()
               ^^^^^^^^^^^^^^
  File "/Users/rolodato/source/flagsmith/edge-proxy/src/edge_proxy/settings.py", line 146, in get_settings
    return AppConfig()
           ^^^^^^^^^^^
  File "/Users/rolodato/source/flagsmith/edge-proxy/.venv/lib/python3.12/site-packages/pydantic_settings/main.py", line 84, in __init__
    super().__init__(
  File "/Users/rolodato/source/flagsmith/edge-proxy/.venv/lib/python3.12/site-packages/pydantic/main.py", line 176, in __init__
    self.__pydantic_validator__.validate_python(data, self_instance=self)
pydantic_core._pydantic_core.ValidationError: 2 validation errors for AppConfig
environment_key_pairs.0.server_side_key
  String should match pattern 'ser\.*' [type=string_pattern_mismatch, input_value='', input_type=str]
    For further information visit https://errors.pydantic.dev/2.7/v/string_pattern_mismatch
environment_key_pairs.0.client_side_key
  String should have at least 1 character [type=string_too_short, input_value='', input_type=str]
    For further information visit https://errors.pydantic.dev/2.7/v/string_too_short

@rolodato rolodato changed the title feat: Immediately exit if configuration is missing or valid feat: Immediately exit if configuration is missing or invalid Mar 14, 2025
@rolodato rolodato merged commit 30af01f into main Mar 17, 2025
2 checks passed
@rolodato rolodato deleted the feat/logging branch March 17, 2025 12:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants