Skip to content

Commit 2fe1e28

Browse files
authored
Fix/agent authz (#466)
* fix: fix agent authz callback * fix: add namespace_name * fix: add namespace_name * fix: fix agent authorization and add user docs
1 parent e907755 commit 2fe1e28

3 files changed

Lines changed: 48 additions & 1 deletion

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# 权限策略
2+
3+
Agent 权限策略基于 Cedar 声明式授权语言,提供了一套覆盖 User → Agent → Tool 全链路的权限管理方案。通过本指南,你将了解如何在智能体代码中启用权限校验,并通过控制台配置权限策略,确保智能体仅被授权用户访问。
4+
5+
## 前置准备
6+
参考[使用文档](https://www.volcengine.com/docs/86848/2123355?lang=zh),登录火山引擎智能体身份和权限管理平台,按以下步骤创建策略空间与权限策略:
7+
- 进入「权限管控 > 权限策略」,创建策略空间(填写空间名称、描述,选择所属项目和标签);
8+
- 在目标策略空间内新建权限策略,可通过「可视化编辑」或「Cedar 语句编辑」定义规则(例如:允许指定用户调用某智能体);
9+
- 使用「模拟权限校验」功能验证策略是否符合预期。
10+
11+
## 代码实现
12+
13+
在调用智能体之前,需在 [AgentKit Runtime](https://console.volcengine.com/agentkit/region:agentkit+cn-beijing/runtime) 控制台配置 `RUNTIME_IDENTITY_NAMESPACE` 环境变量指定策略空间(默认为 default),以确保权限校验能匹配到对应的策略规则:
14+
```bash
15+
# 设置策略空间名称(替换为你实际创建的策略空间名称)
16+
RUNTIME_IDENTITY_NAMESPACE="你的策略空间名称"
17+
```
18+
19+
在初始化 Agent 时开启授权功能(enable_authz=True),即可触发权限校验流程。以下是部署到 [AgentKit Runtime](https://console.volcengine.com/agentkit/region:agentkit+cn-beijing/runtime) 的代码示例:
20+
21+
```python title="agent.py"
22+
import asyncio
23+
24+
from veadk import Agent, Runner
25+
26+
# 待校验权限的用户ID
27+
user_id = "9d154b10-285f-404c-ba67-0bf648ff9ce0"
28+
29+
# 初始化Agent并开启权限校验
30+
agent = Agent(enable_authz=True)
31+
32+
runner = Runner(agent=agent)
33+
34+
# 调用智能体并传入用户ID(权限校验的核心依据)
35+
response = asyncio.run(runner.run(messages="你好", user_id=user_id))
36+
37+
print(response)
38+
```
39+
40+
运行结果:
41+
- 授权通过:若用户在策略空间中拥有调用该智能体的权限,代码会正常执行并返回智能体的响应结果;
42+
- 授权失败:若用户未被授权访问该智能体,会抛出权限异常,错误信息示例:`Agent <agent role> is not authorized to run by user 9d154b10-285f-404c-ba67-0bf648ff9ce0.`

docs/mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ nav:
6767
- OAuth2 用户联邦出站认证: auth/oauth2-user-federation-outbound.md
6868
- OAuth2 M2M 出站认证: auth/oauth2-m2m-outbound.md
6969
- Trusted MCP 出站认证: auth/trusted-mcp-outbound.md
70+
- 权限策略: auth/permission-policy.md
7071
- 可观测:
7172
- 开启观测: observation/tracing.md
7273
- 在火山引擎观测: observation/ve-tracing.md

veadk/tools/builtin_tools/agent_authorization.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import os
1516
from typing import Optional
1617

1718
from google.genai import types
@@ -59,15 +60,18 @@ async def check_agent_authorization(
5960
role_id = actors[0]
6061

6162
principal = {"Type": "user", "Id": user_id}
62-
operation = {"Type": "action", "Id": "invoke"}
63+
operation = {"Type": "Action", "Id": "invoke"}
6364
resource = {"Type": "agent", "Id": role_id}
6465
original_callers = [{"Type": "agent", "Id": actor} for actor in actors[1:]]
6566

67+
namespace = os.getenv("RUNTIME_IDENTITY_NAMESPACE", "default")
68+
6669
allowed = identity_client.check_permission(
6770
principal=principal,
6871
operation=operation,
6972
resource=resource,
7073
original_callers=original_callers,
74+
namespace=namespace,
7175
)
7276

7377
if allowed:

0 commit comments

Comments
 (0)