All Products
Search
Document Center

Identity as a Service:Agent identity security with AgentRun

Last Updated:May 25, 2026

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

image

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

  1. Generate the SDK configuration

    1. Go to the IDaaS EIAM console.

    2. In the left-side navigation pane, select Agent Security.

    3. Locate the target agent and click Operation in the Edit column.

    4. Click the agent node.

    5. For Authentication Type, select Client Secret Credential, and then click Generate SDK Configuration.

    6. Copy the configuration content.

    For detailed configuration instructions, see Environment Preparation.

  2. 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

  1. Log on to the Function Compute (FC) console.

  2. In the left-side navigation pane, select Function Management > Functions.

  3. Select the Web Function tab and click Create Web Function.

  4. For runtime environment, select Custom Runtime > Java > Java 17.

  5. For Code Upload Method, select Use Sample Code. Leave other settings at their defaults.

  6. 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.

  1. On the IDaaS instance console, click the Enterprise Service node you created by following the Agent Identity Security Configuration Guide.

  2. Under General, copy the OIDC Discovery Endpoint. Open it in a browser and copy the JWKS endpoint, which is the value of the jwks_uri field.

  3. Open the JWKS endpoint in your browser and copy the entire response.

  4. In the Function Compute (FC) console, find the function in the Functions list and click Configure in the Actions column.

  5. On the Function Details tab, click the Trigger in the Function Topology. For Authentication Method, select JWT Authentication.

  6. Paste the response from the JWKS endpoint into the JWKS field.

  7. In the JWT Token Configuration section, set Parameter Name to Authorization.

  8. Finish the configuration.

Step 4: Deploy the agent

The agent is deployed in AgentRun on Alibaba Cloud Function Compute.

AgentRun

  1. 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/
  2. Create an execution role

    1. Log on to the Resource Access Management (RAM) console.

    2. In the navigation pane, select Identities > Roles.

    3. Click Create Role. For Principal Type, select Cloud Service. For Principal Name, select Function Compute (FC).

    4. Set the role name to sample-fc-role.

  3. Create the agent

    1. Log on to the Function Compute (FC) console.

    2. In the left-side navigation pane, select FunctionAI > AgentRun.

    3. When prompted for role authorization, click Authorize. This is only required the first time.

    4. Click Create Agent and select Create from Code.

    5. For Code Source, select Upload Code Package. For Runtime, select Java 17.

    6. Upload the compressed idaas-java-agent-id-demo.zip file.

    7. 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
    8. Enter 9002 for the startup port.

    9. Configure the environment variables.

      Parameter

      Description

      Source

      IDAAS_CLIENT_SECRET

      The 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_ENDPOINT

      The IDaaS JWKS endpoint.

      The format is https://xxx.aliyunidaas.com/api/v2/iauths_system/oauth2/jwks.

      Replace xxx with the corresponding value from the issuer field in the SDK configuration file from Step 2: Configure the IDaaS SDK.

      AGENT_AUDIENCE

      The agent's audience.

      Find it under the General section of the agent node created in the Agent Identity Security Configuration Guide.

      AGENT_SCOPE

      The 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) or Human (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_IDENTIFIER

      The 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_IDENTIFIER

      The third-party service API key identifier.

      Find it in the External Service node created in the Agent Identity Security Configuration Guide.

      ENTERPRISE_SERVICE_SCOPE

      The 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_URL

      The 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_URL

      The 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.

    10. For the execution role, select the sample-fc-role that you created earlier.

    11. Click Start Deployment.

    12. 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

API_URL

The request path for the agent service.

Local deployment: http://localhost:9002/openai/v1/chat/completions

AgentRun deployment: {endpoint}/openai/v1/chat/completions. After deploying the agent in AgentRun, go to the Versions & Grayscale page in the navigation pane and replace {endpoint} with the access URL of the created endpoint.

IDAAS_AUTHORIZE_ENDPOINT

The IDaaS authorization endpoint.

The format is:

https://xxx.aliyunidaas.com/login/app/common/oauth2/authorize

Replace xxx with the corresponding value from the issuer field in the SDK configuration file from Step 2: Configure the IDaaS SDK.

IDAAS_LOGOUT_ENDPOINT

The IDaaS logout endpoint.

The format is:

https://xxx.aliyunidaas.com/login/app/common/oauth2/logout

Replace xxx with the corresponding value from the issuer field in the SDK configuration file from Step 2: Configure the IDaaS SDK.

CLIENT_ID

The client ID of the Client created in IDaaS.

Find it in the Client node created in the Agent Identity Security Configuration Guide.

SCOPE

The access scope, in the format: audience|scope.

In the agent node created in the Agent Identity Security Configuration Guide, find the audience under General and the scope under Permission Configuration.

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

  1. Go to the IDaaS EIAM console.

  2. In the left-side navigation pane, click Application Management > M2M Application.

  3. Locate the Client node created in the Agent Identity Security Configuration Guide and click Manage in the Actions column.

  4. Click the Sign-In tab. In the Grant Types section, select the Implicit Mode checkbox.

  5. For Implicit Mode Parameters, set Return Type to token.

  6. For Redirect URIs, enter the startup URL of the frontend UI: http://127.0.0.1:9001/.

  7. Click Show Advanced Settings and for the logout callback URL, enter the frontend UI startup URL: http://127.0.0.1:9001/.

  8. Finish the configuration.

Step 6: Verify the configuration

  1. Open a browser on your local machine and go to http://127.0.0.1:9001.

  2. Click Log in with IDaaS account and sign in with your IDaaS account.

  3. 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 LLM_API_KEY_IDENTIFIER environment variable is set correctly.

Enterprise service call failed.

The access token lacks the required scopes.

Ensure ENTERPRISE_SERVICE_SCOPE includes the correct audience identifier and scope.

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 -Dcloud_idaas_config_path parameter is set correctly.

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.