-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.sh
More file actions
executable file
·242 lines (194 loc) · 7.06 KB
/
Copy pathdemo.sh
File metadata and controls
executable file
·242 lines (194 loc) · 7.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
COMPOSE_FILE="$REPO_ROOT/docker-compose.demo.yml"
COMPOSE_PROJECT_NAME="${AGENT_GATEWAY_DEMO_COMPOSE_PROJECT:-agent_gateway_demo}"
COMPOSE_CMD=()
COMPOSE_DISPLAY=""
PRINCIPAL="${AGENT_GATEWAY_DEMO_PRINCIPAL:-org-alice}"
IDENTITY="${AGENT_GATEWAY_DEMO_IDENTITY:-agent-alpha}"
HANDLE="${AGENT_GATEWAY_DEMO_HANDLE:-$IDENTITY}"
STATE_ROOT="${AGENT_GATEWAY_DEMO_STATE_DIR:-${XDG_STATE_HOME:-$HOME/.local/state}/agent-gateway/demo-agents}"
TPM2_PKCS11_STORE="${AGENT_GATEWAY_DEMO_TPM2_PKCS11_STORE:-$STATE_ROOT/tpm2-pkcs11}"
USER_PIN="${AGENT_GATEWAY_TPM_USER_PIN:-agentgateway}"
SO_PIN="${AGENT_GATEWAY_TPM_SO_PIN:-agentgateway-so}"
DATABASE_URL="${AGENT_GATEWAY_DATABASE_URL:-postgres://agent_gateway_admin:agent_gateway_dev@127.0.0.1:5432/agent_gateway}"
GATEWAY="${AGENT_GATEWAY_DEMO_GATEWAY:-127.0.0.1:8443}"
GATEWAY_CA="${AGENT_GATEWAY_DEMO_GATEWAY_CA:-$REPO_ROOT/certs/server-ca.pem}"
MOCK_CA="${AGENT_GATEWAY_DEMO_MOCK_CA:-$REPO_ROOT/certs/mock-ca.pem}"
SIDECAR_BIN="$REPO_ROOT/target/debug/agent_gateway_sidecar"
GATEWAY_IMAGE="${AGENT_GATEWAY_DEMO_GATEWAY_IMAGE:-ghcr.io/securitylevel5/agent-gateway:main}"
export COMPOSE_PROJECT_NAME
export AGENT_GATEWAY_DEMO_GATEWAY_IMAGE="$GATEWAY_IMAGE"
usage() {
cat >&2 <<'EOF'
Usage:
demo.sh setup
demo.sh teardown
Commands:
setup Start and verify the full local demo.
teardown Delete demo agents, state, generated files, containers, and volumes.
EOF
}
require_cmd() {
command -v "$1" >/dev/null 2>&1 || {
echo "error: required command not found: $1" >&2
exit 1
}
}
select_compose() {
if command -v podman >/dev/null 2>&1; then
if podman compose version >/dev/null 2>&1; then
COMPOSE_CMD=(podman compose)
COMPOSE_DISPLAY="podman compose"
return
fi
if command -v podman-compose >/dev/null 2>&1; then
COMPOSE_CMD=(podman-compose)
COMPOSE_DISPLAY="podman-compose"
return
fi
echo "error: podman is installed, but neither 'podman compose' nor 'podman-compose' is available" >&2
exit 1
fi
if command -v docker >/dev/null 2>&1; then
if docker compose version >/dev/null 2>&1; then
COMPOSE_CMD=(docker compose)
COMPOSE_DISPLAY="docker compose"
return
fi
echo "error: docker is installed, but 'docker compose' is not available" >&2
exit 1
fi
echo "error: install podman with compose support or docker with the compose plugin" >&2
exit 1
}
compose() {
"${COMPOSE_CMD[@]}" -p "$COMPOSE_PROJECT_NAME" -f "$COMPOSE_FILE" "$@"
}
wait_for_postgres() {
echo "==> Waiting for Postgres"
for _ in $(seq 1 60); do
if psql "$DATABASE_URL" -tAc "SELECT 1" >/dev/null 2>&1; then
return
fi
sleep 1
done
echo "error: Postgres did not become ready" >&2
compose logs --no-color postgres >&2 || true
exit 1
}
apply_migrations() {
echo "==> Applying database migrations"
if [[ "$(psql "$DATABASE_URL" -tAc "SELECT to_regclass('public.agent_gateway_schema_version') IS NOT NULL")" == "t" ]]; then
echo "authorization registry already migrated"
return
fi
psql "$DATABASE_URL" -v ON_ERROR_STOP=1 -f "$REPO_ROOT/migrations/0001_signed_authorization_registry.sql"
}
state_dir() {
printf '%s/%s\n' "$STATE_ROOT" "$HANDLE"
}
enroll() {
demo_env=(
"AGENT_GATEWAY_DATABASE_URL=$DATABASE_URL"
"AGENT_GATEWAY_DEMO_GATEWAY=$GATEWAY"
"AGENT_GATEWAY_DEMO_GATEWAY_CA=$GATEWAY_CA"
"AGENT_GATEWAY_DEMO_MOCK_CA=$MOCK_CA"
"AGENT_GATEWAY_DEMO_SIDECAR_BIN=$SIDECAR_BIN"
"AGENT_GATEWAY_DEMO_STATE_DIR=$STATE_ROOT"
"AGENT_GATEWAY_TPM_USER_PIN=$USER_PIN"
"AGENT_GATEWAY_TPM_SO_PIN=$SO_PIN"
"AGENT_GATEWAY_RESET_TPM_STORE=false"
"CLAUDE_CODE_PROXY_RESOLVES_HOSTS=1"
"CURL_CA_BUNDLE=$MOCK_CA"
"NODE_EXTRA_CA_CERTS=$MOCK_CA"
"SSL_CERT_FILE=$MOCK_CA"
"TPM2_PKCS11_STORE=$TPM2_PKCS11_STORE"
"AGENT_GATEWAY_DEMO_DASHBOARD_URL=http://localhost:3000"
)
echo "==> Registering demo principal $PRINCIPAL"
env "${demo_env[@]}" "$REPO_ROOT/registry-cli/register-principal-key.sh" "$PRINCIPAL"
echo "==> Granting demo scopes"
env "${demo_env[@]}" "$REPO_ROOT/registry-cli/grant-principal-scope.sh" "$PRINCIPAL" docstore messaging api.anthropic.com
echo "==> Creating demo agent $HANDLE"
env "${demo_env[@]}" "$SCRIPT_DIR/demo-agent.sh" create \
--identity "$IDENTITY" \
--handle "$HANDLE" \
--grant docstore \
--grant api.anthropic.com >/dev/null
}
# ── Main ──────────────────────────────────────────────────────────────────────
cmd_setup() {
select_compose
require_cmd cargo
require_cmd openssl
require_cmd psql
cd "$REPO_ROOT"
echo "==> Generating demo TLS certificates"
"$SCRIPT_DIR/generate-server-certs.sh"
if [[ ! -f "$REPO_ROOT/config.toml" ]]; then
echo "==> Creating config.toml from config.example.toml"
cp "$REPO_ROOT/config.example.toml" "$REPO_ROOT/config.toml"
fi
echo "==> Building local sidecar"
cargo build -p agent_gateway_sidecar
echo "==> Using gateway image $GATEWAY_IMAGE"
echo "==> Starting Postgres and mock HTTPS services"
compose up -d --force-recreate postgres mock-services
wait_for_postgres
apply_migrations
echo "==> Starting gateway, otel-collector, and dashboard"
compose up -d --force-recreate gateway otel-collector dashboard
enroll
cat <<EOF
Demo is ready.
Dashboard: http://localhost:3000
Mock service URLs available through the gateway:
https://docstore/health
https://docstore/documents
https://messaging/health
https://messaging/messages
Prompt the demo agent with:
./demo/demo-agent.sh prompt "$HANDLE" --prompt "Access https://docstore/documents using curl, then summarize what you found."
Delete the demo agent when finished:
./demo/demo-agent.sh delete "$HANDLE"
Reset the entire demo with:
./demo/demo.sh teardown
Useful logs:
$COMPOSE_DISPLAY -p "$COMPOSE_PROJECT_NAME" -f docker-compose.demo.yml logs -f gateway
$(state_dir)/sidecar.log
EOF
}
delete_demo_agents() {
[[ -d "$STATE_ROOT" ]] || return
local dir handle
for dir in "$STATE_ROOT"/*; do
[[ -d "$dir" ]] || continue
[[ -f "$dir/identity" && -f "$dir/client/machine-client-spki.der" ]] || continue
handle="${dir##*/}"
echo "==> Deleting demo agent $handle"
if ! env "${demo_env[@]}" "$SCRIPT_DIR/demo-agent.sh" delete "$handle"; then
echo "warning: failed to delete demo agent $handle; continuing teardown" >&2
fi
done
}
cmd_teardown() {
select_compose
cd "$REPO_ROOT"
delete_demo_agents
echo "==> Stopping compose services and deleting volumes"
compose down -v --remove-orphans
echo "==> Removing demo state and generated files"
rm -rf -- "$STATE_ROOT" "$REPO_ROOT/certs"
rm -f -- "$REPO_ROOT/config.toml"
echo "Demo teardown complete."
}
[[ $# -eq 1 ]] || { usage; exit 2; }
case "$1" in
setup) cmd_setup ;;
teardown) cmd_teardown ;;
-h|--help) usage ;;
*) echo "Unknown command: $1" >&2; usage; exit 2 ;;
esac