Check in admin page in working state
This commit is contained in:
@ -1,9 +1,17 @@
|
||||
"""Models for the API."""
|
||||
|
||||
from typing import Annotated
|
||||
from pydantic import AfterValidator, BaseModel, Field, IPvAnyAddress, IPvAnyNetwork
|
||||
from .crypto import validate_public_key
|
||||
from .backend import Client
|
||||
import secrets
|
||||
from typing import Annotated, Literal, Self, Union
|
||||
from pydantic import (
|
||||
AfterValidator,
|
||||
BaseModel,
|
||||
ConfigDict,
|
||||
Field,
|
||||
IPvAnyAddress,
|
||||
IPvAnyNetwork,
|
||||
model_validator,
|
||||
)
|
||||
from sshecret.crypto import validate_public_key
|
||||
|
||||
|
||||
def public_key_validator(value: str) -> str:
|
||||
@ -12,6 +20,7 @@ def public_key_validator(value: str) -> str:
|
||||
return value
|
||||
raise ValueError("Error: Public key must be a valid RSA public key.")
|
||||
|
||||
|
||||
class SecretListView(BaseModel):
|
||||
"""Model containing a list of all available secrets."""
|
||||
|
||||
@ -53,3 +62,52 @@ class ClientCreate(BaseModel):
|
||||
name: str
|
||||
public_key: Annotated[str, AfterValidator(public_key_validator)]
|
||||
sources: list[IPvAnyAddress | IPvAnyNetwork] = Field(default_factory=list)
|
||||
|
||||
|
||||
class AutoGenerateOpts(BaseModel):
|
||||
"""Option to auto-generate a password."""
|
||||
|
||||
auto_generate: Literal[True]
|
||||
length: int = 32
|
||||
|
||||
|
||||
class SecretUpdate(BaseModel):
|
||||
"""Model to update a secret."""
|
||||
|
||||
value: str | AutoGenerateOpts = Field(
|
||||
description="Secret as string value or auto-generated with optional length",
|
||||
examples=["MySecretString", {"auto_generate": True, "length": 32}]
|
||||
)
|
||||
|
||||
def get_secret(self) -> str:
|
||||
"""Get secret.
|
||||
|
||||
This returns the specified one, or generates one according to auto-generation.
|
||||
"""
|
||||
if isinstance(self.value, str):
|
||||
return self.value
|
||||
secret = secrets.token_urlsafe(self.value.length)
|
||||
return secret
|
||||
|
||||
|
||||
class SecretCreate(SecretUpdate):
|
||||
"""Model to create a secret."""
|
||||
|
||||
name: str
|
||||
clients: list[str] | None = Field(default=None, description="Assign the secret to a list of clients.")
|
||||
|
||||
model_config: ConfigDict = ConfigDict(
|
||||
json_schema_extra={
|
||||
"examples": [
|
||||
{
|
||||
"name": "MySecret",
|
||||
"clients": ["client-1", "client-2"],
|
||||
"value": { "auto_generate": True, "length": 32 }
|
||||
},
|
||||
{
|
||||
"name": "MySecret",
|
||||
"value": "mysecretstring",
|
||||
}
|
||||
]
|
||||
}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user