|
5 | 5 | This module provides utilities for generating authentication tokens for RDS MySQL database connections. |
6 | 6 | """ |
7 | 7 |
|
8 | | -from volcenginesdkcore.signv4 import SignerV4 |
9 | | -from volcenginesdkcore.endpoint.providers.default_provider import DefaultEndpointProvider |
| 8 | +from volcenginesdkcore.endpoint.providers.standard_provider import StandardEndpointResolver |
| 9 | +from volcenginesdkcore.interceptor import InterceptorChain, InterceptorContext, SignRequestInterceptor, \ |
| 10 | + ResolveEndpointInterceptor |
| 11 | +from volcenginesdkcore.interceptor import Request |
10 | 12 |
|
| 13 | +DEFAULT_SERVICE = 'rds_mysql' |
| 14 | +DEFAULT_API_VERSION = '2022-01-01' |
| 15 | +DEFAULT_API = 'ConnectDatabase' |
| 16 | +DEFAULT_EXPIRES = 900 |
11 | 17 |
|
12 | | -def build_auth_token(credentials, db_user, instance_id, region, expires=None): |
| 18 | + |
| 19 | +def build_auth_token(api_client, db_user, instance_id, expires=None): |
13 | 20 | """ |
14 | 21 | Build an authentication token (presigned URL) for connecting to RDS MySQL database. |
15 | 22 |
|
16 | | - :param credentials: CredentialValue object with ak, sk, and optional session_token |
| 23 | + :param api_client: ApiClient instance |
17 | 24 | :param db_user: Database username |
18 | 25 | :param instance_id: RDS instance ID |
19 | | - :param region: Service region (e.g., 'cn-beijing') |
20 | 26 | :param expires: Token expiration time in seconds (default: 900, i.e., 15 minutes) |
21 | 27 | :return: Presigned URL string for database authentication |
22 | 28 | :raises ValueError: If required parameters are missing or invalid |
23 | 29 | """ |
24 | | - # Validate inputs |
25 | | - if credentials is None: |
26 | | - raise ValueError("credentials must not be None") |
| 30 | + # Validate api_client |
| 31 | + if api_client is None: |
| 32 | + raise ValueError("api_client must not be None") |
27 | 33 |
|
28 | | - if not hasattr(credentials, 'ak') or not credentials.ak: |
29 | | - raise ValueError("credentials.ak must not be empty") |
| 34 | + configuration = api_client.configuration |
| 35 | + region = configuration.region |
30 | 36 |
|
31 | | - if not hasattr(credentials, 'sk') or not credentials.sk: |
32 | | - raise ValueError("credentials.sk must not be empty") |
| 37 | + # Validate inputs |
| 38 | + if not region: |
| 39 | + raise ValueError("region must not be empty") |
33 | 40 |
|
34 | 41 | if not db_user: |
35 | 42 | raise ValueError("db_user must not be empty") |
36 | 43 |
|
37 | 44 | if not instance_id: |
38 | 45 | raise ValueError("instance_id must not be empty") |
39 | 46 |
|
40 | | - if not region: |
41 | | - raise ValueError("region must not be empty") |
42 | | - |
43 | 47 | # Set default expiration time |
44 | 48 | if expires is None or expires <= 0: |
45 | | - expires = 900 # 15 minutes |
46 | | - |
47 | | - # Service configuration |
48 | | - service = 'rds_mysql' |
49 | | - |
50 | | - # Get endpoint |
51 | | - endpoint_provider = DefaultEndpointProvider() |
52 | | - resolved_endpoint = endpoint_provider.endpoint_for(service, region) |
53 | | - host = resolved_endpoint.host |
| 49 | + expires = DEFAULT_EXPIRES |
54 | 50 |
|
55 | 51 | # Build query parameters |
56 | 52 | query = { |
57 | | - 'Action': 'ConnectDatabase', |
58 | | - 'Version': '2022-01-01', |
| 53 | + 'Action': DEFAULT_API, |
| 54 | + 'Version': DEFAULT_API_VERSION, |
59 | 55 | 'X-Expires': str(expires), |
60 | 56 | 'DBUser': db_user, |
61 | 57 | 'InstanceId': instance_id, |
62 | 58 | } |
63 | 59 |
|
64 | | - # Sign the URL |
65 | | - signed_query = SignerV4.sign_url( |
66 | | - path='/', |
67 | | - method='GET', |
68 | | - query=query, |
69 | | - ak=credentials.ak, |
70 | | - sk=credentials.sk, |
71 | | - region=region, |
72 | | - service=service, |
73 | | - session_token=getattr(credentials, 'session_token', None) |
74 | | - ) |
75 | | - |
76 | | - return signed_query |
| 60 | + # Create Request with presign mode |
| 61 | + request = Request(configuration, |
| 62 | + resource_path='/{}/{}/{}/get/text_plain/'.format(DEFAULT_API, DEFAULT_API_VERSION, |
| 63 | + DEFAULT_SERVICE), |
| 64 | + method='GET', |
| 65 | + query_params=query) |
| 66 | + request.host = None # Force endpoint resolution by interceptor |
| 67 | + request.endpoint_provider = StandardEndpointResolver() |
| 68 | + request.service = DEFAULT_SERVICE |
| 69 | + request.is_presign = True |
| 70 | + |
| 71 | + # Create interceptor chain: |
| 72 | + # ResolveEndpointInterceptor - resolves endpoint + scheme |
| 73 | + # SignRequestInterceptor - presign URL signing |
| 74 | + chain = InterceptorChain() |
| 75 | + chain.append_request_interceptor(ResolveEndpointInterceptor()) |
| 76 | + chain.append_request_interceptor(SignRequestInterceptor()) |
| 77 | + |
| 78 | + context = InterceptorContext(request=request) |
| 79 | + context = chain.execute_request(context) |
| 80 | + |
| 81 | + return '{url}?{query}'.format(url=context.request.url, query=context.request.signed_query) |
0 commit comments