Integrate OpenClaw with KMS Credential Manager

Updated at:

When deploying OpenClaw on Alibaba Cloud ECS, the configuration file typically contains a plaintext API key, which poses a security risk. By integrating KMS Credential Manager, you can host the plaintext API key in KMS and use an ECS instance RAM role to enable AccessKey-free access, ensuring that no plaintext keys exist on the ECS instance. This topic describes how to integrate OpenClaw with KMS Credential Manager.

Background

OpenClaw is an AI-powered programming assistant. When deployed on an ECS instance, it requires an LLM API key in the configuration file. By default, this key is stored as plaintext in the openclaw.json file, exposing it to leakage risks. Integrating with KMS Credential Manager mitigates these risks.

Comparison item

KMS does not manage the API key

KMS-managed API key

Storage security

Plaintext at rest: Stored in openclaw.json or environment variables.

Encrypted storage: Encrypted in KMS and protected by a Hardware Security Module (HSM).

Distribution and transmission

High risk: Transmitted via email, instant messaging, or shared documents. Prone to secondary leaks.

Identity-based authorization: Authorized through a RAM role. The plaintext key is never exposed during transmission.

O&M response speed

Inefficient and manual: Log in to each server to update the configuration and restart the service.

Centralized management: Rotate keys in the KMS console. Services dynamically fetch updated keys with zero downtime.

Observability and audit

Blind spot: You cannot track who used or exported the API key or when.

End-to-end audit: ActionTrail logs every decryption call, including identity and source.

Permission control granularity

Coarse-grained: Relies on OS file permissions. If the system is compromised, all keys are exposed.

Fine-grained policies: API-level controls based on IP address, time, and identity.

Leakage risk level

High: Accidentally committing the config file to a repository permanently invalidates the API key.

Low: The config file contains only a credential name. Even if the source code is public, attackers cannot retrieve the API key.

How it works

This solution combines KMS Credential Manager, an instance RAM role, and OpenClaw's external plugin command (exec) to build a secure, automated credential retrieval chain.

  1. Configuration phase:

    • Store your LLM API key in KMS Credential Manager.

    • Create a RAM role and grant it least privilege access to that specific KMS credential.

    • Attach that RAM role to the ECS instance running OpenClaw.

  2. Runtime phase:

    • At startup, OpenClaw reads openclaw.json and runs the external command specified in the secrets definition.

    • The kmscli tool uses the RAM role attached to the ECS instance to call KMS. No AccessKey is needed.

    • KMS validates the RAM role's permissions and returns the encrypted credential.

    • The kmscli tool decrypts the credential value and outputs it to standard output, where OpenClaw reads it.

    • OpenClaw uses the credential for LLM API calls.

Implementation steps

Step 1: Purchase and enable a KMS instance

  1. Log on to the Key Management Service console. In the top navigation bar, select a region. In the left-side navigation pane, choose Resources > Instances.

  2. On the Instances page, click Create Instance. Select a KMS instance type and click Buy Now.

    • Key management type: Choose Software Key Management or Hardware Key Management.

    • Credential count: Set based on your needs. For example, 100.

  3. After purchase, return to the Instances page. On the Software Key Management or Hardware Key Management tab, locate your software key management instance. In the Actions column, click Enable.

Note

For more information, see Purchase and enable a KMS instance.

Step 2: Create a KMS credential

Create a credential in the KMS Credential Manager to store your LLM API key.

  1. Create a key

    1. In the top menu bar, select a region. In the navigation pane on the left, click Resources > Keys.

    2. On the Keys page, click the Customer Master Keys tab. Set Instance ID to your software key management instance. Click Create Key.

      • Key Alias: Enter an easy-to-identify alias for the key, such as cmk/openclaw/bailiansk.

      • Key Type: Choose Symmetric Key.

    Note

    Record the key ARN from the key details page. You need it in Step 3.

  2. Create a credential

    1. In the navigation pane on the left, click Resources > Secrets.

    2. Click the Generic Secrets tab. Select your Instance ID. Click Create Secret. Complete the following settings.

      • Secret Name: Enter secret/openclaw/bailiansk.

      • Secret Value: Paste the LLM API key, for example, sk-xxxxxxxxxxxxxxxxxxxxxxxx. For how to obtain the Bailian API key, see Obtain an API key.

      • CMK: Select the key you created earlier: cmk/openclaw/bailiansk.

    3. Click OK.

    Note

    Record the credential ARN from the credential details page. You need it in Step 3.

Step 3: Create a RAM role and attach a permission policy

