isa-model 0.4.3__py3-none-any.whl → 0.4.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
isa_model/core/config.py CHANGED
@@ -54,7 +54,7 @@ class LocalGPUGlobalConfig:
54
54
  enable_local_gpu: bool = True
55
55
  auto_detect_gpu: bool = True
56
56
  workspace_dir: str = "./local_deployments"
57
- preferred_backend: str = "transformers" # vllm, tensorrt_llm, transformers
57
+ preferred_backend: str = "api" # cloud api only
58
58
 
59
59
  # Default resource settings
60
60
  default_gpu_memory_fraction: float = 0.9
@@ -114,7 +114,7 @@ class GlobalConfig:
114
114
  local_gpu_memory_fraction: float = 0.9
115
115
  local_workspace_dir: str = "./local_deployments"
116
116
  auto_detect_gpu: bool = True
117
- preferred_local_backend: str = "transformers" # vllm, tensorrt_llm, transformers
117
+ preferred_local_backend: str = "api" # cloud api only
118
118
 
119
119
  # Local service defaults
120
120
  local_health_check_interval: int = 30 # seconds
@@ -263,7 +263,7 @@ class ConfigManager:
263
263
  "enable_local_gpu": os.getenv("ISA_ENABLE_LOCAL_GPU", "true").lower() == "true",
264
264
  "auto_detect_gpu": os.getenv("ISA_AUTO_DETECT_GPU", "true").lower() == "true",
265
265
  "workspace_dir": os.getenv("ISA_LOCAL_WORKSPACE_DIR", "./local_deployments"),
266
- "preferred_backend": os.getenv("ISA_PREFERRED_LOCAL_BACKEND", "transformers"),
266
+ "preferred_backend": os.getenv("ISA_PREFERRED_LOCAL_BACKEND", "api"),
267
267
  "default_gpu_memory_fraction": float(os.getenv("ISA_GPU_MEMORY_FRACTION", "0.9")),
268
268
  "health_check_interval": int(os.getenv("ISA_LOCAL_HEALTH_CHECK_INTERVAL", "30")),
269
269
  "max_concurrent_services": int(os.getenv("ISA_MAX_CONCURRENT_SERVICES", "3")),
@@ -2,8 +2,6 @@ from typing import Dict, Optional, List, Any
2
2
  import logging
3
3
  from pathlib import Path
4
4
  from datetime import datetime
5
- from huggingface_hub import hf_hub_download, snapshot_download
6
- from huggingface_hub.errors import HfHubHTTPError
7
5
  from .model_storage import ModelStorage, LocalModelStorage
8
6
  from .model_repo import ModelRegistry, ModelType, ModelCapability
9
7
  from .model_billing_tracker import ModelBillingTracker, ModelOperationType
@@ -102,73 +100,7 @@ class ModelManager:
102
100
  logger.warning(f"Failed to find cheapest model for {provider}: {e}")
103
101
  return None
104
102
 
105
- async def get_model(self,
106
- model_id: str,
107
- repo_id: str,
108
- model_type: ModelType,
109
- capabilities: List[ModelCapability],
110
- revision: Optional[str] = None,
111
- force_download: bool = False) -> Optional[Path]:
112
- """
113
- Get model files, downloading if necessary
114
-
115
- Args:
116
- model_id: Unique identifier for the model
117
- repo_id: Hugging Face repository ID
118
- model_type: Type of model (LLM, embedding, etc.)
119
- capabilities: List of model capabilities
120
- revision: Specific model version/tag
121
- force_download: Force re-download even if cached
122
-
123
- Returns:
124
- Path to the model files or None if failed
125
- """
126
- # Check if model is already downloaded
127
- if not force_download:
128
- model_path = await self.storage.load_model(model_id)
129
- if model_path:
130
- logger.info(f"Using cached model {model_id}")
131
- return model_path
132
-
133
- try:
134
- # Download model files
135
- logger.info(f"Downloading model {model_id} from {repo_id}")
136
- model_dir = Path(f"./models/temp/{model_id}")
137
- model_dir.mkdir(parents=True, exist_ok=True)
138
-
139
- snapshot_download(
140
- repo_id=repo_id,
141
- revision=revision,
142
- local_dir=model_dir,
143
- local_dir_use_symlinks=False
144
- )
145
-
146
- # Save model and metadata
147
- metadata = {
148
- "repo_id": repo_id,
149
- "revision": revision,
150
- "downloaded_at": str(Path(model_dir).stat().st_mtime)
151
- }
152
-
153
- # Register model
154
- self.registry.register_model(
155
- model_id=model_id,
156
- model_type=model_type,
157
- capabilities=capabilities,
158
- metadata=metadata
159
- )
160
-
161
- # Save model files
162
- await self.storage.save_model(model_id, str(model_dir), metadata)
163
-
164
- return await self.storage.load_model(model_id)
165
-
166
- except HfHubHTTPError as e:
167
- logger.error(f"Failed to download model {model_id}: {e}")
168
- return None
169
- except Exception as e:
170
- logger.error(f"Unexpected error downloading model {model_id}: {e}")
171
- return None
103
+ # Local model download functionality removed - use cloud API services only
172
104
 
