PolarDB secrets

Updated at:

To reduce the risk of password exposure when an application accesses PolarDB, store the account passwords as PolarDB secrets in Key Management Service (KMS). Applications can then dynamically retrieve the passwords from KMS. This topic describes how to manage and use PolarDB secrets.

Background

Poorly protected PolarDB account passwords can lead to risks such as application data leaks, theft of critical user data, and database attacks. To mitigate these risks, store PolarDB passwords as secrets in KMS. Applications can then dynamically retrieve these passwords by using an Alibaba Cloud SDK, KMS instance SDK, or the Secret Client. Automatic rotation is also supported to periodically change the passwords, further reducing the risk of PolarDB account password exposure.

Prerequisites

Supported types

  • Supported managed database engines:

    • PolarDB for MySQL

    • PolarDB for PostgreSQL

    • PolarDB for PostgreSQL (Compatible with Oracle)

  • Supported management mode: Creating and managing new accounts in a dual-account mode.

  • Supported account type: Standard account

Important
  • PolarDB-X 2.0 and PolarDB-X 1.0 are not supported.

  • Importing existing accounts and single-account mode are not supported.

How it works

Using PolarDB secrets eliminates the need to hardcode static database passwords in applications.

  1. An administrator creates a PolarDB secret in KMS based on business requirements.

  2. The application integrates with KMS to retrieve the secret.

  3. The application uses the retrieved account password to connect to the database.

Important

After a PolarDB account password is stored in KMS, do not modify or delete the password in the PolarDB console to avoid business failures.

image

Step 1: Create a PolarDB secret

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

  2. Click the Database Secrets tab. Select an Instance ID, click Create Secret > Create Single Secret, configure the parameters, and then click OK. The following table describes the key parameters.

    Parameter

    Description

    Database Type

    Select PolarDB Secret.

    Secret Name

    A custom credential name. Must be unique within the current region.

    PolarDB Cluster

    Select a PolarDB instance under your Alibaba Cloud account.

    Account Management

    Manage Dual Accounts: Suitable for programmatic database access. KMS creates two standard accounts with identical permissions to avoid connection interruption during rotation.

    Create Account: Configure the account name and permissions. Only Standard Account is supported. For MySQL, you must also select a database and specify permissions.

    Note
    • One-click creation and authorization does not immediately create the new account. The account is created after you review and confirm the credential information.

    • The account name must be unique. If the account name already exists, it cannot be hosted as a credential.

    CMK

    Select the symmetric key used to encrypt the credential value. The key and the credential must belong to the same KMS instance.

    Important
    • The key and the credential must belong to the same KMS instance, and the key must be a symmetric key. For more information about the symmetric keys supported by KMS, see Key types and specifications.

    • If a RAM user or RAM role is used, it must have permissions to perform the GenerateDataKey operation using the encryption master key.

    Tags

    Add tags to the credential for classification. Each tag consists of a key-value pair.

    Note
    • Format of a tag key and a tag value: A tag key or a tag value can be up to 128 characters in length and can contain letters, digits, forward slashes (/), backslashes (\), underscores (_), hyphens (-), periods (.), plus signs (+), equal signs (=), colons (:), at signs (@), and spaces.

    • A tag key cannot start with aliyun or acs:.

    • Each credential supports up to 20 tag key-value pairs.

    Automatic Rotation

    Select whether to enable automatic rotation. When enabled, KMS periodically updates the credential value to reduce the risk of credential leaks.

    Rotation Period

    Set this parameter only when automatic rotation is enabled. Valid values: 6 hours to 365 days.

    Specifies the rotation interval. KMS periodically updates the credential value based on this setting.

    Description

    An optional description of the credential for identification and management.

    Advanced Settings > Policy Settings

    Set the access policy for the credential to control which RAM users or roles can read or modify it.

    You can select the default policy first and modify it later as needed.

Note
  • Before creating a PolarDB secret, ensure the PolarDB cluster status is Normal.

  • After creating a PolarDB secret, KMS automatically creates a service-linked role named AliyunServiceRoleForKMSSecretsManagerForPolardb and grants it the AliyunServiceRolePolicyForKMSSecretsManagerForPolardb permission policy. You can log on to the RAM console to view the details of service-linked roles and permission policies. For more information, see View a RAM role and View permission policy details.

  • KMS then assumes this role to call PolarDB API operations and manage PolarDB secrets, including rotating the account password.

Step 2: Integrate the secret

KMS provides the Secret Client, Alibaba Cloud SDK, KMS Agent, and KMS instance SDK to get the value of a PolarDB secret by calling the OpenAPI GetSecretValue operation or the KMS instance API GetSecretValue operation. Select an integration method based on requirements.

Note
  • We recommend that you implement an error retry mechanism to improve service stability.

  • KMS supports multiple authentication methods. For higher security, we recommend that you use an ECS instance RAM role or a RAM role.

  • Endpoints:

    For a shared gateway, see Endpoints.

    For a dedicated gateway, use the following format: {Instance-ID}.cryptoservice.kms.aliyuncs.com.

Integration method

Scenario

Supported gateways

Secret Client

The application is developed in Java (Java 8 or later), Go, or Python.

  • shared gateway

  • dedicated gateway