Create a RAM role with least privilege access to the KMS credential, then attach it to your ECS instance.

  1. Create a RAM permission policy.

    1. Log on to the Resource Access Management (RAM) console with your Alibaba Cloud account (root account) or a RAM user with AliyunRAMFullAccess permission.

    2. In the navigation pane on the left, choose Permission Management > Permission Policies.

    3. Click Create Permission Policy. Switch to JSON editing mode. Paste the following policy.

      {
          "Version": "1",
          "Statement": [
              {
                  "Action": [
                      "kms:GetSecretValue"
                  ],
                  "Resource": "acs:kms:<region>:<account-id>:secret/secret/openclaw/bailiansk",/*ARN of the credential*/
                  "Effect": "Allow"
              },
              {
                  "Action": [
                      "kms:Decrypt"
                  ],
                  "Resource": "acs:kms:<region>:<account-id>:key/key-hkk69a823******",/*ARN of the key*/
                  "Effect": "Allow"
              }
          ]
      }
      Note

      In the preceding code, <account-id> is the Alibaba Cloud account ID, which can be obtained in the account settings of the console.

      Important

      Obtain the credential ARN and key ARN from their respective details pages created in Step 2.

  2. Create a RAM role and attach the permission policy.

    1. In the navigation pane on the left, choose Identity Management > Roles.

    2. Click Create Role. For Trusted Entity Type, select Alibaba Cloud Service. For Trusted Entity, select Elastic Compute Service (ECS).

    3. On the Permissions tab, select and attach the permission policy you created in the previous step.

Step 4: Create an ECS instance and attach the RAM role

  1. Purchase an ECS instance. Quickly purchase a subscription ECS instance.

  2. Attach the RAM role you created in Step 3 to your ECS instance.

    1. Go to ECS console - Instances. In the top-left corner, select the region and resource group for the target resource.

    2. Click the target ECS instance ID. On the instance details page, click All Operations. Then choose Instance Settings > Attach/Detach RAM Role.

    3. In the dialog box, set Operation type to Grant. Select the RAM role you created in Step 3. Click OK.

Note

For more information, see Instance RAM roles.

Step 5: Install kmscli

kmscli is a lightweight command-line tool that uses the ECS instance RAM role to dynamically retrieve credentials from KMS Credential Manager and output them to standard output. OpenClaw calls this tool at startup to obtain the API key, eliminating plaintext storage in configuration files.

  1. Download the binary file

    Download the kmscli binary for your ECS instance architecture. The following example uses Linux AMD64:

    # Run on your local machine or directly on the ECS instance
    curl -L -o kmscli-linux-amd64 "https://github.com/aliyun/alibabacloud-kms-cli/releases/latest/download/kmscli-linux-amd64"
    Note
    • If you use an ARM architecture, such as a Yitian instance, download kmscli-linux-arm64.

    • If you use macOS, download kmscli-darwin-amd64 or kmscli-darwin-arm64.

    • For a complete list of versions, visit GitHub Releases.

  2. Upload the file to the target directory

    Upload the binary to the ~/.openclaw/ directory on your ECS instance and rename it to kmscli.

    • If you downloaded the file locally and need to upload it:

      # Run this command in your local terminal (replace user and ecs-ip)
      scp kmscli-linux-amd64 user@<ecs-ip>:~/.openclaw/kmscli
    • If you downloaded the file on the ECS instance:

      # Run this command on the ECS instance
      cp kmscli-linux-amd64 ~/.openclaw/kmscli
  3. Grant execute permissions

    # Log on to the ECS instance and run these commands (Linux/macOS)
    sudo chown $(id -u):$(id -g) ~/.openclaw/kmscli
    chmod +x ~/.openclaw/kmscli
  4. Verify the permissions

    Run the following command to confirm the file is in place with execute permissions:

    ls -l ~/.openclaw/kmscli
    # The expected output should include the "-rwxr-xr-x" permission flag, for example: -rwxr-xr-x 1 admin admin ... kmscli

Step 6: Deploy OpenClaw on ECS and update the configuration

  1. Deploy OpenClaw using the official documentation or OpenClaw deployment guide.

  2. After deployment, update the OpenClaw configuration file to replace the plaintext API key with a KMS credential reference.

    1. Go to the OpenClaw installation directory. Back up the original configuration file.

      cp ~/.openclaw/openclaw.json ~/.openclaw/openclaw.json.bak
    2. Edit the openclaw.json file. Add the secrets configuration. Replace the plaintext value of the apiKey field with a secret reference. A full example follows.

      • Add secrets configuration (at the root level):

        {
          "secrets": {
            "providers": {
              "kms_bailian": {
                "source": "exec",
                "command": "/home/<username>/.openclaw/kmscli",
                "args": ["openclaw", "getsecret"],
                "jsonOnly": true
              }
            }
          },
          "models": { ... }
        }
      • Modify apiKey configuration (under models.providers):

        {
          "models": {
            "providers": {
              "dashscope": {
                "apiKey": {
                  "source": "exec",
                  "provider": "kms_bailian",
                  "id": "<secret-name>"
                }
              }
            }
          }
        }
  3. Audit and reload the secrets configuration.

    # Audit the configuration
    openclaw secrets audit --check
    # Reload the configuration
    openclaw secrets reload

