reach_commons 0.18.38__py3-none-any.whl → 0.18.39__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ from reach_commons.reach_aws.db_config import get_secret
2
+
3
+ __all__ = ["get_secret"]
@@ -0,0 +1,87 @@
1
+ import base64
2
+ import json
3
+ import os
4
+ from typing import Any, Dict
5
+
6
+ import boto3
7
+ from botocore.exceptions import ClientError
8
+
9
+ ENV = os.environ.get("ENV", "Staging")
10
+
11
+
12
+ def _get_secret_json(secret_arn: str, region_name: str = "us-east-1") -> Dict[str, Any]:
13
+ """Fetch and parse a JSON secret from AWS Secrets Manager."""
14
+ session = boto3.Session(region_name=region_name)
15
+ client = session.client("secretsmanager")
16
+
17
+ try:
18
+ response = client.get_secret_value(SecretId=secret_arn)
19
+ except ClientError as exc:
20
+ raise RuntimeError(
21
+ f"Failed to fetch secret from AWS Secrets Manager: secret_arn={secret_arn}"
22
+ ) from exc
23
+
24
+ secret_string = _extract_secret_string(response, secret_arn)
25
+ try:
26
+ return json.loads(secret_string)
27
+ except json.JSONDecodeError as exc:
28
+ raise ValueError(
29
+ f"Secret value is not valid JSON: secret_arn={secret_arn}"
30
+ ) from exc
31
+
32
+
33
+ def _extract_secret_string(response: Dict[str, Any], secret_arn: str) -> str:
34
+ if response.get("SecretBinary"):
35
+ decoded = base64.b64decode(response["SecretBinary"])
36
+ return decoded.decode("utf-8")
37
+ secret_string = response.get("SecretString")
38
+ if not secret_string:
39
+ raise ValueError(
40
+ f"Secret did not contain SecretString or SecretBinary: secret_arn={secret_arn}"
41
+ )
42
+ return secret_string
43
+
44
+
45
+ def get_secret(
46
+ secret_arn: str,
47
+ region_name: str = "us-east-1",
48
+ host=os.getenv("db_host_proxy"),
49
+ db_name=os.getenv("db_name"),
50
+ ) -> Dict[str, Any]:
51
+ """
52
+ Load DB credentials from AWS Secrets Manager and host from SSM Parameter Store.
53
+
54
+ Example:
55
+ # from reach_commons.reach_aws import get_secret
56
+ # config = get_secret(
57
+ # os.environ[ "RDS_SECRET_ARN"],
58
+ #)
59
+ """
60
+
61
+ if not secret_arn:
62
+ raise ValueError(f"RDS secret ARN is not configured")
63
+ if not host:
64
+ raise ValueError(f"RDS host is not configured")
65
+
66
+ secrets_data = _get_secret_json(secret_arn, region_name)
67
+
68
+ if not isinstance(secrets_data, dict):
69
+ raise ValueError(
70
+ f"Secret payload must be a JSON object: secret_arn={secret_arn}"
71
+ )
72
+
73
+ secrets_data["host"] = host
74
+ secrets_data["dbname"] = db_name
75
+
76
+ missing = [
77
+ key
78
+ for key in ("host", "username", "password", "dbname")
79
+ if key not in secrets_data
80
+ ]
81
+ if missing:
82
+ raise ValueError(
83
+ "Secret is missing required fields: "
84
+ f"missing={missing}, secret_arn={secret_arn}"
85
+ )
86
+
87
+ return secrets_data
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: reach_commons
3
- Version: 0.18.38
3
+ Version: 0.18.39
4
4
  Summary: Reach Commons is a versatile utility library designed to streamline and enhance development workflows within the Reach ecosystem.
5
5
  License: MIT
6
6
  Author: Engineering
@@ -15,8 +15,9 @@ reach_commons/mongo/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSu
15
15
  reach_commons/mongo/customer_persistence.py,sha256=acrtpyCWr9vLVq61saJ3_Vp4DYHFBTM9XqoYC72J84w,3735
16
16
  reach_commons/mongo/customer_persistence_async.py,sha256=BmcP8TXyyQah-GYM3wcKi1baqSCycjw7UadlxGywyQM,3892
17
17
  reach_commons/mongo/validation/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
18
- reach_commons/reach_aws/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
18
+ reach_commons/reach_aws/__init__.py,sha256=xb97rt0lBd0wz9ZhULQ7YVAOceOS-AkvNVM4jfLOjYE,86
19
19
  reach_commons/reach_aws/commons.py,sha256=qQba0li75BIpmyVc0sDVrrxbtYvDCedF6RmFD-V4MYQ,259
20
+ reach_commons/reach_aws/db_config.py,sha256=J0wRMutunOakSxYQbgvrIs1EphVuUWGVfLbHq_zmLOY,2649
20
21
  reach_commons/reach_aws/dynamo_db.py,sha256=BL3QcKzx4uZic-Ui12tln_GMSKe297FdfyIzFPE7veE,7140
21
22
  reach_commons/reach_aws/exceptions.py,sha256=x0RL5ktNtzxg0KykhEVWReBq_dEtciK6B2vMs_s4C9k,915
22
23
  reach_commons/reach_aws/firehose.py,sha256=1xFKLWMv3bNo3PPW5gtaL6NqzUDyVil6B768slj2wbY,5674
@@ -29,6 +30,6 @@ reach_commons/redis_manager.py,sha256=yRed53ZKlbIb6rZnL53D1F_aB-xWT3nbeUP2cqYzho
29
30
  reach_commons/sms_smart_encoding.py,sha256=92y0RmZ0l4ONHpC9qeO5KfViSNq64yE2rc7lhNDSZqE,1241
30
31
  reach_commons/utils.py,sha256=dMgKIGqTgoSItuBI8oz81gKtW3qi21Jkljv9leS_V88,8475
31
32
  reach_commons/validations.py,sha256=x_lkrtlrCAJC_f7mZb19JjfKFbYlPFv-P84K_lbZyYs,1056
32
- reach_commons-0.18.38.dist-info/METADATA,sha256=Z_JM-y0N__A8dJQLCypLq5fXSAAaoV1P9BpehrClet8,1863
33
- reach_commons-0.18.38.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
34
- reach_commons-0.18.38.dist-info/RECORD,,
33
+ reach_commons-0.18.39.dist-info/METADATA,sha256=vFcpcHbymVs11HSuD6S3ckIK9mcmeAiTJ9QWVlFXDXU,1863
34
+ reach_commons-0.18.39.dist-info/WHEEL,sha256=FMvqSimYX_P7y0a7UY-_Mc83r5zkBZsCYPm7Lr0Bsq4,88
35
+ reach_commons-0.18.39.dist-info/RECORD,,