File size: 1,114 Bytes
2f1e7ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
"""Resolve the deployment administrator credential once, without safe defaults."""

import logging
import os
import secrets
from functools import lru_cache


@lru_cache(maxsize=1)
def _warn_legacy_alias():
    logging.getLogger(__name__).warning(
        "BRAINPAT_TOKEN is deprecated; configure SYSTEM_PAT instead."
    )


def resolve_system_pat(environ=None) -> str | None:
    env = os.environ if environ is None else environ
    canonical = env.get("SYSTEM_PAT") or None
    legacy = env.get("BRAINPAT_TOKEN") or None
    if canonical and legacy and not secrets.compare_digest(canonical, legacy):
        raise ValueError("SYSTEM_PAT_CONFLICT: configure one administrator credential")
    if not canonical and legacy:
        _warn_legacy_alias()
    resolved = canonical or legacy
    if resolved == "brainpat_default_token":
        raise ValueError("SYSTEM_PAT_INVALID: default administrator credentials are forbidden")
    if not resolved and env.get("ENV", "production").lower() != "development":
        raise ValueError("SYSTEM_PAT_REQUIRED: configure an administrator credential")
    return resolved