| 1 |
""" |
| 2 |
Definition and loading of standard environment variables, plus a wrappers for loading |
| 3 |
and parsing values. |
| 4 |
|
| 5 |
This does not include service URL env vars (see environments.py for loading of those) |
| 6 |
""" |
| 7 |
|
| 8 |
from __future__ import annotations |
| 9 |
|
| 10 |
import logging |
| 11 |
import os |
| 12 |
import pathlib |
| 13 |
import typing as t |
| 14 |
|
| 15 |
log = logging.getLogger(__name__) |
| 16 |
|
| 17 |
ENVNAME_VAR = "GLOBUS_SDK_ENVIRONMENT" |
| 18 |
HTTP_TIMEOUT_VAR = "GLOBUS_SDK_HTTP_TIMEOUT" |
| 19 |
SSL_VERIFY_VAR = "GLOBUS_SDK_VERIFY_SSL" |
| 20 |
|
| 21 |
|
| 22 |
def get_environment_name(inputenv: str | None = None) -> str: |
| 23 |
if inputenv is None: |
| 24 |
value = os.getenv(ENVNAME_VAR, "production") |
| 25 |
else: |
| 26 |
value = inputenv |
| 27 |
log.debug(f"get_environment_name() got value: {value}") |
| 28 |
return value |
| 29 |
|
| 30 |
|
| 31 |
def get_ssl_verify(value: bool | str | pathlib.Path | None = None) -> bool | str: |
| 32 |
if value is None: |
| 33 |
value = os.getenv(SSL_VERIFY_VAR, "1") |
| 34 |
value = _ssl_verify_cast(value) |
| 35 |
log.debug(f"get_ssl_verify() got value: {value}") |
| 36 |
return value |
| 37 |
|
| 38 |
|
| 39 |
def get_http_timeout(value: float | None = None) -> float | None: |
| 40 |
if value is not None: |
| 41 |
result: float = value |
| 42 |
else: |
| 43 |
var = os.getenv(HTTP_TIMEOUT_VAR, "60") |
| 44 |
|
| 45 |
# TODO: re-evaluate converting `""` to a default timeout -- it seems like this |
| 46 |
# should error instead, but the behavior has been established for a long time, |
| 47 |
# so will need proper deprecation |
| 48 |
if var == "": |
| 49 |
result = 60.0 |
| 50 |
else: |
| 51 |
result = _float_cast(var) |
| 52 |
|
| 53 |
log.debug(f"get_http_timeout() got value: {result}") |
| 54 |
if result == -1.0: |
| 55 |
return None |
| 56 |
return result |
| 57 |
|
| 58 |
|
| 59 |
def _ssl_verify_cast(value: t.Any) -> bool | str: |
| 60 |
if isinstance(value, bool): |
| 61 |
return value |
| 62 |
if not isinstance(value, (str, pathlib.Path)): |
| 63 |
msg = f"Value {value} of type {type(value)} cannot be used for SSL verification" |
| 64 |
raise ValueError(msg) |
| 65 |
if isinstance(value, str): |
| 66 |
if value.lower() in {"y", "yes", "t", "true", "on", "1"}: |
| 67 |
return True |
| 68 |
if value.lower() in {"n", "no", "f", "false", "off", "0"}: |
| 69 |
return False |
| 70 |
if os.path.isfile(value): |
| 71 |
return value |
| 72 |
if isinstance(value, pathlib.Path) and value.is_file(): |
| 73 |
return str(value.absolute()) |
| 74 |
raise ValueError( |
| 75 |
"SSL verification value must be a valid boolean value " |
| 76 |
f"or a path to a file that exists (got {value})" |
| 77 |
) |
| 78 |
|
| 79 |
|
| 80 |
def _float_cast(value: str) -> float: |
| 81 |
try: |
| 82 |
return float(value) |
| 83 |
except ValueError as e: |
| 84 |
log.error(f'Value "{value}" can\'t cast to float') |
| 85 |
raise ValueError(f"Invalid config float: {value}") from e |