Integrate OpenClaw with KMS Credential Manager
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.
-
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.
-
-
Runtime phase:
-
At startup, OpenClaw reads
openclaw.jsonand runs the external command specified in thesecretsdefinition. -
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
Log on to the Key Management Service console. In the top navigation bar, select a region. In the left-side navigation pane, choose .
-
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.
-
-
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.
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.
-
Create a key
-
In the top menu bar, select a region. In the navigation pane on the left, click .
-
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.
-
NoteRecord the key ARN from the key details page. You need it in Step 3.
-
-
Create a credential
-
In the navigation pane on the left, click .
-
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.
-
-
Click OK.
NoteRecord 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.
-
Create a RAM permission policy.
-
Log on to the Resource Access Management (RAM) console with your Alibaba Cloud account (root account) or a RAM user with
AliyunRAMFullAccesspermission. -
In the navigation pane on the left, choose Permission Management > Permission Policies.
-
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" } ] }NoteIn the preceding code,
<account-id>is the Alibaba Cloud account ID, which can be obtained in the account settings of the console.ImportantObtain the credential ARN and key ARN from their respective details pages created in Step 2.
-
-
Create a RAM role and attach the permission policy.
-
In the navigation pane on the left, choose Identity Management > Roles.
-
Click Create Role. For Trusted Entity Type, select Alibaba Cloud Service. For Trusted Entity, select Elastic Compute Service (ECS).
-
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
-
Purchase an ECS instance. Quickly purchase a subscription ECS instance.
-
Attach the RAM role you created in Step 3 to your ECS instance.
Go to ECS console - Instances. In the top-left corner, select the region and resource group for the target resource.
-
Click the target ECS instance ID. On the instance details page, click All Operations. Then choose .
-
In the dialog box, set Operation type to Grant. Select the RAM role you created in Step 3. Click OK.
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.
-
Download the binary file
Download the
kmsclibinary 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-amd64orkmscli-darwin-arm64. -
For a complete list of versions, visit GitHub Releases.
-
-
Upload the file to the target directory
Upload the binary to the
~/.openclaw/directory on your ECS instance and rename it tokmscli.-
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
-
-
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 -
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
-
Deploy OpenClaw using the official documentation or OpenClaw deployment guide.
-
After deployment, update the OpenClaw configuration file to replace the plaintext API key with a KMS credential reference.
-
Go to the OpenClaw installation directory. Back up the original configuration file.
cp ~/.openclaw/openclaw.json ~/.openclaw/openclaw.json.bak -
Edit the
openclaw.jsonfile. Add thesecretsconfiguration. Replace the plaintext value of theapiKeyfield 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>" } } } } }
-
-
-
Audit and reload the
secretsconfiguration.# 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.jsonfile 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
GetSecretValueusing 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.jsonas.bakbefore 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:
-
Restore from the backup file:
cp openclaw.json.bak openclaw.json. -
When reconfiguring, maintain the original node order.
-
Edit individual fields within the provider node rather than replacing the entire node.
-
-