Help Center/ Data Encryption Workshop/ Best Practices/ Key Management Service/ Using an ECS Instance Agency to Securely Access KMS
Updated on 2026-09-20 GMT+08:00

Using an ECS Instance Agency to Securely Access KMS

KMS allows you to manage the lifecycle of keys and encrypt and decrypt data. You can use IAM to configure an agency for ECSs to obtain temporary AKs, protecting AKs/SKs. Temporary AKs feature shorter validity periods, frequent updates, and higher security. The following describes how to create an agency, configure policies, authorize permissions, assign permissions to ECSs, and access KMS through APIs. ECS can automatically rotate temporary secrets.

Scenario

Keys are stored and can be accessed temporarily to prevent AK/SK leakage.

How It Works

You can configure an agency for ECS on IAM to obtain the temporary AK, thereby protecting AKs/SKs.

Access credentials can be classified into permanent and temporary credentials based on their validity periods. Temporary access credentials have a shorter validity period and are updated frequently, so they are more secure than permanent access credentials (such as usernames and passwords). You can assign an IAM agency to an ECS instance, so that applications in the ECS instance can use the temporary AK, SK, and security token to access KMS. The temporary access keys are dynamically obtained every time they are required. They can also be cached in the memory and updated periodically.

Notes and Constraints.

Only the administrator or an IAM user with the ECS permission can configure an agency for an ECS instance.

Procedure

  1. Create an ECS agency on IAM.

    1. Log in to the management console.Log in to the management console.
    2. Click on the left and choose Management & Governance > Identity and Access Management.
    3. In the navigation pane on the left, choose Agencies.
    4. Click Create Agency in the upper right corner.
    5. Configure the parameters on the displayed dialog box. For details about the parameters, see Table 1.
      Table 1 Parameters for creating an agency

      Parameter

      Description

      Agency Name

      Enter a custom agency name. The following uses ECS_TO_KMS as an example.

      Agency Type

      Select Cloud service.

      Cloud Service

      Select ECS BMS.

      Validity Period

      Select a duration. The value can be Unlimited, 1 day, or Custom.

      Description

      (Optional) Enter agency description.

    6. Click OK. In the displayed dialog box, click Authorize.
    7. Click Create Policy in the upper right corner. If you already have a policy, skip this step.
      1. Configure policy parameters on the Create Policy page. For details about the parameters, see Table 2.
        Figure 1 Creating a policy
        Table 2 Parameters for creating a policy

        Parameter

        Description

        Policy Name

        Enter a policy name.

        Policy View

        Select Visual editor.

        Policy Content

        • Allow: Select Allow.
        • Select service: Select Key Management Service (KMS).
          NOTE:
          • If only the KMS service permission is added, the KMS API may fail to be called.
          • You need to add policies for services one by one. After the policies are configured for a service, click Add Permissions to add policies for other services.
        • Select action: Select read and write permissions as required.
        • (Optional) Select resource: Select the scope of resources.
          • Specific: Access specific secrets.
            NOTE:

            You can select Specify resource path, and then click Add Resource Path to specify an accessible secret.

          • All: Access all secrets.
        • (Optional) Add request condition: Click Add Request Condition, select a condition key and an operator, and enter values as required.

        Description

        (Optional) Enter policy description.

    8. Click Next and select a policy for the agency. Then, click Next again.
    9. Select an authorization scope. You are advised to select All resources.
      • All resources: IAM users will be able to use all resources, including those in enterprise projects, region-specific projects, and global services under your account based on assigned permissions.
      • Enterprise projects: The selected permissions will be applied to resources in the enterprise projects you select.
      • Region-specific projects: The selected permissions will be applied to resources in the region-specific projects you select.
      Figure 2 Selecting a scope
    10. Click OK. Confirm the information and click Finish.

  2. Assign an agency (for example, ECS_TO_KMS) to an ECS instance.

    • If no ECS has been created, create an agency, for example, ECS_TO_KMS for Agency under Advanced Settings. For details, see Purchasing a Custom ECS.
    • To use an existing ECS instance, perform the following steps:
      1. Click on the left and choose Computing > Elastic Cloud Server.
      2. Click the name of an ECS instance to go to the Summary page.
      3. In the Management Information area, click and select an agency (for example, ECS_TO_KMS).
        Figure 3 Selecting an agency

  3. In an application running on the ECS instance, call an API to obtain the temporary agency secrets, including the temporary AK, SK, and security token, to access KMS.

    1. Obtain the temporary AK and SK (in the Security Key directory). For details, see Obtaining Metadata.
      • URI

        http://169.254.169.254/openstack/latest/securitykey

      • Method

        GET requests are supported.

      • The following data is returned:
        {
              "credential":{
                  "access": "LDHZK30XXXXXXXXXXXXV",
                  "secret":"gyqcdzVXXXXXXXXXXXXXXXXXXXXXXXMl6",
                  "securitytoken": "El9FI2C65qXXXXXXXXXXXXXXXXXXXXXnkcaoV",
                  "expires_at": "2022-07-14T12:09:24.147000Z"
                          }
        }
        • Extract the values of access, secret, and securitytoken to access KMS. The values do not need to be stored locally.
        • ECS automatically rotates temporary secrets to ensure that they are secure and valid.
    2. Use the temporary AK/SK and security token to access KMS.
      package com.huaweicloud.sdk.test;
      
      import com.huaweicloud.sdk.core.auth.ICredential;
      import com.huaweicloud.sdk.core.auth.BasicCredentials;
      import com.huaweicloud.sdk.core.exception.ConnectionException;
      import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
      import com.huaweicloud.sdk.core.exception.ServiceResponseException;
      import com.huaweicloud.sdk.kms.v2.region.KmsRegion;
      import com.huaweicloud.sdk.kms.v2.*;
      import com.huaweicloud.sdk.kms.v2.model.*;
      
      public class ListSecretsSolution {
          public static void main(String[] args) {      
              String ak = "<access>";        
              String sk = "<secret>";
              String securitytoken = "<securitytoken>";
              ICredential auth = new BasicCredentials()
                      .withAk(ak)
                      .withSk(sk)
                      .withSecurityToken(securitytoken);
              final KmsClient kmsClient = KmsClient.newBuilder().withCredential(auth)
                  .withRegion(new Region("__need replace__", "__need replace__")).build();
      
              final ListKeysRequest listKeysRequest = new ListKeysRequest().withBody(new     ListKeysRequestBody());
              final ListKeysResponse listKeysResponse = kmsClient.listKeys(listKeysRequest);
              System.out.println(listKeysResponse);
               }
           }
      }