173
105
  async def list_models(self) -> List[Dict[str, Any]]:
174
106
  """List all downloaded models with their metadata"""
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: isa_model
3
- Version: 0.4.3
3
+ Version: 0.4.4
4
4
  Summary: Unified AI model serving framework
5
5
  Author: isA_Model Contributors
6
6
  Classifier: Development Status :: 3 - Alpha
@@ -89,6 +89,11 @@ Requires-Dist: isa-model[cloud,k8s,monitoring,storage]; extra == "production"
89
89
  Provides-Extra: staging
90
90
  Requires-Dist: isa-model[cloud,langchain,monitoring,storage]; extra == "staging"
91
91
  Requires-Dist: python-consul>=1.1.0; extra == "staging"
92
+ Provides-Extra: staging-minimal
93
+ Requires-Dist: isa-model[cloud,langchain,storage]; extra == "staging-minimal"
94
+ Requires-Dist: influxdb-client>=1.36.0; extra == "staging-minimal"
95
+ Requires-Dist: python-logging-loki>=0.3.1; extra == "staging-minimal"
96
+ Requires-Dist: python-consul>=1.1.0; extra == "staging-minimal"
92
97
  Provides-Extra: all
93
98
  Requires-Dist: isa-model[audio,cloud,gpu-cloud,k8s,langchain,local,monitoring,storage,training,vision]; extra == "all"
94
99
 
@@ -1,6 +1,6 @@
1
1
  isa_model/__init__.py,sha256=lYYKstKw33oavW6xS0-9cpsdYq-h0cfV_ZlGAwICRaU,868
2
2
  isa_model/client.py,sha256=7OUA6yi0G94I8U4GOFcoXvCjWxk2ds1-3CDCivJXA6M,67628
3
- isa_model/core/config.py,sha256=9OL8_EkBcnAH-RgyWUi3jblKo42m7K1JDeHa9C5CPL4,19519
3
+ isa_model/core/config.py,sha256=EWadZRo3LY2gZzL81MnqK-39lUDhaFtLiVE4aoEdXZI,19456
4
4
  isa_model/core/dependencies.py,sha256=2ZgGDjtYitBEVy8H3UppQSb_BId3--f2kQw-Lm4Umh8,10050
5
5
  isa_model/core/pricing_manager.py,sha256=NWQLhNIzUDqS5_jBfVcJGrdOdRasFyifSNCliaIDvqU,17122
6
6
  isa_model/core/types.py,sha256=jdO_q0FDuzvWURXZtxMV1Zj1XgARX9kopTviWFuq_FU,8713
@@ -21,7 +21,7 @@ isa_model/core/models/__init__.py,sha256=bDzyE0KHIJxxoN3q08pvW_hHBeHux2aMdeKY4Gl
21
21
  isa_model/core/models/config_models.py,sha256=Gy0oNndDsKVQ92Bz6aZp1JMqr4Z0jdoEJCCscIrpogI,22955
22
22
  isa_model/core/models/deployment_billing_tracker.py,sha256=hW2e3vUkASvCycYhBdrZcsEtBjYo3TLTVpfwfVkaXnQ,16406
