88 lines
2.3 KiB
Python
88 lines
2.3 KiB
Python
from datetime import datetime
|
|
from typing import Literal, Optional
|
|
|
|
from pydantic import BaseModel, model_validator
|
|
|
|
|
|
# --- SSH Key file registry ---
|
|
|
|
class SSHKeyCreate(BaseModel):
|
|
name: str
|
|
path: str
|
|
description: Optional[str] = None
|
|
|
|
|
|
class SSHKeyUpdate(BaseModel):
|
|
name: Optional[str] = None
|
|
path: Optional[str] = None
|
|
description: Optional[str] = None
|
|
|
|
|
|
class SSHKeyRead(BaseModel):
|
|
id: int
|
|
name: str
|
|
path: str
|
|
description: Optional[str] = None
|
|
created_at: datetime
|
|
|
|
model_config = {"from_attributes": True}
|
|
|
|
|
|
# --- Object SSH credential list ---
|
|
|
|
class ObjectSSHCredentialCreate(BaseModel):
|
|
type: Literal["password", "key"]
|
|
username: str
|
|
password: Optional[str] = None # plaintext, encrypted on write (for type=password)
|
|
ssh_key_id: Optional[int] = None # for type=key
|
|
priority: int = 0
|
|
|
|
@model_validator(mode="after")
|
|
def check_fields(self) -> "ObjectSSHCredentialCreate":
|
|
if self.type == "password" and not self.password:
|
|
raise ValueError("password is required for type=password")
|
|
if self.type == "key" and self.ssh_key_id is None:
|
|
raise ValueError("ssh_key_id is required for type=key")
|
|
return self
|
|
|
|
|
|
class ObjectSSHCredentialRead(BaseModel):
|
|
id: int
|
|
type: str
|
|
username: str
|
|
ssh_key_id: Optional[int] = None
|
|
priority: int
|
|
|
|
model_config = {"from_attributes": True}
|
|
|
|
|
|
# --- Per-category credentials ---
|
|
|
|
class ObjectCategoryCredentialCreate(BaseModel):
|
|
category: str
|
|
protocol: Literal["ssh", "http"]
|
|
type: Literal["password", "key"] = "password"
|
|
username: str
|
|
password: Optional[str] = None # plaintext, encrypted on write
|
|
ssh_key_id: Optional[int] = None # for type=key, ssh only
|
|
priority: int = 0
|
|
|
|
@model_validator(mode="after")
|
|
def check_fields(self) -> "ObjectCategoryCredentialCreate":
|
|
if self.type == "password" and not self.password:
|
|
raise ValueError("password is required for type=password")
|
|
if self.type == "key" and self.ssh_key_id is None:
|
|
raise ValueError("ssh_key_id is required for type=key")
|
|
return self
|
|
|
|
|
|
class ObjectCategoryCredentialRead(BaseModel):
|
|
id: int
|
|
category: str
|
|
protocol: str
|
|
type: str
|
|
username: str
|
|
ssh_key_id: Optional[int] = None
|
|
priority: int
|
|
|
|
model_config = {"from_attributes": True}
|