From 2861b2fb0d4022b30a3cbd21d66734160962b2b7 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 14:10:49 +0000 Subject: [PATCH] database creation --- api/kiwi_vpn_api/config.py | 7 ++-- api/kiwi_vpn_api/routers/install.py | 57 +++++++++++++++++------------ 2 files changed, 37 insertions(+), 27 deletions(-) diff --git a/api/kiwi_vpn_api/config.py b/api/kiwi_vpn_api/config.py index bb9b255..1f2b2cc 100644 --- a/api/kiwi_vpn_api/config.py +++ b/api/kiwi_vpn_api/config.py @@ -29,8 +29,9 @@ class DBConfig(BaseModel): db_type: DBType = DBType.sqlite @property - def database(self) -> Database: - return SqliteDatabase("tmp/vpn.db") + async def database(self) -> Database: + if self.db_type == DBType.sqlite: + return SqliteDatabase("tmp/vpn.db") class JWTConfig(BaseModel): @@ -43,7 +44,7 @@ class CryptoConfig(BaseModel): schemes: list[str] = ["bcrypt"] @property - def cryptContext(self) -> CryptContext: + async def cryptContext(self) -> CryptContext: return CryptContext(schemes=self.schemes, deprecated="auto") diff --git a/api/kiwi_vpn_api/routers/install.py b/api/kiwi_vpn_api/routers/install.py index 271bcb3..184b025 100644 --- a/api/kiwi_vpn_api/routers/install.py +++ b/api/kiwi_vpn_api/routers/install.py @@ -2,10 +2,10 @@ import json from pathlib import Path from secrets import token_hex -from fastapi import APIRouter, Depends, status -from fastapi.responses import JSONResponse +from fastapi import APIRouter, Depends, HTTPException, status +from peewee import Database -from ..config import CRYPT_CONTEXT, DB, BaseConfig +from ..config import CRYPT_CONTEXT, BaseConfig from ..db import Certificate, DistinguishedName, User, UserCapability router = APIRouter(prefix="/install") @@ -42,7 +42,7 @@ async def get_config( has_config: bool = Depends(has_config), ): if has_config: - return JSONResponse(status_code=status.HTTP_403_FORBIDDEN) + raise HTTPException(status_code=status.HTTP_403_FORBIDDEN) return config @@ -64,7 +64,7 @@ async def set_config( has_config: bool = Depends(has_config), ): if has_config: - return JSONResponse(status_code=status.HTTP_403_FORBIDDEN) + raise HTTPException(status_code=status.HTTP_403_FORBIDDEN) if config.jwt.secret is None: config.jwt.secret = token_hex(32) @@ -73,42 +73,51 @@ async def set_config( kv.write(config.json(indent=2)) -async def is_installed(): - return DB.table_exists(User) +async def connect_db(config: BaseConfig = Depends(load_config)) -> Database: + db = await config.db.database + db.connect() + return db -@router.get("/check_installed", responses={ +async def has_tables(db: Database = Depends(connect_db)): + return db.table_exists(User) + + +@router.get("/db", responses={ status.HTTP_200_OK: { "model": bool, }, }) -async def check_installed(is_installed: bool = Depends(is_installed)): - return is_installed +async def check_db( + has_tables: bool = Depends(has_tables), +): + return has_tables -@router.get( - "/create_db", +@router.put( + "/db", responses={ status.HTTP_200_OK: { "content": None, }, status.HTTP_400_BAD_REQUEST: { - "description": "Could not create Database", + "description": "Database exists", "content": None, }, }, ) -async def create_db(is_installed: bool = Depends(is_installed)): - if is_installed: - return JSONResponse( - status_code=status.HTTP_400_BAD_REQUEST, - ) +async def create_db( + admin_name: str, + admin_password: str, + db: Database = Depends(connect_db), +): + if await has_tables(db): + raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST) - DB.create_tables([Certificate, DistinguishedName, User, UserCapability]) + db.create_tables([Certificate, DistinguishedName, User, UserCapability]) - admin = User.create(name="admin", password=CRYPT_CONTEXT.hash("secret")) + admin = User.create( + name=admin_name, + password=CRYPT_CONTEXT.hash(admin_password), + ) UserCapability.create(user=admin, capability="admin") - - User.create(name="johndoe", password=CRYPT_CONTEXT.hash("secret")) - - DB.close()