config routing
This commit is contained in:
parent
162d1729d1
commit
a8815a193f
2 changed files with 28 additions and 36 deletions
|
@ -1,8 +1,6 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from enum import Enum
|
||||
import json
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
from jose.constants import ALGORITHMS
|
||||
|
@ -53,23 +51,3 @@ class BaseConfig(BaseModel):
|
|||
db: DBConfig = Field(default_factory=DBConfig)
|
||||
jwt: JWTConfig = Field(default_factory=JWTConfig)
|
||||
crypto: CryptoConfig = Field(default_factory=CryptoConfig)
|
||||
|
||||
def save(self, filename: Path) -> None:
|
||||
with open(filename, "w") as kv:
|
||||
kv.write(self.json(indent=2))
|
||||
|
||||
|
||||
CONFIG_FILE = "tmp/config.json"
|
||||
|
||||
|
||||
async def get_default_config() -> BaseConfig:
|
||||
return BaseConfig()
|
||||
|
||||
|
||||
async def is_configured() -> bool:
|
||||
return Path(CONFIG_FILE).is_file()
|
||||
|
||||
|
||||
async def get_current_config() -> BaseConfig:
|
||||
with open(CONFIG_FILE, "r") as kv:
|
||||
return BaseConfig.parse_obj(json.load(kv))
|
||||
|
|
|
@ -1,37 +1,50 @@
|
|||
import json
|
||||
from pathlib import Path
|
||||
from secrets import token_hex
|
||||
|
||||
from fastapi import APIRouter, Depends, status
|
||||
from fastapi.responses import JSONResponse
|
||||
|
||||
from ..config import (CONFIG_FILE, CRYPT_CONTEXT, DB, BaseConfig,
|
||||
get_current_config, get_default_config, is_configured)
|
||||
from ..config import CRYPT_CONTEXT, DB, BaseConfig
|
||||
from ..db import Certificate, DistinguishedName, User, UserCapability
|
||||
|
||||
router = APIRouter(prefix="/install")
|
||||
|
||||
|
||||
@router.get("/config/default", response_model=BaseConfig)
|
||||
async def get_default_config(config: BaseConfig = Depends(get_default_config)):
|
||||
return config
|
||||
CONFIG_FILE = "tmp/config.json"
|
||||
|
||||
|
||||
async def has_config() -> bool:
|
||||
return Path(CONFIG_FILE).is_file()
|
||||
|
||||
|
||||
async def load_config() -> BaseConfig:
|
||||
try:
|
||||
with open(CONFIG_FILE, "r") as kv:
|
||||
return BaseConfig.parse_obj(json.load(kv))
|
||||
|
||||
except FileNotFoundError:
|
||||
return BaseConfig()
|
||||
|
||||
|
||||
@router.get(
|
||||
"/config",
|
||||
response_model=BaseConfig,
|
||||
responses={
|
||||
status.HTTP_404_NOT_FOUND: {
|
||||
"description": "Not configured",
|
||||
status.HTTP_403_FORBIDDEN: {
|
||||
"description": "Must be admin",
|
||||
"content": None,
|
||||
},
|
||||
},
|
||||
)
|
||||
async def get_config(
|
||||
is_configured: bool = Depends(is_configured),
|
||||
config: BaseConfig = Depends(load_config),
|
||||
has_config: bool = Depends(has_config),
|
||||
):
|
||||
if not is_configured:
|
||||
return JSONResponse(status_code=status.HTTP_404_NOT_FOUND)
|
||||
if has_config:
|
||||
return JSONResponse(status_code=status.HTTP_403_FORBIDDEN)
|
||||
|
||||
return await get_current_config()
|
||||
return config
|
||||
|
||||
|
||||
@router.put(
|
||||
|
@ -48,15 +61,16 @@ async def get_config(
|
|||
)
|
||||
async def set_config(
|
||||
config: BaseConfig,
|
||||
is_configured: bool = Depends(is_configured),
|
||||
has_config: bool = Depends(has_config),
|
||||
):
|
||||
if is_configured:
|
||||
if has_config:
|
||||
return JSONResponse(status_code=status.HTTP_403_FORBIDDEN)
|
||||
|
||||
if config.jwt.secret is None:
|
||||
config.jwt.secret = token_hex(32)
|
||||
|
||||
config.save(CONFIG_FILE)
|
||||
with open(CONFIG_FILE, "w") as kv:
|
||||
kv.write(config.json(indent=2))
|
||||
|
||||
|
||||
async def is_installed():
|
||||
|
|
Loading…
Reference in a new issue