Overview
This document explains how to build an agent-centric identity and access management system using the identity security capabilities of the Alibaba Cloud IDaaS Agent. This solution lets you securely manage digital identities for agents, centralize access credentials, such as API keys and OAuth tokens, and enable agents to securely access large models, third-party services, and internal services on behalf of a user or themselves.
Scenarios
As AI agents become more common, they need to securely access various external services:
-
Large model service: An agent needs to call a large model to perform AI inference. This example uses Model Studio.
-
Third-party service: An agent needs to access third-party services. This example uses the Amap MCP Server.
-
Internal enterprise service: An agent needs to access internal enterprise services. This example uses a service deployed on Function Compute.
Traditional methods, such as hardcoding credentials in code or storing them in configuration files, pose security risks, such as credential leakage and challenges with rotation. This solution uses IDaaS to:
-
Centralize credential management in IDaaS, allowing the agent to retrieve credentials dynamically at runtime.
-
Use M2M (Machine-to-Machine) identity authentication to ensure only authorized agents can retrieve credentials.
-
Dynamically issue access tokens to enable granular access control.
Solution architecture
Workflow
Workflow
|
Phase |
Description |
Key credentials |
|
User authentication |
The user signs in via SSO to obtain an access token (AT_u) to access the agent. |
AT_u (aud: audience identifier for the agent, scope: permission scope) |
|
User identity verification |
The agent validates the AT_u and its permissions. |
IDaaS JWKS public key |
|
Agent initialization |
The agent uses its M2M application identity to dynamically obtain various credentials from IDaaS. |
LLM API key, third-party service API key, and enterprise service access token (AT_s, aud: audience identifier for the enterprise service, scope: permission scope for the enterprise service) |
|
AI request processing |
The agent calls the LLM and various tools to complete the AI request. |
On-demand use of credentials. |
Prerequisites
-
You have created an API key on Model Studio with permissions to call its models.
-
You have added Amap in Model Studio's MCP Square.
-
You have activated Function Compute.
-
You have configured agent identity security in IDaaS and set up the Agent node, Client node, Large Language Model (LLM) node (using the Model Studio API key), External Service node (using the Model Studio API key), and Enterprise Service node. For more information, see Agent Identity Security Configuration Guide.
Steps
Step 1: Get the agent sample code
Clone the agent sample code using Git, or download it directly. Open a terminal and run the following command.
git clone https://github.com/cloud-idaas/idaas-java-agent-id-demo.git
Step 2: Configure the IDaaS SDK
-
Generate the SDK configuration
-
Go to the IDaaS EIAM console.
-
In the left-side navigation pane, select Agent Security.
-
Locate the target agent and click Operation in the Edit column.
-
Click the agent node.
-
For Authentication Type, select Client Secret Credential, and then click Generate SDK Configuration.
-
Copy the configuration content.
For detailed configuration instructions, see Environment Preparation.
-
-
Configure the SDK files
Paste the copied SDK configuration into the following two files in the agent sample code directory:
-
src/main/resources/cloud_idaas_config_for_computer.json(for local deployment) -
src/main/resources/cloud_idaas_config_for_agent_run.json(for AgentRun deployment)
-
Step 3: Deploy the enterprise service
3.1 Deploy the function
-
Log on to the Function Compute (FC) console.
-
In the left-side navigation pane, select Function Management > Functions.
-
Select the Web Function tab and click Create Web Function.
-
For runtime environment, select Custom Runtime > Java > Java 17.
-
For Code Upload Method, select Use Sample Code. Leave other settings at their defaults.
-
Finish creating the function.
3.2 Configure JWT authentication
Configure JWT authentication for the function's HTTP trigger. This ensures that only callers with an access token issued by IDaaS can access the enterprise service.
-
On the IDaaS instance console, click the Enterprise Service node you created by following the Agent Identity Security Configuration Guide.
-
Under General, copy the OIDC Discovery Endpoint. Open it in a browser and copy the JWKS endpoint, which is the value of the
jwks_urifield. -
Open the JWKS endpoint in your browser and copy the entire response.
-
In the Function Compute (FC) console, find the function in the Functions list and click Configure in the Actions column.
-
On the Function Details tab, click the Trigger in the Function Topology. For Authentication Method, select JWT Authentication.
-
Paste the response from the JWKS endpoint into the JWKS field.
-
In the JWT Token Configuration section, set Parameter Name to Authorization.
-
Finish the configuration.
Step 4: Deploy the agent
The agent is deployed in AgentRun on Alibaba Cloud Function Compute.
AgentRun
-
Package and compress the agent sample code
Run the following commands in your terminal:
cd idaas-java-agent-id-demo mvn clean package cd ../ zip -r idaas-java-agent-id-demo.zip idaas-java-agent-id-demo/ -
Create an execution role
-
Log on to the Resource Access Management (RAM) console.
-
In the navigation pane, select .
-
Click Create Role. For Principal Type, select Cloud Service. For Principal Name, select Function Compute (FC).
-
Set the role name to
sample-fc-role.
-
-
Create the agent
-
Log on to the Function Compute (FC) console.
-
In the left-side navigation pane, select FunctionAI > AgentRun.
-
When prompted for role authorization, click Authorize. This is only required the first time.
-
Click Create Agent and select Create from Code.
-
For Code Source, select Upload Code Package. For Runtime, select Java 17.
-
Upload the compressed
idaas-java-agent-id-demo.zipfile. -
Enter the following startup command:
java -Dcloud_idaas_config_path=classpath:cloud_idaas_config_for_agent_run.json -jar idaas-java-agent-id-demo/target/idaas-java-agent-id-demo-1.0.jar -
Enter
9002for the startup port. -
Configure the environment variables.
Parameter
Description
Source
IDAAS_CLIENT_SECRETThe agent's client secret credential. Required for Client Secret authentication.
Find it under the General section of the agent node created in the Agent Identity Security Configuration Guide.
ENV_PRIVATE_KEY
The agent's private key. Required for public/private key authentication.
This is the private key corresponding to the public key you uploaded when creating the public/private key credential for the agent node, as described in the Agent Identity Security Configuration Guide.
JWKS_ENDPOINTThe IDaaS JWKS endpoint.
The format is
https://xxx.aliyunidaas.com/api/v2/iauths_system/oauth2/jwks.Replace
xxxwith the corresponding value from theissuerfield in the SDK configuration file from Step 2: Configure the IDaaS SDK.AGENT_AUDIENCEThe agent's audience.
Find it under the General section of the agent node created in the Agent Identity Security Configuration Guide.
AGENT_SCOPEThe agent's scope.
Find it under the Permission Configuration section of the agent node created in the Agent Identity Security Configuration Guide.
ACCESS_IDENTITY
The agent identity mode:
Machine(machine identity) orHuman(user identity).This example uses
Machine(machine identity), which is suitable for scenarios where the agent operates independently. Downstream services recognize the agent's machine identity.LLM_API_KEY_IDENTIFIERThe large language model API key identifier.
Find it in the Large Language Model (LLM) node created in the Agent Identity Security Configuration Guide.
EXTERNAL_SERVER_API_KEY_IDENTIFIERThe third-party service API key identifier.
Find it in the External Service node created in the Agent Identity Security Configuration Guide.
ENTERPRISE_SERVICE_SCOPEThe enterprise service access scope (audience|scope).
Use the format "audience|scope". Be sure to include the double quotes.
In the Enterprise Service node created in the Agent Identity Security Configuration Guide, find the audience under General and the Permission Identifier under Permission Configuration.
EXTERNAL_SERVER_URLThe third-party service endpoint.
The Amap MCP Server HTTP endpoint. You can find it in the Amap MCP within the Model Studio platform.
ENTERPRISE_SERVICE_URLThe enterprise service endpoint.
As described in 3.2 Configure JWT authentication, on the Function Details tab, click the Trigger in the Function Topology to view the Internet Endpoint.
-
For the execution role, select the
sample-fc-rolethat you created earlier. -
Click Start Deployment.
-
After the deployment is complete, click Details on the agent card. In the left-side navigation pane, select Versions & Grayscale, and then click Create Endpoint.
-
Step 5: Deploy the client
5.1 Configure the frontend UI
Modify the frontend/config.js file in the agent sample code:
window.APP_CONFIG = {
API_URL: 'http://localhost:9002/openai/v1/chat/completions',
IDAAS_AUTHORIZE_ENDPOINT: 'https://xxx.aliyunidaas.com/login/app/common/oauth2/authorize',
IDAAS_LOGOUT_ENDPOINT: 'https://xxx.aliyunidaas.com/login/app/common/oauth2/logout',
CLIENT_ID: '{client_id}',
SCOPE: 'https://agentserver.example.com|agent.access',
};
|
Parameter |
Description |
Source |
|
|
The request path for the agent service. |
Local deployment: AgentRun deployment: |
|
|
The IDaaS authorization endpoint. |
The format is:
Replace |
|
|
The IDaaS logout endpoint. |
The format is:
Replace |
|
|
The client ID of the Client created in IDaaS. |
Find it in the Client node created in the Agent Identity Security Configuration Guide. |
|
|
The access scope, in the format: |
In the agent node created in the Agent Identity Security Configuration Guide, find the |
5.2 Start the frontend UI
Run the following commands in your terminal (Node.js is required):
cd frontend
npx http-server -p 9001
5.3 Configure the client application
-
Go to the IDaaS EIAM console.
-
In the left-side navigation pane, click Application Management > M2M Application.
-
Locate the Client node created in the Agent Identity Security Configuration Guide and click Manage in the Actions column.
-
Click the Sign-In tab. In the Grant Types section, select the Implicit Mode checkbox.
-
For Implicit Mode Parameters, set Return Type to token.
-
For Redirect URIs, enter the startup URL of the frontend UI:
http://127.0.0.1:9001/. -
Click Show Advanced Settings and for the logout callback URL, enter the frontend UI startup URL:
http://127.0.0.1:9001/. -
Finish the configuration.
Step 6: Verify the configuration
-
Open a browser on your local machine and go to
http://127.0.0.1:9001. -
Click Log in with IDaaS account and sign in with your IDaaS account.
-
After you sign in, enter a test prompt in the chat box. For example:
-
"What is the weather in Hangzhou right now?": The agent calls the Amap MCP Tool to query the weather.
-
"Call my enterprise service": The agent calls the enterprise service tool.
-
Error handling
|
Symptom |
Possible cause |
Solution |
|
Access token validation failed. |
The token is expired or invalid. |
Log in again to get a new access token. |
|
Failed to retrieve the large model API key. |
The API key identifier is configured incorrectly. |
Ensure the |
|
Enterprise service call failed. |
The access token lacks the required scopes. |
Ensure |
|
JWT authentication failed. |
The JWKS configuration is incorrect. |
Ensure the JWKS configuration for the Function Compute trigger is correct. |
|
Failed to load the SDK configuration. |
The configuration file path is incorrect. |
Ensure the |
Appendix
OpenAPI authentication for AgentRun
For AgentRun deployments, IDaaS supports the OpenAPI authentication method. With this method, you can use an STS token from an execution role to call the IDaaS OpenAPI and obtain an agent's access token, without configuring a client secret or private key.
Modify the IDaaS SDK configuration
Modify src/main/resources/cloud_idaas_config_for_agent_run.json by adding openApiEndpoint and modifying authnConfiguration, leaving the other fields unchanged:
{
"idaasInstanceId": "idaas_xxx",
"clientId": "app_xxx",
"issuer":"https://xxx/api/v2/iauths_system/oauth2",
"tokenEndpoint": "https://xxx/api/v2/iauths_system/oauth2/token",
"scope": "api.example.com|read:file",
"openApiEndpoint":"eiam.[region_id].aliyuncs.com",
"developerApiEndpoint":"eiam-developerapi.[region_id].aliyuncs.com",
"authnConfiguration": {
"identityType": "CLIENT",
"authnMethod": "PLUGIN",
"pluginName": "alibabacloudPluginCredentialProvider"
}
}
For detailed instructions, see Environment preparation.
Configure the execution role
The AgentRun execution role must have permission to call the IDaaS GenerateOauthToken OpenAPI. An example permission policy is as follows:
{
"Version": "1",
"Statement": [
{
"Effect": "Allow",
"Action": "eiam:GenerateOauthToken",
"Resource": [
"acs:eiam:{regionId}:{accountId}:instance/{InstanceId}/application/{ApplicationId}"
]
}
]
}
Replace {regionId}, {accountId}, {InstanceId}, and {ApplicationId} with your region ID, Alibaba Cloud account ID, IDaaS instance ID, and agent ID.
To create a permission policy and grant it to an execution role, see Alibaba Cloud OpenAPI authentication.
When deploying a service in AgentRun, you must modify the SDK configuration and configure the execution role as described above. All other steps in the deployment process remain the same.