config routing

This commit is contained in:
Jörn-Michael Miehe 2022-03-16 13:28:15 +00:00
parent 162d1729d1
commit a8815a193f
2 changed files with 28 additions and 36 deletions

View file

@ -1,8 +1,6 @@
from __future__ import annotations from __future__ import annotations
from enum import Enum from enum import Enum
import json
from pathlib import Path
from typing import Optional from typing import Optional
from jose.constants import ALGORITHMS from jose.constants import ALGORITHMS
@ -53,23 +51,3 @@ class BaseConfig(BaseModel):
db: DBConfig = Field(default_factory=DBConfig) db: DBConfig = Field(default_factory=DBConfig)
jwt: JWTConfig = Field(default_factory=JWTConfig) jwt: JWTConfig = Field(default_factory=JWTConfig)
crypto: CryptoConfig = Field(default_factory=CryptoConfig) 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))

View file

@ -1,37 +1,50 @@
import json
from pathlib import Path
from secrets import token_hex from secrets import token_hex
from fastapi import APIRouter, Depends, status from fastapi import APIRouter, Depends, status
from fastapi.responses import JSONResponse from fastapi.responses import JSONResponse
from ..config import (CONFIG_FILE, CRYPT_CONTEXT, DB, BaseConfig, from ..config import CRYPT_CONTEXT, DB, BaseConfig
get_current_config, get_default_config, is_configured)
from ..db import Certificate, DistinguishedName, User, UserCapability from ..db import Certificate, DistinguishedName, User, UserCapability
router = APIRouter(prefix="/install") router = APIRouter(prefix="/install")
@router.get("/config/default", response_model=BaseConfig) CONFIG_FILE = "tmp/config.json"
async def get_default_config(config: BaseConfig = Depends(get_default_config)):
return config
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( @router.get(
"/config", "/config",
response_model=BaseConfig, response_model=BaseConfig,
responses={ responses={
status.HTTP_404_NOT_FOUND: { status.HTTP_403_FORBIDDEN: {
"description": "Not configured", "description": "Must be admin",
"content": None, "content": None,
}, },
}, },
) )
async def get_config( 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: if has_config:
return JSONResponse(status_code=status.HTTP_404_NOT_FOUND) return JSONResponse(status_code=status.HTTP_403_FORBIDDEN)
return await get_current_config() return config
@router.put( @router.put(
@ -48,15 +61,16 @@ async def get_config(
) )
async def set_config( async def set_config(
config: BaseConfig, 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) return JSONResponse(status_code=status.HTTP_403_FORBIDDEN)
if config.jwt.secret is None: if config.jwt.secret is None:
config.jwt.secret = token_hex(32) 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(): async def is_installed():