Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.
Python awsaws-emulatoraws-localaws-sdkdevtoolsdockerdynamodbec2emulatorlambdalocalstacklocalstack-alternativeministackmock-awsopen-sourcepythons3sqsterraform
fix(s3): mint the MRAP alias from [a-z0-9] as S3 does (#1652)
iot-rocket909c78eb22c7
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 30c9e7a7..364cdc9f 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -9,6 +9,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).### Fixed- **CloudFormation — `Fn::Cidr`, `Fn::GetAZs`, `Fn::FindInMap` and the condition functions follow AWS** — `Fn::Cidr` answered `10.0.{i}.0/…` whatever block it was given (now the block is split: `192.168.0.0/16, 2, 8` is `192.168.0.0/24,192.168.1.0/24`, measured; IPv6 included), `Fn::GetAZs` answered a fabricated `a/b/c` for any region (now the stack's own zones, and an empty list for another region, measured), `Fn::FindInMap` answered `""` for a missing key and had no `DefaultValue` (now the default when given, otherwise `Template error: Unable to get mapping for M::x::y` before the stack exists, measured), and `Fn::And`/`Fn::Or`/`Fn::Not` in a value position leaked the unresolved call (now a boolean; as an Output value they are refused with `The Value field of every Outputs member must evaluate to a String.`, measured). Contributed by @iot-rocket.+- **S3 — the Multi-Region Access Point alias starts with a letter and follows the documented pattern** — the alias was the first 13 hex characters of a uuid, so it could come out as digits only (`1240729989764.mrap`), which is not what S3 mints (`mfzwi23gnjvgw.mrap`) and made `test_cfn_s3_multi_region_access_point` fail whenever no letter was drawn (`str.islower()` is False without a cased character). The alias now follows the documented pattern `^[a-z][a-z0-9]*[.]mrap$`: a letter, then twelve lowercase letters or digits; the test checks that pattern. Contributed by @iot-rocket.### Added- **CloudFormation — `ListImports`, `UpdateTerminationProtection`, `SetStackPolicy`, `GetStackPolicy`, `CancelUpdateStack` and `ContinueUpdateRollback`** — the first four were routed to the service and answered `InvalidAction`, the last two did not exist, so a stack in `UPDATE_ROLLBACK_FAILED` had no way out short of a reset. `ListImports` names the stacks importing an export (a `ValidationError` when none does); termination protection is set on create or later and makes `DeleteStack` refuse with the stack unchanged (`DescribeStacks` reports `EnableTerminationProtection`); a stack policy is stored and returned (it is not enforced); `CancelUpdateStack` stops an `UPDATE_IN_PROGRESS` update before its next resource and rolls it back ("User Initiated"); `ContinueUpdateRollback` retries the deletes that failed the rollback, skips the `ResourcesToSkip` (their status becomes `UPDATE_COMPLETE`, the resource stays) and lands the stack in `UPDATE_ROLLBACK_COMPLETE`. Contributed by @iot-rocket.### Fixeddiff --git a/ministack/services/s3.py b/ministack/services/s3.pyindex fc3d363d..f1f5a586 100644--- a/ministack/services/s3.py+++ b/ministack/services/s3.py@@ -32,8 +32,10 @@ import hashlibimport jsonimport loggingimport os+import randomimport reimport shutil+import stringimport structimport threadingimport time@@ -91,11 +93,15 @@ _mraps = AccountScopedDict() # Alias -> {Name, Alias, Regions: [bucket, ...], Cdef new_mrap_alias() -> str:- """The alias S3 mints for a Multi-Region Access Point: a 13-character- string suffixed with ``.mrap`` (e.g. ``mfzwi23gnjvgw.mrap``). The suffix is- part of the alias itself — GetAtt/GetMultiRegionAccessPoint return it, and- the hostname is ``<alias>.accesspoint.s3-global.amazonaws.com``."""- return new_uuid().replace("-", "")[:13].lower() + ".mrap"+ """The alias S3 mints for a Multi-Region Access Point: a letter followed+ by twelve lowercase letters or digits, suffixed with ``.mrap`` (e.g.+ ``mfzwi23gnjvgw.mrap``; the documented pattern is+ ``^[a-z][a-z0-9]*[.]mrap$``). The suffix is part of the alias itself —+ GetAtt/GetMultiRegionAccessPoint return it, and the hostname is+ ``<alias>.accesspoint.s3-global.amazonaws.com``."""+ alphabet = string.ascii_lowercase + string.digits+ base = random.choice(string.ascii_lowercase) + "".join(random.choices(alphabet, k=12))+ return base + ".mrap"def resolve_mrap_bucket(alias: str):diff --git a/tests/test_cfn.py b/tests/test_cfn.pyindex 727cce2f..1ec7c448 100644--- a/tests/test_cfn.py+++ b/tests/test_cfn.py@@ -7023,10 +7023,11 @@ def test_cfn_s3_multi_region_access_point(cfn, s3):assert stack["StackStatus"] == "CREATE_COMPLETE"alias = {o["OutputKey"]: o["OutputValue"] for o in stack["Outputs"]}["Alias"]- # The alias S3 mints ends in ".mrap" (e.g. mfzwi23gnjvgw.mrap); templates- # build "<alias>.accesspoint.s3-global.amazonaws.com" from it.+ # The alias S3 mints ends in ".mrap" (e.g. mfzwi23gnjvgw.mrap; documented+ # pattern ^[a-z][a-z0-9]*[.]mrap$); templates build+ # "<alias>.accesspoint.s3-global.amazonaws.com" from it.base, _, suffix = alias.rpartition(".")- assert suffix == "mrap" and len(base) == 13 and base.islower()+ assert suffix == "mrap" and re.fullmatch(r"[a-z][a-z0-9]{12}", base)cfn.delete_stack(StackName="cfn-s3-mrap")_wait_stack(cfn, "cfn-s3-mrap")@@ -7037,6 +7038,17 @@ def test_cfn_s3_multi_region_access_point(cfn, s3):assert ei.value.code in (403, 404)+def test_s3_mrap_alias_matches_the_documented_pattern():+ # In-process: the generator alone. A digit-only draw was possible before+ # (uuid hex prefix), which is outside ^[a-z][a-z0-9]*[.]mrap$.+ from ministack.services.s3 import new_mrap_alias++ pattern = re.compile(r"^[a-z][a-z0-9]{12}\.mrap$")+ for _ in range(200):+ alias = new_mrap_alias()+ assert pattern.fullmatch(alias), alias++def test_cfn_auto_named_s3_bucket_stable_across_updates(cfn, s3):"""Regression: auto-named S3 buckets (no explicit BucketName) must keepthe same physical resource ID across stack updates. Before the fix,