|
2 | 2 |
|
3 | 3 | import botocore
|
4 | 4 | import botocore.auth
|
5 |
| -from botocore.exceptions import UnknownClientMethodError |
| 5 | +from botocore.exceptions import ParamValidationError, UnknownClientMethodError |
6 | 6 | from botocore.signers import (
|
7 | 7 | RequestSigner,
|
8 | 8 | S3PostPresigner,
|
@@ -200,6 +200,15 @@ def add_generate_db_auth_token(class_attributes, **kwargs):
|
200 | 200 | class_attributes['generate_db_auth_token'] = generate_db_auth_token
|
201 | 201 |
|
202 | 202 |
|
| 203 | +def add_dsql_generate_db_auth_token_methods(class_attributes, **kwargs): |
| 204 | + class_attributes['generate_db_connect_auth_token'] = ( |
| 205 | + dsql_generate_db_connect_auth_token |
| 206 | + ) |
| 207 | + class_attributes['generate_db_connect_admin_auth_token'] = ( |
| 208 | + dsql_generate_db_connect_admin_auth_token |
| 209 | + ) |
| 210 | + |
| 211 | + |
203 | 212 | async def generate_db_auth_token(
|
204 | 213 | self, DBHostname, Port, DBUsername, Region=None
|
205 | 214 | ):
|
@@ -256,6 +265,86 @@ async def generate_db_auth_token(
|
256 | 265 | return presigned_url[len(scheme) :]
|
257 | 266 |
|
258 | 267 |
|
| 268 | +async def _dsql_generate_db_auth_token( |
| 269 | + self, Hostname, Action, Region=None, ExpiresIn=900 |
| 270 | +): |
| 271 | + """Generate a DSQL database token for an arbitrary action. |
| 272 | + :type Hostname: str |
| 273 | + :param Hostname: The DSQL endpoint host name. |
| 274 | + :type Action: str |
| 275 | + :param Action: Action to perform on the cluster (DbConnectAdmin or DbConnect). |
| 276 | + :type Region: str |
| 277 | + :param Region: The AWS region where the DSQL Cluster is hosted. If None, the client region will be used. |
| 278 | + :type ExpiresIn: int |
| 279 | + :param ExpiresIn: The token expiry duration in seconds (default is 900 seconds). |
| 280 | + :return: A presigned url which can be used as an auth token. |
| 281 | + """ |
| 282 | + possible_actions = ("DbConnect", "DbConnectAdmin") |
| 283 | + |
| 284 | + if Action not in possible_actions: |
| 285 | + raise ParamValidationError( |
| 286 | + report=f"Received {Action} for action but expected one of: {', '.join(possible_actions)}" |
| 287 | + ) |
| 288 | + |
| 289 | + if Region is None: |
| 290 | + Region = self.meta.region_name |
| 291 | + |
| 292 | + request_dict = { |
| 293 | + 'url_path': '/', |
| 294 | + 'query_string': '', |
| 295 | + 'headers': {}, |
| 296 | + 'body': { |
| 297 | + 'Action': Action, |
| 298 | + }, |
| 299 | + 'method': 'GET', |
| 300 | + } |
| 301 | + scheme = 'https://' |
| 302 | + endpoint_url = f'{scheme}{Hostname}' |
| 303 | + prepare_request_dict(request_dict, endpoint_url) |
| 304 | + presigned_url = await self._request_signer.generate_presigned_url( |
| 305 | + operation_name=Action, |
| 306 | + request_dict=request_dict, |
| 307 | + region_name=Region, |
| 308 | + expires_in=ExpiresIn, |
| 309 | + signing_name='dsql', |
| 310 | + ) |
| 311 | + return presigned_url[len(scheme) :] |
| 312 | + |
| 313 | + |
| 314 | +async def dsql_generate_db_connect_auth_token( |
| 315 | + self, Hostname, Region=None, ExpiresIn=900 |
| 316 | +): |
| 317 | + """Generate a DSQL database token for the "DbConnect" action. |
| 318 | + :type Hostname: str |
| 319 | + :param Hostname: The DSQL endpoint host name. |
| 320 | + :type Region: str |
| 321 | + :param Region: The AWS region where the DSQL Cluster is hosted. If None, the client region will be used. |
| 322 | + :type ExpiresIn: int |
| 323 | + :param ExpiresIn: The token expiry duration in seconds (default is 900 seconds). |
| 324 | + :return: A presigned url which can be used as an auth token. |
| 325 | + """ |
| 326 | + return await _dsql_generate_db_auth_token( |
| 327 | + self, Hostname, "DbConnect", Region, ExpiresIn |
| 328 | + ) |
| 329 | + |
| 330 | + |
| 331 | +async def dsql_generate_db_connect_admin_auth_token( |
| 332 | + self, Hostname, Region=None, ExpiresIn=900 |
| 333 | +): |
| 334 | + """Generate a DSQL database token for the "DbConnectAdmin" action. |
| 335 | + :type Hostname: str |
| 336 | + :param Hostname: The DSQL endpoint host name. |
| 337 | + :type Region: str |
| 338 | + :param Region: The AWS region where the DSQL Cluster is hosted. If None, the client region will be used. |
| 339 | + :type ExpiresIn: int |
| 340 | + :param ExpiresIn: The token expiry duration in seconds (default is 900 seconds). |
| 341 | + :return: A presigned url which can be used as an auth token. |
| 342 | + """ |
| 343 | + return await _dsql_generate_db_auth_token( |
| 344 | + self, Hostname, "DbConnectAdmin", Region, ExpiresIn |
| 345 | + ) |
| 346 | + |
| 347 | + |
259 | 348 | class AioS3PostPresigner(S3PostPresigner):
|
260 | 349 | async def generate_presigned_post(
|
261 | 350 | self,
|
|
0 commit comments