agent-gate-sec 0.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. agent_gate_sec-0.2.0/LICENSE +21 -0
  2. agent_gate_sec-0.2.0/MANIFEST.in +13 -0
  3. agent_gate_sec-0.2.0/PKG-INFO +382 -0
  4. agent_gate_sec-0.2.0/README.md +350 -0
  5. agent_gate_sec-0.2.0/agent_gate/__init__.py +3 -0
  6. agent_gate_sec-0.2.0/agent_gate/__main__.py +5 -0
  7. agent_gate_sec-0.2.0/agent_gate/cli.py +118 -0
  8. agent_gate_sec-0.2.0/agent_gate/client/__init__.py +4 -0
  9. agent_gate_sec-0.2.0/agent_gate/client/cache.py +57 -0
  10. agent_gate_sec-0.2.0/agent_gate/client/moss_client.py +115 -0
  11. agent_gate_sec-0.2.0/agent_gate/collector/__init__.py +95 -0
  12. agent_gate_sec-0.2.0/agent_gate/collector/env_info.py +54 -0
  13. agent_gate_sec-0.2.0/agent_gate/collector/network_info.py +68 -0
  14. agent_gate_sec-0.2.0/agent_gate/collector/process_info.py +73 -0
  15. agent_gate_sec-0.2.0/agent_gate/collector/system_info.py +54 -0
  16. agent_gate_sec-0.2.0/agent_gate/collector/user_info.py +45 -0
  17. agent_gate_sec-0.2.0/agent_gate/config.py +252 -0
  18. agent_gate_sec-0.2.0/agent_gate/normalizer.py +117 -0
  19. agent_gate_sec-0.2.0/agent_gate/sdk.py +103 -0
  20. agent_gate_sec-0.2.0/agent_gate/server/__init__.py +30 -0
  21. agent_gate_sec-0.2.0/agent_gate/server/app.py +137 -0
  22. agent_gate_sec-0.2.0/agent_gate/server/dashboard.py +468 -0
  23. agent_gate_sec-0.2.0/agent_gate/server/middleware.py +27 -0
  24. agent_gate_sec-0.2.0/agent_gate/server/models.py +156 -0
  25. agent_gate_sec-0.2.0/agent_gate/server/routes.py +266 -0
  26. agent_gate_sec-0.2.0/agent_gate/storage/__init__.py +4 -0
  27. agent_gate_sec-0.2.0/agent_gate/storage/db.py +151 -0
  28. agent_gate_sec-0.2.0/agent_gate/storage/repository.py +261 -0
  29. agent_gate_sec-0.2.0/agent_gate_sec.egg-info/PKG-INFO +382 -0
  30. agent_gate_sec-0.2.0/agent_gate_sec.egg-info/SOURCES.txt +44 -0
  31. agent_gate_sec-0.2.0/agent_gate_sec.egg-info/dependency_links.txt +1 -0
  32. agent_gate_sec-0.2.0/agent_gate_sec.egg-info/entry_points.txt +2 -0
  33. agent_gate_sec-0.2.0/agent_gate_sec.egg-info/requires.txt +13 -0
  34. agent_gate_sec-0.2.0/agent_gate_sec.egg-info/top_level.txt +1 -0
  35. agent_gate_sec-0.2.0/config/agent_gate.yaml +97 -0
  36. agent_gate_sec-0.2.0/docs/agent_collector_design_v1.md +1187 -0
  37. agent_gate_sec-0.2.0/docs/agent_gate_v2_design.md +715 -0
  38. agent_gate_sec-0.2.0/pyproject.toml +55 -0
  39. agent_gate_sec-0.2.0/scripts/agent-gate-wrap.sh +55 -0
  40. agent_gate_sec-0.2.0/scripts/agent_gate.service +29 -0
  41. agent_gate_sec-0.2.0/sdk/python/gate_client.py +93 -0
  42. agent_gate_sec-0.2.0/sdk/typescript/package.json +15 -0
  43. agent_gate_sec-0.2.0/sdk/typescript/src/gate_client.ts +156 -0
  44. agent_gate_sec-0.2.0/sdk/typescript/tsconfig.json +18 -0
  45. agent_gate_sec-0.2.0/setup.cfg +4 -0
  46. agent_gate_sec-0.2.0/tests/test_server.py +906 -0
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 AgentGate Team
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,13 @@
1
+ # Deployment artifacts
2
+ include scripts/agent-gate-wrap.sh
3
+ include scripts/agent_gate.service
4
+
5
+ # Reference config
6
+ include config/agent_gate.yaml
7
+
8
+ # Docs
9
+ include README.md
10
+ recursive-include docs *.md
11
+
12
+ # SDKs
13
+ recursive-include sdk *
@@ -0,0 +1,382 @@
1
+ Metadata-Version: 2.4
2
+ Name: agent-gate-sec
3
+ Version: 0.2.0
4
+ Summary: AgentGate - AI Agent 数据采集与安全分析中间层服务
5
+ Author: AgentGate Team
6
+ License: MIT
7
+ Project-URL: Repository, https://github.com/org/AgentGate
8
+ Project-URL: Documentation, https://agent-gate.readthedocs.io
9
+ Keywords: ai-agent,security,data-collection,agent-moss
10
+ Classifier: Development Status :: 3 - Alpha
11
+ Classifier: Intended Audience :: Developers
12
+ Classifier: License :: OSI Approved :: MIT License
13
+ Classifier: Programming Language :: Python :: 3.10
14
+ Classifier: Programming Language :: Python :: 3.11
15
+ Classifier: Programming Language :: Python :: 3.12
16
+ Requires-Python: >=3.10
17
+ Description-Content-Type: text/markdown
18
+ License-File: LICENSE
19
+ Requires-Dist: fastapi>=0.109.0
20
+ Requires-Dist: uvicorn[standard]>=0.27.0
21
+ Requires-Dist: pydantic>=2.5.0
22
+ Requires-Dist: httpx>=0.27.0
23
+ Requires-Dist: psutil>=5.9.0
24
+ Requires-Dist: pyyaml>=6.0
25
+ Requires-Dist: loguru>=0.7.0
26
+ Provides-Extra: dev
27
+ Requires-Dist: pytest>=8.0; extra == "dev"
28
+ Requires-Dist: pytest-asyncio>=0.23.0; extra == "dev"
29
+ Requires-Dist: pytest-cov>=4.0; extra == "dev"
30
+ Requires-Dist: httpx>=0.27.0; extra == "dev"
31
+ Dynamic: license-file
32
+
33
+ # AgentGate
34
+
35
+ > AI Agent 数据采集与安全分析中间层服务
36
+
37
+ AgentGate 是一个**独立常驻服务**,位于 AI Agent 与 [AgentMoss](https://atomgit.com/kenhkl/AgentMoss) 之间:接收 Agent 的工具调用数据,采集系统上下文,转换格式后提交 AgentMoss 安全分析,回传 Allow/Deny 决策。
38
+
39
+ ```
40
+ ┌──────────────┐ HTTP/Unix Socket ┌──────────────────┐ HTTP/Unix Socket ┌─────────────┐
41
+ │ AI Agent │ ── AgentGateInput ──────▶│ AgentGate │ ── AnalyzeRequest ────▶│ AgentMoss │
42
+ │ (任意Agent) │ │ (独立服务) │ │ (安全分析) │
43
+ │ │◀─ AgentGateResponse ────│ · 系统上下文采集 │◀─ AnalyzeResponse ────│ │
44
+ └──────────────┘ │ · 格式标准化 │ └─────────────┘
45
+ │ · 决策缓存 │
46
+ │ · Session 存储 │
47
+ └──────────────────┘
48
+ ```
49
+
50
+ ---
51
+
52
+ ## 1. 安装
53
+
54
+ ```bash
55
+ git clone git@gitcode.com:kenhkl/AgentGate.git
56
+ cd AgentGate
57
+ pip install --break-system-packages -e ".[dev]"
58
+ ```
59
+
60
+ ## 2. 启动服务
61
+
62
+ AgentGate 依赖 AgentMoss 做安全分析,需要**先启动 AgentMoss,再启动 AgentGate**。
63
+
64
+ ### 2.1 启动 AgentMoss
65
+
66
+ ```bash
67
+ cd /home/hkl/gitcode/AgentMoss
68
+
69
+ # HTTP 模式(默认端口 9090)
70
+ python3 -m agent_moss server --mode http --port 9090
71
+ ```
72
+
73
+ 验证:
74
+ ```bash
75
+ curl http://127.0.0.1:9090/api/v1/health
76
+ # → {"status": "ok", ...}
77
+ ```
78
+
79
+ ### 2.2 启动 AgentGate
80
+
81
+ ```bash
82
+ cd /home/hkl/gitcode/AgentGate
83
+
84
+ # HTTP 模式(默认端口 9100)
85
+ agent-gate server --mode http --port 9100
86
+
87
+ # 或指定数据库路径
88
+ agent-gate server --mode http --port 9100 --db-path /tmp/agent_gate.db
89
+
90
+ # Unix Socket 模式(同机更低延迟)
91
+ agent-gate server --mode socket --socket /var/run/agent_gate/agent_gate.sock
92
+ ```
93
+
94
+ 验证:
95
+ ```bash
96
+ curl http://127.0.0.1:9100/api/v1/health
97
+ # → {"status": "ok", "version": "0.1.0"}
98
+ ```
99
+
100
+ ### 2.3 数据存储路径
101
+
102
+ `db_path` 默认为 `"auto"`,根据运行环境自动选择:
103
+
104
+ | 场景 | 实际路径 |
105
+ |------|----------|
106
+ | 开发(git clone 中运行) | `data/agent_gate.db` |
107
+ | 用户安装(`pip install`) | `~/.local/share/agent_gate/agent_gate.db` |
108
+ | 系统服务(systemd) | 显式 `--db-path /var/lib/agent_gate/agent_gate.db` |
109
+
110
+ ---
111
+
112
+ ## 3. 验证联调效果
113
+
114
+ ### 3.1 端到端测试(自动启动双服务)
115
+
116
+ ```bash
117
+ python3 examples/opendesk/test_e2e.py
118
+ ```
119
+
120
+ 自动启动 AgentMoss + AgentGate,执行安全命令和危险命令,验证:
121
+ - 危险命令 `rm -rf /` → **Deny** (critical)
122
+ - 危险命令 `curl | bash` → **Deny** (high)
123
+ - Session 历史自动累积
124
+ - 系统上下文完整采集
125
+
126
+ ### 3.2 手动 curl 测试
127
+
128
+ ```bash
129
+ # 安全命令
130
+ curl -s -X POST http://127.0.0.1:9100/api/v1/analyze \
131
+ -H "Content-Type: application/json" \
132
+ -d '{"agent_type":"test","session_id":"sess-001",
133
+ "tool_name":"bash","command":"ls -la","cwd":"/tmp"}' | python3 -m json.tool
134
+
135
+ # 危险命令
136
+ curl -s -X POST http://127.0.0.1:9100/api/v1/analyze \
137
+ -H "Content-Type: application/json" \
138
+ -d '{"agent_type":"test","session_id":"sess-001",
139
+ "tool_name":"bash","command":"rm -rf /","cwd":"/root"}' | python3 -m json.tool
140
+ ```
141
+
142
+ ---
143
+
144
+ ## 4. 三方 Agent 如何调用 AgentGate
145
+
146
+ ### 4.1 Python Agent(SDK)
147
+
148
+ ```python
149
+ from agent_gate.sdk import GateClient
150
+
151
+ client = GateClient(base_url="http://127.0.0.1:9100")
152
+
153
+ # 安全分析
154
+ response = client.analyze(
155
+ agent_type="my-agent",
156
+ session_id="sess-001",
157
+ tool_name="bash",
158
+ command="ls -la",
159
+ cwd="/home/user/project",
160
+ )
161
+
162
+ if response["decision"] == "Allow":
163
+ # 执行命令
164
+ print("允许执行")
165
+ else:
166
+ print(f"被阻止: {response['reason']}")
167
+ ```
168
+
169
+ ### 4.2 TypeScript / Electron Agent(SDK)
170
+
171
+ ```typescript
172
+ import { GateClient } from './gate_client';
173
+
174
+ const client = new GateClient({ baseUrl: 'http://127.0.0.1:9100' });
175
+
176
+ const result = await client.analyze({
177
+ agentType: 'opendes',
178
+ sessionId: 'sess-001',
179
+ toolName: 'executeBash',
180
+ command: 'ls -la',
181
+ cwd: '/home/user/project',
182
+ });
183
+
184
+ if (result.decision === 'Allow') {
185
+ // 执行命令
186
+ } else {
187
+ console.error(`被阻止: ${result.reason}`);
188
+ }
189
+ ```
190
+
191
+ ### 4.3 任意 Agent(HTTP / curl)
192
+
193
+ ```bash
194
+ curl -s -X POST http://127.0.0.1:9100/api/v1/analyze \
195
+ -H "Content-Type: application/json" \
196
+ -d '{
197
+ "agent_type": "my-agent",
198
+ "agent_version": "1.0",
199
+ "session_id": "sess-abc",
200
+ "tool_name": "bash",
201
+ "command": "rm -rf /tmp/cache",
202
+ "cwd": "/home/user/project",
203
+ "description": "清理缓存",
204
+ "user_prompt": "清理项目临时文件"
205
+ }'
206
+ ```
207
+
208
+ ### 4.4 Shell 包装器(无代码接入)
209
+
210
+ ```bash
211
+ # 通过环境变量配置
212
+ export AGENT_GATE_URL=http://127.0.0.1:9100
213
+ export AGENT_SESSION_ID=shell-session-001
214
+
215
+ # 用包装器执行命令
216
+ agent-gate-wrap ls -la # Allow → 正常执行
217
+ agent-gate-wrap rm -rf / # Deny → 阻断并退出 126
218
+ ```
219
+
220
+ ### 4.5 OpenDesk 集成
221
+
222
+ 参考 `examples/opendesk/`:
223
+ - `agent_gate_plugin.ts` — 插件代码(放到 OpenDesk 源码中)
224
+ - `README.md` — 3 步集成指南
225
+ - `TESTING.md` — 分层测试指南
226
+ - `test_e2e.py` — 端到端自动测试
227
+
228
+ 核心:在 OpenDesk 的 `afterParseToolcall` 钩子中调用 AgentGate API,返回 `reject` 即可阻断工具执行。
229
+
230
+ ---
231
+
232
+ ## 5. API 接口
233
+
234
+ | 方法 | 路径 | 说明 |
235
+ |------|------|------|
236
+ | `POST` | `/api/v1/analyze` | 安全分析(核心接口) |
237
+ | `GET` | `/api/v1/health` | 健康检查 |
238
+ | `GET` | `/api/v1/sessions/{id}/traces` | 查询 session 完整调用轨迹 |
239
+ | `GET` | `/api/v1/agents/{id}/sessions` | 查询某 agent 所有 session |
240
+ | `GET` | `/api/v1/tool_calls` | 条件查询工具调用记录 |
241
+
242
+ ### 请求格式
243
+
244
+ ```json
245
+ {
246
+ "agent_type": "opendes",
247
+ "session_id": "session-abc",
248
+ "tool_name": "bash",
249
+ "command": "rm -rf /tmp/cache/*",
250
+ "cwd": "/home/user/project",
251
+ "description": "清理缓存",
252
+ "user_prompt": "清理项目临时文件",
253
+ "action_history": [],
254
+ "metadata": {}
255
+ }
256
+ ```
257
+
258
+ > `action_history` 可选 — AgentGate 从 SQLite 自动累积同一 session 的历史。
259
+
260
+ ### 响应格式
261
+
262
+ ```json
263
+ {
264
+ "decision": "Deny",
265
+ "reason": "检测到递归强制删除操作",
266
+ "risk_level": "high",
267
+ "risk_type": "dangerous_command",
268
+ "violated_layers": ["1.2"],
269
+ "confidence": 97,
270
+ "analysis_duration_ms": 12.5,
271
+ "collected_context": {
272
+ "os": { "system": "linux", "distribution": "Ubuntu 24.04", ... },
273
+ "resources": { "cpu_count": 8, "memory_total_gb": 31.2, ... },
274
+ "process": { "pid": 12345, "name": "opendesk", ... },
275
+ "user": { "uid": 1000, "username": "developer", ... }
276
+ },
277
+ "source": "agent_moss"
278
+ }
279
+ ```
280
+
281
+ ---
282
+
283
+ ## 6. 关键设计
284
+
285
+ | 特性 | 说明 |
286
+ |------|------|
287
+ | **Fail-Closed** | AgentMoss 不可用时 AgentGate 返回 Deny(安全优先) |
288
+ | **决策缓存** | LRU 缓存 Allow 决策(同 session+同 cwd+同 command 可复用) |
289
+ | **Deny 不缓存** | 每次 Deny 都重新分析,确保安全 |
290
+ | **Session 持久化** | SQLite 存储全量 tool_call 记录,Agent 无需自维护 history |
291
+ | **action_history 自动累积** | 同一 session_id 的调用序列自动拼接,传给 AgentMoss |
292
+ | **系统上下文采集** | 5 模块并行(OS/进程/网络/用户/环境变量),< 50ms |
293
+ | **环境变量安全过滤** | 白名单机制,自动排除 KEY/TOKEN/SECRET 等敏感变量 |
294
+
295
+ ---
296
+
297
+ ## 7. 测试
298
+
299
+ ```bash
300
+ # 单元测试(42 项)
301
+ python3 -m pytest tests/test_server.py -v
302
+
303
+ # 端到端测试(AgentMoss + AgentGate 联调)
304
+ python3 examples/opendesk/test_e2e.py
305
+ ```
306
+
307
+ ---
308
+
309
+ ## 8. 高权限需求
310
+
311
+ | 采集模块 | 所需权限 | 读取路径 |
312
+ |----------|----------|----------|
313
+ | system_info | 普通用户 | `/proc/version`, `/etc/os-release` |
314
+ | process_info | `CAP_SYS_PTRACE` | `/proc/*/status`, capabilities |
315
+ | env_info | 普通用户 | 自身进程环境变量 |
316
+ | network_info | `CAP_NET_ADMIN` | `/proc/net/*` |
317
+ | user_info | 普通用户 | `/etc/passwd`, `/etc/group` |
318
+
319
+ 推荐以专用 `agent-gate` 用户 + Linux capabilities 运行,不使用 root。
320
+
321
+ ---
322
+
323
+ ## 9. Dashboard 可视化面板
324
+
325
+ AgentGate 内置实时 Dashboard(`http://localhost:9100/dashboard/`),提供:
326
+
327
+ - 实时统计卡片(Agents / Sessions / Tool Calls / Allow / Deny / Avg Latency)
328
+ - Risk Distribution 风险分布条
329
+ - Recent Tool Calls 操作记录表格
330
+ - Agent 类型分布
331
+ - Timeline 折线图(过去 60 分钟 Allow/Deny 趋势)
332
+
333
+ ### 在 OpenDesk 中查看
334
+
335
+ AgentGate 已集成到 OpenDesk 桌面端作为侧边栏应用,点击图标即可在应用内查看 Dashboard。
336
+
337
+ ---
338
+
339
+ ## 10. 项目结构
340
+
341
+ ```
342
+ AgentGate/
343
+ ├── agent_gate/ # 主包
344
+ │ ├── cli.py # 命令行入口 (agent-gate server / clean)
345
+ │ ├── config.py # YAML 配置 + XDG 路径自动适配
346
+ │ ├── normalizer.py # AgentGateInput → AgentMoss AnalyzeRequest
347
+ │ ├── sdk.py # Python SDK
348
+ │ ├── server/ # FastAPI 服务
349
+ │ │ ├── app.py # HTTP / Unix Socket 双模式
350
+ │ │ ├── routes.py # /analyze /traces /tool_calls /health
351
+ │ │ ├── models.py # Pydantic 数据模型
352
+ │ │ └── middleware.py # 请求日志
353
+ │ ├── collector/ # 系统数据采集(5 模块并行)
354
+ │ │ ├── system_info.py # OS/内核/CPU/内存/磁盘
355
+ │ │ ├── process_info.py # PID/capabilities/cgroup
356
+ │ │ ├── env_info.py # 环境变量白名单过滤
357
+ │ │ ├── network_info.py # 网络接口/连接数
358
+ │ │ └── user_info.py # UID/GID/用户组
359
+ │ ├── client/ # AgentMoss 客户端
360
+ │ │ ├── moss_client.py # 异步 HTTP + Fail-Closed
361
+ │ │ └── cache.py # LRU 决策缓存
362
+ │ └── storage/ # SQLite 持久化
363
+ │ ├── db.py # 建表/迁移/WAL 模式
364
+ │ └── repository.py # CRUD (agents/sessions/tool_calls)
365
+ ├── sdk/ # 客户端 SDK
366
+ │ ├── python/gate_client.py # Python SDK
367
+ │ └── typescript/ # TypeScript SDK (package.json + tsconfig)
368
+ ├── examples/opendesk/ # OpenDesk 集成
369
+ │ ├── agent_gate_plugin.ts # 插件(afterParseToolcall 钩子)
370
+ │ ├── test_integration.py # 接口测试(不改 OpenDesk)
371
+ │ ├── test_e2e.py # 端到端测试(自动启双服务)
372
+ │ ├── README.md # 集成指南
373
+ │ └── TESTING.md # 分层测试指南
374
+ ├── config/agent_gate.yaml # YAML 配置模板
375
+ ├── scripts/agent-gate-wrap.sh # Shell 包装器
376
+ ├── tests/test_server.py # 单元 + 集成测试(42 项)
377
+ └── docs/agent_collector_design_v1.md # 设计文档
378
+ ```
379
+
380
+ ## 许可证
381
+
382
+ MIT