From a8815a193fdf9e86cc9068a4e2e2c78e66a0ede0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= <40151420+ldericher@users.noreply.github.com> Date: Wed, 16 Mar 2022 13:28:15 +0000 Subject: [PATCH] config routing --- api/kiwi_vpn_api/config.py | 22 --------------- api/kiwi_vpn_api/routers/install.py | 42 +++++++++++++++++++---------- 2 files changed, 28 insertions(+), 36 deletions(-) diff --git a/api/kiwi_vpn_api/config.py b/api/kiwi_vpn_api/config.py index f6968f3..bb9b255 100644 --- a/api/kiwi_vpn_api/config.py +++ b/api/kiwi_vpn_api/config.py @@ -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)) diff --git a/api/kiwi_vpn_api/routers/install.py b/api/kiwi_vpn_api/routers/install.py index a992d1a..271bcb3 100644 --- a/api/kiwi_vpn_api/routers/install.py +++ b/api/kiwi_vpn_api/routers/install.py @@ -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():