Repository navigation
Expand file tree
/
Copy pathcli.py
More file actions
250 lines (194 loc) · 8.34 KB
/
Copy pathcli.py
File metadata and controls
250 lines (194 loc) · 8.34 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
243
244
245
246
247
248
249
250
#!/usr/bin/env python3
"""
ShieldClaw CLI — Manage AI agent identities.
Usage:
python cli.py register --name "claude-code-dev" --scopes gateway:read,gateway:message,gateway:tools
python cli.py list
python cli.py revoke --agent-id agent_abc123
python cli.py rotate-secret --agent-id agent_abc123
python cli.py whoami --token <JWT>
python cli.py get-agent-token --client-id <id> --client-secret <secret>
"""
import argparse
import asyncio
import json
import sys
import os
import httpx
SHIELDCLAW_URL = os.getenv("SHIELDCLAW_URL", "http://localhost:8443")
AUTH0_DOMAIN = os.getenv("AUTH0_DOMAIN", "")
AUTH0_AUDIENCE = os.getenv("AUTH0_AUDIENCE", "")
def get_token_from_args(args) -> str:
"""Get the admin token from --token arg."""
token = getattr(args, "token", None)
if not token:
print("Error: Provide --token or set SHIELDCLAW_ADMIN_TOKEN env var", file=sys.stderr)
sys.exit(1)
return token
async def cmd_register(args):
token = get_token_from_args(args)
payload = {
"agent_name": args.name,
"description": args.description or f"Agent: {args.name}",
}
if args.policy:
payload["policy"] = args.policy
if args.scopes:
payload["scopes"] = [s.strip() for s in args.scopes.split(",")]
if not args.policy and not args.scopes:
print("Error: provide --policy or --scopes (or both)", file=sys.stderr)
sys.exit(1)
async with httpx.AsyncClient() as client:
resp = await client.post(
f"{SHIELDCLAW_URL}/shieldclaw/agents",
headers={"Authorization": f"Bearer {token}"},
json=payload,
)
if resp.status_code != 200:
print(f"Error ({resp.status_code}): {resp.text}", file=sys.stderr)
sys.exit(1)
data = resp.json()
print("\n=== Agent Registered Successfully ===")
print(f" Agent ID: {data['agent_id']}")
print(f" Agent Name: {data['agent_name']}")
print(f" Client ID: {data['client_id']}")
print(f" Client Secret: {data['client_secret']}")
print(f" Scopes: {', '.join(data['scopes'])}")
if "policy_interpretation" in data:
interp = data["policy_interpretation"]
print(f"\n Policy ({interp['confidence']} confidence): {interp['reasoning']}")
for w in interp.get("warnings", []):
print(f" WARNING: {w}")
for note in interp.get("override_note", []):
print(f" Note: {note}")
print("\n IMPORTANT: Save the client_secret now. It cannot be retrieved later.")
print("\n To get a token for this agent:")
print(f" python cli.py get-agent-token --client-id {data['client_id']} --client-secret <secret>")
async def cmd_list(args):
token = get_token_from_args(args)
async with httpx.AsyncClient() as client:
resp = await client.get(
f"{SHIELDCLAW_URL}/shieldclaw/agents",
headers={"Authorization": f"Bearer {token}"},
)
if resp.status_code != 200:
print(f"Error ({resp.status_code}): {resp.text}", file=sys.stderr)
sys.exit(1)
data = resp.json()
agents = data["agents"]
if not agents:
print("No agents registered.")
return
print(f"\n{'Agent ID':<20} {'Name':<25} {'Scopes':<40} {'Revoked'}")
print("-" * 95)
for a in agents:
scopes_str = ", ".join(a["scopes"])
revoked = "YES" if a["revoked"] else "no"
print(f"{a['agent_id']:<20} {a['agent_name']:<25} {scopes_str:<40} {revoked}")
async def cmd_revoke(args):
token = get_token_from_args(args)
async with httpx.AsyncClient() as client:
resp = await client.post(
f"{SHIELDCLAW_URL}/shieldclaw/agents/{args.agent_id}/revoke",
headers={"Authorization": f"Bearer {token}"},
)
if resp.status_code != 200:
print(f"Error ({resp.status_code}): {resp.text}", file=sys.stderr)
sys.exit(1)
print(f"Agent {args.agent_id} has been revoked.")
async def cmd_rotate_secret(args):
token = get_token_from_args(args)
async with httpx.AsyncClient() as client:
resp = await client.post(
f"{SHIELDCLAW_URL}/shieldclaw/agents/{args.agent_id}/rotate-secret",
headers={"Authorization": f"Bearer {token}"},
)
if resp.status_code != 200:
print(f"Error ({resp.status_code}): {resp.text}", file=sys.stderr)
sys.exit(1)
data = resp.json()
print(f"\n=== Secret Rotated for {args.agent_id} ===")
print(f" Client ID: {data['client_id']}")
print(f" New Secret: {data['client_secret']}")
print(" The old secret is now invalid.")
async def cmd_whoami(args):
token = get_token_from_args(args)
async with httpx.AsyncClient() as client:
resp = await client.get(
f"{SHIELDCLAW_URL}/shieldclaw/whoami",
headers={"Authorization": f"Bearer {token}"},
)
if resp.status_code != 200:
print(f"Error ({resp.status_code}): {resp.text}", file=sys.stderr)
sys.exit(1)
data = resp.json()
print(json.dumps(data, indent=2))
async def cmd_get_agent_token(args):
"""Get an access token for an agent using client credentials flow."""
async with httpx.AsyncClient() as client:
resp = await client.post(
f"https://{AUTH0_DOMAIN}/oauth/token",
json={
"grant_type": "client_credentials",
"client_id": args.client_id,
"client_secret": args.client_secret,
"audience": AUTH0_AUDIENCE,
},
)
if resp.status_code != 200:
print(f"Error ({resp.status_code}): {resp.text}", file=sys.stderr)
sys.exit(1)
data = resp.json()
print(f"\n=== Agent Token ===")
print(f" Access Token: {data['access_token']}")
print(f" Expires In: {data.get('expires_in', '?')}s")
print(f" Token Type: {data.get('token_type', 'Bearer')}")
if args.export:
print(f"\n export SHIELDCLAW_AGENT_TOKEN=\"{data['access_token']}\"")
def main():
parser = argparse.ArgumentParser(
description="ShieldClaw — AI Agent Identity Management"
)
subparsers = parser.add_subparsers(dest="command", help="Command to run")
# register
p_reg = subparsers.add_parser("register", help="Register a new agent identity")
p_reg.add_argument("--name", required=True, help="Agent name (e.g. claude-code-dev)")
p_reg.add_argument("--policy", help='Plain-English policy (e.g. "read only, no sensitive data")')
p_reg.add_argument("--scopes", default=None,
help="Comma-separated scopes; overrides --policy if both provided")
p_reg.add_argument("--description", help="Agent description")
p_reg.add_argument("--token", help="Admin JWT (or set SHIELDCLAW_ADMIN_TOKEN)")
# list
p_list = subparsers.add_parser("list", help="List registered agents")
p_list.add_argument("--token", help="JWT (or set SHIELDCLAW_ADMIN_TOKEN)")
# revoke
p_revoke = subparsers.add_parser("revoke", help="Revoke an agent")
p_revoke.add_argument("--agent-id", required=True, help="Agent ID to revoke")
p_revoke.add_argument("--token", help="JWT (or set SHIELDCLAW_ADMIN_TOKEN)")
# rotate-secret
p_rotate = subparsers.add_parser("rotate-secret", help="Rotate an agent's client secret")
p_rotate.add_argument("--agent-id", required=True, help="Agent ID")
p_rotate.add_argument("--token", help="JWT (or set SHIELDCLAW_ADMIN_TOKEN)")
# whoami
p_who = subparsers.add_parser("whoami", help="Show identity for a token")
p_who.add_argument("--token", help="JWT to inspect")
# get-agent-token
p_tok = subparsers.add_parser("get-agent-token", help="Get a token for an agent (client credentials)")
p_tok.add_argument("--client-id", required=True, help="Agent's Auth0 client_id")
p_tok.add_argument("--client-secret", required=True, help="Agent's Auth0 client_secret")
p_tok.add_argument("--export", action="store_true", help="Print export command for shell")
args = parser.parse_args()
if not args.command:
parser.print_help()
sys.exit(1)
commands = {
"register": cmd_register,
"list": cmd_list,
"revoke": cmd_revoke,
"rotate-secret": cmd_rotate_secret,
"whoami": cmd_whoami,
"get-agent-token": cmd_get_agent_token,
}
asyncio.run(commands[args.command](args))
if __name__ == "__main__":
main()