Skip to content

Commit 088725e

Browse files
committed
Add authentication part to picod deisgn
Signed-off-by: VanderChen <vanderchen@outlook.com>
1 parent 0fd9e00 commit 088725e

File tree

1 file changed

+105
-114
lines changed

1 file changed

+105
-114
lines changed

docs/design/PicoD-Design.md

Lines changed: 105 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -217,106 +217,122 @@ graph TB
217217

218218
#### 3. Authentication & Authorization
219219

220-
To ensure secure communication between clients and PicoD, we adopt a JWT + JWKS-based authentication model. This approach eliminates the need for pre-injecting keys into warm sandboxes, which is incompatible with the warm pool strategy used by AgentCube.
220+
PicoD implements a secure, lightweight authentication system designed specifically for sandbox environments.
221221

222-
- **JWT (JSON Web Token):** Clients sign requests with a short-lived JWT using their private key.
222+
The core approach provides an **init interface** (`POST /api/init`) that establishes authentication credentials when a sandbox is allocated to an end user. The primary protection scenario is ensuring that **user-requested sandboxes can only be accessed by the designated user** - we only need to guarantee that the sandbox allocated to a user remains exclusive to that user throughout its lifecycle.
223223

224-
- **JWKS (JSON Web Key Set):** PicoD validates the JWT signature against public keys published at a JWKS URL configured in each sandbox.
224+
The authentication model balances security with operational simplicity, using client-generated tokens and one-time initialization to bind each sandbox securely to its designated end user.
225225

226-
- **Authorization:** Claims within the JWT (e.g., sub, aud, scope) determine which operations the client is permitted to perform.
227-
228-
This design ensures that pre-heated sandboxes can securely accept requests from dynamically assigned clients without requiring pre-injected secrets.
226+
##### Authentication Architecture
229227

