Skip to content

Commit 993a9f6

Browse files
authored
feat(ve_identity): Add Identity Service integration with OAuth2, API Key and Workload authentication (#263)
* feat(ve_identity): Add Identity Service integration with OAuth2, API Key, and Workload authentication This commit introduces a comprehensive integration with Identity Service, enabling ADK agents to securely manage authentication and credentials. ## Key Features ### 1. Unified Authentication Framework - **Three Authentication Types**: - OAuth2 (M2M and USER_FEDERATION flows) - API Key authentication - Workload access token authentication - **Flexible Configuration**: Simple factory functions (`api_key_auth()`, `oauth2_auth()`, `workload_auth()`) for easy setup ### 2. Tool Integration - **VeIdentityFunctionTool**: Funtion tool wrapper with built-in Identity authentication - **VeIdentityMcpTool**: MCP tool wrapper with built-in Identity authentication - **VeIdentityMcpToolset**: Complete MCP toolset management with automatic credential handling ### 3. Authentication Processing - **AuthRequestProcessor**: Handles OAuth2 flows in agent conversations with support for: - Custom OAuth2 auth pollers - Callback URL handling - Token polling with configurable timeout - Mock auth poller for testing - **Auth Mixins**: Reusable authentication logic (`VeIdentityAuthMixin`, `ApiKeyAuthMixin`, `OAuth2AuthMixin`, `WorkloadAuthMixin`) to avoid code duplication ### 4. Token Management - **WorkloadTokenManager**: Manages workload access tokens with: - Automatic caching in session state - Token expiration handling - Support for JWT, user ID, and workload-only authentication modes - Automatic token refresh ### 5. Identity Client - **IdentityClient**: Low-level async client for VolcEngine Identity Service API with: - OAuth2 credential provider management - API key credential provider management - Workload token retrieval - OAuth2 token and API key fetching - Dynamic Client Registration (DCR) support ### 6. Data Models - **OAuth2TokenResponse**: Structured response for OAuth2 token requests - **WorkloadToken**: Workload token with expiration tracking - **OAuth2AuthPoller**: Abstract base for custom token polling implementations - **DCR Models**: Support for RFC 7591 Dynamic Client Registration Protocol - **Authorization Server Metadata**: RFC 8414 compliant metadata handling ### 7. Utility Functions - **is_pending_auth_event()**: Detect pending authentication requests in ADK events - **get_function_call_id()**: Extract function call IDs from auth events - **get_function_call_auth_config()**: Extract auth configuration from events - **generate_headers()**: Convert credentials to HTTP authentication headers * fix(identity): refactor imports to use absolute paths in ve_identity - Fix Non-standard docstrings - Remove veadk prefix in logger * chore(identity):fix type hints for tool_context in auth mixins * feat: add unit tests for ve_identity tools and improve credential refresh Add unit tests for ve_identity auth_config, function_tool, mcp_tool, and mcp_toolset modules. Updated IdentityClient credential refresh logic to support fallback to VeFaaS IAM credentials if environment variables are not set, improving robustness in cloud environments. * feat(ve_identity): Add Agent Identity authentication docs Introduces documentation for Agent Identity authentication, including product overview, API Key outbound, OAuth2 M2M outbound, and OAuth2 USER_FEDERATION outbound. * feat(identity): Integrate VeIdentity config and region support Added VeIdentityConfig to global settings and improved region handling for Agent Identity authentication. Updated configuration files and environment variable documentation. * chore(ve_identity): Make OAuth2 flow and scopes parameters optional Updated OAuth2-related classes and functions to make 'auth_flow' and 'scopes' parameters optional, allowing control plane defaults to be used if not provided. * chore(ve_identity): Remove redundant logging from identity and token modules Eliminated unnecessary logger.info statements from IdentityClient and WorkloadTokenManager to reduce log verbosity and improve clarity. No changes to core logic or functionality. * refactor(ve_identity): introduce BaseRunProcessor to decouple Agent Identity dependency ## Problem Agent class had a hard dependency on Agent Identity's AuthRequestProcessor, causing all agents to depend on Agent Identity even when authentication is not needed. This violates the Dependency Inversion Principle and could break existing agents. ## Solution Introduce an abstract processor layer following the Dependency Inversion Principle: 1. Created `veadk/processors/` package with: - `BaseRunProcessor`: Abstract base class for runtime processors - `NoOpRunProcessor`: Default no-op implementation 2. Modified `veadk/agent.py`: - Added `run_processor: Optional[BaseRunProcessor]` field - Changed dependency from AuthRequestProcessor to BaseRunProcessor - Initialize with NoOpRunProcessor by default - Updated `run()` method parameter from `auth_request_processor` to `run_processor` 3. Updated `veadk/integrations/ve_identity/auth_processor.py`: - Made AuthRequestProcessor inherit from BaseRunProcessor - Renamed main method from `with_auth_loop` to `process_run` * chore(ve_identity): Remove unused _NoOpAuthProcessor import and cleanup Eliminated the unused _NoOpAuthProcessor import and reference from ve_identity/__init__.py. Also removed a trailing blank line in processors/__init__.py and added a missing comma in NoOpRunProcessor's decorator signature for consistency.
1 parent 0f48949 commit 993a9f6

27 files changed

+4798
-3
lines changed

docs/content/2.configurations/1.system.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ model:
4444
# api_key:
4545

4646
volcengine:
47-
access_key:
48-
secret_key:
47+
access_key:
48+
secret_key:
4949

5050
observability:
5151
opentelemetry:
@@ -70,6 +70,11 @@ database:
7070
region: cn-beijing
7171
bucket:
7272

73+
veidentity:
74+
# Agent Identity 服务区域
75+
region: cn-beijing
76+
# 可选:自定义 Agent Identity 服务端点,不提供时自动生成
77+
7378
logging:
7479
# ERROR
7580
# WARNING

docs/content/2.configurations/2.envs.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,20 @@ volcengine:
105105
| 环境变量名称 | 释义 |
106106
| :- | :- |
107107
| `PROMPT_PILOT_API_KEY` | Prompt Pilot 产品密钥 |
108+
109+
## Agent Identity 身份认证
110+
111+
统一前缀: `VEIDENTITY_`
112+
113+
| 环境变量名称 | 释义 |
114+
| :- | :- |
115+
| `VEIDENTITY_REGION` | Agent Identity 服务区域,默认 cn-beijing |
116+
| `VEIDENTITY_ENDPOINT` | Agent Identity 服务端点(可选,不提供时自动生成) |
117+
118+
对应 `yaml` 文件格式:
119+
120+
```yaml [config.yaml]
121+
veidentity:
122+
region: cn-beijing
123+
endpoint: # 可选,不提供时自动生成
124+
```
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
title: 认证与授权
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
---
2+
title: Agent Identity 产品介绍
3+
description: Agent Identity 身份认证产品的开通流程和作用
4+
navigation:
5+
icon: i-lucide-shield-check
6+
---
7+
8+
Agent Identity 是火山引擎提供的一站式身份及权限管理平台,负责用户身份验证、权限控制,确保 Agent 出、入操作的安全性与合规性。
9+
10+
## 核心功能
11+
12+
- **用户身份管理**:支持用户池管理、企业 IdP(SAML/OIDC)及第三方身份联合
13+
- **工作负载身份管理**:为 Agent/工具分配唯一数字身份,维护属性标签
14+
- **第三方凭据托管**:加密托管 API Key 和 OAuth 令牌,杜绝明文凭据泄露
15+
- **权限管控**:基于属性和上下文的动态授权,实现细粒度权限控制
16+
17+
## 开通流程
18+
19+
### 1. 访问 Agent Identity 控制台
20+
21+
访问 **[Agent Identity](https://console.volcengine.com/identity)** 服务开通页,勾选 **同意服务条款**, 点击 **开通并授权**
22+
23+
### 2. 创建出站凭据提供商
24+
25+
根据使用场景创建相应的出站凭据提供商:
26+
27+
| 认证方式 | 提供商类型 | 使用场景 |
28+
|---------|----------|--------|
29+
| API Key | API Key | 服务间通信 |
30+
| OAuth2 M2M | OAuth Client | 后端服务间认证 |
31+
| OAuth2 USER_FEDERATION | OAuth Client | 用户委托认证 |
32+
33+
### 3. 配置凭证信息
34+
35+
根据认证方式配置相应的凭证(如 API Key、Client ID、Client Secret、回调 URL 等)。
36+
37+
### 4. 快速使用
38+
39+
```python
40+
from veadk.integrations.ve_identity import (
41+
VeIdentityFunctionTool,
42+
api_key_auth,
43+
)
44+
45+
auth_config = api_key_auth(provider_name="my-provider")
46+
tool = VeIdentityFunctionTool(
47+
func=my_function,
48+
auth_config=auth_config,
49+
into="api_key",
50+
)
51+
```
52+
53+
## 常见问题
54+
55+
**Q: 如何选择认证方式?**
56+
57+
A:
58+
- **API Key**:简单、固定凭证,适合简单场景
59+
- **OAuth2 M2M**:安全、支持令牌过期和刷新,适合生产环境
60+
- **OAuth2 USER_FEDERATION**:用户授权,适合需要用户同意的场景
61+
62+
**Q: 凭证会被暴露吗?**
63+
64+
A: 不会。Agent Identity 会:
65+
- 加密存储凭证
66+
- 支持凭证自动轮换
67+
- 不在代码中暴露凭证
68+
69+
**Q: 如何处理凭证过期?**
70+
71+
A: Agent Identity 会自动处理:
72+
- 令牌缓存和刷新
73+
- 凭证自动轮换
74+
- 过期前自动更新
75+
76+
## 后续步骤
77+
78+
- [使用 API Key 进行出站认证](./2.api-key-outbound.md)
79+
- [使用 OAuth2 M2M 进行出站认证](./3.oauth2-m2m-outbound.md)
80+
- [使用 OAuth2 USER_FEDERATION 进行出站认证](./4.oauth2-user-federation-outbound.md)
81+
82+
## 相关资源
83+
84+
### Agent Identity 官方文档
85+
- [Agent Identity API 参考](https://www.volcengine.com/docs/86848/1918752)
86+
87+
### 外部资源
88+
- [OAuth2 规范](https://tools.ietf.org/html/rfc6749)
89+
- [OpenID Connect](https://openid.net/connect/)
90+
91+
## 获取帮助
92+
93+
如有问题,请:
94+
1. 查看相应文档的常见问题部分
95+
2. 查看 [Agent Identity 官方文档](https://www.volcengine.com/docs/86848/1913964)
96+
3. 联系火山引擎技术支持
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
---
2+
title: 使用 API Key 进行出站认证
3+
description: 通过 API Key 认证访问外部服务
4+
navigation:
5+
icon: i-lucide-key
6+
---
7+
8+
API Key 认证是最简单的出站认证方式,适用于服务间通信和固定凭证场景。
9+
10+
## 创建 API Key 凭据
11+
12+
1. 登录火山引擎控制台,导航到 **Agent Identity** 服务
13+
2. 在左侧导航树中,选择 **身份认证 > 出站凭据托管**
14+
3. 点击 **新建 > 新建 API Key**,填写 API Key 名称
15+
4. 输入第三方服务提供的 API Key
16+
5. 配置 API Key 传递方式(Header 或 Query)并点击 **确定**
17+
18+
## 使用方式
19+
20+
### VeIdentityFunctionTool
21+
22+
```python
23+
from veadk.integrations.ve_identity import VeIdentityFunctionTool, api_key_auth
24+
import aiohttp
25+
26+
async def call_api(api_key: str, endpoint: str):
27+
headers = {"Authorization": f"Bearer {api_key}"}
28+
async with aiohttp.ClientSession() as session:
29+
async with session.get(endpoint, headers=headers) as resp:
30+
return await resp.json()
31+
32+
auth_config = api_key_auth(provider_name="my-api-provider")
33+
tool = VeIdentityFunctionTool(
34+
func=call_api,
35+
auth_config=auth_config,
36+
into="api_key",
37+
)
38+
```
39+
40+
### VeIdentityMcpToolset
41+
42+
```python
43+
from veadk.integrations.ve_identity import VeIdentityMcpToolset, api_key_auth
44+
from google.adk.agents.mcp import StdioServerParameters
45+
46+
auth_config = api_key_auth(provider_name="my-api-provider")
47+
toolset = VeIdentityMcpToolset(
48+
auth_config=auth_config,
49+
connection_params=StdioServerParameters(
50+
command="python",
51+
args=["-m", "my_api_mcp_server"],
52+
),
53+
)
54+
```
55+
56+
## 示例
57+
58+
```python
59+
import asyncio
60+
from veadk import Agent, Runner
61+
from veadk.integrations.ve_identity import VeIdentityFunctionTool, api_key_auth
62+
import aiohttp
63+
64+
async def query_api(api_key: str, user_id: str):
65+
headers = {"Authorization": f"Bearer {api_key}"}
66+
url = f"https://api.example.com/users/{user_id}"
67+
async with aiohttp.ClientSession() as session:
68+
async with session.get(url, headers=headers) as resp:
69+
return await resp.json()
70+
71+
tool = VeIdentityFunctionTool(
72+
func=query_api,
73+
auth_config=api_key_auth(provider_name="user-api"),
74+
into="api_key",
75+
)
76+
77+
agent = Agent(tools=[tool])
78+
runner = Runner(agent=agent)
79+
asyncio.run(runner.run(messages="查询用户 123"))
80+
```
81+
82+
## 常见问题
83+
84+
**Q: 如何更新 API Key?**
85+
86+
A: 在 Agent Identity 控制台中编辑凭证即可。
87+
88+
**Q: 如何处理 API 调用失败?**
89+
90+
A: 实现错误处理和重试逻辑。
91+
92+
## 相关资源
93+
94+
- [Agent Identity 产品介绍](./1.agent-identity-intro.md)
95+
- [OAuth2 M2M 认证](./3.oauth2-m2m-outbound.md)
96+
- [OAuth2 USER_FEDERATION 认证](./4.oauth2-user-federation-outbound.md)
97+
- [Agent Identity API 参考](https://www.volcengine.com/docs/86848/1918752)
98+
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
---
2+
title: 使用 OAuth2 M2M 进行出站认证
3+
description: 通过 OAuth2 M2M 认证进行机器对机器通信
4+
navigation:
5+
icon: i-lucide-network
6+
---
7+
8+
OAuth2 M2M(Machine to Machine)认证用于服务间通信,比 API Key 更安全且支持令牌刷新。
9+
10+
## 创建 OAuth2 M2M 凭据
11+
12+
### 基础步骤
13+
14+
1. 登录火山引擎控制台,导航到 **Agent Identity** 服务
15+
2. 在左侧导航树中,选择 **身份认证 > 出站凭据托管 > OAuth Client**
16+
3. 点击 **新建 > 新建 OAuth Client**
17+
4.**OAuth2 流程** 中选择 **机器对机器(M2M)**
18+
5. 点击 **确定** 完成创建
19+
20+
### 方式一:使用内置 Vendor(推荐)
21+
22+
选择提供商类型:**Lark****Coze****Google****GitHub** ,填写:
23+
- Client ID
24+
- Client Secret
25+
26+
### 方式二:使用 OIDC 配置
27+
28+
选择提供商类型为 **自定义**,填写:
29+
- 发行者 URL:OIDC 提供商的 Discovery URL(如 `https://accounts.google.com/.well-known/openid-configuration`
30+
- Client ID
31+
- Client Secret
32+
- 权限范围:至少包含 `openid`
33+
34+
### 方式三:使用自定义 OAuth2 配置
35+
36+
选择提供商类型为 **自定义**,填写:
37+
- Client ID
38+
- Client Secret
39+
- 权限范围
40+
- Issuer
41+
- 授权端点
42+
- 令牌端点
43+
44+
## 使用方式
45+
46+
### VeIdentityFunctionTool
47+
48+
```python
49+
from veadk.integrations.ve_identity import VeIdentityFunctionTool, oauth2_auth
50+
import aiohttp
51+
52+
async def call_service(access_token: str, endpoint: str):
53+
headers = {"Authorization": f"Bearer {access_token}"}
54+
async with aiohttp.ClientSession() as session:
55+
async with session.get(endpoint, headers=headers) as resp:
56+
return await resp.json()
57+
58+
auth_config = oauth2_auth(
59+
provider_name="my-oauth2-m2m-provider",
60+
scopes=["api://your-service/.default"],
61+
auth_flow="M2M",
62+
)
63+
tool = VeIdentityFunctionTool(
64+
func=call_service,
65+
auth_config=auth_config,
66+
)
67+
```
68+
69+
### VeIdentityMcpToolset
70+
71+
```python
72+
from veadk.integrations.ve_identity import VeIdentityMcpToolset, oauth2_auth
73+
from google.adk.agents.mcp import StdioServerParameters
74+
75+
auth_config = oauth2_auth(
76+
provider_name="my-oauth2-m2m-provider",
77+
scopes=["api://your-service/.default"],
78+
auth_flow="M2M",
79+
)
80+
toolset = VeIdentityMcpToolset(
81+
auth_config=auth_config,
82+
connection_params=StdioServerParameters(
83+
command="python",
84+
args=["-m", "my_service_mcp_server"],
85+
),
86+
)
87+
```
88+
89+
## 常见问题
90+
91+
**Q: OAuth2 M2M 和 API Key 有什么区别?**
92+
93+
A: API Key 简单固定,OAuth2 M2M 更安全且支持令牌刷新。
94+
95+
**Q: 令牌过期了怎么办?**
96+
97+
A: Agent Identity 会自动刷新令牌。
98+
99+
**Q: 如何配置多个 Scopes?**
100+
101+
A:
102+
```python
103+
auth_config = oauth2_auth(
104+
provider_name="my-provider",
105+
scopes=["api://service1/.default", "api://service2/.default"],
106+
auth_flow="M2M",
107+
)
108+
```
109+
110+
## 相关资源
111+
112+
- [Agent Identity 产品介绍](./1.agent-identity-intro.md)
113+
- [API Key 认证](./2.api-key-outbound.md)
114+
- [OAuth2 USER_FEDERATION 认证](./4.oauth2-user-federation-outbound.md)
115+
- [Agent Identity API 参考](https://www.volcengine.com/docs/86848/1918752)
116+

0 commit comments

Comments
 (0)