# Gateway & Auth Layer - DIDI Backend ๐Ÿ” ## Quick Start ๐Ÿš€ ```bash # Recommended: Start via unified deployment manager ./deploy/didi.sh staging start ``` **Staging URLs:** | Service | URL | |---------|-----| | Kong Gateway | http://localhost:18100 | | Kong Admin API | http://localhost:18101 | | Kong Manager UI | http://localhost:18102 | | Keycloak | http://localhost:18280 | ## Overview ๐Ÿ” The Gateway & Auth Layer provides API management and authentication services: ### Services | Service | Container Name | Staging Ports | Purpose | |---------|---------------|---------------|---------| | **didiKong** | `staging-gatewayAuthLayer-kong` | 18100, 18101, 18102 | API Gateway, routing, rate limiting | | **didiKeycloak** | `staging-gatewayAuthLayer-keycloak` | 18280 | Identity provider, SSO, OAuth2/OIDC | ## Architecture ๐Ÿ—๏ธ ``` โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ Gateway & Auth Layer โ”‚ โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค โ”‚ didiKong โ”‚ didiKeycloak โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ โ€ข API Gateway โ”‚ โ€ข Identity Provider โ”‚ โ”‚ โ€ข Route Management โ”‚ โ€ข User Management โ”‚ โ”‚ โ€ข Rate Limiting โ”‚ โ€ข OAuth2/OIDC โ”‚ โ”‚ โ€ข CORS Handling โ”‚ โ€ข Custom Theme โ”‚ โ”‚ โ€ข Load Balancing โ”‚ โ€ข Realm Import โ”‚ โ”‚ โ”‚ โ”‚ โ”‚ Ports: 18100-18102 โ”‚ Port: 18280 โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ โ”‚ โ”‚ โ–ผ โ–ผ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ Protected Services โ”‚ โ”‚ โ€ข Orchestrator API (port 18000) โ”‚ โ”‚ โ€ข Analysis Service (port 18004) โ”‚ โ”‚ โ€ข Admin Dashboard (port 13003) โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ ``` ## Service Communication Flow ๐Ÿ“ฌ ``` Client Request โ†’ Kong Gateway (18100) โ†’ Route Rules โ†’ Backend Service โ†“ Rate Limiting โ†“ CORS Headers โ†“ (Optional) Keycloak Auth ``` ## Kong Configuration ๐Ÿ› ๏ธ ### DB-less Mode Kong runs in declarative (DB-less) mode with configuration in `didiKong/declarative/kong.yml` ### Configured Routes (Staging) - `/api/v1/catalog/*` โ†’ Orchestrator (18000) - `/api/v1/pipelines/*` โ†’ Orchestrator (18000) - `/api/v1/runs/*` โ†’ Orchestrator (18000) - `/orchestrator/health` โ†’ Orchestrator health check - `/analysis/health` โ†’ Analysis service health check - `/didiai/*` โ†’ AI Gateway (via DIDIAI_GATEWAY_URL) - `/admin/*` โ†’ Admin Dashboard (13003) ### Enabled Plugins - **CORS**: Cross-origin resource sharing - **Rate Limiting**: 100/min, 2000/hr, 10000/day - **Request ID**: UUID tracking with X-Request-ID - **Size Limiting**: 100MB max for media files - **Response Transform**: Add gateway headers ## Keycloak Configuration ๐Ÿ”‘ ### Admin Access - **URL**: http://localhost:8280 (standalone) or http://localhost:18280 (staging) - **Username**: admin - **Password**: keycloak123 ### Imported Realm - **Realm Name**: misinformation - **Theme**: misinformation-theme (custom) - **Location**: `didiKeycloak/realm-import/misinformation-realm.json` ### Pre-configured Elements - Client applications - User roles and groups - Authentication flows - Custom login theme ## Quick Commands ๐ŸŽฏ ```bash # Service Management make up # Start both services make down # Stop both services make restart # Restart both services make status # Check service status make logs # View logs for both services # Individual Service Control make up-kong # Start only Kong make up-keycloak # Start only Keycloak make logs-kong # View Kong logs make logs-keycloak # View Keycloak logs # Kong Management make kong-reload # Reload Kong configuration make kong-validate # Validate kong.yml syntax # Keycloak Management make keycloak-export # Export current realm configuration # Maintenance make clean # Remove containers and volumes make rebuild # Rebuild all images make health # Health check both services ``` ## Environment Configuration ๐Ÿ” ### Kong Environment ```env KONG_DATABASE=off # DB-less mode KONG_DECLARATIVE_CONFIG=/kong/declarative/kong.yml KONG_PROXY_LISTEN=0.0.0.0:8000 KONG_ADMIN_LISTEN=0.0.0.0:8001 ``` ### Keycloak Environment ```env KEYCLOAK_ADMIN=admin KEYCLOAK_ADMIN_PASSWORD=keycloak123 KC_DB_URL=jdbc:postgresql://dataLayer-postgres:5432/keycloak_db KC_DB_USERNAME=postgres KC_DB_PASSWORD=postgres123 ``` ## Testing the Gateway ๐Ÿงช ### Test Kong Gateway (Staging) ```bash # Check Kong status curl http://localhost:18101/status # Test orchestrator route through Kong curl http://localhost:18100/orchestrator/health # Test analysis route through Kong curl http://localhost:18100/analysis/health ``` ### Test Keycloak (Staging) ```bash # Check Keycloak health curl http://localhost:18280/health/ready # Access Keycloak admin console open http://localhost:18280 ``` ## Troubleshooting ๐Ÿ”ง ### Kong won't start? ```bash # Check configuration validity make kong-validate # Check logs make logs-kong # Verify declarative config exists ls -la didiKong/declarative/kong.yml ``` ### Keycloak won't start? ```bash # Check if database exists docker exec dataLayer-postgres psql -U postgres -c "\l" | grep keycloak_db # Create database if missing make init-db # Check logs make logs-keycloak ``` ### Services can't connect? ```bash # Verify network exists docker network ls | grep didi-backend # Check all services are on same network docker inspect gatewayAuthLayer-kong | grep NetworkMode ``` ## Security Considerations ๐Ÿ›ก๏ธ 1. **Change default passwords** in production 2. **Enable HTTPS** for all services 3. **Configure proper CORS origins** (not wildcard) 4. **Set up proper rate limiting** per consumer 5. **Enable authentication** on sensitive routes 6. **Use secrets management** for credentials ## Integration with Other Layers ๐Ÿ”— ### Prerequisites - Data Layer must be running (PostgreSQL for Keycloak) - Orchestration Layer services for API routing - Network `didi-backend` must exist ### Downstream Services - UI Layer will use Kong Gateway for API access - All services can integrate with Keycloak for SSO ## Development ๐Ÿ› ๏ธ ### Access Service Shells ```bash make shell-kong # Kong shell make shell-keycloak # Keycloak shell ``` ### Modify Kong Routes 1. Edit `didiKong/declarative/kong.yml` 2. Validate: `make kong-validate` 3. Reload: `make kong-reload` ### Export Keycloak Configuration ```bash make keycloak-export # Exported to didiKeycloak/realm-export/ ``` ## Next Steps ๐Ÿ“‹ 1. Configure Keycloak clients for each service 2. Set up Kong OAuth2 plugin with Keycloak 3. Add service-specific rate limiting 4. Configure monitoring and alerting 5. Set up SSL/TLS termination --- **Version**: 1.0.0 **Network**: `didi-backend` **Project**: `didiBackend`