mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-11-22 00:45:10 +00:00
feat(vaults): report bad keys to the vault that served them
This commit is contained in:
+41
-1
@@ -1,7 +1,7 @@
|
||||
from typing import Iterator, Optional, Union
|
||||
from uuid import UUID
|
||||
|
||||
from requests import Session
|
||||
from requests import Response, Session
|
||||
|
||||
from unshackle.core import __version__
|
||||
from unshackle.core.vault import Vault
|
||||
@@ -205,6 +205,46 @@ class API(Vault):
|
||||
|
||||
return total_added
|
||||
|
||||
def flag_bad_key(self, service: str, kid: Union[UUID, str], key: str, source: str) -> None:
|
||||
"""Report a pair that failed to decrypt. The vault only accepts pairs it served."""
|
||||
if self.no_push:
|
||||
return
|
||||
if isinstance(kid, UUID):
|
||||
kid = kid.hex
|
||||
response = self.session.post(
|
||||
url=f"{self.uri}/bad-keys/{kid}",
|
||||
json={"content_key": key, "service": service},
|
||||
headers={"Accept": "application/json"},
|
||||
timeout=self.timeout,
|
||||
allow_redirects=False,
|
||||
)
|
||||
self._check(response, ignore=frozenset({1, 4}))
|
||||
|
||||
def unflag_bad_key(self, kid: Union[UUID, str], key: str) -> None:
|
||||
if self.no_push:
|
||||
return
|
||||
if isinstance(kid, UUID):
|
||||
kid = kid.hex
|
||||
response = self.session.delete(
|
||||
url=f"{self.uri}/bad-keys/{kid}/{key}",
|
||||
headers={"Accept": "application/json"},
|
||||
timeout=self.timeout,
|
||||
allow_redirects=False,
|
||||
)
|
||||
self._check(response, ignore=frozenset({1}))
|
||||
|
||||
@staticmethod
|
||||
def _check(response: Response, ignore: frozenset[int] = frozenset()) -> None:
|
||||
if response.status_code in (404, 405, 501):
|
||||
return
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
code = int(data.get("code", 0))
|
||||
if not code or code in ignore:
|
||||
return
|
||||
error = {1: Exceptions.AuthRejected, 2: Exceptions.TooManyRequests}.get(code, ValueError)
|
||||
raise error(f"{data.get('message')} ({code})")
|
||||
|
||||
def get_services(self) -> Iterator[str]:
|
||||
response = self.session.post(
|
||||
url=self.uri, headers={"Accept": "application/json"}, timeout=self.timeout, allow_redirects=False
|
||||
|
||||
Reference in New Issue
Block a user