23
23
  isa_model/core/models/model_billing_tracker.py,sha256=er35dsoKAGt8bjkQwO9f3MQ6U_NI6OIuhIn4PEOPEWU,17302
24
- isa_model/core/models/model_manager.py,sha256=vjFYWxmhRGUDlrHkwySjN5d0hfCx-VqBKiMhgTXjs8Y,17093
24
+ isa_model/core/models/model_manager.py,sha256=N_Y3kRRfV_8MPIkfNz1aKcSW_j6tDvTJ2rXRPDrd31U,14611
25
25
  isa_model/core/models/model_metadata.py,sha256=C6ubW12qmXAuqKmE_2BaO4HMB7WJqBkYDVXYCINsc-4,25148
26
26
  isa_model/core/models/model_repo.py,sha256=1018Qi8fMfgXtU8DKfWvf-0hBE16Q7wJIzmEZa7x6bw,19868
27
27
  isa_model/core/models/model_statistics_tracker.py,sha256=4KoKawwtEDAx8FV9ysmZS4nvRqZAgRSSIa-32f_Jhwk,10561
@@ -31,22 +31,12 @@ isa_model/core/models/system_models.py,sha256=I52nTi0UVft8tkJdb2LZrJ_Qxax-JE00_Y
31
31
  isa_model/core/repositories/__init__.py,sha256=RRERY7mWZxhSAZa4m6493l6sFl3CPlyL2bW6qJMEzD8,172
32
32
  isa_model/core/repositories/config_repository.py,sha256=QlL22r_bGEV6mHfmztEIY5Zw3wIFoiR5IQJyIj36wXU,37428
33
33
  isa_model/core/resilience/circuit_breaker.py,sha256=Ccoh3O31xVFJO2A0flnc9SI-sRqQ3sGKbwv3WbgJxBc,12435
34
- isa_model/core/security/secrets.py,sha256=kzRjpSiGwY9z47NUlurK29uY_uMsA5lqk8_6Ywu8Zvw,13319
35
34
  isa_model/core/services/__init__.py,sha256=TEE58Vk8JKIaQx8ELeAaWo-WPz0hjck9x-ZK7pbfiIE,422
36
35
  isa_model/core/services/intelligent_model_selector.py,sha256=PPUWiMcV8DkCPMHhnIlsBgksUY8hKB4SjlFDW1zhLYY,29205
37
- isa_model/core/storage/hf_storage.py,sha256=k271Rg5G7qUJAJ6VXQBTUHGU6y2NYBNWKVeBJm02DRo,14736
38
36
  isa_model/core/storage/local_storage.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
39
37
  isa_model/deployment/__init__.py,sha256=Y3IUEOriJYVZ-3ZEamMs4n6_X0OwtD2eguwBas5zgtg,345
40
38
  isa_model/deployment/core/__init__.py,sha256=TRJ4tNjNFub_ObhZy61iZZpqC0CYsnv1HV1Qp_XWhVI,119
41
39
  isa_model/deployment/core/deployment_manager.py,sha256=In2e5EuXwVJfg6ENjigOrgQyy19DCWX5uuwSa--Czzc,58337
42
- isa_model/deployment/local/__init__.py,sha256=Ld1QbaDHIHnbW2IkSXVTZeDcxnmUXBa074uOSLRu5t0,904
43
- isa_model/deployment/local/config.py,sha256=Kft5EORBcsO2HVizD0ct6VFIIs9sVBN-CjRnOrcm00g,9246
44
- isa_model/deployment/local/gpu_gateway.py,sha256=be6d9eSWRufXvJH9GyklBWlXhOukITY1lnXTM6RPcQs,21954
45
- isa_model/deployment/local/health_checker.py,sha256=_u2vwiwEGCbFA6laUu1JX6GfE6prrOSHiy6PclimGys,16392
46
- isa_model/deployment/local/provider.py,sha256=6E1WfTUvI32aeEQs13TIyuy9xQmZqJeaJULfE2KLe4E,22763
47
- isa_model/deployment/local/tensorrt_service.py,sha256=f05BkJMw2NhiMp18xW1RwRED4bIjZ0gmUS5OgEAGnk0,23026
48
- isa_model/deployment/local/transformers_service.py,sha256=pdC3KppUzSVrWd-CKA8fXPC1uzy45S8FTtQj9odAWpM,23937
49
- isa_model/deployment/local/vllm_service.py,sha256=zVuBopgzG6ulSvHnRE8h_dLQQpNqTDwHbXo88IKXrwk,18849
50
40
  isa_model/deployment/modal/__init__.py,sha256=ZBcToGNtL6ztWY5pvqM1YMiL_F-S1xx9b-uZd8cuajc,380