230228
```mermaid
231229
sequenceDiagram
232-
participant Client as AgentCube Client
233-
participant APIServer as AgentCube API Server
234-
participant JWKS as JWKS Endpoint
235-
participant PicoD as PicoD (Auth Middleware)
236-
participant Service as PicoD Services
237-
238-
Client->>APIServer: Authenticate with Kubernetes token
239-
APIServer-->>Client: Issue JWT (signed with private key)
240-
Client->>APIServer: HTTP Request<br/>Authorization: Bearer JWT
241-
APIServer->>PicoD: Call
242-
PicoD->>PicoD: Parse JWT header (alg, kid)
243-
alt Key cached
244-
PicoD->>PicoD: Load JWK by kid
245-
else Key not cached
246-
PicoD->>JWKS: Fetch JWKS
247-
JWKS-->>PicoD: JWKS (public keys)
248-
PicoD->>PicoD: Select JWK by kid
249-
end
250-
PicoD->>PicoD: Verify signature + validate claims
251-
alt Valid & Authorized
252-
PicoD->>Service: Execute/Filesystem operation
253-
Service-->>PicoD: Response
254-
PicoD-->>Client: 200 OK
255-
else Invalid/Unauthorized
256-
PicoD-->>Client: 401 Unauthorized / 403 Forbidden
257-
end
258-
259-
```
260-
261-
- **Client Authentication with API Server**
230+
participant Client as SDK Client
231+
participant Frontend as AgentCube Frontend
232+
participant PicoD as PicoD Daemon
262233
263-
- The client first authenticates against the AgentCube API Server using its Kubernetes token.
264-
- The API Server issues a JWT signed with the client’s private key.
265-
266-
- **Client Request to PicoD**
267-
268-
- The client sends an HTTP request to PicoD with the JWT in the `Authorization: Bearer <JWT>` header.
269-
- The request targets one of PicoD’s REST endpoints (`/api/execute`, `/api/files`, etc.).
270-
271-
- **JWT Parsing in PicoD**
234+
Note over Client, PicoD: Sandbox Allocation & Initialization
235+
Client->>Frontend: POST /api/init (token/public_key)
236+
Frontend->>PicoD: Forward POST /api/init (with internal auth)
237+
PicoD->>PicoD: Encrypt & store credentials locally
238+
PicoD-->>Frontend: 200 OK (init successful)
239+
Frontend-->>Client: 200 OK (init successful)
272240
273-
- PicoD extracts the JWT header and claims.
274-
- The `kid` (Key ID) is used to identify which public key should be used for verification.
275-
276-
- **JWKS Lookup**
241+
Note over Client, PicoD: Authenticated Operations
242+
Client->>PicoD: POST /api/execute (Authorization: Bearer <token>)
243+
PicoD->>PicoD: Validate token against stored credentials
244+
PicoD-->>Client: Command execution result
277245
278-
- PicoD checks its local JWKS cache for the key.
279-
- If not found, PicoD fetches the JWKS document from the configured JWKS URL.
280-
- The JWKS contains one or more public keys; PicoD selects the correct one based on `kid`.
281-
282-
- **Signature Verification & Claim Validation**
283-
284-
- PicoD verifies the JWT signature using the selected public key.
285-
- Claims are validated:
286-
- `iss` must match the trusted API Server.
287-
- `aud` must equal `picod`.
288-
- `exp` and `iat` must be valid.
289-
- `jti` checked for replay protection.
290-
291-
- **Authorization Enforcement**
292-
293-
- PicoD inspects claims like `scope` or `roles` to determine allowed operations.
294-
- Example: `scope=execute` permits `/api/execute`; `scope=files:read` permits file downloads.
295-
- If claims don’t authorize the request, PicoD returns `403 Forbidden`.
296-
297-
- **Response Handling**
298-
299-
- If valid and authorized, PicoD forwards the request to the appropriate service (Filesystem or Process).
300-
- The service executes the operation and returns a response.
301-
- PicoD sends back `200 OK` with the result.
302-
- If invalid, send back `401 Unauthorization` or `403 Forbindden`.
246+
Client->>PicoD: POST /api/files (Authorization: Bearer <token>)
247+
PicoD->>PicoD: Validate token
248+
PicoD-->>Client: File operation result
249+
```
250+
251+
##### Security Considerations
252+
253+
**1. One-Time Initialization**
254+
- Init interface can only be called once per sandbox lifecycle
255+
- Credentials cannot be modified after initial setup
256+
- Implementation includes atomic file operations to prevent race conditions
257+
258+
**2. Frontend-Only Access Control**
259+
- Init endpoint restricted to internal network calls from AgentCube frontend
260+
- Rate limiting on init endpoint to prevent brute force attempts
261+
262+
**3. Credential Security**
263+
- Client-generated tokens/keypairs ensure frontend never stores user credentials
264+
- Local encryption prevents credential exposure if container is compromised
265+
- Automatic credential cleanup on container termination
266+
267+
**4. Warmpool Compatibility**
268+
- Containers start without authentication credentials
269+
- Init interface called only when sandbox allocated to specific user
270+
271+
##### Core Authentication Components
272+
273+
**1. Initialization Interface**
274+
275+
- **Endpoint**: `POST /api/init`
276+
- **Purpose**: One-time setup of authentication credentials when sandbox is allocated to end user
277+
- **Access Control**: Frontend-only (protected against external access)
278+
- **Request Body**:
279+
280+
```json
281+
{
282+
"auth_type": "token|keypair",
283+
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
284+
"public_key": "-----BEGIN PUBLIC KEY-----\n...",
285+
"metadata": {
286+
"user_id": "user123",
287+
"session_id": "sess456",
288+
"allocated_at": "2025-11-25T14:30:00Z"
289+
}
290+
}
291+
```
292+
293+
- **Response**:
294+
295+
```json
296+
{
297+
"status": "initialized",
298+
"auth_type": "token",
299+
"expires_at": "2025-11-25T18:30:00Z"
300+
}
301+
```
302+
303+
**2. Token Storage & Encryption**
304+
305+
- **Local Storage**: Credentials encrypted and stored in `/var/lib/picod/auth.enc`
306+
- **Encryption**: AES-256-GCM with container-specific key derived from sandbox metadata
307+
- **File Permissions**: 600 (owner read/write only)
308+
- **Storage Format**:
309+
310+
```json
311+
{
312+
"auth_type": "token",
313+
"encrypted_credentials": "base64_encrypted_data",
314+
"salt": "random_salt",
315+
"initialized_at": "2025-11-25T14:30:00Z",
316+
"metadata": {
317+
"user_id": "user123",
318+
"session_id": "sess456"
319+
}
320+
}
321+
```
322+
323+
**3. Request Authentication**
324+
325+
All API requests (except `/api/init`) require authentication:
326+
327+
- **Header**: `Authorization: Bearer <token>`
328+
- **Validation Process**:
329+
1. Extract token from Authorization header
330+
2. Decrypt stored credentials
331+
3. Validate token signature/format
332+
4. Check token expiration (if applicable)
333+
5. Verify token matches stored credentials
303334

304-
### Alternative Approaches Considered
305335

306-
1. **Pre-injected Key Pairs (Cloud-Init, Environment Variables, Secret Mounts)**
307-
308-
- **Pros:** Simple to implement; widely used in VM/container setups.
309-
- **Cons:** Incompatible with warm pools (keys must be injected before sandbox assignment); insecure if environment variables are exposed; poor rotation support.
310-
311-
2. **Static Shared Secret (Environment Variable or Config File)**
312-
313-
- **Pros:** Very simple; no external dependency.
314-
- **Cons:** Weak security; no rotation; risk of leakage; unsuitable for multi-tenant environments.
315-
316-
3. **Mutual TLS (mTLS)**
317-
318-
- **Pros:** Strong authentication; proven in production.
319-
- **Cons:** Requires certificate distribution and management; heavy for lightweight sandboxes; not flexible for dynamic warm pools.
320336
#### 4. Core Capabilities
321337
PicoD provides a lightweight REST API that replaces traditional SSH‑based operations with secure, stateless HTTP endpoints. The two primary capabilities are code execution and file transfer, exposed via JSON or multipart requests.
322338

@@ -596,31 +612,6 @@ classDiagram
596612
597613
```
598614

599-
## Security Considerations
600-
601-
Because PicoD runs as a daemon inside sandbox environments, security is a critical design priority. The following measures ensure that execution and file operations remain isolated, authenticated, and controlled.
602-
603-
**Token Management**
604-
605-
- JWT required for all requests
606-
- Short-lived tokens validated via JWKS
607-
- Stateless, no token storage
608-
609-
**File Access Control**
610-
611-
- Path sanitization prevents directory traversal
612-
- Restricted to sandbox workspace only
613-
- Enforced by OS-level permissions
614-
615-
**Logging & Auditing**
616-
617-
- Centralized logging and audit handled by AgentCube APIServer
618-
619-
**Update & Patch Management**
620-
621-
- Minimal attack surface
622-
- Immutable, signed builds
623-
- Regular updates recommended
624615

625616
## Future Enhancements
626617

0 commit comments

Comments
 (0)