From 25d85952ce2ab7f88b2306055d5e0f9b911b31bf Mon Sep 17 00:00:00 2001 From: Zamil Majdy Date: Wed, 23 Oct 2024 09:22:30 +0700 Subject: [PATCH 1/6] fix(platform): Enable auth on local-backend mode by default --- autogpt_platform/backend/.env.example | 4 ++-- autogpt_platform/backend/README.md | 5 +++-- .../backend/backend/util/settings.py | 2 +- autogpt_platform/docker-compose.yml | 20 +++++++++++++++++++ .../src/lib/autogpt-server-api/baseClient.ts | 1 - 5 files changed, 26 insertions(+), 6 deletions(-) diff --git a/autogpt_platform/backend/.env.example b/autogpt_platform/backend/.env.example index 0cb4d7abd723..0ec84ca83e66 100644 --- a/autogpt_platform/backend/.env.example +++ b/autogpt_platform/backend/.env.example @@ -20,13 +20,13 @@ PYRO_HOST=localhost SENTRY_DSN= ## User auth with Supabase is required for any of the 3rd party integrations with auth to work. -ENABLE_AUTH=false +ENABLE_AUTH=true SUPABASE_URL=http://localhost:8000 SUPABASE_SERVICE_ROLE_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyAgCiAgICAicm9sZSI6ICJzZXJ2aWNlX3JvbGUiLAogICAgImlzcyI6ICJzdXBhYmFzZS1kZW1vIiwKICAgICJpYXQiOiAxNjQxNzY5MjAwLAogICAgImV4cCI6IDE3OTk1MzU2MDAKfQ.DaYlNEoUrrEn2Ig7tqibS-PHK5vgusbcbo7X36XVt4Q SUPABASE_JWT_SECRET=your-super-secret-jwt-token-with-at-least-32-characters-long # For local development, you may need to set FRONTEND_BASE_URL for the OAuth flow for integrations to work. -# FRONTEND_BASE_URL=http://localhost:3000 +FRONTEND_BASE_URL=http://localhost:3000 ## == INTEGRATION CREDENTIALS == ## # Each set of server side credentials is required for the corresponding 3rd party diff --git a/autogpt_platform/backend/README.md b/autogpt_platform/backend/README.md index 00194b304e1c..fcd8b3461932 100644 --- a/autogpt_platform/backend/README.md +++ b/autogpt_platform/backend/README.md @@ -58,7 +58,7 @@ We use the Poetry to manage the dependencies. To set up the project, follow thes 6. Migrate the database. Be careful because this deletes current data in the database. ```sh - docker compose up db redis -d + docker compose up db -d poetry run prisma migrate deploy ``` @@ -66,9 +66,10 @@ We use the Poetry to manage the dependencies. To set up the project, follow thes ### Starting the server without Docker -Run the following command to build the dockerfiles: +Run the following command to run database in docker but the application locally: ```sh +docker compose --profile local up --build -d poetry run app ``` diff --git a/autogpt_platform/backend/backend/util/settings.py b/autogpt_platform/backend/backend/util/settings.py index 1ac875fdf92a..b59156f696b8 100644 --- a/autogpt_platform/backend/backend/util/settings.py +++ b/autogpt_platform/backend/backend/util/settings.py @@ -133,7 +133,7 @@ class Config(UpdateTrackingModel["Config"], BaseSettings): ) frontend_base_url: str = Field( - default="", + default="http://localhost:3000", description="Can be used to explicitly set the base URL for the frontend. " "This value is then used to generate redirect URLs for OAuth flows.", ) diff --git a/autogpt_platform/docker-compose.yml b/autogpt_platform/docker-compose.yml index be6f1f49ede5..4689ad48ad5a 100644 --- a/autogpt_platform/docker-compose.yml +++ b/autogpt_platform/docker-compose.yml @@ -142,3 +142,23 @@ services: extends: file: ./supabase/docker/docker-compose.yml service: vector + + local_dependencies: + <<: *supabase-services + profiles: + - local + image: busybox + command: /bin/true + depends_on: + - studio + - kong + - auth + - rest + - realtime + - storage + - imgproxy + - meta + - functions + - analytics + - db + - vector diff --git a/autogpt_platform/frontend/src/lib/autogpt-server-api/baseClient.ts b/autogpt_platform/frontend/src/lib/autogpt-server-api/baseClient.ts index dc80bb0401d4..e93f31f4feb3 100644 --- a/autogpt_platform/frontend/src/lib/autogpt-server-api/baseClient.ts +++ b/autogpt_platform/frontend/src/lib/autogpt-server-api/baseClient.ts @@ -273,7 +273,6 @@ export default class BaseAutoGPTServerAPI { if ( response.status === 403 && - response.statusText === "Not authenticated" && typeof window !== "undefined" // Check if in browser environment ) { window.location.href = "/login"; From 24de7e9ffcc71dd6709a3c06ccd25b9897d2d31c Mon Sep 17 00:00:00 2001 From: Zamil Majdy Date: Wed, 23 Oct 2024 09:31:27 +0700 Subject: [PATCH 2/6] Add return --- .../frontend/src/lib/autogpt-server-api/baseClient.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/autogpt_platform/frontend/src/lib/autogpt-server-api/baseClient.ts b/autogpt_platform/frontend/src/lib/autogpt-server-api/baseClient.ts index e93f31f4feb3..60f2a33369ab 100644 --- a/autogpt_platform/frontend/src/lib/autogpt-server-api/baseClient.ts +++ b/autogpt_platform/frontend/src/lib/autogpt-server-api/baseClient.ts @@ -276,6 +276,7 @@ export default class BaseAutoGPTServerAPI { typeof window !== "undefined" // Check if in browser environment ) { window.location.href = "/login"; + return null; } let errorDetail; From 9321023d29aef71d8f8a6059e81d4350b3e40e8b Mon Sep 17 00:00:00 2001 From: Zamil Majdy Date: Wed, 23 Oct 2024 09:36:52 +0700 Subject: [PATCH 3/6] use --detach just to be fancy --- autogpt_platform/backend/README.md | 2 +- autogpt_platform/docker-compose.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/autogpt_platform/backend/README.md b/autogpt_platform/backend/README.md index fcd8b3461932..ab91027df23a 100644 --- a/autogpt_platform/backend/README.md +++ b/autogpt_platform/backend/README.md @@ -69,7 +69,7 @@ We use the Poetry to manage the dependencies. To set up the project, follow thes Run the following command to run database in docker but the application locally: ```sh -docker compose --profile local up --build -d +docker compose --profile local up deps --build --detach poetry run app ``` diff --git a/autogpt_platform/docker-compose.yml b/autogpt_platform/docker-compose.yml index 4689ad48ad5a..02079cd13e6e 100644 --- a/autogpt_platform/docker-compose.yml +++ b/autogpt_platform/docker-compose.yml @@ -143,7 +143,7 @@ services: file: ./supabase/docker/docker-compose.yml service: vector - local_dependencies: + deps: <<: *supabase-services profiles: - local From 93109745db83c34fda21d7762b9b84f18e47f012 Mon Sep 17 00:00:00 2001 From: Zamil Majdy Date: Wed, 23 Oct 2024 09:39:36 +0700 Subject: [PATCH 4/6] Add missing redis --- autogpt_platform/docker-compose.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/autogpt_platform/docker-compose.yml b/autogpt_platform/docker-compose.yml index 02079cd13e6e..a1ae16b3ea63 100644 --- a/autogpt_platform/docker-compose.yml +++ b/autogpt_platform/docker-compose.yml @@ -162,3 +162,4 @@ services: - analytics - db - vector + - redis From 2733f8153f51f5c3c0eed97e2c7107330ce6f854 Mon Sep 17 00:00:00 2001 From: Zamil Majdy Date: Wed, 23 Oct 2024 10:06:55 +0700 Subject: [PATCH 5/6] Cleanup enable_auth --- autogpt_platform/backend/backend/data/user.py | 4 ++-- autogpt_platform/backend/backend/server/ws_api.py | 4 ++-- autogpt_platform/backend/backend/util/settings.py | 4 ++-- autogpt_platform/backend/backend/util/test.py | 2 +- autogpt_platform/backend/test/__init__.py | 3 +++ 5 files changed, 10 insertions(+), 7 deletions(-) diff --git a/autogpt_platform/backend/backend/data/user.py b/autogpt_platform/backend/backend/data/user.py index 6c8696379e37..dca1b026951d 100644 --- a/autogpt_platform/backend/backend/data/user.py +++ b/autogpt_platform/backend/backend/data/user.py @@ -37,8 +37,8 @@ async def get_user_by_id(user_id: str) -> Optional[User]: return User.model_validate(user) if user else None -async def create_default_user(enable_auth: str) -> Optional[User]: - if not enable_auth.lower() == "true": +async def create_default_user(enable_auth: bool) -> Optional[User]: + if enable_auth: user = await prisma.user.find_unique(where={"id": DEFAULT_USER_ID}) if not user: user = await prisma.user.create( diff --git a/autogpt_platform/backend/backend/server/ws_api.py b/autogpt_platform/backend/backend/server/ws_api.py index 5e35693be444..8cbda22e1ed1 100644 --- a/autogpt_platform/backend/backend/server/ws_api.py +++ b/autogpt_platform/backend/backend/server/ws_api.py @@ -28,7 +28,7 @@ async def lifespan(app: FastAPI): docs_url = "/docs" if settings.config.app_env == AppEnvironment.LOCAL else None -app = FastAPI(lifespan=lifespan) +app = FastAPI(lifespan=lifespan, docs_url=docs_url) _connection_manager = None logger.info(f"CORS allow origins: {settings.config.backend_cors_allow_origins}") @@ -66,7 +66,7 @@ async def event_broadcaster(manager: ConnectionManager): async def authenticate_websocket(websocket: WebSocket) -> str: - if settings.config.enable_auth.lower() == "true": + if settings.config.enable_auth: token = websocket.query_params.get("token") if not token: await websocket.close(code=4001, reason="Missing authentication token") diff --git a/autogpt_platform/backend/backend/util/settings.py b/autogpt_platform/backend/backend/util/settings.py index b59156f696b8..f521bf104f7d 100644 --- a/autogpt_platform/backend/backend/util/settings.py +++ b/autogpt_platform/backend/backend/util/settings.py @@ -69,8 +69,8 @@ class Config(UpdateTrackingModel["Config"], BaseSettings): default="localhost", description="The default hostname of the Pyro server.", ) - enable_auth: str = Field( - default="false", + enable_auth: bool = Field( + default=True, description="If authentication is enabled or not", ) enable_credit: str = Field( diff --git a/autogpt_platform/backend/backend/util/test.py b/autogpt_platform/backend/backend/util/test.py index 704d6507e081..011faed9beac 100644 --- a/autogpt_platform/backend/backend/util/test.py +++ b/autogpt_platform/backend/backend/util/test.py @@ -31,7 +31,7 @@ async def __aenter__(self): await db.connect() await initialize_blocks() - await create_default_user("false") + await create_default_user(False) return self diff --git a/autogpt_platform/backend/test/__init__.py b/autogpt_platform/backend/test/__init__.py index e69de29bb2d1..d10438719da5 100644 --- a/autogpt_platform/backend/test/__init__.py +++ b/autogpt_platform/backend/test/__init__.py @@ -0,0 +1,3 @@ +import os + +os.environ["ENABLE_AUTH"] = "false" From b5636ffa993e59682c7cfc656ae42d13f7410e67 Mon Sep 17 00:00:00 2001 From: Zamil Majdy Date: Wed, 23 Oct 2024 10:27:31 +0700 Subject: [PATCH 6/6] One more auth cleanup --- autogpt_platform/backend/backend/data/user.py | 24 +++++++++---------- autogpt_platform/backend/backend/util/test.py | 2 +- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/autogpt_platform/backend/backend/data/user.py b/autogpt_platform/backend/backend/data/user.py index dca1b026951d..477b3bae6526 100644 --- a/autogpt_platform/backend/backend/data/user.py +++ b/autogpt_platform/backend/backend/data/user.py @@ -37,19 +37,17 @@ async def get_user_by_id(user_id: str) -> Optional[User]: return User.model_validate(user) if user else None -async def create_default_user(enable_auth: bool) -> Optional[User]: - if enable_auth: - user = await prisma.user.find_unique(where={"id": DEFAULT_USER_ID}) - if not user: - user = await prisma.user.create( - data={ - "id": DEFAULT_USER_ID, - "email": "default@example.com", - "name": "Default User", - } - ) - return User.model_validate(user) - return None +async def create_default_user() -> Optional[User]: + user = await prisma.user.find_unique(where={"id": DEFAULT_USER_ID}) + if not user: + user = await prisma.user.create( + data={ + "id": DEFAULT_USER_ID, + "email": "default@example.com", + "name": "Default User", + } + ) + return User.model_validate(user) async def get_user_metadata(user_id: str) -> UserMetadataRaw: diff --git a/autogpt_platform/backend/backend/util/test.py b/autogpt_platform/backend/backend/util/test.py index 011faed9beac..d1e2d83f7f3f 100644 --- a/autogpt_platform/backend/backend/util/test.py +++ b/autogpt_platform/backend/backend/util/test.py @@ -31,7 +31,7 @@ async def __aenter__(self): await db.connect() await initialize_blocks() - await create_default_user(False) + await create_default_user() return self