mirror of
https://github.com/outbackdingo/optimclaw.git
synced 2026-08-31 08:39:24 +00:00
* feat: multi-tenant auth with per-user scoping Multi-user authentication and authorization for IronClaw gateway: - Token-based auth mapping tokens to user IDs via GATEWAY_USER_TOKENS - Per-user SSE broadcast scoping - Per-user rate limiting with poisoned lock recovery - Handler auth and ownership checks for jobs, settings, routines - Extension secrets scoped per-user - Chat handlers use authenticated identity - Reverse proxy deployment documentation - Comprehensive integration tests for auth, SSE, rate limiting, and job isolation * fix: scope memory tools per-user in multi-tenant mode Memory tools (search, write, read, tree) held a single workspace created at startup with GATEWAY_USER_ID. In multi-tenant mode, all users' tool calls searched the default user's scope. Add WorkspaceResolver trait that resolves workspaces per-request using JobContext.user_id. In single-user mode, returns the startup workspace. In multi-tenant mode (GATEWAY_USER_TOKENS configured), creates and caches per-user workspaces on demand. Includes regression tests for workspace resolution and user isolation. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]> * fix: comprehensive multi-tenant isolation audit Address all review findings from @serrrfirat plus 7 additional gaps found via full security audit: Reviewer findings (5): - WorkspacePool now applies search config, memory layers, embedding cache, identity read scopes, and global config scopes (was bare) - jobs_summary_handler uses per-user queries instead of global counters - jobs_prompt_handler restructured to not 404 agent jobs + ownership check - jobs_restart_handler agent branch now verifies user ownership - agent_job_summary_for_user added to Database trait + both backends Audit findings (7): - Delete dead handlers/memory.rs (stale copies with no auth) - Add AuthenticatedUser to logs_events, logs_level_get, logs_level_set - Add AuthenticatedUser to extensions_tools_handler, gateway_status_handler - Add auth + ownership checks to all 6 routines handlers - Add auth to all 4 skills handlers with audit logging on mutations - Scope extension setup SSE broadcast to user (broadcast_for_user) - Fix pre-existing test compilation errors in extensions/manager.rs 17 new multi-tenant isolation tests covering: - WorkspacePool config propagation and scope merging - Jobs handler per-user isolation (summary, restart, prompt, cancel) - Routines handler auth enforcement and cross-user rejection - Auth middleware enforcement on logs, skills, status endpoints Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]> * fix: second-pass multi-tenant audit — scope SSE broadcasts, DB queries, dead handlers Second audit pass applying learned patterns across the codebase: - OAuth callback SSE broadcasts now use broadcast_for_user (lines 773, 912) - jobs_list_handler uses list_agent_jobs_for_user instead of fetching all users' jobs and filtering in Rust - list_agent_jobs_for_user added to Database trait + postgres + libsql - Dead handler files (extensions.rs, static_files.rs) hardened with AuthenticatedUser to prevent auth regression if migrated Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]> * fix: address review findings — token hashing, broadcast scoping, error handling Security fixes: - Hash tokens with SHA-256 at construction time so authentication compares fixed-size 32-byte digests, eliminating length-oracle timing leaks - Scope auth SSE broadcasts per-user in chat_auth_token_handler — AuthRequired/AuthCompleted events were leaking across tenants - Propagate DB errors in restart handlers instead of silently swallowing via `if let Ok(Some(...))` pattern Code quality: - Log SSE serialization failures instead of silently producing empty strings via unwrap_or_default() - Remove dead `pub type AuthState = MultiAuthState` alias - Replace `.unwrap()` with `Arc::clone(db)` in app.rs multi-tenant workspace setup (db is guaranteed Some in context, but unwrap violates project convention) - Fix telegram setup test to inject UserIdentity into request extensions (handler now requires AuthenticatedUser) - Add safety comments on test-only expect/unwrap calls for CI - Apply cargo fmt to fix pre-existing formatting Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]> * fix: address review findings — unify workspace pool, fix SSE regression, cache job owners - Unify WorkspacePool and PerUserWorkspaceResolver: WorkspacePool now implements WorkspaceResolver, eliminating duplicate per-user workspace construction logic. app.rs uses WorkspacePool directly. - Fix sse_tx: None scheduler regression: change scheduler/worker SSE broadcasting from broadcast::Sender<SseEvent> to Arc<SseManager>, restoring SSE event delivery for scheduled agent jobs. - Cache job owner in orchestrator: add job_owner_cache to OrchestratorState so job_event_handler avoids a DB round-trip on every event after the first per job. - Deduplicate ext_user_id computation in main.rs. - Remove unused _gateway_state variable. - Fix pre-existing test: first_token() returns None in multi-user mode by design; align test assertion. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]> * style: fix formatting in app.rs Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]> * refactor: extract memory handlers back into handlers/memory.rs Move memory API handlers out of server.rs into their own module, consistent with how jobs, routines, and skills handlers are organized. The resolve_workspace() helper moves with them since it is only used by memory handlers. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]> --------- Co-authored-by: Claude Opus 4.6 (1M context) <[email protected]> Co-authored-by: [email protected] <[email protected]>
198 lines
7.6 KiB
Rust
198 lines
7.6 KiB
Rust
//! Orchestrator for managing sandboxed worker containers.
|
|
//!
|
|
//! The orchestrator runs in the main agent process and provides:
|
|
//! - An internal HTTP API for worker communication (LLM proxy, status, secrets)
|
|
//! - Per-job bearer token authentication
|
|
//! - Container lifecycle management (create, monitor, stop)
|
|
//!
|
|
//! ```text
|
|
//! ┌───────────────────────────────────────────────┐
|
|
//! │ Orchestrator │
|
|
//! │ │
|
|
//! │ Internal API (default :50051, configurable) │
|
|
//! │ POST /worker/{id}/llm/complete │
|
|
//! │ POST /worker/{id}/llm/complete_with_tools │
|
|
//! │ GET /worker/{id}/job │
|
|
//! │ GET /worker/{id}/credentials │
|
|
//! │ POST /worker/{id}/status │
|
|
//! │ POST /worker/{id}/complete │
|
|
//! │ │
|
|
//! │ ContainerJobManager │
|
|
//! │ create_job() -> container + token │
|
|
//! │ stop_job() │
|
|
//! │ list_jobs() │
|
|
//! │ │
|
|
//! │ TokenStore │
|
|
//! │ per-job bearer tokens (in-memory only) │
|
|
//! │ per-job credential grants (in-memory only) │
|
|
//! └───────────────────────────────────────────────┘
|
|
//! ```
|
|
|
|
pub mod api;
|
|
pub mod auth;
|
|
pub mod job_manager;
|
|
pub mod reaper;
|
|
|
|
pub use api::OrchestratorApi;
|
|
pub use auth::{CredentialGrant, TokenStore};
|
|
pub use job_manager::{
|
|
CompletionResult, ContainerHandle, ContainerJobConfig, ContainerJobManager, JobMode,
|
|
};
|
|
pub use reaper::{ReaperConfig, SandboxReaper};
|
|
|
|
use std::collections::{HashMap, VecDeque};
|
|
use std::sync::Arc;
|
|
|
|
use tokio::sync::{Mutex, broadcast};
|
|
use uuid::Uuid;
|
|
|
|
use crate::channels::web::types::SseEvent;
|
|
use crate::db::Database;
|
|
use crate::llm::LlmProvider;
|
|
use crate::secrets::SecretsStore;
|
|
|
|
/// Resolve the orchestrator port from the `ORCHESTRATOR_PORT` environment
|
|
/// variable, falling back to 50051.
|
|
fn resolve_orchestrator_port() -> u16 {
|
|
std::env::var("ORCHESTRATOR_PORT")
|
|
.ok()
|
|
.and_then(|v| v.parse().ok())
|
|
.unwrap_or(50051)
|
|
}
|
|
|
|
/// Result of orchestrator setup, containing all handles needed by the agent.
|
|
pub struct OrchestratorSetup {
|
|
pub container_job_manager: Option<Arc<ContainerJobManager>>,
|
|
pub job_event_tx: Option<broadcast::Sender<(Uuid, String, SseEvent)>>,
|
|
pub prompt_queue: Arc<Mutex<HashMap<Uuid, VecDeque<api::PendingPrompt>>>>,
|
|
pub docker_status: crate::sandbox::DockerStatus,
|
|
}
|
|
|
|
/// Detect Docker availability, create the container job manager, and start
|
|
/// the orchestrator internal API in the background.
|
|
pub async fn setup_orchestrator(
|
|
config: &crate::config::Config,
|
|
llm: &Arc<dyn LlmProvider>,
|
|
db: Option<&Arc<dyn Database>>,
|
|
secrets_store: Option<&Arc<dyn SecretsStore + Send + Sync>>,
|
|
) -> OrchestratorSetup {
|
|
let prompt_queue = Arc::new(Mutex::new(
|
|
HashMap::<Uuid, VecDeque<api::PendingPrompt>>::new(),
|
|
));
|
|
|
|
let docker_status = if config.sandbox.enabled {
|
|
let detection = crate::sandbox::check_docker().await;
|
|
match detection.status {
|
|
crate::sandbox::DockerStatus::Available => {
|
|
tracing::info!("Docker is available");
|
|
}
|
|
crate::sandbox::DockerStatus::NotInstalled => {
|
|
tracing::warn!(
|
|
"Docker is not installed -- sandbox disabled for this session. {}",
|
|
detection.platform.install_hint()
|
|
);
|
|
}
|
|
crate::sandbox::DockerStatus::NotRunning => {
|
|
tracing::warn!(
|
|
"Docker is installed but not running -- sandbox disabled for this session. {}",
|
|
detection.platform.start_hint()
|
|
);
|
|
}
|
|
crate::sandbox::DockerStatus::Disabled => {}
|
|
}
|
|
detection.status
|
|
} else {
|
|
crate::sandbox::DockerStatus::Disabled
|
|
};
|
|
|
|
let (job_event_tx, container_job_manager) = if config.sandbox.enabled && docker_status.is_ok() {
|
|
let (tx, _) = broadcast::channel(256);
|
|
let job_event_tx = Some(tx);
|
|
|
|
let token_store = TokenStore::new();
|
|
let orchestrator_port = resolve_orchestrator_port();
|
|
let job_config = ContainerJobConfig {
|
|
image: config.sandbox.image.clone(),
|
|
memory_limit_mb: config.sandbox.memory_limit_mb,
|
|
cpu_shares: config.sandbox.cpu_shares,
|
|
orchestrator_port,
|
|
claude_code_api_key: std::env::var("ANTHROPIC_API_KEY").ok(),
|
|
claude_code_oauth_token: crate::config::ClaudeCodeConfig::extract_oauth_token(),
|
|
claude_code_model: config.claude_code.model.clone(),
|
|
claude_code_max_turns: config.claude_code.max_turns,
|
|
claude_code_memory_limit_mb: config.claude_code.memory_limit_mb,
|
|
claude_code_allowed_tools: config.claude_code.allowed_tools.clone(),
|
|
};
|
|
let jm = Arc::new(ContainerJobManager::new(job_config, token_store.clone()));
|
|
|
|
let orchestrator_state = api::OrchestratorState {
|
|
llm: Arc::clone(llm),
|
|
job_manager: Arc::clone(&jm),
|
|
token_store,
|
|
job_event_tx: job_event_tx.clone(),
|
|
prompt_queue: Arc::clone(&prompt_queue),
|
|
store: db.cloned(),
|
|
secrets_store: secrets_store.cloned(),
|
|
user_id: "default".to_string(),
|
|
job_owner_cache: Arc::new(std::sync::RwLock::new(std::collections::HashMap::new())),
|
|
};
|
|
|
|
tokio::spawn(async move {
|
|
if let Err(e) = OrchestratorApi::start(orchestrator_state, orchestrator_port).await {
|
|
tracing::error!("Orchestrator API failed: {}", e);
|
|
}
|
|
});
|
|
|
|
if config.claude_code.enabled {
|
|
tracing::info!(
|
|
"Claude Code sandbox mode available (model: {}, max_turns: {})",
|
|
config.claude_code.model,
|
|
config.claude_code.max_turns
|
|
);
|
|
}
|
|
(job_event_tx, Some(jm))
|
|
} else {
|
|
(None, None)
|
|
};
|
|
|
|
OrchestratorSetup {
|
|
container_job_manager,
|
|
job_event_tx,
|
|
prompt_queue,
|
|
docker_status,
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::config::helpers::lock_env;
|
|
|
|
#[test]
|
|
fn resolve_orchestrator_port_from_env() {
|
|
let _guard = lock_env();
|
|
|
|
// Safety: env-var mutation requires unsafe in edition 2024;
|
|
// lock_env() serializes concurrent access from other test threads.
|
|
|
|
// Absent env var → default 50051
|
|
unsafe { std::env::remove_var("ORCHESTRATOR_PORT") };
|
|
assert_eq!(resolve_orchestrator_port(), 50051);
|
|
|
|
// Valid custom port
|
|
unsafe { std::env::set_var("ORCHESTRATOR_PORT", "50052") };
|
|
assert_eq!(resolve_orchestrator_port(), 50052);
|
|
|
|
// Non-numeric value → fallback to default
|
|
unsafe { std::env::set_var("ORCHESTRATOR_PORT", "not_a_port") };
|
|
assert_eq!(resolve_orchestrator_port(), 50051);
|
|
|
|
// Out of u16 range → fallback to default
|
|
unsafe { std::env::set_var("ORCHESTRATOR_PORT", "99999") };
|
|
assert_eq!(resolve_orchestrator_port(), 50051);
|
|
|
|
// Cleanup
|
|
unsafe { std::env::remove_var("ORCHESTRATOR_PORT") };
|
|
}
|
|
}
|