-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
295 lines (250 loc) · 9.99 KB
/
app.py
File metadata and controls
295 lines (250 loc) · 9.99 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
"""Segmentation Copilot — Streamlit UI (Phase 2: pure HTTP client of the API).
Every action goes through the FastAPI service at `SCOPILOT_API_BASE`. No
direct imports of `segmentation_copilot.tools`, `segmentation_copilot.core`,
or the database — this file is now interchangeable with the CLI client
and any other UI.
The Phase-2 plan calls out a CI grep test
(`tests/test_no_core_in_app.py`) that fails if any of those imports come
back; do not bring them back without removing the test.
Run with:
uvicorn services.api.main:app & # in another terminal
streamlit run app.py
"""
from __future__ import annotations
import io
import json
import os
from datetime import datetime, time, timedelta
from typing import Any
import httpx
import streamlit as st
DEFAULT_API_BASE = os.environ.get("SCOPILOT_API_BASE", "http://localhost:8000")
# ---------------------------------------------------------------------------
# Thin HTTP wrapper around the REST API
# ---------------------------------------------------------------------------
class ApiError(RuntimeError):
pass
def _client() -> httpx.Client:
base = st.session_state.get("api_base", DEFAULT_API_BASE)
token = st.session_state.get("api_token", "")
headers = {"Authorization": f"Bearer {token}"} if token else {}
return httpx.Client(base_url=base, headers=headers, timeout=120.0)
def _call(method: str, path: str, **kwargs: Any) -> Any:
with _client() as c:
resp = c.request(method, path, **kwargs)
if not resp.is_success:
raise ApiError(f"HTTP {resp.status_code}: {resp.text}")
if resp.headers.get("content-type", "").startswith("application/json"):
return resp.json()
return resp.text
# ---------------------------------------------------------------------------
# UI
# ---------------------------------------------------------------------------
def main() -> None:
st.set_page_config(page_title="Segmentation Copilot", layout="wide")
st.title("Segmentation Copilot")
st.caption(
"AI Security Analyst that turns Cisco SG-ACL hit logs into a "
"TrustSec contract matrix proposal. Backed by the segmentation-copilot API."
)
_sidebar()
tab_run, tab_inputs, tab_matrix, tab_history, tab_proposals = st.tabs(
["Run analysis", "Missing SGTs", "Matrix", "History", "Proposals"]
)
with tab_run:
_run_panel()
with tab_inputs:
_missing_sgts_panel()
with tab_matrix:
_matrix_panel()
with tab_history:
_history_panel()
with tab_proposals:
_proposals_panel()
def _sidebar() -> None:
with st.sidebar:
st.header("API")
st.session_state.setdefault("api_base", DEFAULT_API_BASE)
st.session_state.setdefault("api_token", "")
st.session_state["api_base"] = st.text_input(
"API base URL", value=st.session_state["api_base"]
)
st.session_state["api_token"] = st.text_input(
"API bearer token (optional)",
value=st.session_state["api_token"],
type="password",
)
if st.button("Test connection"):
try:
_call("GET", "/readyz")
st.success("API reachable")
except ApiError as exc:
st.error(str(exc))
st.divider()
st.header("New run")
uploaded = st.file_uploader(
"Upload a syslog file", type=["log", "txt"], accept_multiple_files=False
)
st.subheader("Analysis window (metadata)")
default_end = datetime.utcnow()
default_start = default_end - timedelta(days=1)
col1, col2 = st.columns(2)
with col1:
start_date = st.date_input("Start date", value=default_start.date())
start_time = st.time_input("Start time", value=time(0, 0))
with col2:
end_date = st.date_input("End date", value=default_end.date())
end_time = st.time_input("End time", value=time(23, 59))
if st.button("Create run and ingest", type="primary", disabled=uploaded is None):
try:
run_resp = _call(
"POST", "/v1/runs",
json={
"source_type": "upload",
"window_start": datetime.combine(start_date, start_time).isoformat(),
"window_end": datetime.combine(end_date, end_time).isoformat(),
},
)
run_id = run_resp["run"]["id"]
lines = uploaded.getvalue().decode("utf-8", errors="replace").splitlines()
summary = _call(
"POST", f"/v1/runs/{run_id}/ingest", json={"lines": lines}
)
st.session_state["active_run_id"] = run_id
st.session_state["ingest_summary"] = summary
st.success(f"Run {run_id} created and ingested.")
except ApiError as exc:
st.error(str(exc))
st.divider()
st.header("SGT dictionary")
dict_file = st.file_uploader("Upload SGT dict (JSON)", type=["json"])
if dict_file is not None and st.button("Load dictionary"):
try:
data = json.loads(dict_file.getvalue().decode("utf-8"))
_call("POST", "/v1/sgt/bulk",
json={"entries": {str(k): str(v) for k, v in data.items()}})
st.success(f"Loaded {len(data)} entries.")
except ApiError as exc:
st.error(str(exc))
def _active_run_id() -> int | None:
return st.session_state.get("active_run_id")
def _run_panel() -> None:
run_id = _active_run_id()
if run_id is None:
st.info("Upload a log file in the sidebar to start a run.")
return
summary = st.session_state.get("ingest_summary")
if summary:
st.subheader("Ingest summary")
st.json(summary)
if st.button("Classify flows", type="primary"):
try:
result = _call("POST", f"/v1/runs/{run_id}/classify")
st.session_state["classify_counts"] = result["counts"]
st.success("Classification complete.")
except ApiError as exc:
st.error(str(exc))
if "classify_counts" in st.session_state:
st.subheader("Classification counts")
st.json(st.session_state["classify_counts"])
if st.button("Build matrix"):
try:
matrix = _call("POST", f"/v1/runs/{run_id}/matrix")
st.session_state["matrix"] = matrix
st.success("Matrix built — see the Matrix tab.")
except ApiError as exc:
st.error(str(exc))
def _missing_sgts_panel() -> None:
run_id = _active_run_id()
if run_id is None:
st.info("No active run.")
return
try:
data = _call("GET", f"/v1/runs/{run_id}/missing-sgts")
except ApiError as exc:
st.error(str(exc))
return
missing = data["missing"]
if not missing:
st.success("No missing SGTs.")
return
st.warning(f"{len(missing)} SGT/DGT IDs are missing from the dictionary.")
for sgt_id in missing:
name = st.text_input(f"Name for SGT {sgt_id}", key=f"sgt_name_{sgt_id}")
if name and st.button(f"Register SGT {sgt_id}", key=f"sgt_btn_{sgt_id}"):
try:
_call("POST", "/v1/sgt", json={"sgt_id": sgt_id, "name": name})
st.success(f"Registered {sgt_id} → {name}")
except ApiError as exc:
st.error(str(exc))
def _matrix_panel() -> None:
matrix = st.session_state.get("matrix")
if not matrix:
st.info("Build the matrix from the Run tab.")
return
st.markdown(matrix["markdown"])
st.download_button(
"Download matrix (.md)",
data=matrix["markdown"].encode("utf-8"),
file_name="trustsec_matrix.md",
mime="text/markdown",
)
csv_buf = io.StringIO()
csv_buf.write("Source SGT,Destination SGT,Contract Name,Protocol,"
"Source Port,Destination Port,Action\n")
for c in matrix["contracts"]:
for ace in c["aces"]:
csv_buf.write(
f"{c['src_sgt_name']},{c['dst_sgt_name']},{c['name']},"
f"{ace['protocol']},{ace['src_port']},{ace['dst_port']},{ace['action']}\n"
)
st.download_button(
"Download matrix (.csv)",
data=csv_buf.getvalue().encode("utf-8"),
file_name="trustsec_matrix.csv",
mime="text/csv",
)
def _history_panel() -> None:
try:
runs = _call("GET", "/v1/runs")["runs"]
except ApiError as exc:
st.error(str(exc))
return
if not runs:
st.info("No prior runs.")
return
st.dataframe(runs, use_container_width=True)
def _proposals_panel() -> None:
try:
data = _call("GET", "/v1/proposals")
except ApiError as exc:
st.error(str(exc))
return
proposals = data["proposals"]
if not proposals:
st.info("No proposals.")
return
for p in proposals:
with st.expander(
f"[{p['status']}] {p['src_sgt']} → {p['dst_sgt']} — {p['trigger']}"
):
st.markdown(f"**Rationale:** {p['rationale']}")
st.json(p["proposed_aces"])
if p["status"] in ("pending", "notified"):
col_a, col_r = st.columns(2)
if col_a.button("Approve", key=f"approve_{p['id']}"):
try:
_call("POST", f"/v1/proposals/{p['id']}/decision",
json={"decision": "approved"})
st.success("Approved.")
except ApiError as exc:
st.error(str(exc))
if col_r.button("Reject", key=f"reject_{p['id']}"):
try:
_call("POST", f"/v1/proposals/{p['id']}/decision",
json={"decision": "rejected"})
st.success("Rejected.")
except ApiError as exc:
st.error(str(exc))
if __name__ == "__main__":
main()