diff --git a/api/.gitignore b/api/.gitignore index de2d5e0..5b372a8 100644 --- a/api/.gitignore +++ b/api/.gitignore @@ -150,3 +150,5 @@ cython_debug/ # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ + +tmp/ diff --git a/api/kiwi_vpn_api/config.py b/api/kiwi_vpn_api/config.py index 6aa3d4e..960cdd8 100644 --- a/api/kiwi_vpn_api/config.py +++ b/api/kiwi_vpn_api/config.py @@ -1,7 +1,7 @@ from peewee import SqliteDatabase from passlib.context import CryptContext -DB = SqliteDatabase("vpn.db") +DB = SqliteDatabase("tmp/vpn.db") PRODUCTION_MODE = False # to get a string like this run: diff --git a/api/kiwi_vpn_api/db/create_db.py b/api/kiwi_vpn_api/db/create_db.py deleted file mode 100755 index db01ab9..0000000 --- a/api/kiwi_vpn_api/db/create_db.py +++ /dev/null @@ -1,18 +0,0 @@ -#!/usr/bin/env python3 - -from ..config import CRYPT_CONTEXT, DB -from .model import Certificate, DistinguishedName, User, UserCapability - - -def main(): - DB.connect() - DB.create_tables([Certificate, DistinguishedName, User, UserCapability]) - - admin = User.create(name="admin", password=CRYPT_CONTEXT.hash("secret")) - UserCapability.create(user=admin, capability="admin") - - DB.close() - - -if __name__ == "__main__": - main() diff --git a/api/kiwi_vpn_api/main.py b/api/kiwi_vpn_api/main.py index 5c540fc..e60fe3d 100755 --- a/api/kiwi_vpn_api/main.py +++ b/api/kiwi_vpn_api/main.py @@ -3,6 +3,8 @@ import uvicorn from fastapi import FastAPI +from kiwi_vpn_api.routers import install + from .config import PRODUCTION_MODE from .routers import auth, user @@ -21,6 +23,7 @@ api = FastAPI( redoc_url=None, ) +api.include_router(install.router) api.include_router(auth.router) api.include_router(user.router) diff --git a/api/kiwi_vpn_api/routers/install.py b/api/kiwi_vpn_api/routers/install.py new file mode 100644 index 0000000..a1c06d3 --- /dev/null +++ b/api/kiwi_vpn_api/routers/install.py @@ -0,0 +1,35 @@ +from fastapi import APIRouter, Depends, HTTPException, status + +from ..config import DB, CRYPT_CONTEXT +from ..db.model import Certificate, DistinguishedName, User, UserCapability + +router = APIRouter(prefix="/install") + + +async def is_installed(): + return DB.table_exists(User) + + +@router.get("/check_installed") +async def check_installed(is_installed: bool = Depends(is_installed)): + return {"is_installed": is_installed} + + +@router.get("/create_db") +async def create_db(is_installed: bool = Depends(is_installed)): + credentials_exception = HTTPException( + status_code=status.HTTP_401_UNAUTHORIZED, + detail="Could not validate credentials", + headers={"WWW-Authenticate": "Bearer"}, + ) + if is_installed: + raise credentials_exception + + DB.create_tables([Certificate, DistinguishedName, User, UserCapability]) + + admin = User.create(name="admin", password=CRYPT_CONTEXT.hash("secret")) + UserCapability.create(user=admin, capability="admin") + + User.create(name="johndoe", password=CRYPT_CONTEXT.hash("secret")) + + DB.close()