Create command dispatching classes

This commit is contained in:
2025-05-18 09:40:09 +02:00
parent 64536b40f6
commit 26ef9b45d4
9 changed files with 591 additions and 0 deletions

View File

@ -0,0 +1,56 @@
"""Get secret."""
import logging
from typing import final, override
from sshecret.backend.models import Operation
from sshecret_sshd import exceptions
from .base import CommandDispatcher
LOG = logging.getLogger(__name__)
@final
class GetSecret(CommandDispatcher):
"""Get Secret."""
name = "get_secret"
@override
async def exec(self) -> None:
"""Execute command."""
if len(self.arguments) != 1:
raise exceptions.UnknownClientOrSecretError()
secret_name = self.arguments[0]
if secret_name not in self.client.secrets:
await self.audit(
Operation.DENY,
message="Client requested invalid secret",
secret=secret_name,
)
raise exceptions.SecretNotFoundError()
try:
secret = await self.backend.get_client_secret(self.client.name, secret_name)
except Exception as exc:
LOG.error(
"Got exception while getting client %s secret %s: %s",
self.client.name,
secret_name,
exc,
exc_info=True,
)
raise exceptions.BackendError(backend_error=str(exc)) from exc
if not secret:
await self.audit(
Operation.DENY,
message="Client requested invalid secret",
secret=secret_name,
)
raise exceptions.SecretNotFoundError()
await self.audit(
Operation.READ, message="Client requested secret", secret=secret_name
)
self.print(secret, newline=False)