forked from aws/aws-sdk-pandas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecretsmanager.py
68 lines (52 loc) · 2.1 KB
/
secretsmanager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""Secrets Manager module."""
import base64
import json
import logging
from typing import Any, Dict, Optional, Union, cast
import boto3
from awswrangler import _utils
_logger: logging.Logger = logging.getLogger(__name__)
def get_secret(name: str, boto3_session: Optional[boto3.Session] = None) -> Union[str, bytes]:
"""Get secret value.
Parameters
----------
name: str:
Specifies the secret containing the version that you want to retrieve.
You can specify either the Amazon Resource Name (ARN) or the friendly name of the secret.
boto3_session : boto3.Session(), optional
Boto3 Session. The default boto3 session will be used if boto3_session receive None.
Returns
-------
Union[str, bytes]
Secret value.
Examples
--------
>>> import awswrangler as wr
>>> value = wr.secretsmanager.get_secret("my-secret")
"""
session: boto3.Session = _utils.ensure_session(session=boto3_session)
client: boto3.client = _utils.client(service_name="secretsmanager", session=session)
response: Dict[str, Any] = client.get_secret_value(SecretId=name)
if "SecretString" in response:
return cast(str, response["SecretString"])
return base64.b64decode(response["SecretBinary"])
def get_secret_json(name: str, boto3_session: Optional[boto3.Session] = None) -> Dict[str, Any]:
"""Get JSON secret value.
Parameters
----------
name: str:
Specifies the secret containing the version that you want to retrieve.
You can specify either the Amazon Resource Name (ARN) or the friendly name of the secret.
boto3_session : boto3.Session(), optional
Boto3 Session. The default boto3 session will be used if boto3_session receive None.
Returns
-------
Dict[str, Any]
Secret JSON value parsed as a dictionary.
Examples
--------
>>> import awswrangler as wr
>>> value = wr.secretsmanager.get_secret_json("my-secret-with-json-content")
"""
value: Union[str, bytes] = get_secret(name=name, boto3_session=boto3_session)
return cast(Dict[str, Any], json.loads(value))