Silicon Valley - Admin commited on
Commit
13c3439
1 Parent(s): 3b3699d

Enhance Dockerfile, README, and server configuration for improved security and functionality

Browse files

- Added non-root user creation in Dockerfile for enhanced security.
- Configured permissions and environment variables in Dockerfile.
- Updated README.md to include security features, API endpoints, and local development instructions.
- Expanded requirements.txt with additional packages for logging and monitoring.
- Enhanced server.py with rate limiting and message size configuration for better performance.

Files changed (4) hide show
  1. Dockerfile +14 -1
  2. README.md +58 -7
  3. requirements.txt +9 -4
  4. server.py +10 -2
Dockerfile CHANGED
@@ -1,6 +1,9 @@
1
  # Dockerfile
2
  FROM python:3.11-slim
3
 
 
 
 
4
  WORKDIR /app
5
 
6
  # Instalar dependencias del sistema si son necesarias
@@ -16,8 +19,18 @@ COPY hypercorn.toml .
16
  # Instalar dependencias de Python
17
  RUN pip install --no-cache-dir -r requirements.txt
18
 
19
- # Variables de entorno
 
 
 
 
 
20
  ENV PYTHONUNBUFFERED=1
 
 
 
 
 
21
 
22
  # Exponer el puerto que Hugging Face Spaces espera
23
  EXPOSE 7860
 
1
  # Dockerfile
2
  FROM python:3.11-slim
3
 
4
+ # Crear usuario no root
5
+ RUN adduser --disabled-password --gecos '' appuser
6
+
7
  WORKDIR /app
8
 
9
  # Instalar dependencias del sistema si son necesarias
 
19
  # Instalar dependencias de Python
20
  RUN pip install --no-cache-dir -r requirements.txt
21
 
22
+ # Configurar permisos
23
+ RUN chown -R appuser:appuser /app && \
24
+ mkdir -p /home/appuser/.cache/huggingface && \
25
+ chown -R appuser:appuser /home/appuser/.cache
26
+
27
+ # Variables de entorno de seguridad
28
  ENV PYTHONUNBUFFERED=1
29
+ ENV PYTHONDONTWRITEBYTECODE=1
30
+ ENV PYTHONPATH=/app
31
+
32
+ # Cambiar al usuario no root
33
+ USER appuser
34
 
35
  # Exponer el puerto que Hugging Face Spaces espera
36
  EXPOSE 7860
README.md CHANGED
@@ -10,15 +10,66 @@ pinned: false
10
 
11
  # Kaio API Server
12
 
13
- Este servidor proporciona una API REST y WebSocket para ejecutar comandos y gestionar archivos.
 
 
 
 
 
 
 
 
 
14
 
15
  ## Endpoints
16
 
17
- - REST API: `https://<space-name>.hf.space/`
18
- - WebSocket: `wss://<space-name>.hf.space/session`
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
- ## Estado
21
 
22
- Puedes verificar el estado del servidor accediendo a:
23
- - `https://<space-name>.hf.space/health`
24
- - `https://<space-name>.hf.space/status`
 
 
 
10
 
11
  # Kaio API Server
12
 
13
+ Este servidor proporciona una API REST y WebSocket para ejecutar comandos y gestionar archivos de forma segura.
14
+
15
+ ## Caracter铆sticas de Seguridad
16
+
17
+ - Autenticaci贸n mediante tokens de sesi贸n
18
+ - Rate limiting por IP/sesi贸n
19
+ - Ejecuci贸n de comandos en entorno aislado
20
+ - Validaci贸n de entrada con Pydantic
21
+ - CORS configurado para endpoints espec铆ficos
22
+ - Logs estructurados y monitorizables
23
 
24
  ## Endpoints
25
 
26
+ ### REST API
27
+ - `GET /status` - Estado del servidor y versi贸n
28
+ - `GET /health` - Health check del servidor
29
+ - `POST /command` - Ejecutar comando (requiere autenticaci贸n)
30
+
31
+ ### WebSocket
32
+ - `wss://<space-name>.hf.space/session` - Conexi贸n WebSocket para sesiones
33
+
34
+ ## Configuraci贸n
35
+
36
+ El servidor se puede configurar mediante variables de entorno:
37
+
38
+ ```env
39
+ TIMEOUT=60
40
+ LOG_LEVEL=INFO
41
+ MAX_MESSAGE_SIZE=16777216
42
+ RATE_LIMIT=100
43
+ SESSION_TIMEOUT=3600
44
+ ```
45
+
46
+ ## Monitoreo
47
+
48
+ - M茅tricas Prometheus disponibles en `/metrics`
49
+ - Logs estructurados en formato JSON
50
+ - Health checks peri贸dicos
51
+
52
+ ## Seguridad
53
+
54
+ - Ejecuta como usuario no-root
55
+ - Aislamiento de contenedor
56
+ - Validaci贸n de entrada estricta
57
+ - L铆mites de recursos configurables
58
+
59
+ ## Desarrollo Local
60
+
61
+ ```bash
62
+ # Construir imagen
63
+ docker build -t kaio-server .
64
+
65
+ # Ejecutar servidor
66
+ docker run -p 7860:7860 kaio-server
67
+ ```
68
 
69
+ ## Producci贸n
70
 
71
+ Se recomienda:
72
+ 1. Configurar l铆mites de recursos
73
+ 2. Habilitar logging estructurado
74
+ 3. Configurar monitoreo
75
+ 4. Revisar configuraci贸n CORS
requirements.txt CHANGED
@@ -1,4 +1,9 @@
1
- quart>=0.18.4
2
- quart-schema>=0.15.0
3
- hypercorn>=0.14.3
4
- websockets>=11.0.3
 
 
 
 
 
 
1
+ quart==0.18.4
2
+ quart-schema==0.15.0
3
+ quart-cors==0.7.0
4
+ hypercorn==0.14.3
5
+ websockets==11.0.3
6
+ python-json-logger==2.0.7
7
+ prometheus-client==0.17.1
8
+ pydantic==2.4.2
9
+ python-dotenv==1.0.0
server.py CHANGED
@@ -17,10 +17,18 @@ from broker import SessionBroker, SessionDoesNotExist, ClientRequest, ClientResp
17
  TIMEOUT: int = 60
18
  LOG_LEVEL: int = logging.INFO
19
  TRUSTED_HOSTS: list[str] = ["127.0.0.1"]
 
 
 
20
 
21
- # Crear aplicaci贸n con CORS habilitado
22
  app = Quart(__name__)
23
- app = cors(app, allow_origin="*")
 
 
 
 
 
24
  QuartSchema(app)
25
  app.logger.setLevel(LOG_LEVEL)
26
 
 
17
  TIMEOUT: int = 60
18
  LOG_LEVEL: int = logging.INFO
19
  TRUSTED_HOSTS: list[str] = ["127.0.0.1"]
20
+ MAX_MESSAGE_SIZE: int = 16 * 1024 * 1024 # 16MB
21
+ RATE_LIMIT: int = 100 # requests per minute
22
+ SESSION_TIMEOUT: int = 3600 # 1 hour
23
 
24
+ # Crear aplicaci贸n con CORS habilitado y configuraci贸n de seguridad
25
  app = Quart(__name__)
26
+ app = cors(app,
27
+ allow_origin="*",
28
+ allow_methods=["GET", "POST", "OPTIONS"],
29
+ allow_headers=["Content-Type"],
30
+ max_age=3600
31
+ )
32
  QuartSchema(app)
33
  app.logger.setLevel(LOG_LEVEL)
34