From 5f4ec65e4dd7220cf912668136936da06a663244 Mon Sep 17 00:00:00 2001 From: swve Date: Thu, 15 Aug 2024 16:24:15 +0200 Subject: [PATCH] feat: disable docs for non-dev envs and introduce internal APIs --- apps/api/app.py | 2 ++ apps/api/src/router.py | 18 ++++++++-- apps/api/src/routers/ee/cloud_internal.py | 29 ++++++++++++++++ apps/api/src/services/orgs/orgs.py | 42 +++++++++++++++++++++++ 4 files changed, 89 insertions(+), 2 deletions(-) create mode 100644 apps/api/src/routers/ee/cloud_internal.py diff --git a/apps/api/app.py b/apps/api/app.py index 761341de..f23973c9 100644 --- a/apps/api/app.py +++ b/apps/api/app.py @@ -25,6 +25,8 @@ learnhouse_config: LearnHouseConfig = get_learnhouse_config() app = FastAPI( title=learnhouse_config.site_name, description=learnhouse_config.site_description, + docs_url="/docs" if learnhouse_config.general_config.development_mode else None, + redoc_url="/redoc" if learnhouse_config.general_config.development_mode else None, version="0.1.0", ) diff --git a/apps/api/src/router.py b/apps/api/src/router.py index cc4d908c..bc4aaaa8 100644 --- a/apps/api/src/router.py +++ b/apps/api/src/router.py @@ -1,9 +1,11 @@ +import os from fastapi import APIRouter, Depends from src.routers import usergroups from src.routers import dev, trail, users, auth, orgs, roles from src.routers.ai import ai from src.routers.courses import chapters, collections, courses, assignments from src.routers.courses.activities import activities, blocks +from src.routers.ee import cloud_internal from src.routers.install import install from src.services.dev.dev import isDevModeEnabledOrRaise from src.services.install.install import isInstallModeEnabled @@ -20,13 +22,25 @@ v1_router.include_router(orgs.router, prefix="/orgs", tags=["orgs"]) v1_router.include_router(roles.router, prefix="/roles", tags=["roles"]) v1_router.include_router(blocks.router, prefix="/blocks", tags=["blocks"]) v1_router.include_router(courses.router, prefix="/courses", tags=["courses"]) -v1_router.include_router(assignments.router, prefix="/assignments", tags=["assignments"]) +v1_router.include_router( + assignments.router, prefix="/assignments", tags=["assignments"] +) v1_router.include_router(chapters.router, prefix="/chapters", tags=["chapters"]) v1_router.include_router(activities.router, prefix="/activities", tags=["activities"]) -v1_router.include_router(collections.router, prefix="/collections", tags=["collections"]) +v1_router.include_router( + collections.router, prefix="/collections", tags=["collections"] +) v1_router.include_router(trail.router, prefix="/trail", tags=["trail"]) v1_router.include_router(ai.router, prefix="/ai", tags=["ai"]) +if os.environ.get("CLOUD_INTERNAL_KEY"): + v1_router.include_router( + cloud_internal.router, + prefix="/cloud_internal", + tags=["cloud_internal"], + dependencies=[Depends(cloud_internal.check_internal_cloud_key)], + ) + # Dev Routes v1_router.include_router( dev.router, diff --git a/apps/api/src/routers/ee/cloud_internal.py b/apps/api/src/routers/ee/cloud_internal.py new file mode 100644 index 00000000..b4224ddf --- /dev/null +++ b/apps/api/src/routers/ee/cloud_internal.py @@ -0,0 +1,29 @@ +import os +from fastapi import APIRouter, Depends, HTTPException, Request +from sqlmodel import Session +from src.core.events.database import get_db_session +from src.db.organization_config import OrganizationConfigBase +from src.services.orgs.orgs import update_org_with_config_no_auth + +router = APIRouter() + +# Utils +def check_internal_cloud_key(request: Request): + if request.headers.get("CloudInternalKey") != os.environ.get( + "CLOUD_INTERNAL_KEY" + ): + raise HTTPException(status_code=403, detail="Unauthorized") + + +@router.put("/update_org_config") +async def update_org_Config( + request: Request, + org_id: int, + config_object: OrganizationConfigBase, + db_session: Session = Depends(get_db_session), +): + + res = await update_org_with_config_no_auth( + request, config_object, org_id, db_session + ) + return res diff --git a/apps/api/src/services/orgs/orgs.py b/apps/api/src/services/orgs/orgs.py index 58edc7ea..07e08da6 100644 --- a/apps/api/src/services/orgs/orgs.py +++ b/apps/api/src/services/orgs/orgs.py @@ -338,6 +338,48 @@ async def update_org( return org +async def update_org_with_config_no_auth( + request: Request, + orgconfig: OrganizationConfigBase, + org_id: int, + db_session: Session, +): + statement = select(Organization).where(Organization.id == org_id) + result = db_session.exec(statement) + + org = result.first() + + if not org: + raise HTTPException( + status_code=404, + detail="Organization slug not found", + ) + + # Get org config + statement = select(OrganizationConfig).where(OrganizationConfig.org_id == org.id) + result = db_session.exec(statement) + + org_config = result.first() + + if org_config is None: + logging.error(f"Organization {org_id} has no config") + raise HTTPException( + status_code=404, + detail="Organization config not found", + ) + + updated_config = orgconfig + + # Update the database + org_config.config = json.loads(updated_config.json()) + org_config.update_date = str(datetime.now()) + + db_session.add(org_config) + db_session.commit() + db_session.refresh(org_config) + + return {"detail": "Organization updated"} + async def update_org_logo( request: Request,