51
41
  isa_model/deployment/modal/config.py,sha256=8XhBMIbx6oDTf-P9ipQ58xmBYDbNZekZ4gixorBDIpw,4267
52
42
  isa_model/deployment/modal/deployer.py,sha256=YNCtbO8FTVstz8OG6Kh8p3AM05dtbg5i73-JsuNy4KM,31961
@@ -98,7 +88,6 @@ isa_model/inference/repositories/__init__.py,sha256=SYTQX1E5L6zTuo_p_KnDjYefoCKw
98
88
  isa_model/inference/repositories/inference_repository.py,sha256=QnfSzkcLQ5CPcABTmSYBRAv_5SVk0ayjVW6B1Q0SKaQ,31718
99
89
  isa_model/inference/services/__init__.py,sha256=yfLz0YGl8ixk6LfTRL6cRTvZMb9F_Pv1QRgGyNc9xYM,386
100
90
  isa_model/inference/services/base_service.py,sha256=NJIvq7YpGw55ah-axDR2hcu40B2gm6L_WYXyfX0rSaE,5816
101
- isa_model/inference/services/custom_model_manager.py,sha256=HUHSDOWArJYMfdvaI-gfCJkVRVFdftScOw7BgS-h3zo,10829
102
91
  isa_model/inference/services/audio/__init__.py,sha256=Hgtk3j5H4U3YxNlfG8UaU2eUNOWgrpSA8LN_tKEFWMk,616
103
92
  isa_model/inference/services/audio/base_realtime_service.py,sha256=hSP89_hnzLBnmBvFOQlU_tW8UT2QKWKVR9Z7fwsVPa8,8125
104
93
  isa_model/inference/services/audio/base_stt_service.py,sha256=qahYTLpf8gruvhEtS5bWDXPiYbgxXF3nYnqTq3Ckc0E,13361
@@ -128,7 +117,6 @@ isa_model/inference/services/llm/__init__.py,sha256=aiNdB692nopCy8QaopVDB0e-J7mg
128
117
  isa_model/inference/services/llm/base_llm_service.py,sha256=CUw24rMytVXAUcberTzogKRLwYZiYifyg-kcNfHiHkg,36402
129
118
  isa_model/inference/services/llm/cerebras_llm_service.py,sha256=8BU9I7HHO481nn7ddsiP4nl2ItYTCQJzJyaIArKA0ZA,25126
130
119
  isa_model/inference/services/llm/huggingface_llm_service.py,sha256=mWnOGh3OsRyaL002Ax71Mb7oXp254VDDdP0QiQ-p9Yk,22733
131
- isa_model/inference/services/llm/local_llm_service.py,sha256=_ILRD-oKcolf972aXe3zPS_tBu8SD-xH_Iw29alpkHM,27606
132
120
  isa_model/inference/services/llm/ollama_llm_service.py,sha256=78VNSspzlQrXDqAxUR52jLGIKnBw4e_4LT2unAFMiTk,17967
133
121
  isa_model/inference/services/llm/openai_llm_service.py,sha256=BpYugS2Vsrc-SS69cnW2VqFv4JXMbgglXvvbNgUZNZY,43874
134
122
  isa_model/inference/services/llm/yyds_llm_service.py,sha256=ZHl2ukcDVkwYahF4OV5etTvJKa9Ni6O1TkJp75pQWaA,12495
