reach_commons 0.18.37__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.
- reach_commons/reach_aws/__init__.py +3 -0
- reach_commons/reach_aws/db_config.py +87 -0
- reach_commons/reach_aws/reach_rate_limiter.py +17 -3
- {reach_commons-0.18.37.dist-info → reach_commons-0.18.39.dist-info}/METADATA +1 -1
- {reach_commons-0.18.37.dist-info → reach_commons-0.18.39.dist-info}/RECORD +6 -5
- {reach_commons-0.18.37.dist-info → reach_commons-0.18.39.dist-info}/WHEEL +0 -0
|
@@ -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
|
|
@@ -133,7 +133,10 @@ class ReachRateLimiter:
|
|
|
133
133
|
Loads config from Redis hash:
|
|
134
134
|
limit_per_window, interval_seconds, jitter_seconds
|
|
135
135
|
|
|
136
|
-
|
|
136
|
+
Behavior:
|
|
137
|
+
- If config exists in Redis: read only (never overwrite).
|
|
138
|
+
- If config does NOT exist yet: seed Redis ONCE with defaults (so you can edit live).
|
|
139
|
+
- If Redis is unavailable: fallback to defaults (no writes).
|
|
137
140
|
"""
|
|
138
141
|
now = self._now()
|
|
139
142
|
|
|
@@ -149,7 +152,18 @@ class ReachRateLimiter:
|
|
|
149
152
|
jitter = self.default_jitter
|
|
150
153
|
|
|
151
154
|
try:
|
|
152
|
-
|
|
155
|
+
cfg_key = self._cfg_key()
|
|
156
|
+
raw = self.redis.hgetall(cfg_key) or {}
|
|
157
|
+
|
|
158
|
+
# If config was never created, seed it once with defaults
|
|
159
|
+
if not raw:
|
|
160
|
+
rc = self.redis.redis_connection
|
|
161
|
+
rc.hsetnx(cfg_key, "limit_per_window", str(self.default_limit))
|
|
162
|
+
rc.hsetnx(cfg_key, "interval_seconds", str(self.default_interval))
|
|
163
|
+
rc.hsetnx(cfg_key, "jitter_seconds", str(self.default_jitter))
|
|
164
|
+
|
|
165
|
+
# Re-read after seeding (so we now depend on Redis config)
|
|
166
|
+
raw = self.redis.hgetall(cfg_key) or {}
|
|
153
167
|
|
|
154
168
|
# raw typically has bytes keys/values
|
|
155
169
|
limit = self._parse_int(
|
|
@@ -162,7 +176,7 @@ class ReachRateLimiter:
|
|
|
162
176
|
raw.get(b"jitter_seconds") or raw.get("jitter_seconds"), jitter
|
|
163
177
|
)
|
|
164
178
|
|
|
165
|
-
#
|
|
179
|
+
# If someone puts garbage in Redis
|
|
166
180
|
if limit <= 0:
|
|
167
181
|
limit = self.default_limit
|
|
168
182
|
if interval <= 0:
|
|
@@ -15,13 +15,14 @@ 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=
|
|
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
|
|
23
24
|
reach_commons/reach_aws/kms.py,sha256=ZOfyJMQUgxJEojRoB7-aCxtATpNx1Ig522IUYH11NZ4,4678
|
|
24
|
-
reach_commons/reach_aws/reach_rate_limiter.py,sha256=
|
|
25
|
+
reach_commons/reach_aws/reach_rate_limiter.py,sha256=G0An8cD0BDU7ZAuK3Xxjy2_fo7nL3ksf1LBHBlnX65s,8201
|
|
25
26
|
reach_commons/reach_aws/s3.py,sha256=2MLlDNFx0SROJBpE_KjJefyrB7lMqTlrYuRhSZx4iKs,3945
|
|
26
27
|
reach_commons/reach_aws/sqs.py,sha256=IKKWrd-qbhMMVYUvGbaq1ouVRdx-0u-SqwYaTcp0tWY,21645
|
|
27
28
|
reach_commons/reach_base_model.py,sha256=vgdGDcZr3iXMmyRhmBhOf_LKWB_6QzT3r_Yiyo6OmEk,3009
|
|
@@ -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.
|
|
33
|
-
reach_commons-0.18.
|
|
34
|
-
reach_commons-0.18.
|
|
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,,
|
|
File without changes
|