Data source management
Centrally manage data source credentials with KMS-based encryption and automatic rotation, enabling runtime credential updates without code changes.
This feature is available only in the Enterprise Edition of MSE Nacos. It is not supported in the Professional Edition, Developer Edition, or Serverless Edition.
Background and challenges
Core capabilities
Data source management addresses these challenges:
-
Encrypted storage: Data source credentials are encrypted with KMS, eliminating the risk of plaintext exposure.
-
Automatic rotation: Integrates with KMS for scheduled credential rotation, enabling runtime updates without restarting applications.
-
Configuration-based integration: Applications read data source information from the Nacos configuration center, enabling integration with zero code changes.
Prerequisites
1. Prepare a database instance
-
Create an ApsaraDB RDS or PolarDB database instance: You can use an existing instance or create a new one.
2. Configure Key Management Service (KMS)
-
Activate Key Management Service (KMS): Purchase and enable a KMS instance. Ensure the KMS instance image version is dkms-3.0.0 or later. To upgrade the image, see Upgrade the image version of a KMS instance.
-
Create a KMS key: Create a key and obtain its
KeyId. This key is used to encrypt database credentials. -
Create a KMS credential: Associate the credential with the target database instance and configure a rotation account.
3. Bind KMS key
-
Bind the KMS key in the MSE console to ensure data source credentials are stored encrypted.
Create a data source
The data source management feature is built on the Nacos configuration center. Data source configurations are stored in an encrypted format within the configuration center.
-
On the instance list page of the configuration center, click the name of the target instance, and then choose Data source management.
If you have not bound a KMS key, a dialog box appears. Follow the prompts to bind a key.
-
Select the target Namespaces and click Create Data Source. Configure the following parameters:
-
Data source name: Enter a name for the data source.
-
Description: Enter a description for the data source, such as
Internal data source for Application A to access Database X. -
Data source type:
-
spring-datasource: For Java Spring applications that access a relational database (such as MySQL, ApsaraDB RDS, or PolarDB).
-
spring-redis: For Java Spring applications that access an in-memory database (such as Redis or Tair). (Application-side integration for this type is in development and will be released soon.)
-
-
Subtype: Currently, only the open source Apache Druid connection pool is supported.
-
Database type: Select the database type, and then select the database instance.
-
Database instance endpoint: For example,
rm-02345**********imo.mysql.rds.aliyuncs.com. -
Database engine type: Currently supports MySQL, SQL Server, PostgreSQL, and MariaDB. This affects the format of the JDBC connection string used by the application.
-
Database port: The port number of the database, such as
3306. -
Database name: The name of the database.
-
Database URL parameters: Additional JDBC parameters beyond the endpoint, port, and database name. For example:
connectTimeout=20000&socketTimeout=60000&serverTimeZone=Asia/Shanghai. -
Initial connections: The initial number of connections in the application-side data source connection pool.
-
Minimum connections: The minimum number of connections to maintain when the business is idle.
-
Maximum connections: The maximum number of connections to create during high traffic.
-
Max wait time: The maximum time a data request can wait for a connection from the pool.
-
Initial connections: The initial number of connections from the application to the data source.
-
Minimum connections: The minimum number of connections to maintain when the business is idle.
-
Maximum connections: The maximum number of connections allowed during high concurrency.
-
Max wait time: The maximum time a business request can wait for a connection from the pool.
-
Associated KMS credential (Important): Select a KMS credential. The account and password for accessing the database are stored in this credential.
-
You can select a database credential or a generic credential. If you use a generic credential, it must be a JSON object that contains the
AccountNameandAccountPasswordkeys. -
If you specify a database instance and database when you configure the KMS credential, the system validates that they match the database instance selected for the data source. This helps prevent misconfigurations.
-
-
More parameters: You can set custom parameters in addition to the common ones, such as
testWhileIdle,testOnBorrow, andtestOnReturn. Example:# Specifies whether to enable detection of idle connections. spring.datasource.druid.testWhileIdle=true # Specifies whether to test connections when you borrow them from the pool. We recommend that you disable this feature. spring.datasource.druid.testOnBorrow=false # Specifies whether to test connections when you return them to the pool. We recommend that you disable this feature. spring.datasource.druid.testOnReturn=false
-
-
Click OK. In the Confirm Data Source Creation dialog box, click OK again to create the data source.
Data source details
On the Data source management page, click a data source name to view its details:
-
Raw configuration content: You can view the DataId, Group, and content of the underlying configuration. The plaintext is displayed only in the console.
The data source management feature is built on the Nacos configuration center. Data source configurations are stored in an encrypted format within the configuration center.
-
Listener query: Reuses the listener query feature of the configuration center. Here, you can view the application nodes that are listening to this data source.
-
Change events:
-
Records events for creating or updating the data source and for credential rotation. You can view a side-by-side comparison of the content before and after the change in the event details.
-
For credentials involved in a credential rotation event, view the before and after values on the KMS credential management page.
-
-
Push trajectory: Reuses the push trajectory feature of the configuration center to record configuration changes and push events for the data source.
Edit a data source
On the Data source management list page, click Edit for the target data source.
Usage notes
Configuration update mechanism
-
After you edit a data source, the encrypted configuration in the configuration center is updated synchronously.
-
If an application subscribes to the data source, Apache Druid provides exception protection for runtime changes.
Impact of runtime changes
-
After changing the database endpoint or port: If the new configuration cannot be accessed, existing connections are not affected.
-
After changing the credential: If the new credential does not have permission to access the data source, existing connections remain usable.
Although the application-side data source supports exception protection, you should be cautious when updating production workloads. Observe your business metrics in real time. Exception protection can probe connection-layer availability and credential permissions, but it cannot fully cover end-to-end business logic (for example, the new database instance may not have a required business table).
Credential rotation
KMS supports two credential rotation modes for data sources:
-
Scheduled rotation: You can set a rotation schedule when you create a credential. The credential is then rotated automatically at the specified interval.
-
Immediate rotation: You can rotate a credential immediately from the KMS credential management console if a leak occurs or for any other urgent reason.
When a KMS credential is rotated, MSE Nacos updates the encrypted data source configuration and pushes the change to the application in real time. Druid gracefully replaces online connections with no business impact.
Delete a data source
On the Data source management list page, click Delete for the target data source, and then enter the data source name to confirm the deletion.
Note:
-
Deleting a data source also deletes the associated encrypted configuration from the underlying configuration center. This action cannot be undone. Proceed with caution.
-
Application-side runtime exception protection ensures that deleting a data source does not affect existing connections for online services. However, the application will fail to start if it tries to connect to the deleted data source.
Application-side integration
On the data source details page, click Application-side integration to view the integration steps.
For parameter configurations, always refer to the settings provided in the Application-side Integration section of the console. The parameters in each step are dynamically generated for your specific 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.25 or later.
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.25</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2023.0.3.3</version>
</dependency>
Step 2: Modify the configuration
-
Modify the
application.propertiesfile to enable dynamic data source rotation and specify the corresponding Nacos configuration for the 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 server address. The public endpoint is used by default. Change it as needed.
spring.cloud.nacos.config.server-addr={nacos server}
# Specify the namespace.
spring.cloud.nacos.config.namespace={namespace ID}
# Specify the KMS region ID.
spring.cloud.nacos.config.kms_region_id={instance region}
# Enable Druid data source management and specify the data ID for the encrypted rotation configuration.
spring.nacos.config.proxy.druid.enabled=true
spring.nacos.config.proxy.druid.data-id={dataID}
Step 3: Set access credentials
You must grant permissions to the credential in advance. Go to Security Protection > Authentication & Authorization > Client-side authentication. For authorization granularity, select "Decryption permission for KMS encrypted configurations" and follow the instructions to complete the process.
-
After you grant permissions, the encrypted data source configuration is decrypted in memory at runtime. You must set the credentials that have permission to decrypt the configuration.
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 ID and AccessKey secret. Ensure that the corresponding account or role has been granted the "Decryption permission for KMS encrypted configurations" permission. For more information about access credential formats, see Configure client access credentials.
-
FAQ
1. Supported data source types
This feature supports end-to-end hosting and zero-impact runtime rotation for Spring applications that use Druid. Redis support is coming soon, with plans to extend to AI-related secrets such as app keys and tokens.
2. Credential invalidation during rotation
During rotation, KMS notifies MSE to update the credentials and keeps two versions active. Credentials older than the latest two versions become invalid. A future update will add session management for specific data sources so that old credentials are invalidated only after all online connections stop using them.
3. Detecting exception protection triggers
If the application's connection pool triggers exception protection, it sends a failure notification to Nacos. You can detect this by searching for the notify-error keyword in the Nacos config.log file. A future Nacos version will report client listener callback status directly in the listener query feature.