@@ -139,7 +127,6 @@ isa_model/inference/services/ml/base_ml_service.py,sha256=mLBA6ENowa3KVzNqHyhWxf
139
127
  isa_model/inference/services/ml/sklearn_ml_service.py,sha256=Lf9JrwvI25lca7JBbjB_e66eAUtXFbwxZ3Hs13dVGkA,5512
140
128
  isa_model/inference/services/vision/__init__.py,sha256=1GO2NoC7p8IJ92mI6fGcPaN4MeFzLhdNdNlAnFYpzpE,1839
141
129
  isa_model/inference/services/vision/base_vision_service.py,sha256=mjrfcUT01HBi0k1qeIL3CkpkvQIuL_jar-N03W8sMV8,10531
142
- isa_model/inference/services/vision/blip_vision_service.py,sha256=tmGCznQ9qBsidLV_mnKEtvpSUPvBUbwJdwviKYXrrkA,12020
143
130
  isa_model/inference/services/vision/isa_vision_service.py,sha256=OPuIZmG_lYOgajGfrQj4uLzVk5Y4H0PkeSNViIiL1O0,22960
144
131
  isa_model/inference/services/vision/openai_vision_service.py,sha256=LeD910WWyJd6QiJncSM3x_Whj-a32Vr1_2FG4gfjtc4,10179
145
132
  isa_model/inference/services/vision/replicate_vision_service.py,sha256=smRkSCTwk5mvyKVnvyplqPNuVYjRZngVBWxTCbFmrxA,20679
@@ -187,7 +174,7 @@ isa_model/serving/api/schemas/__init__.py,sha256=Tu_hzxoKW1ZHpww3-5ER4A2hNuDByZ0
187
174
  isa_model/serving/api/schemas/common.py,sha256=HVaAS7wlvqrwC1gMZ2Cvo0vzHB053x2uOTAwUoY2vsE,696
188
175
  isa_model/serving/api/schemas/ui_analysis.py,sha256=IpOcIvmUeXN1UtZsbGozMfV1vvz7AVF2PVXjjxYl_0k,4089
189
176
  isa_model/utils/gpu_utils.py,sha256=HbMvJzSsOCcjOJluUrszAJ58dC8LPnyA_nQn9s_1I6c,11730
