rsnarsna
feat(alli-backend): Wave-1 standardization, standalone sys.env node & vault crypto, and DEC-014 security hardening
8628c98 Download lambdas/workspace_env_handler.py from Rsnarsna/auto: direct link, hf CLI and curl.
- Browser
- Download file 2.77 kB
-
https://huggingface.co/spaces/Rsnarsna/auto/resolve/main/lambdas/workspace_env_handler.py
- Command line
-
hf download hf://spaces/Rsnarsna/auto/lambdas/workspace_env_handler.py
-
curl -L -o workspace_env_handler.py https://huggingface.co/spaces/Rsnarsna/auto/resolve/main/lambdas/workspace_env_handler.py
2.77 kB
| """ | |
| ALLI Lambda Handler: Workspace Environment Variable Management | |
| Provides pure business logic for masked list, validated encrypted upsert, | |
| delete, and explicit secret reveal without leaking plaintext to audit trails. | |
| """ | |
| from typing import Any, Dict, List | |
| from database.mongo import workflow_repo | |
| from language_core.workspace_env_contracts import ( | |
| ENV_MAX_KEYS_PER_ORG, | |
| ENV_MAX_VALUE_BYTES, | |
| validate_env_key, | |
| validate_env_value, | |
| ) | |
| from security.env_crypto import is_encryption_key_configured | |
| def list_workspace_env_logic(org_id: str) -> Dict[str, Any]: | |
| """List masked environment variables and constraints for a workspace.""" | |
| items = workflow_repo.list_workspace_env(org_id) | |
| return { | |
| "org_id": org_id, | |
| "items": items, | |
| "count": len(items), | |
| "limits": { | |
| "max_keys": ENV_MAX_KEYS_PER_ORG, | |
| "max_value_bytes": ENV_MAX_VALUE_BYTES, | |
| "encryption_key_configured": is_encryption_key_configured(), | |
| }, | |
| } | |
| def upsert_workspace_env_logic( | |
| org_id: str, | |
| raw_key: Any, | |
| raw_value: Any, | |
| *, | |
| is_secret: bool = True, | |
| description: str = "", | |
| actor_user_id: str = "", | |
| ) -> Dict[str, Any]: | |
| """Validate key/value, enforce workspace quota, and persist encrypted secret.""" | |
| ok_key, clean_key_or_code, key_err = validate_env_key(raw_key) | |
| if not ok_key: | |
| raise ValueError(f"[{clean_key_or_code}] {key_err}") | |
| ok_val, clean_val_or_code, val_err = validate_env_value(raw_value) | |
| if not ok_val: | |
| raise ValueError(f"[{clean_val_or_code}] {val_err}") | |
| entry = workflow_repo.upsert_workspace_env( | |
| org_id=org_id, | |
| key=clean_key_or_code, | |
| plaintext_value=clean_val_or_code, | |
| is_secret=is_secret, | |
| description=description, | |
| actor_user_id=actor_user_id, | |
| ) | |
| return {"success": True, "item": entry} | |
| def delete_workspace_env_logic(org_id: str, raw_key: Any) -> Dict[str, Any]: | |
| """Validate key syntax and delete workspace env entry.""" | |
| ok_key, clean_key_or_code, key_err = validate_env_key(raw_key) | |
| if not ok_key: | |
| raise ValueError(f"[{clean_key_or_code}] {key_err}") | |
| deleted = workflow_repo.delete_workspace_env(org_id, clean_key_or_code) | |
| return {"success": deleted, "key": clean_key_or_code} | |
| def reveal_workspace_env_logic(org_id: str, raw_key: Any) -> Dict[str, Any]: | |
| """Reveal a single workspace variable value explicitly for authorized roles.""" | |
| ok_key, clean_key_or_code, key_err = validate_env_key(raw_key) | |
| if not ok_key: | |
| raise ValueError(f"[{clean_key_or_code}] {key_err}") | |
| entry = workflow_repo.reveal_workspace_env(org_id, clean_key_or_code) | |
| if entry is None: | |
| raise KeyError(clean_key_or_code) | |
| return entry | |