|
3 | 3 | # Keep LLM Gateway Demo |
4 | 4 | # |
5 | 5 | # Runs the keep-llm-gateway in front of the Anthropic API and demonstrates |
6 | | -# policy enforcement. Uses `claude -p` for streaming and curl for structured |
7 | | -# requests (redaction). |
| 6 | +# policy enforcement: streaming, redaction, and audit logging. |
8 | 7 | # |
9 | 8 | # Prerequisites: |
10 | | -# - claude CLI installed with valid credentials |
11 | | -# - ANTHROPIC_API_KEY set (for curl-based tests) |
| 9 | +# - ANTHROPIC_API_KEY set in your environment |
12 | 10 | # |
13 | 11 | # Usage: |
14 | 12 | # ./examples/llm-gateway-demo/demo.sh |
@@ -26,6 +24,11 @@ cleanup() { |
26 | 24 | } |
27 | 25 | trap cleanup EXIT |
28 | 26 |
|
| 27 | +if [ -z "${ANTHROPIC_API_KEY:-}" ]; then |
| 28 | + echo "ANTHROPIC_API_KEY is not set. Export it and re-run." |
| 29 | + exit 1 |
| 30 | +fi |
| 31 | + |
29 | 32 | echo "=== Keep LLM Gateway Demo ===" |
30 | 33 | echo "" |
31 | 34 |
|
@@ -36,7 +39,6 @@ echo " done" |
36 | 39 | echo "" |
37 | 40 |
|
38 | 41 | # ── Start gateway ───────────────────────────────────────────────── |
39 | | -# Generate config from template, filling in runtime paths. |
40 | 42 | sed \ |
41 | 43 | -e "s|RULES_DIR|$SCRIPT_DIR/rules|" \ |
42 | 44 | -e "s|LOG_OUTPUT|$DEMO_DIR/audit.jsonl|" \ |
@@ -49,67 +51,91 @@ sleep 1 |
49 | 51 | echo " Gateway running (PID $GW_PID)" |
50 | 52 | echo "" |
51 | 53 |
|
52 | | -# ── Test 1: claude -p through gateway ───────────────────────────── |
| 54 | +GW_URL="http://localhost:${GW_PORT}/v1/messages" |
| 55 | +GW_HEADERS=(-H "Content-Type: application/json" -H "anthropic-version: 2023-06-01" -H "x-api-key: $ANTHROPIC_API_KEY") |
| 56 | + |
| 57 | +# ── Test 1: Streaming request ───────────────────────────────────── |
53 | 58 | echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" |
54 | | -echo "Test 1: claude -p through the gateway (streaming)" |
| 59 | +echo "Test 1: Streaming request through the gateway" |
55 | 60 | echo "" |
56 | | -echo " ANTHROPIC_BASE_URL=http://localhost:${GW_PORT} claude -p 'What is 2+2?'" |
| 61 | + |
| 62 | +RESPONSE=$(curl -s -w "\n%{http_code}" "$GW_URL" "${GW_HEADERS[@]}" \ |
| 63 | + -d '{ |
| 64 | + "model": "claude-haiku-4-5-20251001", |
| 65 | + "max_tokens": 30, |
| 66 | + "stream": true, |
| 67 | + "messages": [{"role": "user", "content": "What is 2+2? Answer in one word."}] |
| 68 | + }' 2>&1) |
| 69 | + |
| 70 | +HTTP_CODE=$(echo "$RESPONSE" | tail -1) |
| 71 | +BODY=$(echo "$RESPONSE" | sed '$d') |
| 72 | + |
| 73 | +if [ "$HTTP_CODE" = "200" ]; then |
| 74 | + EVENT_COUNT=$(echo "$BODY" | grep -c "^event:" || true) |
| 75 | + echo " ✓ HTTP 200 — Received $EVENT_COUNT SSE events (buffered, policy-evaluated, replayed)" |
| 76 | +else |
| 77 | + echo " ✗ HTTP $HTTP_CODE — $BODY" |
| 78 | + echo "" |
| 79 | + echo " Check that ANTHROPIC_API_KEY is valid." |
| 80 | + exit 1 |
| 81 | +fi |
57 | 82 | echo "" |
58 | 83 |
|
59 | | -CLAUDE_OUTPUT=$(ANTHROPIC_BASE_URL="http://localhost:${GW_PORT}" \ |
60 | | - claude -p "What is 2+2? Answer in exactly one word, no punctuation." \ |
61 | | - --model claude-haiku-4-5-20251001 \ |
62 | | - --max-turns 1 \ |
63 | | - 2>&1) || true |
| 84 | +# ── Test 2: Non-streaming request ───────────────────────────────── |
| 85 | +echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" |
| 86 | +echo "Test 2: Non-streaming request through the gateway" |
| 87 | +echo "" |
| 88 | + |
| 89 | +RESPONSE=$(curl -s -w "\n%{http_code}" "$GW_URL" "${GW_HEADERS[@]}" \ |
| 90 | + -d '{ |
| 91 | + "model": "claude-haiku-4-5-20251001", |
| 92 | + "max_tokens": 30, |
| 93 | + "messages": [{"role": "user", "content": "What is 2+2? Answer in one word."}] |
| 94 | + }' 2>&1) |
| 95 | + |
| 96 | +HTTP_CODE=$(echo "$RESPONSE" | tail -1) |
| 97 | +BODY=$(echo "$RESPONSE" | sed '$d') |
64 | 98 |
|
65 | | -if [ -n "$CLAUDE_OUTPUT" ]; then |
66 | | - echo " ✓ Claude response: $CLAUDE_OUTPUT" |
| 99 | +if [ "$HTTP_CODE" = "200" ]; then |
| 100 | + ANSWER=$(echo "$BODY" | python3 -c "import json,sys; r=json.load(sys.stdin); print(r['content'][0]['text'])" 2>/dev/null || echo "$BODY") |
| 101 | + echo " ✓ HTTP 200 — Model said: $ANSWER" |
67 | 102 | else |
68 | | - echo " ✗ claude -p produced no output" |
69 | | - echo " Make sure claude is installed and has valid credentials." |
| 103 | + echo " ✗ HTTP $HTTP_CODE — $BODY" |
70 | 104 | fi |
71 | 105 | echo "" |
72 | 106 |
|
73 | | -# ── Test 2: Secret redaction ────────────────────────────────────── |
| 107 | +# ── Test 3: Secret redaction ────────────────────────────────────── |
74 | 108 | echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" |
75 | | -echo "Test 2: Secret redaction in tool results" |
| 109 | +echo "Test 3: Secret redaction in tool results" |
76 | 110 | echo "" |
77 | 111 | echo " Sending a tool_result containing an AWS key and password." |
78 | 112 | echo " The gateway redacts secrets before forwarding to the model." |
79 | 113 | echo "" |
80 | 114 |
|
81 | | -if [ -z "${ANTHROPIC_API_KEY:-}" ]; then |
82 | | - echo " ~ Skipped: ANTHROPIC_API_KEY not set (needed for curl-based tests)" |
| 115 | +RESPONSE=$(curl -s -w "\n%{http_code}" "$GW_URL" "${GW_HEADERS[@]}" \ |
| 116 | + -d '{ |
| 117 | + "model": "claude-haiku-4-5-20251001", |
| 118 | + "max_tokens": 100, |
| 119 | + "messages": [ |
| 120 | + {"role": "user", "content": "Read the .env file"}, |
| 121 | + {"role": "assistant", "content": [ |
| 122 | + {"type": "tool_use", "id": "tool_1", "name": "bash", "input": {"command": "cat .env"}} |
| 123 | + ]}, |
| 124 | + {"role": "user", "content": [ |
| 125 | + {"type": "tool_result", "tool_use_id": "tool_1", "content": "DB_HOST=localhost\nAWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE\npassword = hunter2\nAPP_NAME=myapp"} |
| 126 | + ]} |
| 127 | + ] |
| 128 | + }' 2>&1) |
| 129 | + |
| 130 | +HTTP_CODE=$(echo "$RESPONSE" | tail -1) |
| 131 | +BODY=$(echo "$RESPONSE" | sed '$d') |
| 132 | + |
| 133 | +if [ "$HTTP_CODE" = "200" ]; then |
| 134 | + echo " ✓ HTTP 200 — Model responded (secrets were redacted before reaching it)" |
| 135 | + ANSWER=$(echo "$BODY" | python3 -c "import json,sys; r=json.load(sys.stdin); print(r['content'][0]['text'][:200])" 2>/dev/null || echo "(parse error)") |
| 136 | + echo " Model said: $ANSWER" |
83 | 137 | else |
84 | | - RESPONSE=$(curl -s -w "\n%{http_code}" \ |
85 | | - "http://localhost:${GW_PORT}/v1/messages" \ |
86 | | - -H "Content-Type: application/json" \ |
87 | | - -H "anthropic-version: 2023-06-01" \ |
88 | | - -H "x-api-key: $ANTHROPIC_API_KEY" \ |
89 | | - -d '{ |
90 | | - "model": "claude-haiku-4-5-20251001", |
91 | | - "max_tokens": 100, |
92 | | - "messages": [ |
93 | | - {"role": "user", "content": "Read the .env file"}, |
94 | | - {"role": "assistant", "content": [ |
95 | | - {"type": "tool_use", "id": "tool_1", "name": "bash", "input": {"command": "cat .env"}} |
96 | | - ]}, |
97 | | - {"role": "user", "content": [ |
98 | | - {"type": "tool_result", "tool_use_id": "tool_1", "content": "DB_HOST=localhost\nAWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE\npassword = hunter2\nAPP_NAME=myapp"} |
99 | | - ]} |
100 | | - ] |
101 | | - }' 2>&1) |
102 | | - |
103 | | - HTTP_CODE=$(echo "$RESPONSE" | tail -1) |
104 | | - BODY=$(echo "$RESPONSE" | sed '$d') |
105 | | - |
106 | | - if [ "$HTTP_CODE" = "200" ]; then |
107 | | - echo " ✓ HTTP 200 — Model responded (secrets were redacted before reaching it)" |
108 | | - ANSWER=$(echo "$BODY" | python3 -c "import json,sys; r=json.load(sys.stdin); print(r['content'][0]['text'][:200])" 2>/dev/null || echo "(parse error)") |
109 | | - echo " Model said: $ANSWER" |
110 | | - else |
111 | | - echo " ✗ HTTP $HTTP_CODE — $BODY" |
112 | | - fi |
| 138 | + echo " ✗ HTTP $HTTP_CODE — $BODY" |
113 | 139 | fi |
114 | 140 | echo "" |
115 | 141 |
|
@@ -144,5 +170,8 @@ echo "" |
144 | 170 | echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" |
145 | 171 | echo "Demo complete." |
146 | 172 | echo "" |
| 173 | +echo "You can also point claude at the gateway:" |
| 174 | +echo " ANTHROPIC_BASE_URL=http://localhost:${GW_PORT} claude" |
| 175 | +echo "" |
147 | 176 | echo "Response deny (blocking destructive commands) is covered by unit tests:" |
148 | | -echo " make test-unit ARGS='-run TestProxy_StreamingDenyResponse ./internal/gateway'" |
| 177 | +echo " make test-unit ARGS='-run TestProxy_DenyResponse ./internal/gateway'" |