velocity-python 0.9.3__py3-none-any.whl → 0.9.5__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.
velocity/__init__.py CHANGED
@@ -1,4 +1,4 @@
1
- __version__ = version = "0.9.3"
1
+ __version__ = version = "0.9.5"
2
2
 
3
3
  import importlib as _importlib
4
4
 
@@ -40,7 +40,8 @@ import logging
40
40
 
41
41
  from velocity.aws import feature_flags
42
42
  from velocity.cache import memo, read_cache
43
- from velocity.db import _pgddl, audit
43
+ from velocity.db import Expr, _pgddl, audit
44
+ from velocity.db.exceptions import DbLockTimeoutError
44
45
 
45
46
  from .base import (
46
47
  CIDR_LIST,
@@ -77,6 +78,17 @@ EVENT_COLUMNS = {
77
78
  # or enforce) itself, so the rule holds whether or not this module is imported.
78
79
  # core infrastructure (velocity.aws.schema "core"): every site has the table
79
80
  SCHEMA = {
81
+ # check_rate's upsert conflicts on bucket, so the unique index is what
82
+ # makes the count atomic; sys_created serves the hourly prune.
83
+ RATE_TABLE: {
84
+ "columns": {"bucket": "TEXT", "window_start": "TIMESTAMPTZ", "hits": "BIGINT"},
85
+ "indexes": [
86
+ {"columns": ["sys_created"], "name": "idx__sys_rate_limit__sys_created"}
87
+ ],
88
+ "unique_indexes": [
89
+ {"columns": ["bucket"], "name": "uidx__sys_rate_limit__bucket"}
90
+ ],
91
+ },
80
92
  EVENTS_TABLE: {
81
93
  "columns": dict(EVENT_COLUMNS),
82
94
  "indexes": [
@@ -212,17 +224,65 @@ def check_payload(body_size, *, max_bytes, enabled):
212
224
 
213
225
 
214
226
  def check_rate(tx, bucket, *, limit, now, table=RATE_TABLE):
215
- """Fixed one-minute window per bucket, kept in a velocity table: one find
216
- and one insert/update per request — the cost the switch buys."""
217
- key = f"{bucket}:{now.strftime('%Y%m%d%H%M')}"
218
- rate = tx.table(table)
219
- row = rate.find({"bucket": key})
220
- if row is None:
221
- rate.insert({"bucket": key, "window_start": now, "hits": 1})
222
- hits = 1
223
- else:
224
- hits = int(row.get("hits") or 0) + 1
225
- rate.update({"hits": hits}, where={"bucket": key})
227
+ """One bucket's :func:`check_rates`."""
228
+ return check_rates(tx, [bucket], limit=limit, now=now, table=table)
229
+
230
+
231
+ def check_rates(tx, buckets, *, limit, now, table=RATE_TABLE):
232
+ """Fixed one-minute window per bucket, kept in a velocity table: one
233
+ atomic upsert per bucket (``ON CONFLICT (bucket) DO UPDATE SET hits =
234
+ hits + 1 RETURNING hits``), so concurrent requests in one window each
235
+ count. The first refusal wins; later buckets are not counted after it.
236
+
237
+ Every bucket counts on ONE side transaction, committed before the
238
+ request goes on, as :func:`record_event` writes: a request that ends in
239
+ an error (a bad token, a refusal — this one included) rolls its own
240
+ transaction back, and the hits that most need counting would be lost
241
+ with it; the row locks are released at once instead of serializing the
242
+ caller's other requests behind a slow one. The lock wait is bounded
243
+ (``EVENT_LOCK_TIMEOUT``): a bucket still contended after it — a flood on
244
+ one address or user — is refused like an exceeded one (a 403 and its
245
+ event), not a 500. Any other failure (no unique index on ``bucket``: run
246
+ the schema lane) raises, so the guard fails closed."""
247
+ keys = [(b, f"{b}:{now.strftime('%Y%m%d%H%M')}") for b in buckets]
248
+ open_transaction = getattr(getattr(tx, "engine", None), "side_transaction", None)
249
+ if not callable(open_transaction):
250
+ return _count_buckets(tx, table, keys, now, limit)
251
+ try:
252
+ with open_transaction() as rate_tx:
253
+ if "postgres" in str(_pgddl.server_name(rate_tx) or "").lower():
254
+ rate_tx.execute(
255
+ "SELECT set_config('lock_timeout', %s, true)",
256
+ (EVENT_LOCK_TIMEOUT,),
257
+ )
258
+ return _count_buckets(rate_tx, table, keys, now, limit)
259
+ except DbLockTimeoutError:
260
+ names = ", ".join(b for b, _ in keys)
261
+ return "rate_limit", f"{names} contended past {EVENT_LOCK_TIMEOUT}"
262
+
263
+
264
+ def _count_buckets(tx, table, keys, now, limit):
265
+ for bucket, key in keys:
266
+ refusal = _rate_refusal(_count_hit(tx, table, key, now), bucket, limit)
267
+ if refusal:
268
+ return refusal
269
+ return None
270
+
271
+
272
+ def _count_hit(tx, table, key, now):
273
+ row = tx.table(table).merge(
274
+ {"bucket": key, "window_start": now, "hits": 1},
275
+ pk={"bucket": key},
276
+ on_conflict={"hits": Expr(f'COALESCE("{table}".hits, 0) + 1')},
277
+ insert_only=["window_start"],
278
+ returning=["hits"],
279
+ )
280
+ if not row or row.get("hits") is None:
281
+ raise RuntimeError(f"security: rate bucket {key!r} returned no count")
282
+ return int(row["hits"])
283
+
284
+
285
+ def _rate_refusal(hits, bucket, limit):
226
286
  if hits > limit:
227
287
  return "rate_limit", f"{bucket} exceeded {limit} requests/minute ({hits})"
228
288
  return None
@@ -320,9 +380,8 @@ def enforce(tx, aws_event, context, *, now=None):
320
380
  if not refusal and truthy(config.get("security_rate_limit_enabled")):
321
381
  if not ip_in(facts["ip"], parse_cidrs(config.get("rate_limit_exempt"))):
322
382
  limit = int(config.get("security_rate_limit_per_minute") or 120)
323
- refusal = check_rate(tx, f"ip:{facts['ip']}", limit=limit, now=now)
324
- if not refusal and user:
325
- refusal = check_rate(tx, f"user:{user}", limit=limit, now=now)
383
+ buckets = [f"ip:{facts['ip']}"] + ([f"user:{user}"] if user else [])
384
+ refusal = check_rates(tx, buckets, limit=limit, now=now)
326
385
 
327
386
  if refusal:
328
387
  guard, reason = refusal
@@ -98,15 +98,32 @@ def get_amplify_app_id_and_branch(
98
98
  def initialize_build_environment(
99
99
  project_root: Union[str, Path] = ".",
100
100
  include_run_on_build_env: bool = False,
101
+ *,
102
+ ssm: bool = False,
103
+ ssm_path: str = "/Global/{branch}",
104
+ profile: Optional[str] = None,
101
105
  ) -> Optional[Tuple[str, str]]:
106
+ """Set the build's identity (``AppStage``, ``AWS_BRANCH``, ``AWS_APP_ID``)
107
+ from the Amplify branch, and with ``ssm=True`` also export that branch's
108
+ configuration from SSM (``ssm_path``, see :func:`ssm_config.export_path`).
109
+
110
+ Returns ``(app_id, branch)``, or ``None`` when the branch cannot be
111
+ determined (no ``AWS_BRANCH`` and no Amplify CLI), leaving the
112
+ environment untouched. Configuration never comes from a local file.
113
+ """
102
114
  try:
103
- import env
104
- except ImportError:
115
+ app_id, branch = get_amplify_app_id_and_branch(project_root)
116
+ except RuntimeError as exc:
117
+ print(f"[WARN] Build environment not set: {exc}")
105
118
  return None
106
-
107
- app_id, branch = get_amplify_app_id_and_branch(project_root)
108
119
  print(f"[INFO] Setting environment to: {branch}")
109
- env.set(branch)
120
+ if ssm:
121
+ from velocity.aws import ssm_config
122
+
123
+ names = ssm_config.export_path(
124
+ ssm_path.format(branch=branch), profile=profile, overwrite=True
125
+ )
126
+ print(f"[INFO] Exported {len(names)} settings from SSM")
110
127
  os.environ["AppStage"] = branch
111
128
  os.environ["AWS_BRANCH"] = branch
112
129
  os.environ["AWS_APP_ID"] = app_id
@@ -25,7 +25,7 @@ from __future__ import annotations
25
25
 
26
26
  import logging
27
27
  import os
28
- from typing import Optional
28
+ from typing import MutableMapping, Optional
29
29
 
30
30
  logger = logging.getLogger(__name__)
31
31
 
@@ -253,6 +253,59 @@ def require(key: str) -> str:
253
253
  return value
254
254
 
255
255
 
256
+ def export_path(
257
+ prefix: str,
258
+ environ: Optional[MutableMapping[str, str]] = None,
259
+ *,
260
+ profile: Optional[str] = None,
261
+ region: Optional[str] = None,
262
+ overwrite: bool = False,
263
+ client=None,
264
+ ) -> list[str]:
265
+ """Copy every SSM parameter directly under ``prefix`` into ``environ``.
266
+
267
+ For scripts and builds run outside Lambda that need a whole environment's
268
+ configuration as variables (``export_path("/Global/demo")``), read from
269
+ SSM instead of a local file. Parameters are decrypted, one level deep
270
+ (``/Global/demo/DBHost`` becomes ``DBHost``), and all pages are read.
271
+
272
+ A variable already in ``environ`` is kept unless ``overwrite`` is true.
273
+ Returns the names that were set, sorted, never the values. Raises
274
+ ``LookupError`` when the path holds no parameters, so a mistyped
275
+ environment fails instead of running with nothing set.
276
+
277
+ ``environ`` defaults to ``os.environ``; ``client`` is an SSM client (else
278
+ one is made from ``profile`` and ``region``, default :func:`get_region`).
279
+ """
280
+ target = os.environ if environ is None else environ
281
+ path = "/" + prefix.strip("/")
282
+ if client is None:
283
+ import boto3
284
+
285
+ session = boto3.Session(profile_name=profile) if profile else boto3
286
+ client = session.client("ssm", region_name=region or get_region("us-east-1"))
287
+
288
+ values: dict[str, str] = {}
289
+ kwargs = {"Path": path, "Recursive": False, "WithDecryption": True}
290
+ while True:
291
+ page = client.get_parameters_by_path(**kwargs)
292
+ for parameter in page.get("Parameters", []):
293
+ values[parameter["Name"].rsplit("/", 1)[-1]] = parameter["Value"]
294
+ token = page.get("NextToken")
295
+ if not token:
296
+ break
297
+ kwargs["NextToken"] = token
298
+ if not values:
299
+ raise LookupError(f"SSM path {path} holds no parameters")
300
+
301
+ exported = []
302
+ for key, value in values.items():
303
+ if overwrite or key not in target:
304
+ target[key] = value
305
+ exported.append(key)
306
+ return sorted(exported)
307
+
308
+
256
309
  def clear_cache() -> None:
257
310
  """Clear the SSM cache (useful in tests)."""
258
311
  _cache.clear()
@@ -1,8 +1,5 @@
1
1
  import unittest
2
2
  from velocity.db.servers import postgres
3
- import env
4
-
5
- env.set()
6
3
 
7
4
  test_db = "test_db_postgres"
8
5
  engine = postgres.initialize(database=test_db)
@@ -1,8 +1,8 @@
1
1
  """Collection gate for the PostgreSQL integration suite.
2
2
 
3
- These tests create/drop real databases and call ``env.set()`` (which loads
4
- secrets into ``os.environ``), so they require a live PostgreSQL plus the shared
5
- secrets package. They are skipped by default to keep the unit suite runnable and
3
+ These tests create/drop real databases with the connection settings in the
4
+ environment (``DBHost``, ``DBUser``, ...), so they require a live PostgreSQL.
5
+ They are skipped by default to keep the unit suite runnable and
6
6
  hermetic; opt in with ``VELOCITY_INTEGRATION=1`` (``run_tests.py --integration``
7
7
  sets this automatically).
8
8
  """
@@ -8,9 +8,6 @@ from velocity.db.exceptions import (
8
8
  DbSchemaLockedError,
9
9
  DbTableMissingError,
10
10
  )
11
- import env
12
-
13
- env.set()
14
11
 
15
12
  test_db = "test_db_schema_locking"
16
13
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: velocity-python
3
- Version: 0.9.3
3
+ Version: 0.9.5
4
4
  Summary: A rapid application development library for interfacing with data storage
5
5
  Author-email: Velocity Team <info@codeclubs.org>
6
6
  License-Expression: MIT
@@ -1,4 +1,4 @@
1
- velocity/__init__.py,sha256=DQgH-dX5pIE9RPdaSeKM4Sn56mz5yioAz5zB5YYKRYI,362
1
+ velocity/__init__.py,sha256=LydmayfShX4hdXo_y2g8VqSvpWvs8FwHc3SdQWzLfYQ,362
2
2
  velocity/cache.py,sha256=pthvw7gDVEXRzkTcQtA5YVf4Q1IxAamWsba4UReT0RE,8933
3
3
  velocity/hooks.py,sha256=jY_qtAapNHvptmryFSD-Zc7RvLhV6e5nJEH3x2xTgCw,20575
4
4
  velocity/logging.py,sha256=MAsZap1cDSdOBNRc_9ezQxup5dHOS0ARxU9NI_-UK3E,4037
@@ -23,7 +23,7 @@ velocity/aws/scheduler.py,sha256=TzhvCcPpgNpRtIdobxQ-A4KsykjAXSM9VUcSr35Lgds,210
23
23
  velocity/aws/schema.py,sha256=k2eaV-WFIs5xrjuFqGKDyYJOkgvqkRJF07sORG2Krfc,5995
24
24
  velocity/aws/settings.py,sha256=aAQL7_dtcC5VSBnk60SX2KwU6jYDmKuyBQVp12Mr5as,39464
25
25
  velocity/aws/sms.py,sha256=2CdcLlIEy_V5K5Qx_pZ2vrakvEK7IhP7y1JUttBgyG4,16253
26
- velocity/aws/ssm_config.py,sha256=xkcBxPuCcdjpeArxdcGnSBiuyG2txlUraSqtCtb_kIo,7987
26
+ velocity/aws/ssm_config.py,sha256=VKjP86WOGBfRZnkMpU22Pu-alxNS1rPUvngyQlTSMcs,10049
27
27
  velocity/aws/tiles.py,sha256=_38EH9ngxQxLeiZnTHBta-TXg1VkO2hPvaoPHWEKAlc,28011
28
28
  velocity/aws/user_data.py,sha256=qLdmzUpkapUToEWaQ_jSeJa6hHgTHIqz0p0Cp2JuFx4,15564
29
29
  velocity/aws/validation.py,sha256=rj1bDYULxR2Wu512vbIhEcmU4Uv_IcwgFX2yS1ZBLf8,18115
@@ -40,7 +40,7 @@ velocity/aws/capabilities/infra.py,sha256=nUorjg7KpGhLW9ShUGdyh6yAwSKVy7xCAKKXwa
40
40
  velocity/aws/capabilities/jobs.py,sha256=mf6sU7NG_j4Vzth6RARpazbUD7hx79FmTowXD0xunfc,5192
41
41
  velocity/aws/capabilities/provisioning.py,sha256=4PF5QzQ_Pa51v5XN1OOn1juDxWqWPO3WI5fupqA9Agw,17844
42
42
  velocity/aws/capabilities/retention.py,sha256=V-vSbfPdpm7w67kaXi3OkmqtqEYFDeDn6P8kCs_rEzs,3198
43
- velocity/aws/capabilities/security.py,sha256=tuq1DhZ_q_kTUmpfsSyzXIAGcAzARmnuT3C0SiKmqSU,22721
43
+ velocity/aws/capabilities/security.py,sha256=37mqO5LtSL2gyaVspSh-l-boUJDL23es0s9u4alVoGI,25429
44
44
  velocity/aws/capabilities/single_session.py,sha256=UQhBc4hR-qSZfpBM_o8434Xpg6Bt3EbzI7ugZIxJh8Y,3798
45
45
  velocity/aws/capabilities/workflow.py,sha256=P7k6n2oZVovToAcHJUQwLdkD7duS4wKwBQKXaAMAnnU,1875
46
46
  velocity/aws/deploy/__init__.py,sha256=nOLW_NqxEv7i2xRSoc4wmawfAEo9z7oPrjxJagKZzaM,4832
@@ -50,7 +50,7 @@ velocity/aws/deploy/frontend_checks.py,sha256=R2y3GlVBKTBqcr46suYn_mpnHkR0s3kgMs
50
50
  velocity/aws/deploy/operations.py,sha256=y5I1otf4gDh37dnPexnjtJhDfYDZQXaP6xC5ZIG-v2o,39406
51
51
  velocity/aws/deploy/policies.py,sha256=aO92HJOCN8L6SE0_BlnTaN6QGywVIPD6FeifvhfdQdM,30747
52
52
  velocity/aws/deploy/reconcile.py,sha256=3xZXXiiVeCw4nUV8lKyk1XCgRbpNCLVYXIXbOLXes9I,20130
53
- velocity/aws/deploy/targets.py,sha256=vmZzGbLDKNtWkwd2ado_ZhT8AT6nXYbJD5nIgDiBzRc,6632
53
+ velocity/aws/deploy/targets.py,sha256=EgeD1f3adnUmefvvBKDVYak9SXLgxB8UeVi9vnvyP-s,7470
54
54
  velocity/aws/handlers/__init__.py,sha256=0yvVADxMHK8JOhgGRxMIz1XkNGlUk08AOmdR4qhNP2U,906
55
55
  velocity/aws/handlers/base_handler.py,sha256=1A_EO_71clM3-h-hAIUJxs_2LL7w3kkwj9Nz33loJMw,19918
56
56
  velocity/aws/handlers/context.py,sha256=Yrq-vtl2-0WeoTaFVMuKOQRM2Cnyq21rNvVtUvGjxx0,61416
@@ -152,8 +152,8 @@ velocity/db/tests/test_sql_builder.py,sha256=6iG-QV24c5KUpc5ZGyt8yEjOlL-JLEucZrg
152
152
  velocity/db/tests/test_tablehelper.py,sha256=1eE9kdGLiXb3mGs_kW7hmCkLTESXFAUzxkV-gdlSELs,19767
153
153
  velocity/db/tests/test_view_helper.py,sha256=IUBXoOJ3x4s8gNy8LM4H8WNeMBZcCCX1WUOZLGkF-VI,2974
154
154
  velocity/db/tests/postgres/__init__.py,sha256=zh28mNshfTljxi_3WAvvmkAxImLduEoHoUT_MAwkzxc,19
155
- velocity/db/tests/postgres/common.py,sha256=i1o6PcZxhamo7EK0CWFP9AqE_EmaA7jzHA2TV6bBjKs,1399
156
- velocity/db/tests/postgres/conftest.py,sha256=OiQH_haNA-zq2Gix90uxz4fnNfrouAeVxjRpclRCWTQ,687
155
+ velocity/db/tests/postgres/common.py,sha256=VtEP0goUWvZ4pcaSg8TskcoQencNJN7P_BEhF3Hrbzg,1377
156
+ velocity/db/tests/postgres/conftest.py,sha256=Azgl-wUkfxmunmPE4dRiFj6HyhyEwuNo6taLDExhpiM,668
157
157
  velocity/db/tests/postgres/test_column.py,sha256=JMIVwZggKbHzLQaB8Ca7Cnt2Q6T1bFwjuiuWxnyxlSU,770
158
158
  velocity/db/tests/postgres/test_connections.py,sha256=Uowjk2Q2BuwyvQ4suZGTMKgIyl5qdMD3P7YOdDoucUc,631
159
159
  velocity/db/tests/postgres/test_database.py,sha256=W5xTQGUUotVakG0t47TjzqOxdZYIEZclM20ZxurtDHY,484
@@ -164,7 +164,7 @@ velocity/db/tests/postgres/test_integrity_triggers.py,sha256=MT_Ia5xAWLE9iGUPyFW
164
164
  velocity/db/tests/postgres/test_result.py,sha256=hD-NyHDBimNAztbOo8SBUu-KOLtHoLQ_0bVJpwnWUCc,461
165
165
  velocity/db/tests/postgres/test_row.py,sha256=cp7zuBiiXvhB-1ZL-gVphKiszvIASHNF7fauDc_YL9o,4518
166
166
  velocity/db/tests/postgres/test_row_comprehensive.py,sha256=O7PJqBjSaEFk5HpXhRPXrxTnfBBqXP8Rd_fEdHLKIyg,24653
167
- velocity/db/tests/postgres/test_schema_locking.py,sha256=hKn1jZeEHyEF4gVhIWp4bZwHfkJXwsS_u3SjmUExdAE,22786
167
+ velocity/db/tests/postgres/test_schema_locking.py,sha256=3OA4TyqJ8pmo6gBWaTj1OHIEl4ewpc2kdtCK4eN9V0A,22764
168
168
  velocity/db/tests/postgres/test_schema_locking_unit.py,sha256=vDll84F1gAJHKEPOwDc4WEpLTuOKU2tfliEY3HkUkIg,3710
169
169
  velocity/db/tests/postgres/test_sequence.py,sha256=5lYQasSkTmPvw3scLbkabr-4d2IKOqQ3-dUi_-l2mPQ,664
170
170
  velocity/db/tests/postgres/test_sql_comprehensive.py,sha256=ZuxKNDKmolysmOHU7l3s5p7AvcA450nxVCBK1zQBedY,19028
@@ -254,9 +254,9 @@ velocity/workflow/points.py,sha256=g_vQ3cdHdx_JloVCS5dVjo_jJoSolO92K5p3suDzxlE,1
254
254
  velocity/workflow/schema.py,sha256=1ZAkGZ_4YrTAfY6TIPCviWQJGLhhmAJmkxisfLyy2og,21597
255
255
  velocity/workflow/traveler.py,sha256=Ik1InoEo69sZB1wVV-UinVo4P0P5QlcV_hh858p3OWE,20562
256
256
  velocity/workflow/views.py,sha256=oTPjpuheND8y4VbcTY8c-4fMdSHKb0jgGzXMuT_G7_s,32175
257
- velocity_python-0.9.3.dist-info/licenses/LICENSE,sha256=aoN245GG8s9oRUU89KNiGTU4_4OtnNmVi4hQeChg6rM,1076
258
- velocity_python-0.9.3.dist-info/METADATA,sha256=_nZU1cbMUQt43EI7z-lauEw_BrCiIRMOqU3dUUnfYIM,17789
259
- velocity_python-0.9.3.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
260
- velocity_python-0.9.3.dist-info/entry_points.txt,sha256=nXUKJHkOGQCSPoagdmMNd_qycrle1LVU-GhYknLGM1M,56
261
- velocity_python-0.9.3.dist-info/top_level.txt,sha256=JW2vJPmodgdgSz7H6yoZvnxF8S3fTMIv-YJWCT1sNW0,9
262
- velocity_python-0.9.3.dist-info/RECORD,,
257
+ velocity_python-0.9.5.dist-info/licenses/LICENSE,sha256=aoN245GG8s9oRUU89KNiGTU4_4OtnNmVi4hQeChg6rM,1076
258
+ velocity_python-0.9.5.dist-info/METADATA,sha256=chY03lP1fZGDziIoOfldiQr7vXntxyTYUvTdysmn--Q,17789
259
+ velocity_python-0.9.5.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
260
+ velocity_python-0.9.5.dist-info/entry_points.txt,sha256=nXUKJHkOGQCSPoagdmMNd_qycrle1LVU-GhYknLGM1M,56
261
+ velocity_python-0.9.5.dist-info/top_level.txt,sha256=JW2vJPmodgdgSz7H6yoZvnxF8S3fTMIv-YJWCT1sNW0,9
262
+ velocity_python-0.9.5.dist-info/RECORD,,