11package com .volcengine .feature .rds .auth ;
22
3+ import com .volcengine .ApiClient ;
4+ import com .volcengine .endpoint .DefaultEndpointProvider ;
35import com .volcengine .sign .Credentials ;
46import com .volcengine .sign .VolcstackSign ;
57
911import java .util .*;
1012
1113/**
12- * RDS MySQL database connection utility class
14+ * RDS MySQL database connection utility class.
15+ * Generates a presigned URL that can be used as the authentication token (password) for database connections.
1316 */
1417public class ConnectUtils {
1518
@@ -19,13 +22,38 @@ public class ConnectUtils {
1922 private static final int DEFAULT_EXPIRES_SECONDS = 900 ; // 15 minutes
2023
2124 /**
22- * Generate an authorization token for database connection (used as password)
25+ * Generate an authorization token for database connection (used as password).
26+ * Extracts credentials, region, and disableSSL from the ApiClient, similar to Go SDK's Config.
27+ *
28+ * @param apiClient ApiClient containing Credentials, Region, and DisableSSL settings
29+ * @param dbUser Database user account
30+ * @param instanceId Database instance ID
31+ * @param expires Expiration time in seconds, defaults to 900 seconds (15 minutes) if <= 0
32+ * @return Presigned URL string that can be used as the authorization token for database connection
33+ * @throws Exception if parameters are invalid or signing fails
34+ */
35+ public static String buildAuthToken (
36+ ApiClient apiClient ,
37+ String dbUser ,
38+ String instanceId ,
39+ int expires
40+ ) throws Exception {
41+ if (apiClient == null ) {
42+ throw new IllegalArgumentException ("apiClient must not be null" );
43+ }
44+ return buildAuthToken (apiClient .getCredentials (), apiClient .getRegion (),
45+ dbUser , instanceId , expires , apiClient .getDisableSSL ());
46+ }
47+
48+ /**
49+ * Generate an authorization token for database connection (used as password).
50+ * Uses HTTPS by default.
2351 *
2452 * @param credentials Credentials containing AccessKey and SecretKey for signing
2553 * @param region Region, e.g., "cn-beijing"
2654 * @param dbUser Database user account
2755 * @param instanceId Database instance ID
28- * @param expires Expiration time in seconds, defaults to 900 seconds (15 minutes) if < = 0
56+ * @param expires Expiration time in seconds, defaults to 900 seconds (15 minutes) if < = 0
2957 * @return Presigned URL string that can be used as the authorization token for database connection
3058 * @throws Exception if parameters are invalid or signing fails
3159 */
@@ -35,6 +63,29 @@ public static String buildAuthToken(
3563 String dbUser ,
3664 String instanceId ,
3765 int expires
66+ ) throws Exception {
67+ return buildAuthToken (credentials , region , dbUser , instanceId , expires , false );
68+ }
69+
70+ /**
71+ * Generate an authorization token for database connection (used as password).
72+ *
73+ * @param credentials Credentials containing AccessKey and SecretKey for signing
74+ * @param region Region, e.g., "cn-beijing"
75+ * @param dbUser Database user account
76+ * @param instanceId Database instance ID
77+ * @param expires Expiration time in seconds, defaults to 900 seconds (15 minutes) if <= 0
78+ * @param disableSSL If true, use http:// scheme; otherwise use https://
79+ * @return Presigned URL string that can be used as the authorization token for database connection
80+ * @throws Exception if parameters are invalid or signing fails
81+ */
82+ public static String buildAuthToken (
83+ Credentials credentials ,
84+ String region ,
85+ String dbUser ,
86+ String instanceId ,
87+ int expires ,
88+ boolean disableSSL
3889 ) throws Exception {
3990 // Parameter validation
4091 if (credentials == null ||
@@ -47,15 +98,27 @@ public static String buildAuthToken(
4798 throw new IllegalArgumentException ("region must not be empty" );
4899 }
49100
50- if (StringUtils .isEmpty (dbUser ) || StringUtils .isEmpty (instanceId )) {
51- throw new IllegalArgumentException ("dbUser or instanceId must not be empty" );
101+ if (StringUtils .isEmpty (dbUser )) {
102+ throw new IllegalArgumentException ("dbUser must not be empty" );
103+ }
104+
105+ if (StringUtils .isEmpty (instanceId )) {
106+ throw new IllegalArgumentException ("instanceId must not be empty" );
52107 }
53108
109+ // Use default expires if <= 0
110+ if (expires <= 0 ) {
111+ expires = DEFAULT_EXPIRES_SECONDS ;
112+ }
113+
114+ // Build regional endpoint
115+ String endpoint = DefaultEndpointProvider .getRegionalEndpoint (SERVICE_NAME , region );
116+
54117 // Build query parameters
55118 Map <String , String > queryParams = new HashMap <>();
56119 queryParams .put ("Action" , ACTION );
57120 queryParams .put ("Version" , VERSION );
58- queryParams .put ("X-Expires" , expires > 0 ? String .valueOf (expires ) : String . valueOf ( DEFAULT_EXPIRES_SECONDS ));
121+ queryParams .put ("X-Expires" , String .valueOf (expires ));
59122 queryParams .put ("DBUser" , dbUser );
60123 queryParams .put ("InstanceId" , instanceId );
61124
@@ -65,21 +128,26 @@ public static String buildAuthToken(
65128 sign .setService (SERVICE_NAME );
66129 sign .setMethod ("GET" );
67130
68- // Generate presigned URL
69- Map <String , String > presignedParams = sign .presign (queryParams );
131+ // Generate presigned URL with host signing
132+ Map <String , String > presignedParams = sign .presign (queryParams , endpoint );
70133
71134 // Build complete URL
72- return buildUrl (presignedParams );
135+ String scheme = disableSSL ? "http" : "https" ;
136+ return buildUrl (scheme , endpoint , presignedParams );
73137 }
74138
75139 /**
76- * Build complete URL
140+ * Build complete URL with scheme, host, and query parameters.
77141 *
142+ * @param scheme URL scheme ("http" or "https")
143+ * @param endpoint Host endpoint (e.g., "rds-mysql.cn-beijing.volcengineapi.com")
78144 * @param presignedParams Presigned query parameters
79145 * @return Complete URL string
80146 */
81- private static String buildUrl (Map <String , String > presignedParams ) {
147+ private static String buildUrl (String scheme , String endpoint , Map <String , String > presignedParams ) {
82148 StringBuilder url = new StringBuilder ();
149+ url .append (scheme ).append ("://" ).append (endpoint ).append ("?" );
150+
83151 // Sort parameter keys
84152 List <String > keys = new ArrayList <>(presignedParams .keySet ());
85153 Collections .sort (keys );
0 commit comments