mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-24 00:52:24 +00:00
* fix(e2e-stack): print add-mask lines only under GitHub Actions * refactor(e2e-stack): inline the add-mask lines into main --------- Co-authored-by: mateo-berri <277851410+mateo-berri@users.noreply.github.com>
55 lines
1.8 KiB
Python
55 lines
1.8 KiB
Python
import os
|
|
import re
|
|
import sys
|
|
from pathlib import Path
|
|
from typing import Final
|
|
|
|
from pydantic import TypeAdapter, ValidationError
|
|
|
|
secrets_adapter: Final[TypeAdapter[dict[str, str]]] = TypeAdapter(dict[str, str])
|
|
ENV_NAME: Final = re.compile(r"[A-Za-z_][A-Za-z0-9_]*")
|
|
MIN_MASKED_LENGTH: Final = 8
|
|
ACTIONS_RUNNER_FLAG: Final = "GITHUB_ACTIONS"
|
|
|
|
|
|
def main() -> int:
|
|
env_path: Final = Path(sys.argv[1])
|
|
try:
|
|
secrets: Final = {
|
|
key: value.rstrip("\r\n") for key, value in secrets_adapter.validate_json(sys.stdin.read()).items()
|
|
}
|
|
except (ValidationError, UnicodeError):
|
|
_ = sys.stderr.write("expected a JSON object containing string environment values\n")
|
|
return 1
|
|
unusable: Final = tuple(
|
|
key
|
|
for key, value in secrets.items()
|
|
if ENV_NAME.fullmatch(key) is None or any(char in value for char in "'\n\r\0")
|
|
)
|
|
if unusable:
|
|
_ = sys.stderr.write(
|
|
f"these names or values cannot be represented in both bash and dotenv: {' '.join(sorted(unusable))}\n"
|
|
)
|
|
return 1
|
|
if os.environ.get(ACTIONS_RUNNER_FLAG) == "true":
|
|
_ = sys.stdout.write(
|
|
"".join(
|
|
f"::add-mask::{value.replace('%', '%25')}\n"
|
|
for value in secrets.values()
|
|
if len(value) >= MIN_MASKED_LENGTH
|
|
)
|
|
)
|
|
sys.stdout.flush()
|
|
lines: Final = tuple(f"{key}='{value}'" for key, value in secrets.items() if value)
|
|
try:
|
|
with os.fdopen(os.open(env_path, os.O_WRONLY | os.O_APPEND | os.O_CREAT | os.O_NOFOLLOW, 0o600), "w") as handle:
|
|
os.fchmod(handle.fileno(), 0o600)
|
|
_ = handle.write("\n".join(lines) + "\n")
|
|
except OSError:
|
|
_ = sys.stderr.write("could not write the environment file\n")
|
|
return 1
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|