feat(vaults): add configurable network timeout for remote vaults

Default 10s on API/HTTP vault requests, overridable per-vault (`timeout:`) or globally (`vault_timeout`); previously they could hang indefinitely on an unreachable host.
This commit is contained in:
imSp4rky
2026-06-21 20:39:37 -06:00
parent 3f5b5e0fe1
commit ecf0bf41ab
5 changed files with 35 additions and 8 deletions
+10 -4
View File
@@ -10,9 +10,10 @@ from unshackle.core.vault import Vault
class API(Vault):
"""Key Vault using a simple RESTful HTTP API call."""
def __init__(self, name: str, uri: str, token: str, no_push: bool = False):
def __init__(self, name: str, uri: str, token: str, no_push: bool = False, timeout: float = 10.0):
super().__init__(name, no_push)
self.uri = uri.rstrip("/")
self.timeout = timeout
self.session = Session()
self.session.headers.update({"User-Agent": f"unshackle v{__version__}"})
self.session.headers.update({"Authorization": f"Bearer {token}"})
@@ -22,7 +23,7 @@ class API(Vault):
kid = kid.hex
data = self.session.get(
url=f"{self.uri}/{service.lower()}/{kid}", headers={"Accept": "application/json"}
url=f"{self.uri}/{service.lower()}/{kid}", headers={"Accept": "application/json"}, timeout=self.timeout
).json()
code = int(data.get("code", 0))
@@ -55,6 +56,7 @@ class API(Vault):
url=f"{self.uri}/{service.lower()}",
params={"page": page, "total": 10},
headers={"Accept": "application/json"},
timeout=self.timeout,
).json()
code = int(data.get("code", 0))
@@ -89,7 +91,10 @@ class API(Vault):
kid = kid.hex
data = self.session.post(
url=f"{self.uri}/{service.lower()}/{kid}", json={"content_key": key}, headers={"Accept": "application/json"}
url=f"{self.uri}/{service.lower()}/{kid}",
json={"content_key": key},
headers={"Accept": "application/json"},
timeout=self.timeout,
).json()
code = int(data.get("code", 0))
@@ -135,6 +140,7 @@ class API(Vault):
url=f"{self.uri}/{service.lower()}",
json={"content_keys": batch_keys},
headers={"Accept": "application/json"},
timeout=self.timeout,
)
# Check for HTTP errors that suggest batch is too large
@@ -181,7 +187,7 @@ class API(Vault):
return total_added
def get_services(self) -> Iterator[str]:
data = self.session.post(url=self.uri, headers={"Accept": "application/json"}).json()
data = self.session.post(url=self.uri, headers={"Accept": "application/json"}, timeout=self.timeout).json()
code = int(data.get("code", 0))
message = data.get("message")