forked from kenzuya/unshackle
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:
+10
-4
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user