Skip to content

Commit f0d5941

Browse files
committed
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.
1 parent 4e81f59 commit f0d5941

File tree

11 files changed

+239
-100
lines changed

11 files changed

+239
-100
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+
```

veadk/agent.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
DEFAULT_MODEL_EXTRA_CONFIG,
3434
)
3535
from veadk.evaluation import EvalSetRecorder
36+
from veadk.integrations.ve_identity import AuthRequestProcessor
3637
from veadk.knowledgebase import KnowledgeBase
3738
from veadk.memory.long_term_memory import LongTermMemory
3839
from veadk.memory.short_term_memory import ShortTermMemory
@@ -167,9 +168,11 @@ async def _run(
167168
session_id: str,
168169
message: types.Content,
169170
stream: bool,
171+
auth_request_processor: AuthRequestProcessor,
170172
):
171173
stream_mode = StreamingMode.SSE if stream else StreamingMode.NONE
172174

175+
@auth_request_processor.with_auth_loop(runner=runner, message=message)
173176
async def event_generator():
174177
async for event in runner.run_async(
175178
user_id=user_id,
@@ -245,6 +248,7 @@ async def run(
245248
collect_runtime_data: bool = False,
246249
eval_set_id: str = "",
247250
save_session_to_memory: bool = False,
251+
auth_request_processor: AuthRequestProcessor = AuthRequestProcessor(),
248252
):
249253
"""Running the agent. The runner and session service will be created automatically.
250254
@@ -294,13 +298,15 @@ async def run(
294298
final_output = ""
295299
for _prompt in prompt:
296300
message = types.Content(role="user", parts=[types.Part(text=_prompt)])
297-
final_output = await self._run(runner, user_id, session_id, message, stream)
301+
final_output = await self._run(
302+
runner, user_id, session_id, message, stream, auth_request_processor
303+
)
298304

299305
# VeADK features
300306
if save_session_to_memory:
301-
assert self.long_term_memory is not None, (
302-
"Long-term memory is not initialized in agent"
303-
)
307+
assert (
308+
self.long_term_memory is not None
309+
), "Long-term memory is not initialized in agent"
304310
session = await session_service.get_session(
305311
app_name=app_name,
306312
user_id=user_id,

veadk/config.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from dotenv import find_dotenv, load_dotenv
1919
from pydantic import BaseModel, Field
2020

21+
from veadk.configs.auth_configs import VeIdentityConfig
2122
from veadk.configs.database_configs import (
2223
MysqlConfig,
2324
OpensearchConfig,
@@ -64,6 +65,8 @@ class VeADKConfig(BaseModel):
6465
default_factory=VikingKnowledgebaseConfig
6566
)
6667

68+
veidentity: VeIdentityConfig = Field(default_factory=VeIdentityConfig)
69+
6770

6871
def getenv(
6972
env_name: str, default_value: Any = "", allow_false_values: bool = False
@@ -103,4 +106,4 @@ def getenv(
103106
else:
104107
logger.warning("No `config.yaml` file found.")
105108

106-
settings = VeADKConfig()
109+
settings = VeADKConfig()

veadk/configs/auth_configs.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd. and/or its affiliates.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from pydantic_settings import BaseSettings, SettingsConfigDict
16+
17+
18+
class VeIdentityConfig(BaseSettings):
19+
"""Configuration for VolcEngine Identity Service.
20+
21+
This configuration class manages settings for Agent Identity service,
22+
including region and endpoint information.
23+
24+
Attributes:
25+
region: The VolcEngine region for Identity service.
26+
endpoint: The endpoint URL for Identity service API.
27+
If not provided, will be auto-generated based on region.
28+
"""
29+
30+
model_config = SettingsConfigDict(env_prefix="VEIDENTITY_")
31+
32+
region: str = "cn-beijing"
33+
"""The VolcEngine region for Identity service.
34+
"""
35+
36+
endpoint: str = ""
37+
"""The endpoint URL for Identity service API.
38+
39+
If not provided, the endpoint will be auto-generated based on the region.
40+
"""
41+
42+
def get_endpoint(self) -> str:
43+
"""Get the endpoint URL for Identity service.
44+
45+
Returns the configured endpoint if provided, otherwise generates
46+
the endpoint based on the region.
47+
48+
Returns:
49+
The endpoint URL for Identity service API.
50+
51+
Raises:
52+
ValueError: If region is not supported.
53+
"""
54+
if self.endpoint:
55+
return self.endpoint
56+
57+
return f"id.{self.region}.volces.com"
58+

veadk/integrations/ve_identity/auth_config.py

Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,34 @@
2727
from veadk.integrations.ve_identity.identity_client import IdentityClient
2828

2929

30+
def _get_default_region() -> str:
31+
"""Get the default region from VeADK configuration.
32+
33+
Returns:
34+
The configured region from VeIdentityConfig, or "cn-beijing" as fallback.
35+
"""
36+
try:
37+
from veadk.config import settings
38+
return settings.veidentity.region
39+
except Exception:
40+
# Fallback to default if config loading fails
41+
return "cn-beijing"
42+
43+
3044
class AuthConfig(BaseModel, ABC):
3145
"""Base authentication configuration."""
3246

3347
model_config = {"arbitrary_types_allowed": True}
3448

3549
provider_name: str
3650
identity_client: Optional[IdentityClient] = None
37-
region: str = "cn-beijing"
51+
region: str = None # Will be set to default from config if not provided
52+
53+
def __init__(self, **data):
54+
"""Initialize AuthConfig with default region from VeADK config if not provided."""
55+
if 'region' not in data or data['region'] is None:
56+
data['region'] = _get_default_region()
57+
super().__init__(**data)
3858

3959
@field_validator("provider_name")
4060
@classmethod
@@ -138,9 +158,20 @@ def auth_type(self) -> str:
138158
def api_key_auth(
139159
provider_name: str,
140160
identity_client: Optional[IdentityClient] = None,
141-
region: str = "cn-beijing",
161+
region: Optional[str] = None,
142162
) -> ApiKeyAuthConfig:
143-
"""Create an API key authentication configuration."""
163+
"""Create an API key authentication configuration.
164+
165+
Args:
166+
provider_name: Name of the credential provider.
167+
identity_client: Optional IdentityClient instance.
168+
region: VolcEngine region. If not provided, uses the region from VeADK config.
169+
170+
Returns:
171+
ApiKeyAuthConfig instance.
172+
"""
173+
if region is None:
174+
region = _get_default_region()
144175
return ApiKeyAuthConfig(
145176
provider_name=provider_name, identity_client=identity_client, region=region
146177
)
@@ -149,9 +180,20 @@ def api_key_auth(
149180
def workload_auth(
150181
provider_name: str,
151182
identity_client: Optional[IdentityClient] = None,
152-
region: str = "cn-beijing",
183+
region: Optional[str] = None,
153184
) -> WorkloadAuthConfig:
154-
"""Create a workload authentication configuration."""
185+
"""Create a workload authentication configuration.
186+
187+
Args:
188+
provider_name: Name of the credential provider.
189+
identity_client: Optional IdentityClient instance.
190+
region: VolcEngine region. If not provided, uses the region from VeADK config.
191+
192+
Returns:
193+
WorkloadAuthConfig instance.
194+
"""
195+
if region is None:
196+
region = _get_default_region()
155197
return WorkloadAuthConfig(
156198
provider_name=provider_name, identity_client=identity_client, region=region
157199
)
@@ -167,9 +209,27 @@ def oauth2_auth(
167209
on_auth_url: Optional[Callable[[str], Any]] = None,
168210
oauth2_auth_poller: Optional[Callable[[Any], OAuth2AuthPoller]] = None,
169211
identity_client: Optional[IdentityClient] = None,
170-
region: str = "cn-beijing",
212+
region: Optional[str] = None,
171213
) -> OAuth2AuthConfig:
172-
"""Create an OAuth2 authentication configuration."""
214+
"""Create an OAuth2 authentication configuration.
215+
216+
Args:
217+
provider_name: Name of the credential provider.
218+
scopes: List of OAuth2 scopes.
219+
auth_flow: Authentication flow type ("M2M" or "USER_FEDERATION").
220+
callback_url: Optional callback URL for OAuth2.
221+
force_authentication: Whether to force authentication.
222+
response_for_auth_required: Response to return when auth is required.
223+
on_auth_url: Callback function for auth URL.
224+
oauth2_auth_poller: Callback function for auth polling.
225+
identity_client: Optional IdentityClient instance.
226+
region: VolcEngine region. If not provided, uses the region from VeADK config.
227+
228+
Returns:
229+
OAuth2AuthConfig instance.
230+
"""
231+
if region is None:
232+
region = _get_default_region()
173233
return OAuth2AuthConfig(
174234
provider_name=provider_name,
175235
scopes=scopes,

veadk/integrations/ve_identity/auth_mixins.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
ApiKeyAuthConfig,
4545
OAuth2AuthConfig,
4646
WorkloadAuthConfig,
47+
_get_default_region,
4748
)
4849
from veadk.integrations.ve_identity.token_manager import get_workload_token
4950

@@ -86,15 +87,16 @@ def __init__(
8687
*,
8788
provider_name: str,
8889
identity_client: Optional[IdentityClient] = None,
89-
region: str = "cn-beijing",
90+
region: Optional[str] = None,
9091
**kwargs,
9192
):
9293
"""Initialize the Identity authentication mixin.
9394
9495
Args:
9596
provider_name: Name of the credential provider configured in identity service.
9697
identity_client: Optional IdentityClient instance. If not provided, creates a new one.
97-
region: VolcEngine region for the identity client. Defaults to "cn-beijing".
98+
region: VolcEngine region for the identity client. If not provided, uses the region
99+
from VeADK config. Defaults to "cn-beijing" if config is not available.
98100
**kwargs: Additional arguments passed to parent classes.
99101
"""
100102
# Only pass kwargs to super() if we're in a multiple inheritance scenario
@@ -106,6 +108,10 @@ def __init__(
106108
# call it without arguments
107109
super().__init__()
108110

111+
# Use provided region or get from config
112+
if region is None:
113+
region = _get_default_region()
114+
109115
self._identity_client = identity_client or IdentityClient(region=region)
110116
self._provider_name = provider_name
111117

@@ -207,7 +213,7 @@ async def _get_credential(
207213
)
208214

209215
# Fetch API key from identity service
210-
api_key = await self._identity_client.get_api_key(
216+
api_key = self._identity_client.get_api_key(
211217
provider_name=self._provider_name,
212218
agent_identity_token=workload_token,
213219
)
@@ -386,7 +392,7 @@ async def _get_oauth2_token_or_auth_url(
386392
)
387393

388394
# Request OAuth2 token or auth URL
389-
return await self._identity_client.get_oauth2_token_or_auth_url(
395+
return self._identity_client.get_oauth2_token_or_auth_url(
390396
provider_name=self._provider_name,
391397
agent_identity_token=workload_token,
392398
auth_flow=self._auth_flow,
@@ -512,7 +518,7 @@ def _create_default_oauth2_poller(self, auth_uri: str, request_dict: dict):
512518
from veadk.integrations.ve_identity.auth_processor import _DefaultOauth2AuthPoller
513519

514520
async def async_token_fetcher():
515-
response = await self._identity_client.get_oauth2_token_or_auth_url(
521+
response = self._identity_client.get_oauth2_token_or_auth_url(
516522
**request_dict
517523
)
518524
return (

0 commit comments

Comments
 (0)