The secret client is a custom encapsulation based on Key Management Service (KMS) API and KMS instance API that incorporates capabilities to cache and refresh secrets in applications. This improves service stability and simplifies the integration process for developers. The secret client supports retrieving secret values for all types of secrets. This topic describes how to install and use the secret client.
Features
The secret client provides the following features:
Allows developers to integrate secret client capabilities into applications. You can retrieve secret information with a single line of code.
Encapsulates capabilities to cache and refresh secrets in applications.
Encapsulates an API error-based retry mechanism to intelligently handle reported errors.
Provides a plugin design mode that allows you to customize features such as extended cache and error retry.
Supported secret types and programming languages
Supported secret types:
Generic secret.
Resource Access Management (RAM) secret.
ECS secret.
Database secret.
Supported programming languages:
Java 8 and later.
Go.
Python.
Retrieve secret values through a shared gateway
The network type is the Internet or VPC network.
Step 1: Create an access credential
The following RAM identity authentication methods are supported: Elastic Compute Service (ECS) instance RAM role, RamRoleArn, Security Token Service (STS) token, AccessKey pair, and ClientKey (not recommended).
ECS instance RAM role
An ECS instance RAM role is a regular service role that is attached to ECS instances, and the principal is ECS. This allows the ECS instance to obtain STS tokens for temporary access, eliminating the need for AccessKey pairs when calling KMS OpenAPI operations.
Log on to the RAM console, and create an instance RAM role whose Principal Type is an Alibaba Cloud service.
Principal Type: Select Cloud Service.
Principal Name: Select Elastic Compute Service / ECS.
Grant the RAM role access to retrieve KMS secrets.
Method 1: Through identity-based policies
KMS provides system-defined permission policies that can be attached to RAM roles. For more information, see System policies for KMS. You can also create custom policies.
Method 2: Through resource-based policies
KMS supports resource-based policies, which allow you to set access permissions for keys and secrets. This lets you control which Alibaba Cloud accounts, RAM users, and RAM roles have permission to manage or use KMS keys and secrets. For more information, see Key policies and Secret policies.
Log on to the ECS console, and attach the instance RAM role to an ECS instance.
RamRoleArn
RAM users or cloud services can obtain temporary permissions by assuming roles instead of directly using long-term access keys, thereby reducing the risk of key leakage. For instance, in temporary data processing tasks, RAM users or cloud services can temporarily assume a role with a specific RamRoleArn. RamRoleArn is the ARN information of the RAM role. Once the task is completed, the role's permissions are revoked, further mitigating the risk of exposure.
Log on to the RAM console by using an Alibaba Cloud account or a RAM user who has administrative rights.
Grant the RAM role access to KMS. You have two methods to complete the grant.
Method 1: Through identity-based policies
KMS provides system-defined permission policies that can be attached to RAM roles. For more information, see System policies for KMS. You can also create custom policies.
Method 2: Through resource-based policies
KMS supports resource-based policies, which allow you to set access permissions for keys and secrets. This lets you control which Alibaba Cloud accounts, RAM users, and RAM roles have permission to manage or use KMS keys and secrets. For more information, see Key policies and Secret policies.
View the RamRoleArn about a RAM role.
The RamRoleArn follows the format
acs:ram::$accountID:role/$roleName
, where$accountID
is the Alibaba Cloud account and$roleName
is the RAM role name.
STS token
By using STS services, a temporary access credential can be issued to RAM users or RAM roles, allowing them to access KMS with permissions specified by the policy for a limited validity period. After the expiration period, the credential will automatically become invalid.
Log on to the RAM console by using an Alibaba Cloud account or a RAM user who has administrative rights.
Grant AliyunSTSAssumeRoleAccess permission to a RAM user or Grant AliyunSTSAssumeRoleAccess permission to a RAM role.
Grant the RAM user access to KMS. You have two methods to complete the grant.
Method 1: Through identity-based policies
KMS provides system-defined permission policies that can be attached to RAM users. For more information, see System policies for KMS. You can also create custom policies.
Method 2: Through resource-based policies
KMS supports resource-based policies, which allow you to set access permissions for keys and secrets. This lets you control which Alibaba Cloud accounts, RAM users, and RAM roles have permission to manage or use KMS keys and secrets. For more information, see Key policies and Secret policies.
Use the RAM user or RAM role to call the AssumeRole interface of the STS service to obtain temporary access credentials.
AccessKey
Use a RAM user's AccessKey pair as an example. Alibaba Cloud accounts have default administrator privileges for all resources, which cannot be modified. Because compromised AccessKeys risk significant security vulnerabilities, we strongly recommend against creating them for Alibaba Cloud accounts. Instead, create a RAM user solely for API access, generate its AccessKey, and implement the principle of least privilege.
Log on to the RAM console. In the left-side navigation pane, choose Identities > Users, and click on the desired RAM user.
In the Authentication tab, click Create AccessKey and follow the instructions.
Grant the RAM user access to retrieve KMS secrets.
Method 1: Through identity-based policies
KMS provides system-defined permission policies that can be attached to RAM users. For more information, see System policies for KMS. You can also create custom policies.
Method 2: Through resource-based policies
KMS supports resource-based policies, which allow you to set access permissions for keys and secrets. This lets you control which Alibaba Cloud accounts, RAM users, and RAM roles have permission to manage or use KMS keys and secrets. For more information, see Key policies and Secret policies.
ClientKey (not recommended)
For instructions, see the standard creation method in Create an application access point. When configurating parameters, ensure the following settings are correct:
When create network rules, select Public or VPC for Network Type.
When configure the scope of permission rules, select Shared KMS Gateway.
Step 2: Install the secret client
Java
Install the secret client in your project by using Maven. We recommend that you use the latest version. Visit alibabacloud-secretsmanager-client-java to learn more about versions and source code.
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>alibabacloud-secretsmanager-client</artifactId>
<version>1.4.x</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.5.x</version>
</dependency>
Python
Run the pip command to install the secret client. Visit aliyun-secretsmanager-client-python to learn more about installation and source code.
pip install aliyun-secret-manager-client
Go
Use the go get
command to install the secret client. Visit aliyun-secretsmanager-client-go to learn more about installation and source code.
go get -u github.com/aliyun/aliyun-secretsmanager-client-go
Step 3: Initialize the client and retrieve secret values
The sample code for retrieving secret values varies based on the client initialization method. Choose one method for each programming language.
Java
Method 1: Using environment variables or the secretsmanager.properties configuration file
Method 2: Using a custom configuration file
Specify the configuration file name and path. Note that its contents vary depending on your authentication method.
Method 3: Specifying parameters (accessKey, accessSecret, and regionId)
Applies only to the AccessKey authentication method.
NoteThe AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using the AccessKey pair to perform operations is a high-risk operation. In addition, use a RAM user to call API operations or perform routine O&M. We recommend that you do not save the AccessKey ID and AccessKey Secret in your project code. If you do, the AccessKey pair may be leaked and the security of all the resources that belong to your account may be compromised.
In this example, the AccessKey pair is saved in ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables to implement identity authentication.
For more information about how to configure authentication information, see Manage access credentials.
The method used to configure environment variables varies based on the operating system. For more information, see Configure environment variables in Linux, macOS, and Windows.
Python
Method 1: Using environment variables or the secretsmanager.properties configuration file
Environment variables
Method 2: Specifying parameters (accessKey, accessSecret, and regionId)
Applies only to the AccessKey authentication method.
NoteThe AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using the AccessKey pair to perform operations is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. We recommend that you do not save the AccessKey ID and AccessKey Secret in your project code. Otherwise, the AccessKey pair may be leaked and the security of all the resources that belong to your account may be compromised.
In this example, the AccessKey pair is saved in ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables to implement identity authentication.
For more information about how to configure authentication information, see Manage access credentials.
The method that is used to configure environment variables varies based on the operating system. For more information, see Configure environment variables in Linux, macOS, and Windows.
Go
Method 1: Using environment variables or the secretsmanager.properties configuration file
Environment variables
Method 2: Specifying parameters (accessKey, accessSecret, and regionId)
Applies only to the AccessKey authentication method.
NoteThe AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using the AccessKey pair to perform operations is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. We recommend that you do not save the AccessKey ID and AccessKey Secret in your project code. Otherwise, the AccessKey pair may be leaked and the security of all the resources that belong to your account may be compromised.
In this example, the AccessKey pair is saved in ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables to implement identity authentication.
For more information about how to configure authentication information, see Manage access credentials.
The method that is used to configure environment variables varies based on the operating system. For more information, see Configure environment variables in Linux, macOS, and Windows.
Retrieve secret values through a dedicated gateway (not recommended)
KMS uses a private network.
Step 1: Create an access credential
Access requires a client key, which can be created using one of the following methods:
Method 1: Quick creation
Suitable for quick testing and development scenarios. This method uses a default permission policy that cannot be modified. Your application will have access to all keys and secrets in the specified KMS instance.
Method 2: Standard creation
Use this method to configure fine-grained access permissions for resources.
Method 1: Quick creation
Log on to the KMS console. In the top navigation bar, select a region. In the left-side navigation pane, click .
On the Application Access tab, click Create AAP. In the Create AAP panel, configure the parameters.
Parameter
Description
Mode
Select Quick Creation.
Scope (KMS Instance)
Select the KMS instance that you want to access.
Application Access Point Name
Enter the name of the AAP.
Authentication Method
The default value is ClientKey, which cannot be changed.
Default Permission Policy
The default value is
key/*
secret/*
, which cannot be changed. Your application can access all keys and secrets in the specified KMS instance.Click OK. The browser automatically downloads the client key that is created.
The client key contains Application Access Secret(ClientKeyContent) and Password. By default, Application Access Secret(ClientKeyContent) is saved in a file whose name is in the
clientKey_****.json
format. By default, Password is saved in a file whose name is in theclientKey_****_Password.txt
format.
Method 2: Standard creation
For detailed instructions, see Method 2: Standard creation. When configurating parameters, ensure the following settings are correct:
When create a network access rule, select Private for Network Type.
When configure the scope of permission policy, select the specified KMS Instance ID.
Step 2: Install the secret client
Java
Install the secret client in your project by using Maven. We recommend that you use the latest version. Visit alibabacloud-secretsmanager-client-java to learn more about versions and source code.
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>alibabacloud-secretsmanager-client</artifactId>
<version>1.4.x</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.5.x</version>
</dependency>
Python
Run the pip command to install the secret client. Visit aliyun-secretsmanager-client-python to learn more about installation and source code.
pip install aliyun-secret-manager-client
Go
Use the go get
command to install the secret client. Visit aliyun-secretsmanager-client-go to learn more about installation and source code.
go get -u github.com/aliyun/aliyun-secretsmanager-client-go
Step 3: Initialize the secret client and retrieve secret values
The sample code for retrieving secret values varies based on the client initialization method. Choose one method for each programming language.
Java
Method 1: Using environment variables or the secretsmanager.properties configuration file
Method 2: Using a custom configuration file
Specify the custom configuration file name and path.
Method 3: Specifying parameters (accessKey, accessSecret, and regionId)
Applies only to the AccessKey authentication method.
NoteThe AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using the AccessKey pair to perform operations is a high-risk operation. In addition, use a RAM user to call API operations or perform routine O&M. We recommend that you do not save the AccessKey ID and AccessKey Secret in your project code. If you do, the AccessKey pair may be leaked and the security of all the resources that belong to your account may be compromised.
In this example, the AccessKey pair is saved in ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables to implement identity authentication.
For more information about how to configure authentication information, see Manage access credentials.
The method used to configure environment variables varies based on the operating system. For more information, see Configure environment variables in Linux, macOS, and Windows.
Python
Method 1: Using environment variables or the secretsmanager.properties configuration file
You can use either environment variables or the secretsmanager.properties configuration file.
Method 2: Specifying parameters (accessKey, accessSecret, and regionId)
Applies only to the AccessKey authentication method.
NoteThe AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using the AccessKey pair to perform operations is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. We recommend that you do not save the AccessKey ID and AccessKey Secret in your project code. Otherwise, the AccessKey pair may be leaked and the security of all the resources that belong to your account may be compromised.
In this example, the AccessKey pair is saved in ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables to implement identity authentication.
For more information about how to configure authentication information, see Manage access credentials.
The method that is used to configure environment variables varies based on the operating system. For more information, see Configure environment variables in Linux, macOS, and Windows.
Go
The sample code for retrieving secret values varies based on the client initialization method. Choose one of the following methods.
Method 1: Using environment variables or the secretsmanager.properties configuration file
You can use either environment variables or the secretsmanager.properties configuration file.
Method 2: Specifying parameters (accessKey, accessSecret, and regionId)
Applies only to the AccessKey authentication method.
NoteThe AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using the AccessKey pair to perform operations is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. We recommend that you do not save the AccessKey ID and AccessKey Secret in your project code. Otherwise, the AccessKey pair may be leaked and the security of all the resources that belong to your account may be compromised.
In this example, the AccessKey pair is saved in ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables to implement identity authentication.
For more information about how to configure authentication information, see Manage access credentials.
The method that is used to configure environment variables varies based on the operating system. For more information, see Configure environment variables in Linux, macOS, and Windows.