|
| 1 | +package com.volcengine.feature.rds.auth; |
| 2 | + |
| 3 | +import com.volcengine.ApiClient; |
| 4 | +import com.volcengine.ApiException; |
| 5 | +import com.volcengine.Pair; |
| 6 | +import com.volcengine.endpoint.ResolveEndpointOption; |
| 7 | +import com.volcengine.endpoint.ResolvedEndpoint; |
| 8 | +import com.volcengine.endpoint.StandardEndpointProvider; |
| 9 | +import com.volcengine.interceptor.*; |
| 10 | +import com.volcengine.sign.ServiceInfo; |
| 11 | + |
| 12 | +import org.apache.commons.lang.StringUtils; |
| 13 | + |
| 14 | +import java.util.*; |
| 15 | + |
| 16 | +/** |
| 17 | + * RDS MySQL database connection utility class. |
| 18 | + * Generates a presigned URL that can be used as the authentication token (password) for database connections. |
| 19 | + */ |
| 20 | +public class ConnectUtils { |
| 21 | + |
| 22 | + private static final String SERVICE_NAME = "rds_mysql"; |
| 23 | + private static final String ACTION = "ConnectDatabase"; |
| 24 | + private static final String VERSION = "2022-01-01"; |
| 25 | + private static final int DEFAULT_EXPIRES_SECONDS = 900; // 15 minutes |
| 26 | + |
| 27 | + /** |
| 28 | + * Generate an authorization token for database connection (used as password). |
| 29 | + * Aligned with Go SDK's BuildAuthToken implementation. |
| 30 | + * |
| 31 | + * @param apiClient ApiClient containing Credentials, Region, DisableSSL, and UseDualStack settings |
| 32 | + * @param dbUser Database user account |
| 33 | + * @param instanceId Database instance ID |
| 34 | + * @param expires Expiration time in seconds, defaults to 900 seconds (15 minutes) if <= 0 |
| 35 | + * @return Presigned URL string that can be used as the authorization token for database connection |
| 36 | + * @throws ApiException if parameters are invalid or signing fails |
| 37 | + */ |
| 38 | + public static String buildAuthToken(ApiClient apiClient, String dbUser, String instanceId, int expires) throws ApiException { |
| 39 | + // Parameter validation |
| 40 | + if (apiClient == null) { |
| 41 | + throw new IllegalArgumentException("apiClient must not be null"); |
| 42 | + } |
| 43 | + |
| 44 | + if (StringUtils.isEmpty(apiClient.getRegion())) { |
| 45 | + throw new IllegalArgumentException("region must not be empty"); |
| 46 | + } |
| 47 | + |
| 48 | + if (StringUtils.isEmpty(dbUser)) { |
| 49 | + throw new IllegalArgumentException("dbUser must not be empty"); |
| 50 | + } |
| 51 | + |
| 52 | + if (StringUtils.isEmpty(instanceId)) { |
| 53 | + throw new IllegalArgumentException("instanceId must not be empty"); |
| 54 | + } |
| 55 | + |
| 56 | + // Use default expires if <= 0 |
| 57 | + if (expires <= 0) { |
| 58 | + expires = DEFAULT_EXPIRES_SECONDS; |
| 59 | + } |
| 60 | + |
| 61 | + // Resolve endpoint: prefer user-configured endpoint, fallback to StandardEndpointProvider |
| 62 | + String endpoint = getEndpoint(apiClient); |
| 63 | + |
| 64 | + // SSL handling, ResolveEndpointInterceptor dose not support this |
| 65 | + String schema = apiClient.getDisableSSL() ? "http" : "https"; |
| 66 | + |
| 67 | + // Build InterceptorContext with presigned flag |
| 68 | + InterceptorContext context = new InterceptorContext(apiClient.getHttpClient(), null); |
| 69 | + context.setApiClient(apiClient); |
| 70 | + |
| 71 | + RequestInterceptorContext reqCtx = context.getRequestContext(); |
| 72 | + reqCtx.setPresigned(true); |
| 73 | + reqCtx.setSchema(schema); |
| 74 | + reqCtx.setHost(endpoint); |
| 75 | + reqCtx.setMethod("GET"); |
| 76 | + reqCtx.setServiceInfo(new ServiceInfo(SERVICE_NAME, "GET")); |
| 77 | + reqCtx.setHeaderParams(new HashMap<>()); |
| 78 | + |
| 79 | + List<Pair> queryParams = new ArrayList<>(); |
| 80 | + queryParams.add(new Pair("Action", ACTION)); |
| 81 | + queryParams.add(new Pair("Version", VERSION)); |
| 82 | + queryParams.add(new Pair("X-Expires", String.valueOf(expires))); |
| 83 | + queryParams.add(new Pair("DBUser", dbUser)); |
| 84 | + queryParams.add(new Pair("InstanceId", instanceId)); |
| 85 | + reqCtx.setQueryParams(queryParams); |
| 86 | + |
| 87 | + // Execute sign interceptor for presigning |
| 88 | + new SignRequestInterceptor().intercept(context); |
| 89 | + |
| 90 | + return reqCtx.getPresignedUrl(); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Resolve endpoint host from ApiClient. |
| 95 | + * If the user has configured a custom endpoint (via apiClient.setEndpoint), it takes priority. |
| 96 | + * Strips schema prefix if present to return pure host. |
| 97 | + * Falls back to StandardEndpointProvider for region-based resolution when no custom endpoint is set. |
| 98 | + * |
| 99 | + * @return endpoint host without schema |
| 100 | + */ |
| 101 | + private static String getEndpoint(ApiClient apiClient) { |
| 102 | + if (StringUtils.isNotEmpty(apiClient.getEndpoint())) { |
| 103 | + String ep = apiClient.getEndpoint(); |
| 104 | + if (ep.startsWith("https://")) { |
| 105 | + return ep.substring("https://".length()); |
| 106 | + } |
| 107 | + if (ep.startsWith("http://")) { |
| 108 | + return ep.substring("http://".length()); |
| 109 | + } |
| 110 | + return ep; |
| 111 | + } |
| 112 | + StandardEndpointProvider endpointProvider = new StandardEndpointProvider(); |
| 113 | + ResolveEndpointOption option = new ResolveEndpointOption(); |
| 114 | + option.setService(SERVICE_NAME); |
| 115 | + option.setRegion(apiClient.getRegion()); |
| 116 | + option.setUseDualStack(apiClient.getUseDualStack()); |
| 117 | + ResolvedEndpoint resolved = endpointProvider.endpointFor(option); |
| 118 | + return resolved.getEndpoint(); |
| 119 | + } |
| 120 | +} |
0 commit comments