Runtime credential rotation for RDS in Spring applications

更新时间:
复制 MD 格式

This topic describes how to rotate credentials for an ApsaraDB for RDS data source at runtime in a Spring application. This solution uses Key Management Service (KMS) and Nacos to securely store, dynamically update, and seamlessly switch encrypted database credentials. This process ensures your application's security and stability during credential changes.

Solution overview

If your Java application uses the Spring Boot or Spring Cloud framework with databases like MySQL, SQL Server, PostgreSQL, or MariaDB, we recommend implementing runtime data source credential rotation to improve your security posture. The following figure shows the solution architecture.

image

This solution uses KMS to host and encrypt database credentials, Nacos for dynamic configuration management, and Druid to manage database connections. This solution provides the following features:

  • Centralized and encrypted data source configuration: Centrally manage database connection information and use encryption to secure configuration data during storage and transmission.

  • Fully managed credentials with dual-layer access control: Database credentials are fully managed by the system and use a dual-layer authentication mechanism to enhance access security.

  • Automated runtime credential rotation and leak remediation in seconds: Automatically rotate database credentials at runtime based on a defined policy and remediate leaks in seconds to minimize security risks.

  • Zero-downtime account switching: Dynamically switch database access accounts without interrupting your business. This process ensures high availability and maintenance flexibility.

  • Application-side tuning for database performance parameters: Flexibly adjust key performance parameters, such as connection pool size and timeout settings, to improve system stability and responsiveness.

  • Exception protection for invalid parameters: Validate and protect critical parameters to prevent misconfigurations from causing service interruptions, ensuring safe and controlled changes.

Prerequisites

  • Activate ApsaraDB for RDS and create an instance.

    Procedure

    After activating ApsaraDB for RDS, create an instance if you do not have one. For more information about the configurations, see Create an ApsaraDB RDS for MySQL instance.

    To create an instance, go to the instance creation page in the ApsaraDB for RDS console. Configure the following basic settings and leave the others at their default values:

    1. Billing Method: For short-term testing, we recommend that you select Pay-As-You-Go.

    2. Region: Select an Alibaba Cloud region. You cannot change the region after the instance is created.

    3. Edition: Select Basic Edition. This edition is cost-effective for learning or testing but has a longer recovery and restart time in case of a failure.

  • Activate Key Management Service (KMS), create a dedicated KMS instance, and create a key.

    Create a dedicated KMS instance

    After you activate Key Management Service (KMS), if you do not have an available instance, follow these steps to create one. For more information about the configurations, see Create a dedicated KMS instance.

    Go to the Instances page of the KMS console. In the top menu bar, select a region, and then click Create Instance. Select an Alibaba Cloud region and leave the other settings at their default values.

    Create a key

    If you do not have an available key, follow these steps to create one. For more information about the configurations, see Manage keys.

    Go to the Keys page of the KMS console. In the top menu bar, select a region, click the Default Keys tab, and find the master key that you want to use. In the Actions column, click Enable. Configure the following basic settings and leave the other settings at their default values:

    1. Alias: An alias for the key.

    2. Advanced: The source of the key material.

      • Alibaba Cloud KMS: The key material is generated by KMS.

      • External: KMS does not generate key material. You must import your own key material. For more information, see Import symmetric key material.

      Carefully read and select I understand the methods and implications of using external key material.
  • Activate Microservices Engine (MSE) and create an Enterprise Edition instance.

    Create an Enterprise Edition instance

    After you activate Microservices Engine (MSE), if you do not have an available instance, follow these steps to create one. For more information about the configurations, see Create an instance.

    Go to the Instances page of the MSE console. In the top menu bar, select a region, and then click Create Instance. Configure the following basic settings and leave the other settings at their default values:

    1. Billing Method: For short-term testing, we recommend that you select Pay-As-You-Go.

    2. Region: Select an Alibaba Cloud region. You cannot change the region after the instance is created.

    3. Edition: Select Enterprise Edition.

      This feature is supported only by the Enterprise Edition of MSE Nacos. The Professional Edition and Developer Edition do not support this feature.
    4. Instance Name: Set a name for the instance. The name must be globally unique within an Alibaba Cloud region and cannot be changed after creation.

Create a KMS secret

  1. Go to the Secrets page of the KMS console and select Database Credential.

  2. Select an Instance ID and click Create Secret > Create a Single Secret.

  3. In the Create Database Credential panel, select a Database Type for the RDS Credential. Enter a Secret Name and select the associated RDS Instance.

  4. Select Dual-account hosting, enter a Custom Account name, Select Databases, and Specify Permissions.

    In the Set the secret value section, select the Create Account tab.

  5. Select an Encryption Key, set the Rotation Period, and then click OK.

KMS automatically creates a database access account and a strong password for the secret.

Create a data source

Note

