database creation
This commit is contained in:
parent
a8815a193f
commit
2861b2fb0d
2 changed files with 37 additions and 27 deletions
|
@ -29,7 +29,8 @@ class DBConfig(BaseModel):
|
||||||
db_type: DBType = DBType.sqlite
|
db_type: DBType = DBType.sqlite
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def database(self) -> Database:
|
async def database(self) -> Database:
|
||||||
|
if self.db_type == DBType.sqlite:
|
||||||
return SqliteDatabase("tmp/vpn.db")
|
return SqliteDatabase("tmp/vpn.db")
|
||||||
|
|
||||||
|
|
||||||
|
@ -43,7 +44,7 @@ class CryptoConfig(BaseModel):
|
||||||
schemes: list[str] = ["bcrypt"]
|
schemes: list[str] = ["bcrypt"]
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def cryptContext(self) -> CryptContext:
|
async def cryptContext(self) -> CryptContext:
|
||||||
return CryptContext(schemes=self.schemes, deprecated="auto")
|
return CryptContext(schemes=self.schemes, deprecated="auto")
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -2,10 +2,10 @@ import json
|
||||||
from pathlib import Path
|
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, HTTPException, status
|
||||||
from fastapi.responses import JSONResponse
|
from peewee import Database
|
||||||
|
|
||||||
from ..config import CRYPT_CONTEXT, DB, BaseConfig
|
from ..config import CRYPT_CONTEXT, BaseConfig
|
||||||
from ..db import Certificate, DistinguishedName, User, UserCapability
|
from ..db import Certificate, DistinguishedName, User, UserCapability
|
||||||
|
|
||||||
router = APIRouter(prefix="/install")
|
router = APIRouter(prefix="/install")
|
||||||
|
@ -42,7 +42,7 @@ async def get_config(
|
||||||
has_config: bool = Depends(has_config),
|
has_config: bool = Depends(has_config),
|
||||||
):
|
):
|
||||||
if has_config:
|
if has_config:
|
||||||
return JSONResponse(status_code=status.HTTP_403_FORBIDDEN)
|
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
||||||
|
|
||||||
return config
|
return config
|
||||||
|
|
||||||
|
@ -64,7 +64,7 @@ async def set_config(
|
||||||
has_config: bool = Depends(has_config),
|
has_config: bool = Depends(has_config),
|
||||||
):
|
):
|
||||||
if 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:
|
if config.jwt.secret is None:
|
||||||
config.jwt.secret = token_hex(32)
|
config.jwt.secret = token_hex(32)
|
||||||
|
@ -73,42 +73,51 @@ async def set_config(
|
||||||
kv.write(config.json(indent=2))
|
kv.write(config.json(indent=2))
|
||||||
|
|
||||||
|
|
||||||
async def is_installed():
|
async def connect_db(config: BaseConfig = Depends(load_config)) -> Database:
|
||||||
return DB.table_exists(User)
|
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: {
|
status.HTTP_200_OK: {
|
||||||
"model": bool,
|
"model": bool,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
async def check_installed(is_installed: bool = Depends(is_installed)):
|
async def check_db(
|
||||||
return is_installed
|
has_tables: bool = Depends(has_tables),
|
||||||
|
):
|
||||||
|
return has_tables
|
||||||
|
|
||||||
|
|
||||||
@router.get(
|
@router.put(
|
||||||
"/create_db",
|
"/db",
|
||||||
responses={
|
responses={
|
||||||
status.HTTP_200_OK: {
|
status.HTTP_200_OK: {
|
||||||
"content": None,
|
"content": None,
|
||||||
},
|
},
|
||||||
status.HTTP_400_BAD_REQUEST: {
|
status.HTTP_400_BAD_REQUEST: {
|
||||||
"description": "Could not create Database",
|
"description": "Database exists",
|
||||||
"content": None,
|
"content": None,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
async def create_db(is_installed: bool = Depends(is_installed)):
|
async def create_db(
|
||||||
if is_installed:
|
admin_name: str,
|
||||||
return JSONResponse(
|
admin_password: str,
|
||||||
status_code=status.HTTP_400_BAD_REQUEST,
|
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])
|
||||||
|
|
||||||
|
admin = User.create(
|
||||||
|
name=admin_name,
|
||||||
|
password=CRYPT_CONTEXT.hash(admin_password),
|
||||||
)
|
)
|
||||||
|
|
||||||
DB.create_tables([Certificate, DistinguishedName, User, UserCapability])
|
|
||||||
|
|
||||||
admin = User.create(name="admin", password=CRYPT_CONTEXT.hash("secret"))
|
|
||||||
UserCapability.create(user=admin, capability="admin")
|
UserCapability.create(user=admin, capability="admin")
|
||||||
|
|
||||||
User.create(name="johndoe", password=CRYPT_CONTEXT.hash("secret"))
|
|
||||||
|
|
||||||
DB.close()
|
|
||||||
|
|
Loading…
Reference in a new issue