190
- isa_model-0.4.3.dist-info/METADATA,sha256=2xHn4pAvo10QKBDQK34v0-acBWGBUZRR6ZUZa-xq7BU,15090
191
- isa_model-0.4.3.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
192
- isa_model-0.4.3.dist-info/top_level.txt,sha256=eHSy_Xb3kNkh2kK11mi1mZh0Wz91AQ5b8k2KFYO-rE8,10
193
- isa_model-0.4.3.dist-info/RECORD,,
177
+ isa_model-0.4.4.dist-info/METADATA,sha256=NsdzSpZFwbR_wBFQQ_9vZhPiPA15ObFLHuJvxXr0jow,15401
178
+ isa_model-0.4.4.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
179
+ isa_model-0.4.4.dist-info/top_level.txt,sha256=eHSy_Xb3kNkh2kK11mi1mZh0Wz91AQ5b8k2KFYO-rE8,10
180
+ isa_model-0.4.4.dist-info/RECORD,,
@@ -1,358 +0,0 @@
1
- """
2
- Secrets Management System
3
-
4
- Provides secure handling of API keys, tokens, and other sensitive data.
5
- Supports multiple backends: environment variables, HashiCorp Vault, AWS Secrets Manager.
6
- """
7
-
8
- import os
9
- import json
10
- import logging
11
- import hashlib
12
- import base64
13
- from typing import Dict, Optional, Any, List
14
- from pathlib import Path
15
- from cryptography.fernet import Fernet
16
- from cryptography.hazmat.primitives import hashes
17
- from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
18
- import structlog
19
-
20
- logger = structlog.get_logger(__name__)
21
-
22
- class SecretsManager:
23
- """Unified secrets management with multiple backend support"""
24
-
25
- def __init__(self, backend: str = "env", **kwargs):
26
- self.backend = backend
27
- self.config = kwargs
28
- self._cache = {}
29
- self._encryption_key = None
30
-
31
- # Initialize encryption key for local storage
32
- self._init_encryption()
33
-
34
- # Initialize backend
35
- if backend == "vault":
36
- self._init_vault()
37
- elif backend == "aws":
38
- self._init_aws()
39
- elif backend == "env":
40
- self._init_env()
41
- else:
42
- raise ValueError(f"Unsupported secrets backend: {backend}")
43
-
44
- logger.info("Secrets manager initialized", backend=backend)
45
-
46
- def _init_encryption(self):
47
- """Initialize encryption for local secret storage"""
48
- # Use a combination of environment and system info for key derivation
49
- password = os.getenv("SECRET_ENCRYPTION_KEY", "default-key-change-in-production").encode()
50
- salt = os.getenv("SECRET_SALT", "default-salt").encode()
51
-
52
- kdf = PBKDF2HMAC(
53
- algorithm=hashes.SHA256(),
54
- length=32,
55
- salt=salt,
56
- iterations=100000,
57
- )
58
- key = base64.urlsafe_b64encode(kdf.derive(password))
59
- self._encryption_key = Fernet(key)
60
-
61
- def _init_env(self):
62
- """Initialize environment variable backend"""
63
- logger.info("Using environment variables for secrets")
64
-
65
- def _init_vault(self):
66
- """Initialize HashiCorp Vault backend"""
67
- try:
68
- import hvac
69
-
70
- vault_url = self.config.get("vault_url", os.getenv("VAULT_URL"))
71
- vault_token = self.config.get("vault_token", os.getenv("VAULT_TOKEN"))
72
-
73
- if not vault_url:
74
- raise ValueError("VAULT_URL required for Vault backend")
75
-
76
- self.vault_client = hvac.Client(url=vault_url, token=vault_token)
77
-
78
- if not self.vault_client.is_authenticated():
79
- raise ValueError("Vault authentication failed")
80
-
81
- logger.info("Vault backend initialized", url=vault_url)
82
-
83
- except ImportError:
84
- raise ImportError("hvac package required for Vault backend: pip install hvac")
85
-
86
- def _init_aws(self):
87
- """Initialize AWS Secrets Manager backend"""
88
- try:
89
- import boto3
90
-
91
- region = self.config.get("region", os.getenv("AWS_REGION", "us-east-1"))
92
- self.secrets_client = boto3.client("secretsmanager", region_name=region)
93
-
94
- logger.info("AWS Secrets Manager backend initialized", region=region)
95
-
96
- except ImportError:
97
- raise ImportError("boto3 package required for AWS backend: pip install boto3")
98
-
99
- def get_secret(self, key: str, default: Optional[str] = None) -> Optional[str]:
100
- """Get a secret value by key"""
101
- # Check cache first
102
- if key in self._cache:
103
- return self._cache[key]
104
-
105
- try:
106
- if self.backend == "env":
107
- value = self._get_env_secret(key, default)
108
- elif self.backend == "vault":
109
- value = self._get_vault_secret(key, default)
110
- elif self.backend == "aws":
111
- value = self._get_aws_secret(key, default)
112
- else:
113
- value = default
114
-
115
- # Cache the value
116
- if value is not None:
117
- self._cache[key] = value
118
-
119
- return value
120
-
121
- except Exception as e:
122
- logger.error("Failed to retrieve secret", key=key, error=str(e))
123
- return default
124
-
125
- def _get_env_secret(self, key: str, default: Optional[str] = None) -> Optional[str]:
126
- """Get secret from environment variables"""
127
- return os.getenv(key, default)
128
-
129
- def _get_vault_secret(self, key: str, default: Optional[str] = None) -> Optional[str]:
130
- """Get secret from HashiCorp Vault"""
131
- try:
132
- secret_path = self.config.get("secret_path", "secret/data/isa-model")
133
- response = self.vault_client.secrets.kv.v2.read_secret_version(path=secret_path)
134
- data = response["data"]["data"]
135
- return data.get(key, default)
136
- except Exception as e:
137
- logger.warning("Failed to retrieve secret from Vault", key=key, error=str(e))
138
- return default
139
-
140
- def _get_aws_secret(self, key: str, default: Optional[str] = None) -> Optional[str]:
141
- """Get secret from AWS Secrets Manager"""
142
- try:
143
- secret_name = self.config.get("secret_name", "isa-model/secrets")
144
- response = self.secrets_client.get_secret_value(SecretId=secret_name)
145
- secrets = json.loads(response["SecretString"])
146
- return secrets.get(key, default)
147
- except Exception as e:
148
- logger.warning("Failed to retrieve secret from AWS", key=key, error=str(e))
149
- return default
150
-
151
- def set_secret(self, key: str, value: str) -> bool:
152
- """Set a secret value (only supported for some backends)"""
153
- try:
154
- if self.backend == "vault":
155
- return self._set_vault_secret(key, value)
156
- elif self.backend == "aws":
157
- return self._set_aws_secret(key, value)
158
- else:
159
- logger.warning("Set operation not supported for backend", backend=self.backend)
160
- return False
161
- except Exception as e:
162
- logger.error("Failed to set secret", key=key, error=str(e))
163
- return False
164
-
165
- def _set_vault_secret(self, key: str, value: str) -> bool:
166
- """Set secret in HashiCorp Vault"""
167
- try:
168
- secret_path = self.config.get("secret_path", "secret/data/isa-model")
169
- # Get existing secrets first
170
- try:
171
- response = self.vault_client.secrets.kv.v2.read_secret_version(path=secret_path)
172
- existing_data = response["data"]["data"]
173
- except:
174
- existing_data = {}
175
-
176
- # Update with new secret
177
- existing_data[key] = value
178
-
179
- # Write back to vault
180
- self.vault_client.secrets.kv.v2.create_or_update_secret(
181
- path=secret_path,
182
- secret=existing_data
183
- )
184
-
185
- # Update cache
186
- self._cache[key] = value
187
- return True
188
-
189
- except Exception as e:
190
- logger.error("Failed to set secret in Vault", key=key, error=str(e))
191
- return False
192
-
193
- def _set_aws_secret(self, key: str, value: str) -> bool:
194
- """Set secret in AWS Secrets Manager"""
195
- try:
196
- secret_name = self.config.get("secret_name", "isa-model/secrets")
197
-
198
- # Get existing secrets
199
- try:
200
- response = self.secrets_client.get_secret_value(SecretId=secret_name)
201
- existing_secrets = json.loads(response["SecretString"])
202
- except:
203
- existing_secrets = {}
204
-
205
- # Update with new secret
206
- existing_secrets[key] = value
207
-
208
- # Update secret
209
- self.secrets_client.update_secret(
210
- SecretId=secret_name,
211
- SecretString=json.dumps(existing_secrets)
212
- )
213
-
214
- # Update cache
215
- self._cache[key] = value
216
- return True
217
-
218
- except Exception as e:
219
- logger.error("Failed to set secret in AWS", key=key, error=str(e))
220
- return False
221
-
222
- def list_secrets(self) -> List[str]:
223
- """List available secret keys"""
224
- try:
225
- if self.backend == "vault":
226
- return self._list_vault_secrets()
227
- elif self.backend == "aws":
228
- return self._list_aws_secrets()
229
- elif self.backend == "env":
230
- # Return common secret environment variables
231
- common_secrets = [
232
- "OPENAI_API_KEY", "REPLICATE_API_TOKEN", "ANTHROPIC_API_KEY",
233
- "DATABASE_URL", "REDIS_URL", "ISA_API_KEY"
234
- ]
235
- return [key for key in common_secrets if os.getenv(key)]
236
- else:
237
- return []
238
- except Exception as e:
239
- logger.error("Failed to list secrets", error=str(e))
240
- return []
241
-
242
- def _list_vault_secrets(self) -> List[str]:
243
- """List secrets in HashiCorp Vault"""
244
- try:
245
- secret_path = self.config.get("secret_path", "secret/data/isa-model")
246
- response = self.vault_client.secrets.kv.v2.read_secret_version(path=secret_path)
247
- return list(response["data"]["data"].keys())
248
- except Exception as e:
249
- logger.warning("Failed to list Vault secrets", error=str(e))
250
- return []
251
-
252
- def _list_aws_secrets(self) -> List[str]:
253
- """List secrets in AWS Secrets Manager"""
254
- try:
255
- secret_name = self.config.get("secret_name", "isa-model/secrets")
256
- response = self.secrets_client.get_secret_value(SecretId=secret_name)
257
- secrets = json.loads(response["SecretString"])
258
- return list(secrets.keys())
259
- except Exception as e:
260
- logger.warning("Failed to list AWS secrets", error=str(e))
261
- return []
262
-
263
- def rotate_secret(self, key: str) -> bool:
264
- """Rotate a secret (implementation depends on secret type)"""
265
- # This is a placeholder for secret rotation logic
266
- logger.info("Secret rotation requested", key=key)
267
- # In production, this would implement proper rotation logic
268
- return True
269
-
270
- def clear_cache(self):
271
- """Clear the secrets cache"""
272
- self._cache.clear()
273
- logger.info("Secrets cache cleared")
274
-
275
- # Global secrets manager instance
276
- _secrets_manager = None
277
-
278
- def get_secrets_manager() -> SecretsManager:
279
- """Get the global secrets manager instance"""
280
- global _secrets_manager
281
-
282
- if _secrets_manager is None:
283
- # Determine backend from environment
284
- backend = os.getenv("SECRETS_BACKEND", "env")
285
-
286
- # Initialize with backend-specific configuration
287
- if backend == "vault":
288
- _secrets_manager = SecretsManager(
289
- backend="vault",
290
- vault_url=os.getenv("VAULT_URL"),
291
- vault_token=os.getenv("VAULT_TOKEN"),
292
- secret_path=os.getenv("VAULT_SECRET_PATH", "secret/data/isa-model")
293
- )
294
- elif backend == "aws":
295
- _secrets_manager = SecretsManager(
296
- backend="aws",
297
- region=os.getenv("AWS_REGION", "us-east-1"),
298
- secret_name=os.getenv("AWS_SECRET_NAME", "isa-model/secrets")
299
- )
300
- else:
301
- _secrets_manager = SecretsManager(backend="env")
302
-
303
- return _secrets_manager
304
-
305
- def get_secret(key: str, default: Optional[str] = None) -> Optional[str]:
306
- """Convenience function to get a secret"""
307
- return get_secrets_manager().get_secret(key, default)
308
-
309
- def set_secret(key: str, value: str) -> bool:
310
- """Convenience function to set a secret"""
311
- return get_secrets_manager().set_secret(key, value)
312
-
313
- # Predefined secret getters for common secrets
314
- def get_openai_api_key() -> Optional[str]:
315
- """Get OpenAI API key"""
316
- return get_secret("OPENAI_API_KEY")
317
-
318
- def get_replicate_api_token() -> Optional[str]:
319
- """Get Replicate API token"""
320
- return get_secret("REPLICATE_API_TOKEN")
321
-
322
- def get_anthropic_api_key() -> Optional[str]:
323
- """Get Anthropic API key"""
324
- return get_secret("ANTHROPIC_API_KEY")
325
-
326
- def get_database_url() -> Optional[str]:
327
- """Get database URL"""
328
- return get_secret("DATABASE_URL")
329
-
330
- def get_redis_url() -> Optional[str]:
331
- """Get Redis URL"""
332
- return get_secret("REDIS_URL", "redis://localhost:6379")
333
-
334
- def get_isa_api_key() -> Optional[str]:
335
- """Get ISA API key"""
336
- return get_secret("ISA_API_KEY")
337
-
338
- # Health check for secrets manager
339
- async def check_secrets_health() -> Dict[str, Any]:
340
- """Check secrets manager health"""
341
- try:
342
- manager = get_secrets_manager()
343
-
344
- # Test basic functionality
345
- test_secret = manager.get_secret("HEALTH_CHECK_TEST", "test")
346
-
347
- return {
348
- "secrets_manager": "ok",
349
- "backend": manager.backend,
350
- "cached_secrets": len(manager._cache),
351
- "status": "healthy"
352
- }
353
- except Exception as e:
354
- return {
355
- "secrets_manager": "error",
356
- "status": "unhealthy",
357
- "error": str(e)
358
- }