The data source feature is built on the Nacos configuration center, where data source configurations are stored in an encrypted format.

  1. Go to the Instances page of the MSE Nacos console and select a region in the top menu bar.

  2. Click the name of the target Enterprise Edition instance. In the left-side navigation pane, choose Data Source Management.

    If the instance is not bound to a KMS key, follow the prompts in the pop-up window to bind a key.
  3. On the Data Source Management page, select the target namespace, and then click Create Data Source.

  4. In the Create Data Source panel, configure the parameters for the data source. For more information about the parameters, see Create a data source.

    The Create Data Source panel includes the following settings: Data Source Name, Description, Data Source Type (for example, spring-datasource or spring-redis), Subtype (for example, druid), Database Type (for example, rds), Database Instance Endpoint, Database Engine Type (for example, MySQL), Database Port (for example, 3306), Database Name, Database Address Parameters, Initial Connections (for example, 20), Minimum Connections (for example, 20), Maximum Connections (for example, 100), Maximum Wait Time (for example, 5000), and Associated KMS Secret.

  1. Click OK. In the Confirm Create Data Source dialog box, click OK again to submit the data source.

Credential rotation

Credential rotation for the data source is managed by KMS. Two rotation methods are available:

  • Scheduled rotation: When you create a secret, you can set a rotation period for automatic rotation.

  • On-demand rotation: If a credential is leaked or needs to be rotated immediately for other reasons, you can trigger an immediate rotation from the KMS console.

When a KMS secret is rotated, either on a schedule or on demand, KMS notifies MSE Nacos to update the encrypted data source configuration. Nacos then pushes the update to the application in real time, which updates the username and password. Druid gracefully replaces online connections to prevent business disruption.

Note

Credential rotation is fully managed by KMS. If you change the password of a managed account directly in the ApsaraDB for RDS console, by calling an OpenAPI operation, or by using a high-privilege database account, the change is not pushed to your application in real time.

Application access

On the data source details page, click Application Access to view the integration steps for each data source.

Note

For parameter configurations, use the actual settings shown in the Application Access section of the console. The parameters for each step in the console are dynamically adapted and linked to the target instance and data source.

Step 1: Upgrade versions

  • Upgrade Spring Cloud to version 2023.0.3.3 or later.

  • Upgrade Druid to version 1.2.27 or later.

<!--  Druid connection pool, including Spring Starter and the Druid core version -->
<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>druid-spring-boot-3-starter</artifactId>
	<version>1.2.27</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.2.27</version>
</dependency>
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
    <version>2023.0.3.3</version>
</dependency>
<!--  Commercial KMS encryption and decryption plugin -->
<dependency>
	<groupId>com.alibaba.nacos</groupId>
	<artifactId>nacos-client-mse-extension</artifactId>
	<version>1.0.5</version>
</dependency> 

Step 2: Modify the configuration

  • Modify application.properties to enable dynamic data source rotation and specify the Nacos configuration for the corresponding data source.

# Import the data source configuration into Spring to initialize the data source.
spring.config.import[0]=optional:nacos:cipher-kms-aes-256-proxy-spring-{resourceName}.properties?group=nacos-datasource
# Specify the Nacos address. The public endpoint is used by default. Change it as needed.
spring.cloud.nacos.config.server-addr={nacos-server-endpoint}
# Specify the namespace ID.
spring.cloud.nacos.config.namespace={namespaceID}
# Specify the KMS region ID.
spring.cloud.nacos.config.kms_region_id={instanceRegion}
# Enable Druid data source management and specify the dataId for the rotation configuration.
spring.nacos.config.proxy.druid.enabled=true
spring.nacos.config.proxy.druid.data-id={dataID}

Step 3: Set the access credential

Because the encrypted data source configuration must be decrypted in memory at runtime, you must configure an access credential with the necessary decryption permissions.

Access credential without an AccessKey (Recommended)

If your application runs on Alibaba Cloud ECS or ACK, we recommend using a credential without an AccessKey. Set the following JVM parameter:

  • Using an ECS RAM role:

    -Dspring.cloud.nacos.config.ramRoleName=${ramRoleName}
  • Using an OIDC role ARN:

    -Dspring.cloud.nacos.config.alibabaCloudRoleSessionName=${sessionName}

Traditional AccessKey/SecretKey

Set the following JVM parameters:

-Dspring.cloud.nacos.config.accessKey=${accessKey}
-Dspring.cloud.nacos.config.secretKey=${secretKey}

Replace ${accessKey} and ${secretKey} with your actual AccessKey and SecretKey.

Regardless of the access credential type you choose, you must grant the necessary permissions to the corresponding account or role. The credential must have KMS decryption permissions so that the application can decrypt the configuration into plaintext in memory.

Go to the instance details page, and navigate to Security > Authentication and Authorization > Client Authentication. For the authorization scope, select Permissions to decrypt data that is encrypted by using KMS and follow the instructions to complete the authorization. For more information about credential types, see Configure client access credentials.

FAQ

1. When are old credentials invalidated?

When KMS rotates a secret, it notifies MSE to update the credential. KMS keeps two versions of the credential active. The credential from two versions ago is invalidated. In the future, this feature will integrate with the session management of specific data sources. An old credential will be invalidated only when no active connections are using it, further improving stability.

2. How to detect exception protection triggers?

When the exception protection mechanism is triggered in the application's connection pool, it reports a push failure to Nacos. You can detect this event by searching the config.log file for the notify-error keyword. Future versions of Nacos will report the client listener's callback status. This will allow you to directly view failed client callbacks in the listener query feature.