Repository navigation
Expand file tree
/
Copy pathcli.py
More file actions
132 lines (109 loc) · 3.62 KB
/
Copy pathcli.py
File metadata and controls
132 lines (109 loc) · 3.62 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
#!/usr/bin/env python3
"""OpenShield CLI.
Usage:
openshield ask "What are my critical findings?" --subscription sub-001
openshield ask "What is my MCSB compliance score?" -s sub-001 --framework mcsb
"""
from __future__ import annotations
import os
import sys
from typing import Optional
import requests
def cmd_ask(
question: str,
subscription_id: str,
token: str,
url: str,
framework: Optional[str] = None,
) -> int:
"""Call POST /api/agent/query and print the result."""
body: dict = {"question": question, "subscription_id": subscription_id}
if framework:
body["framework"] = framework
headers = {
"Authorization": f"Bearer {token}",
"Content-Type": "application/json",
}
try:
resp = requests.post(f"{url}/api/agent/query", json=body, headers=headers, timeout=120)
except requests.exceptions.RequestException as exc:
print(f"error: cannot connect to {url} ({exc})", file=sys.stderr)
return 1
if resp.status_code == 200:
data = resp.json()
print(data.get("answer", ""))
tools = " | ".join(data.get("tools_used") or []) or "-"
confidence = data.get("confidence", "-")
iterations = data.get("iterations", "-")
print(f"\n[tools used: {tools} | confidence: {confidence} | iterations: {iterations}]")
return 0
# 4xx / 5xx
try:
error_msg = resp.json().get("error", resp.text)
except Exception:
error_msg = resp.text
print(f"error: {error_msg}", file=sys.stderr)
return 1
def main(argv: list[str] | None = None) -> int:
import argparse
parser = argparse.ArgumentParser(prog="openshield", description="OpenShield CLI")
subparsers = parser.add_subparsers(dest="command")
ask_parser = subparsers.add_parser("ask", help="Ask the CSPM AI agent a question")
ask_parser.add_argument("question", help="Question to ask the agent")
ask_parser.add_argument(
"--subscription",
"-s",
dest="subscription",
default=None,
help="Azure subscription ID (or set AZURE_SUBSCRIPTION_ID)",
)
ask_parser.add_argument(
"--framework",
"-f",
dest="framework",
default=None,
help="Compliance framework (optional)",
)
ask_parser.add_argument(
"--url",
dest="url",
default=None,
help="API base URL (default: OPENSHIELD_API_URL or http://localhost:5000)",
)
ask_parser.add_argument(
"--token",
dest="token",
default=None,
help="Bearer token (or set OPENSHIELD_TOKEN)",
)
args = parser.parse_args(argv)
if args.command is None:
parser.print_help()
return 0
if args.command == "ask":
subscription_id = args.subscription or os.environ.get("AZURE_SUBSCRIPTION_ID")
if not subscription_id:
print(
"error: --subscription is required (or set AZURE_SUBSCRIPTION_ID)",
file=sys.stderr,
)
return 1
token = args.token or os.environ.get("OPENSHIELD_TOKEN")
if not token:
print(
"error: --token is required (or set OPENSHIELD_TOKEN)",
file=sys.stderr,
)
return 1
url = args.url or os.environ.get("OPENSHIELD_API_URL", "http://localhost:5000")
return cmd_ask(
question=args.question,
subscription_id=subscription_id,
token=token,
url=url,
framework=args.framework,
)
parser.print_help()
return 0
if __name__ == "__main__":
raise SystemExit(main())