Verify the result

  • Configuration verification: You can verify the configuration using the following commands:

    CONFIG_FILE="${OPENCLAW_DIR:-$HOME/.openclaw}/openclaw.json"
    
    # Verify secrets configuration
    jq '.secrets.providers.kms_bailian' "$CONFIG_FILE"
    
    # Verify apiKey configuration
    jq '.models.providers.dashscope.apiKey' "$CONFIG_FILE"
    
    # Check the service process
    ps aux | grep openclaw-gateway
  • Functional verification: Start a conversation in OpenClaw. If the model responds correctly, the API key was retrieved from KMS.

  • Security verification: Confirm that the openclaw.json file contains no plaintext API key.

    Before integration

    The plaintext API key is stored in openclaw.json.

    {
        "models": {
          "mode": "merge",
          "providers": {
            "bailian": {
              "baseUrl": "https://dashscope.aliyuncs.com/compatible-mode/v1",
              "apiKey":"sk-33777********",
              "api": "openai-completions"
            }
          }
        }
      }

    After integration

    The API key is replaced with a secret reference. No plaintext key appears.

    {
      "secrets": {
        "providers": {
          "kms_bailian": {
            "source": "exec",
            "command": "/home/admin/.openclaw/kmscli",
            "args": ["openclaw", "getsecret"],
            "jsonOnly": true
          }
        }
      },
      "models": {
        "providers": {
          "dashscope": {
            "apiKey": {
              "source": "exec",
              "provider": "kms_bailian",
              "id": "<secret-name>"
            }
          }
        }
      }
    }
  • Audit verification: In the ActionTrail console, check the operation events. Confirm that the ECS instance called GetSecretValue using its RAM role.

Command-line tool

The kmscli tool provides the following commands:

# Normal mode (plaintext output, can be executed directly)
kmscli secret getsecret <secretName>
# OpenClaw mode (JSON output, for OpenClaw invocation, requires OpenClaw configuration)# This command requires OpenClaw to pass a JSON request via stdin and cannot be executed manually
kmscli openclaw getsecret

Usage notes

  • We recommend that the ECS instance and KMS are in the same region, using VPC internal network access.

  • By default, VPC network is used to access KMS. You can specify public network access via the environment variable: export ENDPOINT_TYPE=Public. This is required when the ECS instance and KMS are not in the same region.

  • Back up openclaw.json as .bak before modifying the configuration. To restore: cp ~/.openclaw/openclaw.json.bak ~/.openclaw/openclaw.json

Cost and risk notes

  • Cost: KMS is a paid service. Costs include the instance fee, credential storage, and API calls. For more information, see Product billing.

  • Risk: Follow least privilege when configuring the RAM role. Grant only the minimum permissions to access the specific credential. Overly broad permissions such as "Resource": "*" create security risks.

FAQ

  • Insufficient permissions

    • Problem: chmod: changing permissions of 'kmscli': Operation not permitted

    • Solution:

      sudo chown $(id -u):$(id -g) ~/.openclaw/kmscli
      sudo chmod +x ~/.openclaw/kmscli
  • Configuration not applied

    • Problem: Port not listening or process not started.

    • Solution:

      • Ensure the JSON configuration format is correct, and wait a few seconds before checking again.

      • View logs to troubleshoot errors:

        # View OpenClaw logs
        ls -la /tmp/openclaw/
        cat /tmp/openclaw/openclaw-*.log
  • ECS not bound to RAM role

    • Problem: One of the following errors appears in the logs:

      • NoCredentialProviders / ECS metadata access failed

      • get role name failed: GET http://100.100.100.200/latest/meta-data/ram/security-credentials/ 404

      • unable to get credentials from any of the providers in the chain

    • Solution: Refer to Step 3: Create a RAM role and attach a permission policy to complete the RAM role authorization binding. Wait 1-2 minutes after binding for the permissions to take effect.

  • JSON node order issue

    • Problem: OpenClaw fails to start after the configuration is updated.

    • Solution:

      1. Restore from the backup file: cp openclaw.json.bak openclaw.json.

      2. When reconfiguring, maintain the original node order.

      3. Edit individual fields within the provider node rather than replacing the entire node.