Alibaba Cloud SDK

The application is developed in Java (Java 6 or later), PHP, Go, Python, .NET (C# only), C++, or Node.js.

  • dedicated gateway (recommended)

  • shared gateway

KMS Agent

  • Suitable for large-scale applications that are deployed together and need to access KMS.

  • KMS Agent provides services based on a standard HTTP API and supports applications in any programming language.

  • dedicated gateway (recommended)

  • shared gateway

KMS instance SDK (not recommended)

The application is developed in Java (Java 8 or later), PHP, Go, Python, or .NET (C# only).

dedicated gateway

Step 3: Connect to the database

Use the database account password retrieved from the PolarDB secret to connect to the database using a database driver.

Important
  • For information about how to use database drivers, see the driver's official documentation.

  • If the application is developed in Java and connects to a PolarDB for MySQL database, we recommend using the Secret JDBC client. This client automatically retrieves secrets from KMS for database connections without requiring a call to the GetSecretValue operation. The Secret JDBC client does not support PolarDB for PostgreSQL or PolarDB for PostgreSQL (Compatible with Oracle).

More operations

Rotate PolarDB secret

Configuring periodic automatic credential rotation effectively reduces the risk of leakage caused by long-term lack of updates. Additionally, the console supports immediate manual rotation, enabling rapid response to security incidents and timely mitigation of potential threats.

KMS supports only dual-account management when managing a PolarDB secret. When creating a secret, KMS calls the PolarDB API to create two database accounts with identical permissions. During rotation, KMS alternates which account's password it modifies, ensuring one account is always active. The following figure illustrates this process.

image
Important
  • Do not delete the PolarDB cluster or accounts associated with the secret during rotation.

  • When rotating a PolarDB secret, KMS requests PolarDB to reset the password of the corresponding database account (ModifyAccountPassword). Before rotating a PolarDB secret, ensure that all applications retrieve the secret from KMS to prevent service unavailability.

  • If the corresponding database cluster or account in PolarDB is deleted, KMS cannot rotate the PolarDB secret. A secret detection is recommended before rotation; proceed only after the check is successful.

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

  2. Click the Database Secrets tab, select an Instance ID, find the target secret to rotate, and then click Details in the Actions column.

  3. In the Versions section of the secret details page, click Configure Rotation.

    • Periodic rotation: The rotation period can be set from 6 hours to 365 days.

    • Rotate Now: After clicking Rotate Now, the background task starts and the rotation completes in approximately 3 minutes.

    • Rotation results are available in Cloud Monitor.

Secret detection

The secret detection feature checks if the database accounts managed by a secret are active and accessible (not deleted or disabled). If the check is successful, the secret can be rotated. Otherwise, the PolarDB secret must be deleted and recreated.

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

  2. Click the Database Secrets tab, select an Instance ID, find the target secret, and then click Details in the Actions column.

  3. In the Versions section, click Check Account. After the verification is complete, view the result.

Delete PolarDB secret

Warning

Before deleting a secret, make sure it is no longer in use. Otherwise, business may fail.

A secret can be scheduled for deletion or deleted immediately. Deleting a PolarDB secret only removes the secret information from KMS. It does not delete the accounts or passwords in PolarDB.

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

  2. Click the Database Secrets tab, select an Instance ID, find the target secret to delete, and then click Schedule Deletion in the Actions column.

  3. In the Schedule Deletion dialog box, select a deletion method for the secret and click OK.

    • Schedule Deletion: Specify a recovery period of 7 to 30 days. The system deletes the secret after the recovery period ends.

    • Delete Immediately: The system immediately deletes the secret.

    During the recovery period, you can find the secret and click Restore Secret in the Actions column to cancel the deletion.

Configure credential tags

You can assign tags to credentials to organize and manage them. Each tag consists of a key-value pair (Key : Value), including a tag key (Key) and a tag value (Value).

Tag usage notes

  • A tag key or tag value can contain up to 128 characters, including letters, digits, forward slashes (/), backslashes (\), underscores (_), hyphens (-), periods (.), plus signs (+), equal signs (=), colons (:), at signs (@), and spaces.

  • A tag key cannot start with aliyun or acs:. This restriction does not apply to tag values.

  • Each credential can have up to 20 tags.

Procedures

Tag a single credential
  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 > Secrets.

  2. Click the tab for the corresponding credential type, select a Instance ID, and locate the target credential.

  3. Open the tag editor using one of the following methods:

    • Method 1: In the credential list, click the image.png icon in the Tags column for the target credential.

    • Method 2: Click Actions > Details to go to the credential details page, then click the image.png icon next to Tag.

  4. In the Edit Tag dialog box, enter the Tag Key and Tag Value, and click OK.

Tag multiple credentials in bulk
  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 > Secrets.

  2. Click the tab for the corresponding credential type, select a Instance ID, and select the credentials to operate on from the credential list.

    • Add tags: At the bottom of the credential list, click Add Tag, enter the Tag Key and Tag Value, and click OK.

    • Delete tags: At the bottom of the credential list, click Remove Tag, select the tags to unbind in the Batch Remove dialog box, click Cancel, and then click Close in the confirmation dialog box.