Skip to main content

tool_gate_http/
tool_gate_http.rs

1//! Tool gate + real read-only HTTP GET after ALLOW.
2//!
3//! - `http_get` on allow-list → network GET (body truncated)
4//! - deny-list tools → no network, audited
5//!
6//! ```bash
7//! cargo run --example tool_gate_http
8//! ```
9
10use hashbrown::HashMap;
11use omega_cognitive_kernel::types::Action;
12use omega_cognitive_kernel::{AuditLog, Executor, Result, ToolGateExecutor};
13use std::env::temp_dir;
14use std::path::PathBuf;
15
16fn tool(name: &str, url: Option<&str>) -> Action {
17    let mut params = HashMap::new();
18    if let Some(u) = url {
19        params.insert("url".into(), serde_json::json!(u));
20    }
21    Action {
22        name: name.into(),
23        params,
24        constraints_checked: true,
25    }
26}
27
28fn main() -> Result<()> {
29    let root: PathBuf = temp_dir().join(format!(
30        "omega-tool-http-{}",
31        omega_cognitive_kernel::new_uuid()
32    ));
33    std::fs::create_dir_all(&root).ok();
34    let state_marker = root.join("state.json");
35    let audit_path = AuditLog::beside_state(&state_marker).path;
36
37    println!("╔══════════════════════════════════════════════════╗");
38    println!("║  Tool gate + real http_get (GET only)            ║");
39    println!("╚══════════════════════════════════════════════════╝");
40    println!("audit: {}", audit_path.display());
41    println!();
42
43    let mut gate = ToolGateExecutor::new(["http_get".into(), "search_docs".into()])
44        .deny([
45            "delete_database".into(),
46            "shell_exec".into(),
47            "http_post".into(), // POST not implemented — deny by policy
48        ])
49        .with_audit(AuditLog {
50            path: audit_path.clone(),
51        })
52        .enable_http_get(true)
53        .fail_closed(false)
54        .echo(true);
55
56    // Public, stable endpoint (example.com).
57    let proposals = [
58        tool("http_get", Some("https://example.com")),
59        tool("delete_database", None), // DENY — no network
60        tool("search_docs", None),     // ALLOW simulated
61        tool("http_get", Some("file:///etc/passwd")), // should fail scheme check if reached
62    ];
63
64    println!("── proposals ──");
65    // Run one-by-one so a scheme error on last doesn't hide earlier results.
66    for p in &proposals {
67        match gate.execute(std::slice::from_ref(p)) {
68            Ok(rows) => {
69                for r in rows {
70                    let status = r.get("status").and_then(|v| v.as_str()).unwrap_or("?");
71                    let action = r.get("action").and_then(|v| v.as_str()).unwrap_or("?");
72                    if status == "ok" && r.get("http_status").is_some() {
73                        println!(
74                            "  OK  {action} http={} truncated={:?} preview_len={}",
75                            r["http_status"],
76                            r.get("truncated"),
77                            r.get("body_preview")
78                                .and_then(|v| v.as_str())
79                                .map(|s| s.len())
80                                .unwrap_or(0)
81                        );
82                    } else {
83                        println!("  {status} {action} {r}");
84                    }
85                }
86            }
87            Err(e) => println!("  ERR {e}"),
88        }
89    }
90
91    let sum = AuditLog {
92        path: audit_path.clone(),
93    }
94    .summarize()?;
95    println!("\n── audit ──");
96    println!(
97        "allowed={} denied={} events={}",
98        sum.tools_allowed, sum.tools_denied, sum.total_events
99    );
100
101    println!("\n── takeaway ──");
102    println!("1. DENY paths never open sockets.");
103    println!("2. ALLOW http_get can perform a real GET (truncated body).");
104    println!("3. Still not a full LLM proxy — one read-only tool behind the gate.");
105
106    let _ = std::fs::remove_dir_all(&root);
107    Ok(())
108}