Skip to content

tdfs4ds.serve.environments

tdfs4ds.serve.environments

Environments of the gateway (0.3.8.2): the Teradata systems a person can log on to.

Two sources, one list:

  • the administrator's file (TDFS4DS_SERVE_ENVIRONMENTS, default /config/environments.yaml, or .json): read-only here, never rewritten;
  • the remembered environments (/state/environments.json, TDFS4DS_SERVE_ENVIRONMENTS_STATE): added by a person who typed a "New environment" on the login page and logged on to it successfully. The file holds the label, host, logon mechanism, encryption flag and database; never a user name's password or token.

With neither file, one environment default comes from TDFS4DS_TD_HOST / _LOGMECH / _ENCRYPT / _DATABASE.

Entry schema of the administrator's file (a list, or {"environments": [...]})::

id                    slug, required, unique
label                 shown on the login page (default: id)
host                  required
logmechs              subset of TD2 / LDAP / TDNEGO / JWT (default ["TD2"])
encrypt               bool (default true)
database              the feature store's database (optional)
databases_readonly    list (optional)
chroma                "server" | "local" (optional)
env                   extra settings of the worker: ``TDFS4DS_X`` or ``X``; a registered
                      variable, never a secret
min_profile           analyst | scientist | engineer | admin: everybody who logs on here gets at
                      least this profile (the roles can still grant a higher one). It decides
                      which pages and actions the app offers; the database rights of the
                      person's own login are unchanged
api_key_var           NAME of the environment variable holding the bearer key of /v1
service_user_var / service_password_var
                      NAMES of the variables holding the service account of /v1

logger module-attribute

logger = logging.getLogger('tdfs4ds.serve')

LOGMECHS module-attribute

LOGMECHS = ('TD2', 'LDAP', 'TDNEGO', 'JWT')

DEFAULT_FILES module-attribute

DEFAULT_FILES = ('/config/environments.yaml', '/config/environments.yml', '/config/environments.json')

DEFAULT_STATE module-attribute

DEFAULT_STATE = '/state/environments.json'

PROFILES module-attribute

PROFILES = ('analyst', 'scientist', 'engineer', 'admin')

_SLUG module-attribute

_SLUG = re.compile('^[A-Za-z0-9][A-Za-z0-9_\\-]{0,63}$')

_HOST module-attribute

_HOST = re.compile('^[A-Za-z0-9][A-Za-z0-9.\\-_:]{0,252}$')

_KNOWN_KEYS module-attribute

_KNOWN_KEYS = {'id', 'label', 'host', 'logmechs', 'encrypt', 'database', 'databases_readonly', 'chroma', 'env', 'min_profile', 'api_key_var', 'service_user_var', 'service_password_var'}

EnvironmentsError

Bases: ValueError

The environments file cannot be used. The message names the file.

Environment dataclass

id instance-attribute

id: str

host instance-attribute

host: str

label class-attribute instance-attribute

label: str = ''

logmechs class-attribute instance-attribute

logmechs: List[str] = field(default_factory=lambda: ['TD2'])

encrypt class-attribute instance-attribute

encrypt: bool = True

database class-attribute instance-attribute

database: Optional[str] = None

databases_readonly class-attribute instance-attribute

databases_readonly: List[str] = field(default_factory=list)

chroma class-attribute instance-attribute

chroma: Optional[str] = None

env class-attribute instance-attribute

env: dict = field(default_factory=dict)

min_profile class-attribute instance-attribute

min_profile: Optional[str] = None

api_key_var class-attribute instance-attribute

api_key_var: Optional[str] = None

service_user_var class-attribute instance-attribute

service_user_var: Optional[str] = None

service_password_var class-attribute instance-attribute

service_password_var: Optional[str] = None

source class-attribute instance-attribute

source: str = 'file'

added_by class-attribute instance-attribute

added_by: str = ''

added_at class-attribute instance-attribute

added_at: str = ''

last_used_at class-attribute instance-attribute

last_used_at: str = ''

__init__

__init__(id: str, host: str, label: str = '', logmechs: List[str] = (lambda: ['TD2'])(), encrypt: bool = True, database: Optional[str] = None, databases_readonly: List[str] = list(), chroma: Optional[str] = None, env: dict = dict(), min_profile: Optional[str] = None, api_key_var: Optional[str] = None, service_user_var: Optional[str] = None, service_password_var: Optional[str] = None, source: str = 'file', added_by: str = '', added_at: str = '', last_used_at: str = '') -> None

__post_init__

__post_init__()

public

public() -> dict

What the login page may see (no variable names, no extra settings).

EnvironmentRegistry

The administrator's environments plus the remembered ones.

The remembered file is re-read when its modification time changes; a file that cannot be parsed is moved to <file>.bad, error says so, and the list is empty.

admin instance-attribute

admin = list(admin)

state_path instance-attribute

state_path = state_path or os.environ.get('TDFS4DS_SERVE_ENVIRONMENTS_STATE') or DEFAULT_STATE

error instance-attribute

error: Optional[str] = None

__init__

__init__(admin: List[Environment], state_path: Optional[str] = None)

list

list() -> List[Environment]

Administrator's entries first, then the remembered ones (most recently used first); a remembered entry for a target the administrator already offers is hidden.

get

get(env_id: str) -> Optional[Environment]

remember

remember(*, label: str, host: str, logmech: str, encrypt: bool, database: Optional[str], added_by: str) -> Environment

Record an environment a person just logged on to. Returns the environment (the administrator's when it offers the same target, the remembered one otherwise).

touch

touch(env_id: str) -> None

forget

forget(env_id: str) -> bool

Remove a remembered environment. An administrator's entry cannot be forgotten.

_registered

_registered(name: str)

(attr, known) for a setting named TDFS4DS_X or X.

_entries

_entries(data)

validate_environments

validate_environments(data) -> List[str]

Every problem of the file content, all at once (an empty list = usable).

_build

_build(item: dict, source: str) -> Environment

_read_file

_read_file(path: str)

default_environment

default_environment() -> Optional[Environment]

environments_path

environments_path(path=None) -> Optional[str]

The file to read: the argument, else TDFS4DS_SERVE_ENVIRONMENTS, else the first default that exists. An explicit path that does not exist is an error (None only when there is nothing configured and no default exists).

load_environments

load_environments(path=None) -> List[Environment]

The administrator's environments (or the single default one). Raises :class:EnvironmentsError, naming the file, when the file is unusable.

_now_iso

_now_iso() -> str

slugify

slugify(text: str) -> str

_same_target

_same_target(a_host, a_db, b_host, b_db) -> bool

_flag

_flag(name: str, default: bool) -> bool

allow_new_environment

allow_new_environment() -> bool

remember_environments

remember_environments() -> bool