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
feat(cloudfront): add SaaS Manager (multi-tenant distributions) (#1462)
Matt Davidsonf1bdbd8c2c3b
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 63669964..4044a9e2 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -12,6 +12,7 @@ Versioning follows [Semantic Versioning](https://semver.org/).- **DynamoDB — an `UpdateExpression` alias is resolved before the key-attribute check** — `set #pk = :pk` with `#pk` mapped to a non-key attribute was refused with `Cannot update attribute pk. This attribute is part of the key`, because the check matched the key name as raw text inside the `#pk` alias and the `:pk` value reference. It now reads the alias-resolved attribute roots the update evaluator already reports, so only a path that resolves to the partition or sort key is rejected. Reported by @ppettitau.- **RDS — `DescribeDBInstances` honors SDK `Filters`** — AWS Query clients serialize filters as `Filters.Filter.N` with `Values.Value.N`, but MiniStack only parsed its internal `Filters.member.N` form, so filters such as `db-cluster-id` were ignored and unrelated instances were returned. Both wire forms are now parsed with the existing AND-across-filters and OR-across-values semantics.### Added+- **CloudFront — SaaS Manager (multi-tenant distributions)** — connection groups (`CreateConnectionGroup`, `GetConnectionGroup`, `GetConnectionGroupByRoutingEndpoint`, `UpdateConnectionGroup`, `DeleteConnectionGroup`, `ListConnectionGroups`) and distribution tenants (`CreateDistributionTenant`, `GetDistributionTenant`, `GetDistributionTenantByDomain`, `UpdateDistributionTenant`, `DeleteDistributionTenant`, `ListDistributionTenants`, `ListDistributionTenantsByCustomization`) with ETag / `If-Match` optimistic concurrency, plus per-tenant WAF association (`AssociateDistributionTenantWebACL`, `DisassociateDistributionTenantWebACL`), tenant invalidations (`CreateInvalidationForDistributionTenant`, `GetInvalidationForDistributionTenant`, `ListInvalidationsForDistributionTenant`), domain tooling (`VerifyDnsConfiguration`, `GetManagedCertificateDetails`, `ListDomainConflicts`, `UpdateDomainAssociation`), and `ListDistributionsByConnectionMode`. `DistributionConfig` round-trips `ConnectionMode` / `TenantConfig` and `DistributionSummary` carries `ConnectionMode`; tenants require a `tenant-only` distribution (`InvalidAssociation`), CNAMEs are unique across tenants and distribution aliases (`CNAMEAlreadyExists`), a tenant created without a connection group lazily gets the account default group, deleting a distribution or connection group that still has tenants is refused (`ResourceInUse` / `CannotDeleteEntityWhileInUse`), and both new resource families are taggable (native CloudFront tagging and the Resource Groups Tagging API). MiniStack collapses async workflows: tenants and connection groups deploy, domains activate, DNS verifies, and managed certificates issue immediately.- **EC2 — instances can have a real box behind them (`RegisterImage`)** — `RunInstances` returned a `running` record and booted nothing. `RegisterImage` now takes a container reference in `ImageLocation` and returns an `ami-` id; launching that id boots the image as a container whose IP becomes the instance's address, so `ssm:SendCommand` can report a real exit code. Such an AMI is `instance-store` backed: no EBS root volume, `StopInstances` / `StartInstances` answer `UnsupportedOperation`, and a container that dies reports `terminated`. Snapshot-backed registration is accepted as AWS accepts it and fails at `RunInstances` with `InvalidAMIID.Unavailable`, as does a reference that cannot be pulled. Registering is the only opt-in — with nothing registered EC2 never reaches for Docker. Reported by @iot-rocket.- **SSM — Run Command** — `SendCommand`, `GetCommandInvocation`, `ListCommands` and `DescribeInstanceInformation` returned `InvalidAction`, so the standard instance health probe could not run. Invocations are asynchronous as on AWS: `SendCommand` answers `Pending` and the caller polls to a terminal state. `AWS-RunShellScript` runs in the instance's container, so `Status` is `Success` or `Failed` on the real exit code, with the script's output. An instance with no agent answering is refused with `InvalidInstanceId`. Contributed by @bandle.- **EC2 — `RebootInstances` rejects unknown instance ids** — it returned `true` for any id. It now answers `InvalidInstanceID.NotFound`, matching AWS.diff --git a/README.md b/README.mdindex 3707482f..2439c203 100644--- a/README.md+++ b/README.md@@ -434,7 +434,7 @@ subnet = ec2.create_subnet(| **API Gateway v1** | CreateRestApi, GetRestApi, GetRestApis, UpdateRestApi, DeleteRestApi, CreateResource, GetResource, GetResources, UpdateResource, DeleteResource, PutMethod, GetMethod, DeleteMethod, UpdateMethod, PutMethodResponse, GetMethodResponse, DeleteMethodResponse, PutIntegration, GetIntegration, DeleteIntegration, UpdateIntegration, PutIntegrationResponse, GetIntegrationResponse, DeleteIntegrationResponse, CreateDeployment, GetDeployment, GetDeployments, UpdateDeployment, DeleteDeployment, CreateStage, GetStage, GetStages, UpdateStage, DeleteStage, CreateAuthorizer, GetAuthorizer, GetAuthorizers, UpdateAuthorizer, DeleteAuthorizer, CreateModel, GetModel, GetModels, DeleteModel, CreateDocumentationPart, GetDocumentationPart, GetDocumentationParts, UpdateDocumentationPart, DeleteDocumentationPart, CreateApiKey, GetApiKey, GetApiKeys, UpdateApiKey, DeleteApiKey, CreateUsagePlan, GetUsagePlan, GetUsagePlans, UpdateUsagePlan, DeleteUsagePlan, CreateUsagePlanKey, GetUsagePlanKeys, DeleteUsagePlanKey, CreateDomainName, GetDomainName, GetDomainNames, DeleteDomainName, CreateBasePathMapping, GetBasePathMapping, GetBasePathMappings, DeleteBasePathMapping, TagResource, UntagResource, GetTags | REST API (v1) protocol; Lambda proxy format 1.0 (AWS_PROXY), non-proxy Lambda (AWS — the handler's output is the response body, mapping templates not modeled), HTTP proxy (HTTP_PROXY), MOCK integration; data plane via `{apiId}.execute-api.localhost` Host header, path-based `/_aws/execute-api/{apiId}/{stage}/{path}`, or legacy `/restapis/{apiId}/{stage}/_user_request_/{path}`; qualified-alias integration URIs (`arn:...:function:<name>:<alias>`) resolve to the alias's target version; resource tree with `{param}` and `{proxy+}` path matching; routing is on the resource+method pair, so a request the matched resource does not serve falls through to a `{proxy+}` elsewhere in the tree (a methodless intermediate, a CORS-preflight-only node, or any undeclared verb), as it does on AWS; TOKEN and REQUEST Lambda authorizers on the data plane, with the returned IAM policy evaluated against each request's own method ARN and `authorizerResultTtlInSeconds` caching the policy per stage and identity source; JSON Patch for all PATCH operations; state persistence; pin `id` across runs with the `ms-custom-id` tag || **ELBv2 / ALB** | CreateLoadBalancer, DescribeLoadBalancers, DeleteLoadBalancer, DescribeLoadBalancerAttributes, ModifyLoadBalancerAttributes, CreateTargetGroup, DescribeTargetGroups, ModifyTargetGroup, DeleteTargetGroup, DescribeTargetGroupAttributes, ModifyTargetGroupAttributes, CreateListener, DescribeListeners, ModifyListener, DeleteListener, CreateRule, DescribeRules, ModifyRule, DeleteRule, SetRulePriorities, SetSubnets, SetIpAddressType, SetSecurityGroups, RegisterTargets, DeregisterTargets, DescribeTargetHealth, AddTags, RemoveTags, DescribeTags | Control plane + data plane; ALB→Lambda live traffic routing; `path-pattern`, `host-header`, `http-method`, `query-string`, `http-header` rule conditions; `forward`, `redirect`, `fixed-response` actions; data plane via `{lb-name}.alb.localhost` Host header or `/_alb/{lb-name}/` path prefix || **KMS** | CreateKey, ListKeys, DescribeKey, GetPublicKey, Sign, Verify, Encrypt, Decrypt, GenerateMac, VerifyMac, GenerateDataKey, GenerateDataKeyWithoutPlaintext, GenerateDataKeyPair, GenerateDataKeyPairWithoutPlaintext, CreateAlias, DeleteAlias, ListAliases, UpdateAlias, UpdateKeyDescription, EnableKeyRotation, DisableKeyRotation, GetKeyRotationStatus, GetKeyPolicy, PutKeyPolicy, ListKeyPolicies, EnableKey, DisableKey, ScheduleKeyDeletion, CancelKeyDeletion, TagResource, UntagResource, ListResourceTags | 32 actions; CMK specs: RSA (2048/3072/4096), ECC (SECG_P256K1, NIST P-256/384/521), Edwards25519 (`ECC_NIST_EDWARDS25519` — pure Ed25519 via `ED25519_SHA_512` with `MessageType=RAW`; `ED25519_PH_SHA_512` advertised in `SigningAlgorithms` but returns `UnsupportedOperationException` until Ed25519ph lands), HMAC (`HMAC_224`/`HMAC_256`/`HMAC_384`/`HMAC_512` with `KeyUsage=GENERATE_VERIFY_MAC`), and symmetric keys; PKCS1v15, PSS, and ECDSA signing; real RFC 2104 HMAC via `GenerateMac`/`VerifyMac` (4096-byte message cap, `KMSInvalidMacException` on mismatch, `DryRun` supported); envelope encryption, incl. asymmetric data key pairs (`KeyPairSpec` adds RSA_3072); alias resolution; key rotation; key policies; tags; enable/disable/schedule deletion; full Terraform `aws_kms_key` compatible; `cryptography` package included in Docker image |-| **CloudFront** | CreateDistribution, GetDistribution, GetDistributionConfig, ListDistributions, UpdateDistribution, DeleteDistribution, CreateInvalidation, ListInvalidations, GetInvalidation, CreateOriginAccessControl, GetOriginAccessControl, GetOriginAccessControlConfig, ListOriginAccessControls, UpdateOriginAccessControl, DeleteOriginAccessControl, CreateFunction, DeleteFunction, DescribeFunction, GetFunction, ListFunctions, PublishFunction, UpdateFunction, **KeyValueStore (mgmt)**: CreateKeyValueStore, DescribeKeyValueStore, ListKeyValueStores, UpdateKeyValueStore, DeleteKeyValueStore, **Policies**: CreateCachePolicy, GetCachePolicy, GetCachePolicyConfig, UpdateCachePolicy, DeleteCachePolicy, ListDistributionsByCachePolicyId, CreateOriginRequestPolicy, GetOriginRequestPolicy, GetOriginRequestPolicyConfig, UpdateOriginRequestPolicy, DeleteOriginRequestPolicy, ListDistributionsByOriginRequestPolicyId, CreateResponseHeadersPolicy, GetResponseHeadersPolicy, GetResponseHeadersPolicyConfig, UpdateResponseHeadersPolicy, DeleteResponseHeadersPolicy, ListDistributionsByResponseHeadersPolicyId, TagResource, UntagResource, ListTagsForResource | REST/XML API; ETag-based optimistic concurrency; Origin config round-trip; cache / origin request / response headers policies round-trip their full config with `ETag` / `If-Match` optimistic concurrency; Origin Access Control (OAC) with SigV4 signing for S3, MediaStore, Lambda, MediaPackageV2 origins; **CloudFront Functions** are API stubs (in-memory code + DEVELOPMENT/LIVE ETags for Terraform `aws_cloudfront_function`) with `KeyValueStoreAssociations` round-tripped — no `TestFunction`, no viewer-request JS execution at the edge; `UpdateFunction` clears the emulated LIVE stage until `PublishFunction` runs again |+| **CloudFront** | CreateDistribution, GetDistribution, GetDistributionConfig, ListDistributions, UpdateDistribution, DeleteDistribution, CreateInvalidation, ListInvalidations, GetInvalidation, CreateOriginAccessControl, GetOriginAccessControl, GetOriginAccessControlConfig, ListOriginAccessControls, UpdateOriginAccessControl, DeleteOriginAccessControl, CreateFunction, DeleteFunction, DescribeFunction, GetFunction, ListFunctions, PublishFunction, UpdateFunction, **KeyValueStore (mgmt)**: CreateKeyValueStore, DescribeKeyValueStore, ListKeyValueStores, UpdateKeyValueStore, DeleteKeyValueStore, **Policies**: CreateCachePolicy, GetCachePolicy, GetCachePolicyConfig, UpdateCachePolicy, DeleteCachePolicy, ListDistributionsByCachePolicyId, CreateOriginRequestPolicy, GetOriginRequestPolicy, GetOriginRequestPolicyConfig, UpdateOriginRequestPolicy, DeleteOriginRequestPolicy, ListDistributionsByOriginRequestPolicyId, CreateResponseHeadersPolicy, GetResponseHeadersPolicy, GetResponseHeadersPolicyConfig, UpdateResponseHeadersPolicy, DeleteResponseHeadersPolicy, ListDistributionsByResponseHeadersPolicyId, **SaaS Manager**: CreateConnectionGroup, GetConnectionGroup, GetConnectionGroupByRoutingEndpoint, UpdateConnectionGroup, DeleteConnectionGroup, ListConnectionGroups, CreateDistributionTenant, GetDistributionTenant, GetDistributionTenantByDomain, UpdateDistributionTenant, DeleteDistributionTenant, ListDistributionTenants, ListDistributionTenantsByCustomization, AssociateDistributionTenantWebACL, DisassociateDistributionTenantWebACL, CreateInvalidationForDistributionTenant, GetInvalidationForDistributionTenant, ListInvalidationsForDistributionTenant, VerifyDnsConfiguration, GetManagedCertificateDetails, ListDomainConflicts, UpdateDomainAssociation, ListDistributionsByConnectionMode, TagResource, UntagResource, ListTagsForResource | REST/XML API; ETag-based optimistic concurrency; Origin config round-trip; cache / origin request / response headers policies round-trip their full config with `ETag` / `If-Match` optimistic concurrency; Origin Access Control (OAC) with SigV4 signing for S3, MediaStore, Lambda, MediaPackageV2 origins; **CloudFront Functions** are API stubs (in-memory code + DEVELOPMENT/LIVE ETags for Terraform `aws_cloudfront_function`) with `KeyValueStoreAssociations` round-tripped — no `TestFunction`, no viewer-request JS execution at the edge; `UpdateFunction` clears the emulated LIVE stage until `PublishFunction` runs again; **SaaS Manager** multi-tenant distributions: `DistributionConfig` round-trips `ConnectionMode` / `TenantConfig`, tenants require a `tenant-only` distribution, CNAMEs are unique across tenants and distribution aliases, a tenant without an explicit connection group lazily gets the account default group, and MiniStack collapses async workflows (tenants deploy, domains activate, DNS verifies, managed certificates issue immediately) || **CloudFront KeyValueStore (data plane)** | DescribeKeyValueStore, ListKeys, GetKey, PutKey, DeleteKey, UpdateKeys | Separate `cloudfront-keyvaluestore` SDK service (REST/JSON, signing name `cloudfront-keyvaluestore`); ETag-based optimistic concurrency on every mutating op; `UpdateKeys` is atomic (validates whole batch, rejects on first invalid entry); `ListKeys` paginates with opaque `NextToken` capped at 50 results per AWS spec; bridges to the management plane via the shared KVS ARN || **CloudTrail** | LookupEvents, CreateTrail, DeleteTrail, GetTrail, DescribeTrails, ListTrails, UpdateTrail, GetTrailStatus, StartLogging, StopLogging, PutEventSelectors, GetEventSelectors, AddTags, ListTags, RemoveTags | In-memory audit log; recording opt-in via `CLOUDTRAIL_RECORDING=1` (or runtime config endpoint); per-account ring buffer (`CLOUDTRAIL_MAX_EVENTS=10000`); `LookupEvents` supports all 8 AWS `LookupAttributes`; `IsLogging` flips on Start/StopLogging; `CreateTrail`/`UpdateTrail` persist `KmsKeyId` (normalized to a key ARN; omitted when unset) so `aws_cloudtrail` converges without drift || **Resource Groups** | CreateGroup, GetGroup, DeleteGroup, UpdateGroup, ListGroups, GetGroupQuery, UpdateGroupQuery, GetGroupConfiguration, PutGroupConfiguration, GroupResources, UngroupResources, ListGroupResources, ListGroupingStatuses, SearchResources, Tag, Untag, GetTags, GetAccountSettings, UpdateAccountSettings | 19 of 23 spec ops; tag-sync ops omitted (not exposed by AWS CLI / Terraform); `Group` accepts bare name or full ARN |diff --git a/ministack/services/cloudfront.py b/ministack/services/cloudfront.pyindex 018c37ad..d5959dce 100644--- a/ministack/services/cloudfront.py+++ b/ministack/services/cloudfront.py@@ -24,6 +24,20 @@ Supports:GetResponseHeadersPolicyConfig, UpdateResponseHeadersPolicy,DeleteResponseHeadersPolicy, ListDistributionsByResponseHeadersPolicyIdTags: TagResource, UntagResource, ListTagsForResource+ SaaS Manager (multi-tenant distributions):+ CreateConnectionGroup, GetConnectionGroup,+ GetConnectionGroupByRoutingEndpoint, UpdateConnectionGroup,+ DeleteConnectionGroup, ListConnectionGroups,+ CreateDistributionTenant, GetDistributionTenant,+ GetDistributionTenantByDomain, UpdateDistributionTenant,+ DeleteDistributionTenant, ListDistributionTenants,+ ListDistributionTenantsByCustomization,+ AssociateDistributionTenantWebACL, DisassociateDistributionTenantWebACL,+ CreateInvalidationForDistributionTenant,+ GetInvalidationForDistributionTenant, ListInvalidationsForDistributionTenant,+ VerifyDnsConfiguration, GetManagedCertificateDetails,+ ListDomainConflicts, UpdateDomainAssociation,+ ListDistributionsByConnectionMode"""import base64@@ -34,6 +48,7 @@ import randomimport reimport stringfrom datetime import datetime, timezone+from urllib.parse import unquotefrom xml.etree.ElementTree import Element, SubElement, tostringfrom defusedxml.ElementTree import fromstring@@ -84,6 +99,27 @@ _RHP_CFG_RE = re.compile(r"^/2020-05-31/response-headers-policy/([^/]+)/config$"_RHP_ID_RE = re.compile(r"^/2020-05-31/response-headers-policy/([^/]+)/?$")_DIST_BY_RHP_RE = re.compile(r"^/2020-05-31/distributionsByResponseHeadersPolicyId/([^/]+)/?$")+# SaaS Manager path regexes. Get* identifiers may be an ARN — the ASGI layer+# hands us the percent-decoded path, so an ARN's embedded "/" lands in the+# identifier segment. The identifier regexes are greedy and MUST be matched+# after the tenant sub-resource regexes (web-acl, invalidation).+_CONN_GROUP_RE = re.compile(r"^/2020-05-31/connection-group/?$")+_CONN_GROUP_ID_RE = re.compile(r"^/2020-05-31/connection-group/(.+?)/?$")+_CONN_GROUPS_LIST_RE = re.compile(r"^/2020-05-31/connection-groups/?$")+_TENANT_RE = re.compile(r"^/2020-05-31/distribution-tenant/?$")+_TENANT_WEBACL_ASSOC_RE = re.compile(r"^/2020-05-31/distribution-tenant/([^/]+)/associate-web-acl/?$")+_TENANT_WEBACL_DISASSOC_RE = re.compile(r"^/2020-05-31/distribution-tenant/([^/]+)/disassociate-web-acl/?$")+_TENANT_INV_RE = re.compile(r"^/2020-05-31/distribution-tenant/([^/]+)/invalidation/?$")+_TENANT_INV_ID_RE = re.compile(r"^/2020-05-31/distribution-tenant/([^/]+)/invalidation/([^/]+)$")+_TENANT_ID_RE = re.compile(r"^/2020-05-31/distribution-tenant/(.+?)/?$")+_TENANTS_LIST_RE = re.compile(r"^/2020-05-31/distribution-tenants/?$")+_TENANTS_BY_CUSTOMIZATION_RE = re.compile(r"^/2020-05-31/distribution-tenants-by-customization/?$")+_MANAGED_CERT_RE = re.compile(r"^/2020-05-31/managed-certificate/(.+?)/?$")+_VERIFY_DNS_RE = re.compile(r"^/2020-05-31/verify-dns-configuration/?$")+_DOMAIN_CONFLICTS_RE = re.compile(r"^/2020-05-31/domain-conflicts/?$")+_DOMAIN_ASSOCIATION_RE = re.compile(r"^/2020-05-31/domain-association/?$")+_DIST_BY_CONN_MODE_RE = re.compile(r"^/2020-05-31/distributionsByConnectionMode/([^/]+)/?$")+# ---------------------------------------------------------------------------# Read-only surface for resource families MiniStack does not yet persist.# These return AWS-shaped empty collections so the SDK gets a valid response@@ -116,6 +152,9 @@ _kvstores = AccountScopedDict() # Name -> KVS record_cache_policies = AccountScopedDict() # Id -> cache policy record_origin_request_policies = AccountScopedDict() # Id -> origin request policy record_response_headers_policies = AccountScopedDict() # Id -> response headers policy record+_connection_groups = AccountScopedDict() # Id -> connection group record (SaaS Manager)+_distribution_tenants = AccountScopedDict() # Id -> distribution tenant record (SaaS Manager)+_tenant_invalidations = AccountScopedDict() # tenant_id -> [invalidation record, ...]def reset():@@ -128,6 +167,9 @@ def reset():_cache_policies.clear()_origin_request_policies.clear()_response_headers_policies.clear()+ _connection_groups.clear()+ _distribution_tenants.clear()+ _tenant_invalidations.clear()def get_state():@@ -142,6 +184,9 @@ def get_state():"cache_policies": _cache_policies,"origin_request_policies": _origin_request_policies,"response_headers_policies": _response_headers_policies,+ "connection_groups": _connection_groups,+ "distribution_tenants": _distribution_tenants,+ "tenant_invalidations": _tenant_invalidations,})@@ -156,6 +201,9 @@ def restore_state(data):_cache_policies.update(data.get("cache_policies", {}))_origin_request_policies.update(data.get("origin_request_policies", {}))_response_headers_policies.update(data.get("response_headers_policies", {}))+ _connection_groups.update(data.get("connection_groups", {}))+ _distribution_tenants.update(data.get("distribution_tenants", {}))+ _tenant_invalidations.update(data.get("tenant_invalidations", {}))try:@@ -182,6 +230,22 @@ def _inv_id() -> str:return "I" + "".join(random.choices(_ID_CHARS, k=13))+# Real SaaS Manager resource IDs are dt_/cg_-prefixed KSUIDs (27 base62 chars).+_KSUID_CHARS = string.ascii_letters + string.digits+++def _tenant_id() -> str:+ return "dt_" + "".join(random.choices(_KSUID_CHARS, k=27))+++def _conn_group_id() -> str:+ return "cg_" + "".join(random.choices(_KSUID_CHARS, k=27))+++def _routing_endpoint() -> str:+ return "d" + "".join(random.choices(string.ascii_lowercase + string.digits, k=13)) + ".cloudfront.net"++def _now_iso() -> str:return datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.000Z")@@ -453,6 +517,8 @@ def _resolve_taggable_cloudfront_arn(arn: str):"distribution": (_distributions, "NoSuchDistribution", "The specified distribution does not exist.", "ARN"),"function": (_functions, "NoSuchFunctionExists", "The specified function does not exist.", "arn"),"key-value-store": (_kvstores, "EntityNotFound", f"The key value store {name} was not found.", "ARN"),+ "distribution-tenant": (_distribution_tenants, "EntityNotFound", "The distribution tenant was not found.", "Arn"),+ "connection-group": (_connection_groups, "EntityNotFound", "The connection group was not found.", "Arn"),}entry = resources.get(resource_type)if not entry:@@ -1751,6 +1817,104 @@ async def handle_request(method, path, headers, body, query_params):if method == "GET":return _list_kvstores(query_params)+ # SaaS Manager routes. Tenant sub-resource routes must be matched before+ # the greedy _TENANT_ID_RE / _CONN_GROUP_ID_RE identifier routes.+ m = _TENANT_WEBACL_ASSOC_RE.match(path)+ if m:+ if method == "PUT":+ return _associate_tenant_webacl(m.group(1), headers, body)++ m = _TENANT_WEBACL_DISASSOC_RE.match(path)+ if m:+ if method == "PUT":+ return _disassociate_tenant_webacl(m.group(1), headers)++ m = _TENANT_INV_ID_RE.match(path)+ if m:+ if method == "GET":+ return _get_tenant_invalidation(m.group(1), m.group(2))++ m = _TENANT_INV_RE.match(path)+ if m:+ if method == "POST":+ return _create_tenant_invalidation(m.group(1), body)+ if method == "GET":+ return _list_tenant_invalidations(m.group(1))++ m = _TENANT_RE.match(path)+ if m:+ if method == "POST":+ return _create_distribution_tenant(body)+ if method == "GET":+ return _get_distribution_tenant_by_domain(query_params)++ m = _TENANTS_BY_CUSTOMIZATION_RE.match(path)+ if m:+ if method == "POST":+ return _list_distribution_tenants_by_customization(body)++ m = _TENANTS_LIST_RE.match(path)+ if m:+ if method == "POST":+ return _list_distribution_tenants(body)++ m = _TENANT_ID_RE.match(path)+ if m:+ identifier = m.group(1)+ if method == "GET":+ return _get_distribution_tenant(identifier)+ if method == "PUT":+ return _update_distribution_tenant(identifier, headers, body)+ if method == "DELETE":+ return _delete_distribution_tenant(identifier, headers)++ m = _CONN_GROUP_RE.match(path)+ if m:+ if method == "POST":+ return _create_connection_group(body)+ if method == "GET":+ return _get_connection_group_by_routing_endpoint(query_params)++ m = _CONN_GROUPS_LIST_RE.match(path)+ if m:+ if method == "POST":+ return _list_connection_groups(body)++ m = _CONN_GROUP_ID_RE.match(path)+ if m:+ identifier = m.group(1)+ if method == "GET":+ return _get_connection_group(identifier)+ if method == "PUT":+ return _update_connection_group(identifier, headers, body)+ if method == "DELETE":+ return _delete_connection_group(identifier, headers)++ m = _MANAGED_CERT_RE.match(path)+ if m:+ if method == "GET":+ return _get_managed_certificate_details(m.group(1))++ m = _VERIFY_DNS_RE.match(path)+ if m:+ if method == "POST":+ return _verify_dns_configuration(body)++ m = _DOMAIN_CONFLICTS_RE.match(path)+ if m:+ if method == "POST":+ return _list_domain_conflicts(body)++ m = _DOMAIN_ASSOCIATION_RE.match(path)+ if m:+ if method == "POST":+ return _update_domain_association(headers, body)++ m = _DIST_BY_CONN_MODE_RE.match(path)+ if m:+ if method == "GET":+ return _list_distributions_by_connection_mode(m.group(1))+# Read-only list surface for resource families with no backing store.# Family split matches botocore: KeyGroupList-style carry no Marker/# IsTruncated; the OAI/StreamingDistribution/VpcOrigin family does.@@ -1888,43 +2052,63 @@ def _get_distribution_config(dist_id):return 200, {"Content-Type": "text/xml", "ETag": dist["ETag"]}, body+def _dist_config_el(dist):+ """Parsed DistributionConfig element for a stored distribution.++ CloudFormation-provisioned records carry an empty ``config_xml``; parse+ defensively so account-wide scans never fail on them."""+ xml = dist.get("config_xml")+ if xml:+ try:+ return fromstring(xml)+ except Exception:+ pass+ return Element("DistributionConfig")+++def _dist_connection_mode(dist) -> str:+ """A distribution's ConnectionMode; absent in the stored config means direct."""+ return _text(_dist_config_el(dist), "ConnectionMode") or "direct"+++def _build_distribution_list_xml(root, items):+ SubElement(root, "Marker").text = ""+ SubElement(root, "MaxItems").text = "100"+ SubElement(root, "IsTruncated").text = "false"+ SubElement(root, "Quantity").text = str(len(items))+ if items:+ items_el = SubElement(root, "Items")+ for dist in items:+ ds = SubElement(items_el, "DistributionSummary")+ SubElement(ds, "Id").text = dist["Id"]+ SubElement(ds, "ARN").text = dist["ARN"]+ SubElement(ds, "Status").text = dist["Status"]+ SubElement(ds, "LastModifiedTime").text = dist["LastModifiedTime"]+ SubElement(ds, "DomainName").text = dist["DomainName"]+ config_el = _dist_config_el(dist)+ # Field order matches real AWS DistributionSummary shape so+ # SDKs that strict-parse (Go v2, Java v2) don't reject it.+ # All 19 fields below are REQUIRED per botocore service-2.json.+ _add_config_block_with_default(ds, config_el, "Aliases")+ _add_config_block_with_default(ds, config_el, "Origins")+ _add_config_block_with_default(ds, config_el, "DefaultCacheBehavior")+ _add_config_block_with_default(ds, config_el, "CacheBehaviors")+ _add_config_block_with_default(ds, config_el, "CustomErrorResponses")+ SubElement(ds, "Comment").text = _text(config_el, "Comment") or ""+ SubElement(ds, "PriceClass").text = _text(config_el, "PriceClass") or "PriceClass_All"+ SubElement(ds, "Enabled").text = str(dist["enabled"]).lower()+ _add_config_block_with_default(ds, config_el, "ViewerCertificate")+ _add_config_block_with_default(ds, config_el, "Restrictions")+ SubElement(ds, "WebACLId").text = _text(config_el, "WebACLId") or ""+ SubElement(ds, "HttpVersion").text = _text(config_el, "HttpVersion") or "http2"+ SubElement(ds, "IsIPV6Enabled").text = (_text(config_el, "IsIPV6Enabled") or "true").lower()+ SubElement(ds, "Staging").text = str(dist.get("Staging", False)).lower()+ SubElement(ds, "ConnectionMode").text = _text(config_el, "ConnectionMode") or "direct"++def _list_distributions():items = list(_distributions.values())-- def build(root):- SubElement(root, "Marker").text = ""- SubElement(root, "MaxItems").text = "100"- SubElement(root, "IsTruncated").text = "false"- SubElement(root, "Quantity").text = str(len(items))- if items:- items_el = SubElement(root, "Items")- for dist in items:- ds = SubElement(items_el, "DistributionSummary")- SubElement(ds, "Id").text = dist["Id"]- SubElement(ds, "ARN").text = dist["ARN"]- SubElement(ds, "Status").text = dist["Status"]- SubElement(ds, "LastModifiedTime").text = dist["LastModifiedTime"]- SubElement(ds, "DomainName").text = dist["DomainName"]- config_el = fromstring(dist["config_xml"])- # Field order matches real AWS DistributionSummary shape so- # SDKs that strict-parse (Go v2, Java v2) don't reject it.- # All 19 fields below are REQUIRED per botocore service-2.json.- _add_config_block_with_default(ds, config_el, "Aliases")- _add_config_block_with_default(ds, config_el, "Origins")- _add_config_block_with_default(ds, config_el, "DefaultCacheBehavior")- _add_config_block_with_default(ds, config_el, "CacheBehaviors")- _add_config_block_with_default(ds, config_el, "CustomErrorResponses")- SubElement(ds, "Comment").text = _text(config_el, "Comment") or ""- SubElement(ds, "PriceClass").text = _text(config_el, "PriceClass") or "PriceClass_All"- SubElement(ds, "Enabled").text = str(dist["enabled"]).lower()- _add_config_block_with_default(ds, config_el, "ViewerCertificate")- _add_config_block_with_default(ds, config_el, "Restrictions")- SubElement(ds, "WebACLId").text = _text(config_el, "WebACLId") or ""- SubElement(ds, "HttpVersion").text = _text(config_el, "HttpVersion") or "http2"- SubElement(ds, "IsIPV6Enabled").text = (_text(config_el, "IsIPV6Enabled") or "true").lower()- SubElement(ds, "Staging").text = str(dist.get("Staging", False)).lower()-- return _xml_response("DistributionList", build)+ return _xml_response("DistributionList", lambda root: _build_distribution_list_xml(root, items))def _update_distribution(dist_id, headers, body):@@ -1980,6 +2164,13 @@ def _delete_distribution(dist_id, headers):"DistributionNotDisabled", "The distribution you are trying to delete has not been disabled.", 409)+ if any(t["DistributionId"] == dist_id for t in _distribution_tenants.values()):+ return _error(+ "ResourceInUse",+ "The distribution has distribution tenants associated with it and cannot be deleted.",+ 409,+ )+del _distributions[dist_id]_invalidations.pop(dist_id, None)@@ -2493,3 +2684,1029 @@ def _delete_kvs(name, headers):logger.info("DeleteKeyValueStore name=%s", name)return 204, {}, b""+++# ---------------------------------------------------------------------------+# SaaS Manager — connection groups and distribution tenants.+# Shapes verified against botocore cloudfront service-2.json (2020-05-31):+# result shapes without a payload member use an <OperationNameResult> root;+# payload results use the payload shape name as root. Non-flattened lists+# without a member locationName (Domains, Parameters, ValidationTokenDetails)+# serialize items as <member>; named ones use their locationName.+# MiniStack collapses async workflows: resources deploy, domains activate,+# and managed certificates issue immediately.+# ---------------------------------------------------------------------------++_CONNECTION_MODES = {"direct", "tenant-only"}+_WEBACL_CUSTOMIZATION_ACTIONS = {"override", "disable"}+_VALIDATION_TOKEN_HOSTS = {"cloudfront", "self-hosted"}+++def _tenant_arn(tenant_id: str) -> str:+ return f"arn:aws:cloudfront::{get_account_id()}:distribution-tenant/{tenant_id}"+++def _conn_group_arn(cg_id: str) -> str:+ return f"arn:aws:cloudfront::{get_account_id()}:connection-group/{cg_id}"+++def _find_connection_group(identifier: str):+ """Resolve a connection group by ID, name, or ARN (per AWS docs)."""+ identifier = unquote(identifier or "")+ cg = _connection_groups.get(identifier)+ if cg:+ return cg+ for cg in _connection_groups.values():+ if cg["Name"] == identifier or cg["Arn"] == identifier:+ return cg+ return None+++def _find_distribution_tenant(identifier: str):+ """Resolve a distribution tenant by ID, name, or ARN (per AWS docs)."""+ identifier = unquote(identifier or "")+ tenant = _distribution_tenants.get(identifier)+ if tenant:+ return tenant+ for tenant in _distribution_tenants.values():+ if tenant["Name"] == identifier or tenant["Arn"] == identifier:+ return tenant+ return None+++def _domains_overlap(a: str, b: str) -> bool:+ """Case-insensitive match; a leading ``*.`` wildcard covers exactly one label."""+ a, b = (a or "").lower(), (b or "").lower()+ if a == b:+ return True++ def covers(wild, host):+ if not wild.startswith("*."):+ return False+ suffix = wild[2:]+ return host.endswith("." + suffix) and "." not in host[: -len(suffix) - 1]++ return covers(a, b) or covers(b, a)+++def _domain_owners(domain: str, wildcard_overlap: bool = False):+ """All (resource_type, resource_id) pairs currently claiming ``domain``.++ Scans tenant domains and distribution alias CNAMEs, case-insensitively —+ CloudFront treats CNAMEs as globally unique across both families.+ ``wildcard_overlap`` additionally counts single-level wildcard overlaps+ (ListDomainConflicts semantics).+ """+ d = (domain or "").lower()++ def claims(owned):+ return _domains_overlap(d, owned) if wildcard_overlap else owned.lower() == d++ owners = []+ for tenant in _distribution_tenants.values():+ if any(claims(x) for x in tenant["Domains"]):+ owners.append(("distribution-tenant", tenant["Id"]))+ for dist in _distributions.values():+ if any(claims(x) for x in _get_distribution_aliases(dist)):+ owners.append(("distribution", dist["Id"]))+ return owners+++def _get_distribution_aliases(dist):+ config_el = _dist_config_el(dist)+ aliases = _find(config_el, "Aliases")+ items = []+ if aliases is not None:+ items_el = _find(aliases, "Items")+ if items_el is not None:+ items = [c.text or "" for c in items_el]+ return items+++def _set_distribution_aliases(dist, aliases):+ config_el = _dist_config_el(dist)+ block = _find(config_el, "Aliases")+ if block is not None:+ config_el.remove(block)+ block = SubElement(config_el, "Aliases")+ SubElement(block, "Quantity").text = str(len(aliases))+ if aliases:+ items_el = SubElement(block, "Items")+ for alias in aliases:+ SubElement(items_el, "CNAME").text = alias+ dist["config_xml"] = tostring(config_el, encoding="unicode")+++# ---- request XML parsers ----+++def _parse_tenant_domains(el):+ """Parse <Domains><member><Domain>..</Domain></member></Domains>; None when absent."""+ block = _find(el, "Domains")+ if block is None:+ return None+ domains = []+ for child in block:+ d = _text(child, "Domain")+ if d:+ domains.append(d)+ return domains+++def _parse_tenant_parameters(el):+ block = _find(el, "Parameters")+ if block is None:+ return None+ params = []+ for child in block:+ name = _text(child, "Name")+ if name:+ params.append({"Name": name, "Value": _text(child, "Value")})+ return params+++def _parse_customizations(el):+ """Parse a <Customizations> block into a dict, or return an _error tuple.++ Returns ``(customizations_or_None, error_or_None)``.+ """+ block = _find(el, "Customizations")+ if block is None:+ return None, None+ cust = {}+ webacl = _find(block, "WebAcl")+ if webacl is not None:+ action = _text(webacl, "Action")+ if action not in _WEBACL_CUSTOMIZATION_ACTIONS:+ return None, _error("InvalidArgument", "Invalid WebAcl customization Action value.", 400)+ entry = {"Action": action}+ arn = _opt_text(webacl, "Arn")+ if arn:+ entry["Arn"] = arn+ cust["WebAcl"] = entry+ cert = _find(block, "Certificate")+ if cert is not None:+ arn = _text(cert, "Arn")+ if not arn:+ return None, _error("InvalidArgument", "Certificate customization requires Arn.", 400)+ cust["Certificate"] = {"Arn": arn}+ geo = _find(block, "GeoRestrictions")+ if geo is not None:+ rtype = _text(geo, "RestrictionType")+ if rtype not in {"blacklist", "whitelist", "none"}:+ return None, _error("InvalidArgument", "Invalid GeoRestrictions RestrictionType value.", 400)+ locations_el = _find(geo, "Locations")+ locations = [c.text or "" for c in locations_el] if locations_el is not None else []+ cust["GeoRestrictions"] = {"RestrictionType": rtype, "Locations": locations}+ return cust, None+++def _parse_managed_certificate_request(el, domains):+ """Mint a managed-certificate record from <ManagedCertificateRequest>.++ Returns ``(record_or_None, error_or_None)``. MiniStack issues immediately.+ """+ mcr = _find(el, "ManagedCertificateRequest")+ if mcr is None:+ return None, None+ host = _text(mcr, "ValidationTokenHost")+ if host not in _VALIDATION_TOKEN_HOSTS:+ return None, _error("InvalidArgument", "Invalid ValidationTokenHost value.", 400)+ return {+ "CertificateArn": f"arn:aws:acm:us-east-1:{get_account_id()}:certificate/{new_uuid()}",+ "CertificateStatus": "issued",+ "ValidationTokenHost": host,+ "PrimaryDomainName": _opt_text(mcr, "PrimaryDomainName"),+ "Domains": list(domains),+ }, None+++# ---- response XML builders ----+++def _build_tags_block_xml(parent, arn):+ tags_el = SubElement(parent, "Tags")+ items = SubElement(tags_el, "Items")+ for t in _tags.get(arn, []):+ tag_el = SubElement(items, "Tag")+ SubElement(tag_el, "Key").text = t["Key"]+ SubElement(tag_el, "Value").text = t["Value"]+++def _build_customizations_xml(parent, cust):+ if not cust:+ return+ block = SubElement(parent, "Customizations")+ webacl = cust.get("WebAcl")+ if webacl:+ w = SubElement(block, "WebAcl")+ SubElement(w, "Action").text = webacl["Action"]+ if webacl.get("Arn"):+ SubElement(w, "Arn").text = webacl["Arn"]+ cert = cust.get("Certificate")+ if cert:+ c = SubElement(block, "Certificate")+ SubElement(c, "Arn").text = cert["Arn"]+ geo = cust.get("GeoRestrictions")+ if geo:+ g = SubElement(block, "GeoRestrictions")+ SubElement(g, "RestrictionType").text = geo["RestrictionType"]+ if geo.get("Locations"):+ locs = SubElement(g, "Locations")+ for loc in geo["Locations"]:+ SubElement(locs, "Location").text = loc+++def _build_connection_group_xml(parent, cg):+ SubElement(parent, "Id").text = cg["Id"]+ SubElement(parent, "Name").text = cg["Name"]+ SubElement(parent, "Arn").text = cg["Arn"]+ SubElement(parent, "CreatedTime").text = cg["CreatedTime"]+ SubElement(parent, "LastModifiedTime").text = cg["LastModifiedTime"]+ _build_tags_block_xml(parent, cg["Arn"])+ SubElement(parent, "Ipv6Enabled").text = _bstr(cg["Ipv6Enabled"])+ SubElement(parent, "RoutingEndpoint").text = cg["RoutingEndpoint"]+ if cg.get("AnycastIpListId"):+ SubElement(parent, "AnycastIpListId").text = cg["AnycastIpListId"]+ SubElement(parent, "Status").text = "Deployed"+ SubElement(parent, "Enabled").text = _bstr(cg["Enabled"])+ SubElement(parent, "IsDefault").text = _bstr(cg["IsDefault"])+++def _build_connection_group_summary_xml(parent, cg):+ SubElement(parent, "Id").text = cg["Id"]+ SubElement(parent, "Name").text = cg["Name"]+ SubElement(parent, "Arn").text = cg["Arn"]+ SubElement(parent, "RoutingEndpoint").text = cg["RoutingEndpoint"]+ SubElement(parent, "CreatedTime").text = cg["CreatedTime"]+ SubElement(parent, "LastModifiedTime").text = cg["LastModifiedTime"]+ SubElement(parent, "ETag").text = cg["ETag"]+ if cg.get("AnycastIpListId"):+ SubElement(parent, "AnycastIpListId").text = cg["AnycastIpListId"]+ SubElement(parent, "Enabled").text = _bstr(cg["Enabled"])+ SubElement(parent, "Status").text = "Deployed"+ SubElement(parent, "IsDefault").text = _bstr(cg["IsDefault"])+++def _build_domain_results_xml(parent, domains):+ block = SubElement(parent, "Domains")+ for d in domains:+ item = SubElement(block, "member")+ SubElement(item, "Domain").text = d+ SubElement(item, "Status").text = "active"+++def _build_tenant_parameters_xml(parent, params):+ if not params:+ return+ block = SubElement(parent, "Parameters")+ for p in params:+ item = SubElement(block, "member")+ SubElement(item, "Name").text = p["Name"]+ SubElement(item, "Value").text = p["Value"]+++def _build_distribution_tenant_xml(parent, tenant):+ SubElement(parent, "Id").text = tenant["Id"]+ SubElement(parent, "DistributionId").text = tenant["DistributionId"]+ SubElement(parent, "Name").text = tenant["Name"]+ SubElement(parent, "Arn").text = tenant["Arn"]+ _build_domain_results_xml(parent, tenant["Domains"])+ _build_tags_block_xml(parent, tenant["Arn"])+ _build_customizations_xml(parent, tenant.get("Customizations"))+ _build_tenant_parameters_xml(parent, tenant.get("Parameters"))+ SubElement(parent, "ConnectionGroupId").text = tenant["ConnectionGroupId"]+ SubElement(parent, "CreatedTime").text = tenant["CreatedTime"]+ SubElement(parent, "LastModifiedTime").text = tenant["LastModifiedTime"]+ SubElement(parent, "Enabled").text = _bstr(tenant["Enabled"])+ SubElement(parent, "Status").text = "Deployed"+++def _build_distribution_tenant_summary_xml(parent, tenant):+ SubElement(parent, "Id").text = tenant["Id"]+ SubElement(parent, "DistributionId").text = tenant["DistributionId"]+ SubElement(parent, "Name").text = tenant["Name"]+ SubElement(parent, "Arn").text = tenant["Arn"]+ _build_domain_results_xml(parent, tenant["Domains"])+ SubElement(parent, "ConnectionGroupId").text = tenant["ConnectionGroupId"]+ _build_customizations_xml(parent, tenant.get("Customizations"))+ SubElement(parent, "CreatedTime").text = tenant["CreatedTime"]+ SubElement(parent, "LastModifiedTime").text = tenant["LastModifiedTime"]+ SubElement(parent, "ETag").text = tenant["ETag"]+ SubElement(parent, "Enabled").text = _bstr(tenant["Enabled"])+ SubElement(parent, "Status").text = "Deployed"+++def _tenant_response(tenant, status=200):+ def build(root):+ _build_distribution_tenant_xml(root, tenant)++ return _xml_response("DistributionTenant", build, status=status, extra_headers={"ETag": tenant["ETag"]})+++def _conn_group_response(cg, status=200):+ def build(root):+ _build_connection_group_xml(root, cg)++ return _xml_response("ConnectionGroup", build, status=status, extra_headers={"ETag": cg["ETag"]})+++# ---- connection group handlers ----+++def _create_connection_group(body):+ el = _parse_body(body)+ if el is None:+ return _error("MalformedXML", "The XML document is malformed.", 400)+ name = _text(el, "Name")+ if not name:+ return _error("InvalidArgument", "Name is required.", 400)+ for existing in _connection_groups.values():+ if existing["Name"] == name:+ return _error("EntityAlreadyExists", "A connection group with this name already exists.", 409)++ now = _now_iso()+ cg = {+ "Id": _conn_group_id(),+ "Name": name,+ "CreatedTime": now,+ "LastModifiedTime": now,+ "Ipv6Enabled": _xbool(el, "Ipv6Enabled", True),+ "RoutingEndpoint": _routing_endpoint(),+ "AnycastIpListId": _opt_text(el, "AnycastIpListId"),+ "Enabled": _xbool(el, "Enabled", True),+ "IsDefault": False,+ "ETag": new_uuid(),+ }+ cg["Arn"] = _conn_group_arn(cg["Id"])+ _connection_groups[cg["Id"]] = cg+ _ingest_distribution_tags_from_xml(cg["Arn"], _find(el, "Tags"))+ logger.info("CreateConnectionGroup id=%s name=%s", cg["Id"], name)+ return _conn_group_response(cg, status=201)+++def _default_connection_group():+ """The account's default connection group, created lazily like real AWS+ does when a tenant is created without an explicit ConnectionGroupId."""+ for cg in _connection_groups.values():+ if cg["IsDefault"]:+ return cg+ now = _now_iso()+ cg = {+ "Id": _conn_group_id(),+ "CreatedTime": now,+ "LastModifiedTime": now,+ "Ipv6Enabled": True,+ "RoutingEndpoint": _routing_endpoint(),+ "AnycastIpListId": None,+ "Enabled": True,+ "IsDefault": True,+ "ETag": new_uuid(),+ }+ cg["Name"] = f"CreatedByCloudFront-{cg['Id']}"+ cg["Arn"] = _conn_group_arn(cg["Id"])+ _connection_groups[cg["Id"]] = cg+ logger.info("Created default connection group id=%s", cg["Id"])+ return cg+++def _get_connection_group(identifier):+ cg = _find_connection_group(identifier)+ if not cg:+ return _error("EntityNotFound", "The specified connection group does not exist.", 404)+ return _conn_group_response(cg)+++def _get_connection_group_by_routing_endpoint(query_params):+ endpoint = _qval(query_params, "RoutingEndpoint", "")+ if not endpoint:+ return _error("InvalidArgument", "The RoutingEndpoint query string parameter is required.", 400)+ for cg in _connection_groups.values():+ if cg["RoutingEndpoint"] == endpoint:+ return _conn_group_response(cg)+ return _error("EntityNotFound", "The specified connection group does not exist.", 404)+++def _update_connection_group(identifier, headers, body):+ cg = _find_connection_group(identifier)+ if not cg:+ return _error("EntityNotFound", "The specified connection group does not exist.", 404)+ pc = _policy_precheck_if_match(headers, cg)+ if pc is not None:+ return pc+ # boto3 serializes a request with only Id + IfMatch as an empty body;+ # real AWS accepts it as a members-unchanged update.+ el = _parse_body(body)+ if el is None and body:+ return _error("MalformedXML", "The XML document is malformed.", 400)+ if el is not None:+ ipv6 = _xbool(el, "Ipv6Enabled")+ if ipv6 is not None:+ cg["Ipv6Enabled"] = ipv6+ enabled = _xbool(el, "Enabled")+ if enabled is not None:+ cg["Enabled"] = enabled+ anycast = _opt_text(el, "AnycastIpListId")+ if anycast is not None:+ cg["AnycastIpListId"] = anycast+ cg["ETag"] = new_uuid()+ cg["LastModifiedTime"] = _now_iso()+ logger.info("UpdateConnectionGroup id=%s", cg["Id"])+ return _conn_group_response(cg)+++def _delete_connection_group(identifier, headers):+ cg = _find_connection_group(identifier)+ if not cg:+ return _error("EntityNotFound", "The specified connection group does not exist.", 404)+ pc = _policy_precheck_if_match(headers, cg)+ if pc is not None:+ return pc+ if any(t["ConnectionGroupId"] == cg["Id"] for t in _distribution_tenants.values()):+ return _error(+ "CannotDeleteEntityWhileInUse",+ "The connection group has distribution tenants associated with it and cannot be deleted.",+ 409,+ )+ if cg["Enabled"]:+ return _error("ResourceNotDisabled", "The connection group you are trying to delete has not been disabled.", 409)+ del _connection_groups[cg["Id"]]+ logger.info("DeleteConnectionGroup id=%s", cg["Id"])+ return 204, {}, b""+++def _list_connection_groups(body):+ el = _parse_body(body)+ anycast_filter = None+ if el is not None:+ assoc = _find(el, "AssociationFilter")+ if assoc is not None:+ anycast_filter = _opt_text(assoc, "AnycastIpListId")+ groups = [+ cg for cg in _connection_groups.values()+ if not anycast_filter or cg.get("AnycastIpListId") == anycast_filter+ ]++ def build(root):+ items_el = SubElement(root, "ConnectionGroups")+ for cg in groups:+ summary = SubElement(items_el, "ConnectionGroupSummary")+ _build_connection_group_summary_xml(summary, cg)++ return _xml_response("ListConnectionGroupsResult", build)+++# ---- distribution tenant handlers ----+++def _check_tenant_distribution(dist_id):+ """Validate the distribution a tenant attaches to. Returns an error tuple or None."""+ dist = _distributions.get(dist_id)+ if not dist:+ return _error("EntityNotFound", "The specified distribution does not exist.", 404)+ if _dist_connection_mode(dist) != "tenant-only":+ return _error(+ "InvalidAssociation",+ "Distribution tenants can only be associated with multi-tenant (tenant-only) distributions.",+ 409,+ )+ return None+++def _check_tenant_domain_conflicts(domains, exclude_tenant_id=None):+ lowered = [d.lower() for d in domains]+ if len(set(lowered)) != len(lowered):+ return _error("InvalidArgument", "Duplicate domains are not allowed.", 400)+ for domain in domains:+ for _rtype, rid in _domain_owners(domain):+ if rid != exclude_tenant_id:+ return _error("CNAMEAlreadyExists", f"The CNAME {domain} is already in use.", 409)+ return None+++def _create_distribution_tenant(body):+ el = _parse_body(body)+ if el is None:+ return _error("MalformedXML", "The XML document is malformed.", 400)+ dist_id = _text(el, "DistributionId")+ if not dist_id:+ return _error("InvalidArgument", "DistributionId is required.", 400)+ name = _text(el, "Name")+ if not name:+ return _error("InvalidArgument", "Name is required.", 400)+ domains = _parse_tenant_domains(el)+ if not domains:+ return _error("InvalidArgument", "Domains is required.", 400)++ err = _check_tenant_distribution(dist_id)+ if err is not None:+ return err+ for existing in _distribution_tenants.values():+ if existing["Name"] == name:+ return _error("EntityAlreadyExists", "A distribution tenant with this name already exists.", 409)+ err = _check_tenant_domain_conflicts(domains)+ if err is not None:+ return err++ cg_id_text = _text(el, "ConnectionGroupId")+ if cg_id_text:+ cg = _find_connection_group(cg_id_text)+ if not cg:+ return _error("EntityNotFound", "The specified connection group does not exist.", 404)+ else:+ cg = _default_connection_group()++ cust, err = _parse_customizations(el)+ if err is not None:+ return err+ managed_cert, err = _parse_managed_certificate_request(el, domains)+ if err is not None:+ return err++ now = _now_iso()+ tenant = {+ "Id": _tenant_id(),+ "DistributionId": dist_id,+ "Name": name,+ "Domains": domains,+ "Customizations": cust,+ "Parameters": _parse_tenant_parameters(el) or [],+ "ConnectionGroupId": cg["Id"],+ "CreatedTime": now,+ "LastModifiedTime": now,+ "Enabled": _xbool(el, "Enabled", True),+ "ETag": new_uuid(),+ "ManagedCertificate": managed_cert,+ }+ tenant["Arn"] = _tenant_arn(tenant["Id"])+ _distribution_tenants[tenant["Id"]] = tenant+ _tenant_invalidations[tenant["Id"]] = []+ _ingest_distribution_tags_from_xml(tenant["Arn"], _find(el, "Tags"))+ logger.info("CreateDistributionTenant id=%s name=%s dist=%s", tenant["Id"], name, dist_id)+ return _tenant_response(tenant, status=201)+++def _get_distribution_tenant(identifier):+ tenant = _find_distribution_tenant(identifier)+ if not tenant:+ return _error("EntityNotFound", "The specified distribution tenant does not exist.", 404)+ return _tenant_response(tenant)+++def _get_distribution_tenant_by_domain(query_params):+ domain = _qval(query_params, "domain", "")+ if not domain:+ return _error("InvalidArgument", "The domain query string parameter is required.", 400)+ d = domain.lower()+ for tenant in _distribution_tenants.values():+ if any(x.lower() == d for x in tenant["Domains"]):+ return _tenant_response(tenant)+ return _error("EntityNotFound", "The specified distribution tenant does not exist.", 404)+++def _update_distribution_tenant(identifier, headers, body):+ tenant = _find_distribution_tenant(identifier)+ if not tenant:+ return _error("EntityNotFound", "The specified distribution tenant does not exist.", 404)+ pc = _policy_precheck_if_match(headers, tenant)+ if pc is not None:+ return pc+ # boto3 serializes a request with only Id + IfMatch as an empty body;+ # real AWS accepts it as a members-unchanged update.+ el = _parse_body(body)+ if el is None and body:+ return _error("MalformedXML", "The XML document is malformed.", 400)++ # Validate the whole request before committing anything — a rejected+ # update must leave the tenant untouched, like real AWS.+ updates = {}+ if el is not None:+ dist_id = _text(el, "DistributionId")+ if dist_id and dist_id != tenant["DistributionId"]:+ err = _check_tenant_distribution(dist_id)+ if err is not None:+ return err+ if dist_id:+ updates["DistributionId"] = dist_id+ domains = _parse_tenant_domains(el)+ if domains is not None:+ if not domains:+ return _error("InvalidArgument", "Domains must not be empty.", 400)+ err = _check_tenant_domain_conflicts(domains, exclude_tenant_id=tenant["Id"])+ if err is not None:+ return err+ updates["Domains"] = domains+ cg_id_text = _text(el, "ConnectionGroupId")+ if cg_id_text:+ cg = _find_connection_group(cg_id_text)+ if not cg:+ return _error("EntityNotFound", "The specified connection group does not exist.", 404)+ updates["ConnectionGroupId"] = cg["Id"]+ cust, err = _parse_customizations(el)+ if err is not None:+ return err+ if cust is not None:+ updates["Customizations"] = cust+ params = _parse_tenant_parameters(el)+ if params is not None:+ updates["Parameters"] = params+ enabled = _xbool(el, "Enabled")+ if enabled is not None:+ updates["Enabled"] = enabled+ managed_cert, err = _parse_managed_certificate_request(el, updates.get("Domains", tenant["Domains"]))+ if err is not None:+ return err+ if managed_cert is not None:+ updates["ManagedCertificate"] = managed_cert++ tenant.update(updates)+ tenant["ETag"] = new_uuid()+ tenant["LastModifiedTime"] = _now_iso()+ logger.info("UpdateDistributionTenant id=%s", tenant["Id"])+ return _tenant_response(tenant)+++def _delete_distribution_tenant(identifier, headers):+ tenant = _find_distribution_tenant(identifier)+ if not tenant:+ return _error("EntityNotFound", "The specified distribution tenant does not exist.", 404)+ pc = _policy_precheck_if_match(headers, tenant)+ if pc is not None:+ return pc+ if tenant["Enabled"]:+ return _error(+ "ResourceNotDisabled", "The distribution tenant you are trying to delete has not been disabled.", 409+ )+ del _distribution_tenants[tenant["Id"]]+ _tenant_invalidations.pop(tenant["Id"], None)+ logger.info("DeleteDistributionTenant id=%s", tenant["Id"])+ return 204, {}, b""+++def _list_distribution_tenants(body):+ el = _parse_body(body)+ dist_filter = cg_filter = None+ if el is not None:+ assoc = _find(el, "AssociationFilter")+ if assoc is not None:+ dist_filter = _opt_text(assoc, "DistributionId")+ cg_filter = _opt_text(assoc, "ConnectionGroupId")+ tenants = [+ t for t in _distribution_tenants.values()+ if (not dist_filter or t["DistributionId"] == dist_filter)+ and (not cg_filter or t["ConnectionGroupId"] == cg_filter)+ ]+ return _tenant_list_response(tenants, "ListDistributionTenantsResult")+++def _list_distribution_tenants_by_customization(body):+ el = _parse_body(body)+ webacl_filter = cert_filter = None+ if el is not None:+ webacl_filter = _opt_text(el, "WebACLArn")+ cert_filter = _opt_text(el, "CertificateArn")++ def matches(tenant):+ cust = tenant.get("Customizations") or {}+ if webacl_filter and (cust.get("WebAcl") or {}).get("Arn") != webacl_filter:+ return False+ if cert_filter and (cust.get("Certificate") or {}).get("Arn") != cert_filter:+ return False+ return True++ return _tenant_list_response(+ [t for t in _distribution_tenants.values() if matches(t)],+ "ListDistributionTenantsByCustomizationResult",+ )+++def _tenant_list_response(tenants, root_tag):+ def build(root):+ items_el = SubElement(root, "DistributionTenantList")+ for tenant in tenants:+ summary = SubElement(items_el, "DistributionTenantSummary")+ _build_distribution_tenant_summary_xml(summary, tenant)++ return _xml_response(root_tag, build)+++def _associate_tenant_webacl(tenant_id, headers, body):+ tenant = _find_distribution_tenant(tenant_id)+ if not tenant:+ return _error("EntityNotFound", "The specified distribution tenant does not exist.", 404)+ if_match = headers.get("if-match")+ if if_match and if_match != tenant["ETag"]:+ return _error(+ "PreconditionFailed",+ "The precondition given in one or more of the request-header fields evaluated to false.",+ 412,+ )+ el = _parse_body(body)+ webacl_arn = _text(el, "WebACLArn") if el is not None else ""+ if not webacl_arn:+ return _error("InvalidArgument", "WebACLArn is required.", 400)+ cust = tenant.get("Customizations") or {}+ cust["WebAcl"] = {"Action": "override", "Arn": webacl_arn}+ tenant["Customizations"] = cust+ tenant["ETag"] = new_uuid()+ tenant["LastModifiedTime"] = _now_iso()+ logger.info("AssociateDistributionTenantWebACL id=%s", tenant["Id"])++ def build(root):+ SubElement(root, "Id").text = tenant["Id"]+ SubElement(root, "WebACLArn").text = webacl_arn++ return _xml_response(+ "AssociateDistributionTenantWebACLResult", build, extra_headers={"ETag": tenant["ETag"]}+ )+++def _disassociate_tenant_webacl(tenant_id, headers):+ tenant = _find_distribution_tenant(tenant_id)+ if not tenant:+ return _error("EntityNotFound", "The specified distribution tenant does not exist.", 404)+ if_match = headers.get("if-match")+ if if_match and if_match != tenant["ETag"]:+ return _error(+ "PreconditionFailed",+ "The precondition given in one or more of the request-header fields evaluated to false.",+ 412,+ )+ cust = tenant.get("Customizations") or {}+ cust.pop("WebAcl", None)+ tenant["Customizations"] = cust+ tenant["ETag"] = new_uuid()+ tenant["LastModifiedTime"] = _now_iso()+ logger.info("DisassociateDistributionTenantWebACL id=%s", tenant["Id"])++ def build(root):+ SubElement(root, "Id").text = tenant["Id"]++ return _xml_response(+ "DisassociateDistributionTenantWebACLResult", build, extra_headers={"ETag": tenant["ETag"]}+ )+++# ---- tenant invalidation handlers (mirror the distribution ones) ----+++def _create_tenant_invalidation(tenant_id, body):+ tenant = _find_distribution_tenant(tenant_id)+ if not tenant:+ return _error("EntityNotFound", "The specified distribution tenant does not exist.", 404)+ batch_el = _parse_body(body)+ if batch_el is None:+ return _error("MalformedXML", "The XML document is malformed.", 400)++ paths_el = _find(batch_el, "Paths")+ caller_ref = _text(batch_el, "CallerReference")+ path_items = []+ if paths_el is not None:+ items_el = _find(paths_el, "Items")+ if items_el is not None:+ path_items = [child.text for child in items_el if child.text]++ invs = _tenant_invalidations.setdefault(tenant["Id"], [])+ for existing in invs:+ if existing["InvalidationBatch"]["CallerReference"] == caller_ref:+ if set(existing["InvalidationBatch"]["Paths"]["Items"]) != set(path_items):+ return _error(+ "InvalidationBatchAlreadyExists",+ "An invalidation batch with this CallerReference already exists.",+ 400,+ )++ def build(root, _inv=existing):+ _build_invalidation_xml(root, _inv)++ return _xml_response(+ "Invalidation", build, status=201,+ extra_headers={+ "Location": f"/2020-05-31/distribution-tenant/{tenant['Id']}/invalidation/{existing['Id']}",+ },+ )++ inv = {+ "Id": _inv_id(),+ "Status": "Completed",+ "CreateTime": _now_iso(),+ "InvalidationBatch": {+ "Paths": {"Quantity": len(path_items), "Items": path_items},+ "CallerReference": caller_ref,+ },+ }+ invs.append(inv)+ logger.info("CreateInvalidationForDistributionTenant tenant=%s inv=%s", tenant["Id"], inv["Id"])++ def build(root):+ _build_invalidation_xml(root, inv)++ return _xml_response(+ "Invalidation", build, status=201,+ extra_headers={+ "Location": f"/2020-05-31/distribution-tenant/{tenant['Id']}/invalidation/{inv['Id']}",+ },+ )+++def _list_tenant_invalidations(tenant_id):+ tenant = _find_distribution_tenant(tenant_id)+ if not tenant:+ return _error("EntityNotFound", "The specified distribution tenant does not exist.", 404)+ invs = _tenant_invalidations.get(tenant["Id"], [])++ def build(root):+ SubElement(root, "Marker").text = ""+ SubElement(root, "MaxItems").text = "100"+ SubElement(root, "IsTruncated").text = "false"+ SubElement(root, "Quantity").text = str(len(invs))+ if invs:+ items_el = SubElement(root, "Items")+ for inv in invs:+ summary = SubElement(items_el, "InvalidationSummary")+ SubElement(summary, "Id").text = inv["Id"]+ SubElement(summary, "CreateTime").text = inv["CreateTime"]+ SubElement(summary, "Status").text = inv["Status"]++ return _xml_response("InvalidationList", build)+++def _get_tenant_invalidation(tenant_id, inv_id):+ tenant = _find_distribution_tenant(tenant_id)+ if not tenant:+ return _error("EntityNotFound", "The specified distribution tenant does not exist.", 404)+ inv = next((i for i in _tenant_invalidations.get(tenant["Id"], []) if i["Id"] == inv_id), None)+ if not inv:+ return _error("NoSuchInvalidation", "The specified invalidation does not exist.", 404)++ def build(root):+ _build_invalidation_xml(root, inv)++ return _xml_response("Invalidation", build)+++# ---- domain and certificate handlers ----+++def _verify_dns_configuration(body):+ el = _parse_body(body)+ if el is None:+ return _error("MalformedXML", "The XML document is malformed.", 400)+ identifier = _text(el, "Identifier")+ if not identifier:+ return _error("InvalidArgument", "Identifier is required.", 400)+ tenant = _find_distribution_tenant(identifier)+ if not tenant:+ return _error("EntityNotFound", "The specified distribution tenant does not exist.", 404)+ domain = _text(el, "Domain")+ domains = [domain] if domain else tenant["Domains"]++ def build(root):+ list_el = SubElement(root, "DnsConfigurationList")+ for d in domains:+ item = SubElement(list_el, "DnsConfiguration")+ SubElement(item, "Domain").text = d+ SubElement(item, "Status").text = "valid-configuration"++ return _xml_response("VerifyDnsConfigurationResult", build)+++def _get_managed_certificate_details(identifier):+ tenant = _find_distribution_tenant(identifier)+ if not tenant:+ return _error("EntityNotFound", "The specified distribution tenant does not exist.", 404)+ mc = tenant.get("ManagedCertificate")++ def build(root):+ if not mc:+ return+ SubElement(root, "CertificateArn").text = mc["CertificateArn"]+ SubElement(root, "CertificateStatus").text = mc["CertificateStatus"]+ SubElement(root, "ValidationTokenHost").text = mc["ValidationTokenHost"]+ details = SubElement(root, "ValidationTokenDetails")+ for d in mc["Domains"]:+ item = SubElement(details, "member")+ SubElement(item, "Domain").text = d++ return _xml_response("ManagedCertificateDetails", build)+++def _list_domain_conflicts(body):+ el = _parse_body(body)+ if el is None:+ return _error("MalformedXML", "The XML document is malformed.", 400)+ domain = _text(el, "Domain")+ resource_el = _find(el, "DomainControlValidationResource")+ if not domain or resource_el is None:+ return _error("InvalidArgument", "Domain and DomainControlValidationResource are required.", 400)+ tenant_ref = _text(resource_el, "DistributionTenantId")+ dist_ref = _text(resource_el, "DistributionId")+ exclude_ids = set()+ if tenant_ref:+ tenant = _find_distribution_tenant(tenant_ref)+ if not tenant:+ return _error("EntityNotFound", "The specified distribution tenant does not exist.", 404)+ exclude_ids.add(tenant["Id"])+ elif dist_ref:+ if dist_ref not in _distributions:+ return _error("EntityNotFound", "The specified distribution does not exist.", 404)+ exclude_ids.add(dist_ref)+ else:+ return _error("InvalidArgument", "DomainControlValidationResource requires a resource id.", 400)++ conflicts = [+ (rtype, rid)+ for rtype, rid in _domain_owners(domain, wildcard_overlap=True)+ if rid not in exclude_ids+ ]++ def build(root):+ list_el = SubElement(root, "DomainConflicts")+ for rtype, rid in conflicts:+ # The list member's XML name is DomainConflicts per the model.+ item = SubElement(list_el, "DomainConflicts")+ SubElement(item, "Domain").text = domain+ SubElement(item, "ResourceType").text = rtype+ SubElement(item, "ResourceId").text = rid+ SubElement(item, "AccountId").text = get_account_id()++ return _xml_response("ListDomainConflictsResult", build)+++def _update_domain_association(headers, body):+ el = _parse_body(body)+ if el is None:+ return _error("MalformedXML", "The XML document is malformed.", 400)+ domain = _text(el, "Domain")+ target_el = _find(el, "TargetResource")+ if not domain or target_el is None:+ return _error("InvalidArgument", "Domain and TargetResource are required.", 400)++ tenant_ref = _text(target_el, "DistributionTenantId")+ dist_ref = _text(target_el, "DistributionId")+ target_tenant = target_dist = None+ if tenant_ref:+ target_tenant = _find_distribution_tenant(tenant_ref)+ if not target_tenant:+ return _error("EntityNotFound", "The specified distribution tenant does not exist.", 404)+ target = target_tenant+ elif dist_ref:+ target_dist = _distributions.get(dist_ref)+ if not target_dist:+ return _error("EntityNotFound", "The specified distribution does not exist.", 404)+ target = target_dist+ else:+ return _error("InvalidArgument", "TargetResource requires a resource id.", 400)++ if_match = headers.get("if-match")+ if if_match and if_match != target["ETag"]:+ return _error(+ "PreconditionFailed",+ "The precondition given in one or more of the request-header fields evaluated to false.",+ 412,+ )++ # Detach the domain from whichever resource currently claims it.+ d = domain.lower()+ for tenant in _distribution_tenants.values():+ if tenant is target_tenant:+ continue+ if any(x.lower() == d for x in tenant["Domains"]):+ tenant["Domains"] = [x for x in tenant["Domains"] if x.lower() != d]+ tenant["ETag"] = new_uuid()+ tenant["LastModifiedTime"] = _now_iso()+ for dist in _distributions.values():+ if dist is target_dist:+ continue+ aliases = _get_distribution_aliases(dist)+ if any(x.lower() == d for x in aliases):+ _set_distribution_aliases(dist, [x for x in aliases if x.lower() != d])+ dist["ETag"] = new_uuid()+ dist["LastModifiedTime"] = _now_iso()++ # Attach it to the target.+ if target_tenant is not None:+ if not any(x.lower() == d for x in target_tenant["Domains"]):+ target_tenant["Domains"] = [*target_tenant["Domains"], domain]+ resource_id = target_tenant["Id"]+ else:+ aliases = _get_distribution_aliases(target_dist)+ if not any(x.lower() == d for x in aliases):+ _set_distribution_aliases(target_dist, [*aliases, domain])+ resource_id = target_dist["Id"]+ target["ETag"] = new_uuid()+ target["LastModifiedTime"] = _now_iso()+ logger.info("UpdateDomainAssociation domain=%s target=%s", domain, resource_id)++ def build(root):+ SubElement(root, "Domain").text = domain+ SubElement(root, "ResourceId").text = resource_id++ return _xml_response("UpdateDomainAssociationResult", build, extra_headers={"ETag": target["ETag"]})+++def _list_distributions_by_connection_mode(mode):+ if mode not in _CONNECTION_MODES:+ return _error("InvalidArgument", "Invalid ConnectionMode value.", 400)+ items = [d for d in _distributions.values() if _dist_connection_mode(d) == mode]+ return _xml_response("DistributionList", lambda root: _build_distribution_list_xml(root, items))diff --git a/ministack/services/tagging.py b/ministack/services/tagging.pyindex 5227df75..d00bf00d 100644--- a/ministack/services/tagging.py+++ b/ministack/services/tagging.py@@ -454,6 +454,10 @@ def _cloudfront_resource_exists(spec: Arn, arn: str) -> bool:import ministack.services.cloudfront as svcif spec.resource.startswith("distribution/"):return _resource_tail(spec, arn, "distribution/") in svc._distributions+ if spec.resource.startswith("distribution-tenant/"):+ return _resource_tail(spec, arn, "distribution-tenant/") in svc._distribution_tenants+ if spec.resource.startswith("connection-group/"):+ return _resource_tail(spec, arn, "connection-group/") in svc._connection_groupsif spec.resource.startswith("function/"):return _resource_tail(spec, arn, "function/") in svc._functionsif spec.resource.startswith("key-value-store/"):diff --git a/tests/test_cloudfront.py b/tests/test_cloudfront.pyindex de805fdb..563cc239 100644--- a/tests/test_cloudfront.py+++ b/tests/test_cloudfront.py@@ -1733,3 +1733,819 @@ def test_cloudfront_get_monitoring_subscription_errors(cloudfront):disabled = dict(cfg, Enabled=False)upd = cloudfront.update_distribution(DistributionConfig=disabled, Id=dist_id, IfMatch=etag)cloudfront.delete_distribution(Id=dist_id, IfMatch=upd["ETag"])+++# ---------------------------------------------------------------------------+# CloudFront SaaS Manager — connection groups, distribution tenants, managed+# certificates, domain verification. Shapes verified against botocore+# cloudfront service-2.json (2020-05-31).+# ---------------------------------------------------------------------------+++def _tenant_only_distribution_config(caller_reference):+ cfg = copy.deepcopy(_CF_DIST_CONFIG)+ cfg["CallerReference"] = caller_reference+ cfg["Comment"] = "multi-tenant distribution"+ cfg["ConnectionMode"] = "tenant-only"+ cfg["TenantConfig"] = {+ "ParameterDefinitions": [+ {"Name": "tenantName", "Definition": {"StringSchema": {"Required": True}}}+ ]+ }+ return cfg+++def _create_tenant_only_distribution(cloudfront, sfx):+ cfg = _tenant_only_distribution_config(f"cf-mt-{sfx}")+ return cloudfront.create_distribution(DistributionConfig=cfg)["Distribution"]+++def test_cf_saas_create_connection_group(cloudfront):+ sfx = _uuid_mod.uuid4().hex[:8]+ resp = cloudfront.create_connection_group(Name=f"cg-{sfx}")+ assert resp["ResponseMetadata"]["HTTPStatusCode"] == 201+ assert resp["ETag"]+ cg = resp["ConnectionGroup"]+ assert cg["Id"].startswith("cg_")+ assert cg["Name"] == f"cg-{sfx}"+ assert cg["Arn"] == f"arn:aws:cloudfront::000000000000:connection-group/{cg['Id']}"+ assert cg["RoutingEndpoint"].endswith(".cloudfront.net")+ assert cg["Status"] == "Deployed"+ assert cg["Enabled"] is True+ assert cg["Ipv6Enabled"] is True+ assert cg["IsDefault"] is False+ assert cg["CreatedTime"] and cg["LastModifiedTime"]+++def test_cf_saas_create_connection_group_duplicate_name(cloudfront):+ sfx = _uuid_mod.uuid4().hex[:8]+ cloudfront.create_connection_group(Name=f"cg-dup-{sfx}")+ with pytest.raises(ClientError) as exc:+ cloudfront.create_connection_group(Name=f"cg-dup-{sfx}")+ assert exc.value.response["Error"]["Code"] == "EntityAlreadyExists"+++def test_cf_saas_get_connection_group_by_id_name_arn(cloudfront):+ sfx = _uuid_mod.uuid4().hex[:8]+ created = cloudfront.create_connection_group(Name=f"cg-get-{sfx}", Ipv6Enabled=False)+ cg = created["ConnectionGroup"]+ for identifier in (cg["Id"], cg["Name"], cg["Arn"]):+ got = cloudfront.get_connection_group(Identifier=identifier)+ assert got["ConnectionGroup"]["Id"] == cg["Id"]+ assert got["ConnectionGroup"]["Ipv6Enabled"] is False+ assert got["ETag"] == created["ETag"]++ with pytest.raises(ClientError) as exc:+ cloudfront.get_connection_group(Identifier="cg_DOESNOTEXIST")+ assert exc.value.response["Error"]["Code"] == "EntityNotFound"+++def test_cf_saas_get_connection_group_by_routing_endpoint(cloudfront):+ sfx = _uuid_mod.uuid4().hex[:8]+ cg = cloudfront.create_connection_group(Name=f"cg-re-{sfx}")["ConnectionGroup"]+ got = cloudfront.get_connection_group_by_routing_endpoint(RoutingEndpoint=cg["RoutingEndpoint"])+ assert got["ConnectionGroup"]["Id"] == cg["Id"]++ with pytest.raises(ClientError) as exc:+ cloudfront.get_connection_group_by_routing_endpoint(RoutingEndpoint="dnope.cloudfront.net")+ assert exc.value.response["Error"]["Code"] == "EntityNotFound"+++def test_cf_saas_update_connection_group(cloudfront):+ sfx = _uuid_mod.uuid4().hex[:8]+ created = cloudfront.create_connection_group(Name=f"cg-upd-{sfx}")+ cg_id = created["ConnectionGroup"]["Id"]++ with pytest.raises(ClientError) as exc:+ cloudfront.update_connection_group(Id=cg_id, IfMatch="bogus", Ipv6Enabled=False)+ assert exc.value.response["Error"]["Code"] == "PreconditionFailed"++ upd = cloudfront.update_connection_group(+ Id=cg_id, IfMatch=created["ETag"], Ipv6Enabled=False, Enabled=False+ )+ assert upd["ETag"] != created["ETag"]+ assert upd["ConnectionGroup"]["Ipv6Enabled"] is False+ assert upd["ConnectionGroup"]["Enabled"] is False+ assert upd["ConnectionGroup"]["Name"] == f"cg-upd-{sfx}"+++def test_cf_saas_delete_connection_group(cloudfront):+ sfx = _uuid_mod.uuid4().hex[:8]+ created = cloudfront.create_connection_group(Name=f"cg-del-{sfx}")+ cg_id = created["ConnectionGroup"]["Id"]++ with pytest.raises(ClientError) as exc:+ cloudfront.delete_connection_group(Id=cg_id, IfMatch=created["ETag"])+ assert exc.value.response["Error"]["Code"] == "ResourceNotDisabled"++ upd = cloudfront.update_connection_group(Id=cg_id, IfMatch=created["ETag"], Enabled=False)+ cloudfront.delete_connection_group(Id=cg_id, IfMatch=upd["ETag"])+ with pytest.raises(ClientError) as exc:+ cloudfront.get_connection_group(Identifier=cg_id)+ assert exc.value.response["Error"]["Code"] == "EntityNotFound"+++def test_cf_saas_list_connection_groups(cloudfront):+ sfx = _uuid_mod.uuid4().hex[:8]+ cg = cloudfront.create_connection_group(Name=f"cg-list-{sfx}")["ConnectionGroup"]+ groups = cloudfront.list_connection_groups()["ConnectionGroups"]+ match = [g for g in groups if g["Id"] == cg["Id"]]+ assert len(match) == 1+ assert match[0]["Name"] == cg["Name"]+ assert match[0]["RoutingEndpoint"] == cg["RoutingEndpoint"]+ assert match[0]["ETag"]+++def test_cf_saas_create_distribution_tenant(cloudfront):+ sfx = _uuid_mod.uuid4().hex[:8]+ dist = _create_tenant_only_distribution(cloudfront, sfx)+ resp = cloudfront.create_distribution_tenant(+ DistributionId=dist["Id"],+ Name=f"tenant-{sfx}",+ Domains=[{"Domain": f"app-{sfx}.example.com"}],+ Parameters=[{"Name": "tenantName", "Value": "acme"}],+ Tags={"Items": [{"Key": "env", "Value": "test"}]},+ )+ assert resp["ResponseMetadata"]["HTTPStatusCode"] == 201+ assert resp["ETag"]+ tenant = resp["DistributionTenant"]+ assert tenant["Id"].startswith("dt_")+ assert tenant["Arn"] == f"arn:aws:cloudfront::000000000000:distribution-tenant/{tenant['Id']}"+ assert tenant["DistributionId"] == dist["Id"]+ assert tenant["Name"] == f"tenant-{sfx}"+ assert tenant["Domains"] == [{"Domain": f"app-{sfx}.example.com", "Status": "active"}]+ assert tenant["Parameters"] == [{"Name": "tenantName", "Value": "acme"}]+ assert tenant["Enabled"] is True+ assert tenant["Status"] == "Deployed"++ # A default connection group is created lazily when none is specified.+ cg = cloudfront.get_connection_group(Identifier=tenant["ConnectionGroupId"])["ConnectionGroup"]+ assert cg["IsDefault"] is True++ tags = cloudfront.list_tags_for_resource(Resource=tenant["Arn"])["Tags"]["Items"]+ assert {"Key": "env", "Value": "test"} in tags+++def test_cf_saas_create_tenant_with_explicit_connection_group(cloudfront):+ sfx = _uuid_mod.uuid4().hex[:8]+ dist = _create_tenant_only_distribution(cloudfront, sfx)+ cg = cloudfront.create_connection_group(Name=f"cg-exp-{sfx}")["ConnectionGroup"]+ tenant = cloudfront.create_distribution_tenant(+ DistributionId=dist["Id"],+ Name=f"tenant-exp-{sfx}",+ Domains=[{"Domain": f"exp-{sfx}.example.com"}],+ ConnectionGroupId=cg["Id"],+ )["DistributionTenant"]+ assert tenant["ConnectionGroupId"] == cg["Id"]++ # An attached connection group cannot be deleted.+ got = cloudfront.get_connection_group(Identifier=cg["Id"])+ upd = cloudfront.update_connection_group(Id=cg["Id"], IfMatch=got["ETag"], Enabled=False)+ with pytest.raises(ClientError) as exc:+ cloudfront.delete_connection_group(Id=cg["Id"], IfMatch=upd["ETag"])+ assert exc.value.response["Error"]["Code"] == "CannotDeleteEntityWhileInUse"+++def test_cf_saas_create_tenant_validation_errors(cloudfront):+ sfx = _uuid_mod.uuid4().hex[:8]+ with pytest.raises(ClientError) as exc:+ cloudfront.create_distribution_tenant(+ DistributionId="EDOESNOTEXIST0",+ Name=f"tenant-miss-{sfx}",+ Domains=[{"Domain": f"miss-{sfx}.example.com"}],+ )+ assert exc.value.response["Error"]["Code"] == "EntityNotFound"++ # Tenants only attach to tenant-only (multi-tenant) distributions.+ direct_cfg = _custom_origin_distribution_config(f"cf-direct-{sfx}")+ direct = cloudfront.create_distribution(DistributionConfig=direct_cfg)["Distribution"]+ with pytest.raises(ClientError) as exc:+ cloudfront.create_distribution_tenant(+ DistributionId=direct["Id"],+ Name=f"tenant-direct-{sfx}",+ Domains=[{"Domain": f"direct-{sfx}.example.com"}],+ )+ assert exc.value.response["Error"]["Code"] == "InvalidAssociation"++ dist = _create_tenant_only_distribution(cloudfront, sfx)+ cloudfront.create_distribution_tenant(+ DistributionId=dist["Id"],+ Name=f"tenant-dupe-{sfx}",+ Domains=[{"Domain": f"dupe-{sfx}.example.com"}],+ )+ with pytest.raises(ClientError) as exc:+ cloudfront.create_distribution_tenant(+ DistributionId=dist["Id"],+ Name=f"tenant-dupe-{sfx}",+ Domains=[{"Domain": f"dupe2-{sfx}.example.com"}],+ )+ assert exc.value.response["Error"]["Code"] == "EntityAlreadyExists"++ with pytest.raises(ClientError) as exc:+ cloudfront.create_distribution_tenant(+ DistributionId=dist["Id"],+ Name=f"tenant-cname-{sfx}",+ Domains=[{"Domain": f"DUPE-{sfx}.example.com"}],+ )+ assert exc.value.response["Error"]["Code"] == "CNAMEAlreadyExists"+++def test_cf_saas_get_distribution_tenant(cloudfront):+ sfx = _uuid_mod.uuid4().hex[:8]+ dist = _create_tenant_only_distribution(cloudfront, sfx)+ created = cloudfront.create_distribution_tenant(+ DistributionId=dist["Id"],+ Name=f"tenant-get-{sfx}",+ Domains=[{"Domain": f"get-{sfx}.example.com"}],+ )+ tenant = created["DistributionTenant"]+ for identifier in (tenant["Id"], tenant["Name"], tenant["Arn"]):+ got = cloudfront.get_distribution_tenant(Identifier=identifier)+ assert got["DistributionTenant"]["Id"] == tenant["Id"]+ assert got["ETag"] == created["ETag"]++ by_domain = cloudfront.get_distribution_tenant_by_domain(Domain=f"get-{sfx}.example.com")+ assert by_domain["DistributionTenant"]["Id"] == tenant["Id"]++ with pytest.raises(ClientError) as exc:+ cloudfront.get_distribution_tenant(Identifier="dt_DOESNOTEXIST")+ assert exc.value.response["Error"]["Code"] == "EntityNotFound"+ with pytest.raises(ClientError) as exc:+ cloudfront.get_distribution_tenant_by_domain(Domain="nope.example.com")+ assert exc.value.response["Error"]["Code"] == "EntityNotFound"+++def test_cf_saas_update_distribution_tenant(cloudfront):+ sfx = _uuid_mod.uuid4().hex[:8]+ dist = _create_tenant_only_distribution(cloudfront, sfx)+ created = cloudfront.create_distribution_tenant(+ DistributionId=dist["Id"],+ Name=f"tenant-upd-{sfx}",+ Domains=[{"Domain": f"upd-{sfx}.example.com"}],+ )+ tenant = created["DistributionTenant"]++ with pytest.raises(ClientError) as exc:+ cloudfront.update_distribution_tenant(Id=tenant["Id"], IfMatch="bogus", Enabled=False)+ assert exc.value.response["Error"]["Code"] == "PreconditionFailed"++ upd = cloudfront.update_distribution_tenant(+ Id=tenant["Id"],+ IfMatch=created["ETag"],+ Domains=[{"Domain": f"upd-{sfx}.example.com"}, {"Domain": f"upd2-{sfx}.example.com"}],+ Parameters=[{"Name": "tenantName", "Value": "acme2"}],+ Enabled=False,+ )+ assert upd["ETag"] != created["ETag"]+ updated = upd["DistributionTenant"]+ assert [d["Domain"] for d in updated["Domains"]] == [+ f"upd-{sfx}.example.com",+ f"upd2-{sfx}.example.com",+ ]+ assert updated["Parameters"] == [{"Name": "tenantName", "Value": "acme2"}]+ assert updated["Enabled"] is False+ assert updated["Name"] == f"tenant-upd-{sfx}"+++def test_cf_saas_delete_distribution_tenant(cloudfront):+ sfx = _uuid_mod.uuid4().hex[:8]+ dist = _create_tenant_only_distribution(cloudfront, sfx)+ created = cloudfront.create_distribution_tenant(+ DistributionId=dist["Id"],+ Name=f"tenant-del-{sfx}",+ Domains=[{"Domain": f"del-{sfx}.example.com"}],+ )+ tenant_id = created["DistributionTenant"]["Id"]++ with pytest.raises(ClientError) as exc:+ cloudfront.delete_distribution_tenant(Id=tenant_id, IfMatch=created["ETag"])+ assert exc.value.response["Error"]["Code"] == "ResourceNotDisabled"++ upd = cloudfront.update_distribution_tenant(Id=tenant_id, IfMatch=created["ETag"], Enabled=False)+ cloudfront.delete_distribution_tenant(Id=tenant_id, IfMatch=upd["ETag"])+ with pytest.raises(ClientError) as exc:+ cloudfront.get_distribution_tenant(Identifier=tenant_id)+ assert exc.value.response["Error"]["Code"] == "EntityNotFound"+++def test_cf_saas_list_distribution_tenants(cloudfront):+ sfx = _uuid_mod.uuid4().hex[:8]+ dist_a = _create_tenant_only_distribution(cloudfront, f"{sfx}-a")+ dist_b = _create_tenant_only_distribution(cloudfront, f"{sfx}-b")+ cg = cloudfront.create_connection_group(Name=f"cg-flt-{sfx}")["ConnectionGroup"]+ t_a = cloudfront.create_distribution_tenant(+ DistributionId=dist_a["Id"],+ Name=f"tenant-la-{sfx}",+ Domains=[{"Domain": f"la-{sfx}.example.com"}],+ ConnectionGroupId=cg["Id"],+ )["DistributionTenant"]+ t_b = cloudfront.create_distribution_tenant(+ DistributionId=dist_b["Id"],+ Name=f"tenant-lb-{sfx}",+ Domains=[{"Domain": f"lb-{sfx}.example.com"}],+ )["DistributionTenant"]++ all_ids = [t["Id"] for t in cloudfront.list_distribution_tenants()["DistributionTenantList"]]+ assert t_a["Id"] in all_ids and t_b["Id"] in all_ids++ by_dist = cloudfront.list_distribution_tenants(+ AssociationFilter={"DistributionId": dist_a["Id"]}+ )["DistributionTenantList"]+ assert [t["Id"] for t in by_dist] == [t_a["Id"]]+ assert by_dist[0]["Domains"] == [{"Domain": f"la-{sfx}.example.com", "Status": "active"}]+ assert by_dist[0]["ETag"]++ by_cg = cloudfront.list_distribution_tenants(+ AssociationFilter={"ConnectionGroupId": cg["Id"]}+ )["DistributionTenantList"]+ assert [t["Id"] for t in by_cg] == [t_a["Id"]]+++def test_cf_saas_list_distribution_tenants_by_customization(cloudfront):+ sfx = _uuid_mod.uuid4().hex[:8]+ dist = _create_tenant_only_distribution(cloudfront, sfx)+ cert_arn = f"arn:aws:acm:us-east-1:000000000000:certificate/{sfx}"+ tenant = cloudfront.create_distribution_tenant(+ DistributionId=dist["Id"],+ Name=f"tenant-cust-{sfx}",+ Domains=[{"Domain": f"cust-{sfx}.example.com"}],+ Customizations={"Certificate": {"Arn": cert_arn}},+ )["DistributionTenant"]+ assert tenant["Customizations"]["Certificate"]["Arn"] == cert_arn++ by_cert = cloudfront.list_distribution_tenants_by_customization(CertificateArn=cert_arn)[+ "DistributionTenantList"+ ]+ assert [t["Id"] for t in by_cert] == [tenant["Id"]]++ none = cloudfront.list_distribution_tenants_by_customization(+ CertificateArn="arn:aws:acm:us-east-1:000000000000:certificate/none"+ )["DistributionTenantList"]+ assert none == []+++def test_cf_saas_tenant_webacl_associate_disassociate(cloudfront):+ sfx = _uuid_mod.uuid4().hex[:8]+ dist = _create_tenant_only_distribution(cloudfront, sfx)+ created = cloudfront.create_distribution_tenant(+ DistributionId=dist["Id"],+ Name=f"tenant-acl-{sfx}",+ Domains=[{"Domain": f"acl-{sfx}.example.com"}],+ )+ tenant_id = created["DistributionTenant"]["Id"]+ acl_arn = f"arn:aws:wafv2:us-east-1:000000000000:global/webacl/test/{sfx}"++ assoc = cloudfront.associate_distribution_tenant_web_acl(+ Id=tenant_id, WebACLArn=acl_arn, IfMatch=created["ETag"]+ )+ assert assoc["Id"] == tenant_id+ assert assoc["WebACLArn"] == acl_arn+ assert assoc["ETag"] != created["ETag"]++ got = cloudfront.get_distribution_tenant(Identifier=tenant_id)["DistributionTenant"]+ assert got["Customizations"]["WebAcl"] == {"Action": "override", "Arn": acl_arn}++ dis = cloudfront.disassociate_distribution_tenant_web_acl(Id=tenant_id, IfMatch=assoc["ETag"])+ assert dis["Id"] == tenant_id+ got = cloudfront.get_distribution_tenant(Identifier=tenant_id)["DistributionTenant"]+ assert "WebAcl" not in got.get("Customizations", {})+++def test_cf_saas_tenant_invalidations(cloudfront):+ sfx = _uuid_mod.uuid4().hex[:8]+ dist = _create_tenant_only_distribution(cloudfront, sfx)+ tenant = cloudfront.create_distribution_tenant(+ DistributionId=dist["Id"],+ Name=f"tenant-inv-{sfx}",+ Domains=[{"Domain": f"inv-{sfx}.example.com"}],+ )["DistributionTenant"]++ batch = {+ "Paths": {"Quantity": 2, "Items": ["/index.html", "/assets/*"]},+ "CallerReference": f"inv-{sfx}",+ }+ created = cloudfront.create_invalidation_for_distribution_tenant(+ Id=tenant["Id"], InvalidationBatch=batch+ )+ assert created["ResponseMetadata"]["HTTPStatusCode"] == 201+ inv = created["Invalidation"]+ assert inv["Status"] == "Completed"+ assert sorted(inv["InvalidationBatch"]["Paths"]["Items"]) == ["/assets/*", "/index.html"]++ got = cloudfront.get_invalidation_for_distribution_tenant(+ DistributionTenantId=tenant["Id"], Id=inv["Id"]+ )["Invalidation"]+ assert got["Id"] == inv["Id"]++ listed = cloudfront.list_invalidations_for_distribution_tenant(Id=tenant["Id"])[+ "InvalidationList"+ ]+ assert [i["Id"] for i in listed["Items"]] == [inv["Id"]]++ with pytest.raises(ClientError) as exc:+ cloudfront.get_invalidation_for_distribution_tenant(+ DistributionTenantId=tenant["Id"], Id="IDOESNOTEXIST0"+ )+ assert exc.value.response["Error"]["Code"] == "NoSuchInvalidation"+ with pytest.raises(ClientError) as exc:+ cloudfront.list_invalidations_for_distribution_tenant(Id="dt_DOESNOTEXIST")+ assert exc.value.response["Error"]["Code"] == "EntityNotFound"+++def test_cf_saas_verify_dns_configuration(cloudfront):+ sfx = _uuid_mod.uuid4().hex[:8]+ dist = _create_tenant_only_distribution(cloudfront, sfx)+ tenant = cloudfront.create_distribution_tenant(+ DistributionId=dist["Id"],+ Name=f"tenant-dns-{sfx}",+ Domains=[{"Domain": f"dns1-{sfx}.example.com"}, {"Domain": f"dns2-{sfx}.example.com"}],+ )["DistributionTenant"]++ all_domains = cloudfront.verify_dns_configuration(Identifier=tenant["Id"])[+ "DnsConfigurationList"+ ]+ assert {d["Domain"] for d in all_domains} == {+ f"dns1-{sfx}.example.com",+ f"dns2-{sfx}.example.com",+ }+ assert {d["Status"] for d in all_domains} == {"valid-configuration"}++ one = cloudfront.verify_dns_configuration(+ Identifier=tenant["Id"], Domain=f"dns2-{sfx}.example.com"+ )["DnsConfigurationList"]+ assert [d["Domain"] for d in one] == [f"dns2-{sfx}.example.com"]++ with pytest.raises(ClientError) as exc:+ cloudfront.verify_dns_configuration(Identifier="dt_DOESNOTEXIST")+ assert exc.value.response["Error"]["Code"] == "EntityNotFound"+++def test_cf_saas_managed_certificate_details(cloudfront):+ sfx = _uuid_mod.uuid4().hex[:8]+ dist = _create_tenant_only_distribution(cloudfront, sfx)+ tenant = cloudfront.create_distribution_tenant(+ DistributionId=dist["Id"],+ Name=f"tenant-cert-{sfx}",+ Domains=[{"Domain": f"cert-{sfx}.example.com"}],+ ManagedCertificateRequest={"ValidationTokenHost": "cloudfront"},+ )["DistributionTenant"]++ details = cloudfront.get_managed_certificate_details(Identifier=tenant["Id"])[+ "ManagedCertificateDetails"+ ]+ assert details["CertificateStatus"] == "issued"+ assert details["CertificateArn"].startswith("arn:aws:acm:us-east-1:000000000000:certificate/")+ assert details["ValidationTokenHost"] == "cloudfront"+ assert [d["Domain"] for d in details["ValidationTokenDetails"]] == [f"cert-{sfx}.example.com"]++ # A tenant without a managed certificate returns empty details.+ plain = cloudfront.create_distribution_tenant(+ DistributionId=dist["Id"],+ Name=f"tenant-nocert-{sfx}",+ Domains=[{"Domain": f"nocert-{sfx}.example.com"}],+ )["DistributionTenant"]+ empty = cloudfront.get_managed_certificate_details(Identifier=plain["Id"])[+ "ManagedCertificateDetails"+ ]+ assert "CertificateArn" not in empty++ with pytest.raises(ClientError) as exc:+ cloudfront.get_managed_certificate_details(Identifier="dt_DOESNOTEXIST")+ assert exc.value.response["Error"]["Code"] == "EntityNotFound"+++def test_cf_saas_list_domain_conflicts(cloudfront):+ sfx = _uuid_mod.uuid4().hex[:8]+ dist = _create_tenant_only_distribution(cloudfront, sfx)+ t_a = cloudfront.create_distribution_tenant(+ DistributionId=dist["Id"],+ Name=f"tenant-dca-{sfx}",+ Domains=[{"Domain": f"conflict-{sfx}.example.com"}],+ )["DistributionTenant"]+ t_b = cloudfront.create_distribution_tenant(+ DistributionId=dist["Id"],+ Name=f"tenant-dcb-{sfx}",+ Domains=[{"Domain": f"other-{sfx}.example.com"}],+ )["DistributionTenant"]++ conflicts = cloudfront.list_domain_conflicts(+ Domain=f"conflict-{sfx}.example.com",+ DomainControlValidationResource={"DistributionTenantId": t_b["Id"]},+ )["DomainConflicts"]+ assert conflicts == [+ {+ "Domain": f"conflict-{sfx}.example.com",+ "ResourceType": "distribution-tenant",+ "ResourceId": t_a["Id"],+ "AccountId": "000000000000",+ }+ ]++ # The querying resource itself is excluded from conflicts.+ own = cloudfront.list_domain_conflicts(+ Domain=f"conflict-{sfx}.example.com",+ DomainControlValidationResource={"DistributionTenantId": t_a["Id"]},+ )["DomainConflicts"]+ assert own == []+++def test_cf_saas_update_domain_association(cloudfront):+ sfx = _uuid_mod.uuid4().hex[:8]+ dist = _create_tenant_only_distribution(cloudfront, sfx)+ t_a = cloudfront.create_distribution_tenant(+ DistributionId=dist["Id"],+ Name=f"tenant-mva-{sfx}",+ Domains=[{"Domain": f"move-{sfx}.example.com"}, {"Domain": f"keep-{sfx}.example.com"}],+ )["DistributionTenant"]+ t_b = cloudfront.create_distribution_tenant(+ DistributionId=dist["Id"],+ Name=f"tenant-mvb-{sfx}",+ Domains=[{"Domain": f"target-{sfx}.example.com"}],+ )["DistributionTenant"]++ moved = cloudfront.update_domain_association(+ Domain=f"move-{sfx}.example.com",+ TargetResource={"DistributionTenantId": t_b["Id"]},+ )+ assert moved["Domain"] == f"move-{sfx}.example.com"+ assert moved["ResourceId"] == t_b["Id"]+ assert moved["ETag"]++ got_a = cloudfront.get_distribution_tenant(Identifier=t_a["Id"])["DistributionTenant"]+ got_b = cloudfront.get_distribution_tenant(Identifier=t_b["Id"])["DistributionTenant"]+ assert [d["Domain"] for d in got_a["Domains"]] == [f"keep-{sfx}.example.com"]+ assert sorted(d["Domain"] for d in got_b["Domains"]) == [+ f"move-{sfx}.example.com",+ f"target-{sfx}.example.com",+ ]++ with pytest.raises(ClientError) as exc:+ cloudfront.update_domain_association(+ Domain=f"keep-{sfx}.example.com",+ TargetResource={"DistributionTenantId": "dt_DOESNOTEXIST"},+ )+ assert exc.value.response["Error"]["Code"] == "EntityNotFound"+++def test_cf_saas_list_distributions_by_connection_mode(cloudfront):+ sfx = _uuid_mod.uuid4().hex[:8]+ mt = _create_tenant_only_distribution(cloudfront, sfx)+ direct_cfg = _custom_origin_distribution_config(f"cf-lbm-{sfx}")+ direct = cloudfront.create_distribution(DistributionConfig=direct_cfg)["Distribution"]++ mt_list = cloudfront.list_distributions_by_connection_mode(ConnectionMode="tenant-only")[+ "DistributionList"+ ]+ mt_ids = [d["Id"] for d in mt_list.get("Items", [])]+ assert mt["Id"] in mt_ids+ assert direct["Id"] not in mt_ids+ assert all(d["ConnectionMode"] == "tenant-only" for d in mt_list.get("Items", []))++ direct_list = cloudfront.list_distributions_by_connection_mode(ConnectionMode="direct")[+ "DistributionList"+ ]+ direct_ids = [d["Id"] for d in direct_list.get("Items", [])]+ assert direct["Id"] in direct_ids+ assert mt["Id"] not in direct_ids+++def test_cf_saas_distribution_round_trips_connection_mode(cloudfront):+ sfx = _uuid_mod.uuid4().hex[:8]+ dist = _create_tenant_only_distribution(cloudfront, sfx)+ got_cfg = cloudfront.get_distribution_config(Id=dist["Id"])["DistributionConfig"]+ assert got_cfg["ConnectionMode"] == "tenant-only"+ defs = got_cfg["TenantConfig"]["ParameterDefinitions"]+ assert defs[0]["Name"] == "tenantName"+ assert defs[0]["Definition"]["StringSchema"]["Required"] is True++ summaries = cloudfront.list_distributions()["DistributionList"]["Items"]+ mine = [d for d in summaries if d["Id"] == dist["Id"]]+ assert mine and mine[0]["ConnectionMode"] == "tenant-only"+++def test_cf_saas_rejected_tenant_update_leaves_tenant_unchanged(cloudfront):+ """A validation failure part-way through UpdateDistributionTenant must not+ commit any of the request's mutations (real AWS validates before writing)."""+ sfx = _uuid_mod.uuid4().hex[:8]+ dist = _create_tenant_only_distribution(cloudfront, sfx)+ created = cloudfront.create_distribution_tenant(+ DistributionId=dist["Id"],+ Name=f"tenant-atomic-{sfx}",+ Domains=[{"Domain": f"atomic-{sfx}.example.com"}],+ Parameters=[{"Name": "tenantName", "Value": "before"}],+ )+ tenant = created["DistributionTenant"]++ with pytest.raises(ClientError) as exc:+ cloudfront.update_distribution_tenant(+ Id=tenant["Id"],+ IfMatch=created["ETag"],+ Domains=[{"Domain": f"atomic-changed-{sfx}.example.com"}],+ Parameters=[{"Name": "tenantName", "Value": "after"}],+ Enabled=False,+ ManagedCertificateRequest={"ValidationTokenHost": "bogus-host"},+ )+ assert exc.value.response["Error"]["Code"] == "InvalidArgument"++ got = cloudfront.get_distribution_tenant(Identifier=tenant["Id"])+ assert got["ETag"] == created["ETag"]+ unchanged = got["DistributionTenant"]+ assert [d["Domain"] for d in unchanged["Domains"]] == [f"atomic-{sfx}.example.com"]+ assert unchanged["Parameters"] == [{"Name": "tenantName", "Value": "before"}]+ assert unchanged["Enabled"] is True++ cg = cloudfront.create_connection_group(Name=f"cg-atomic-{sfx}")["ConnectionGroup"]+ with pytest.raises(ClientError) as exc:+ cloudfront.update_distribution_tenant(+ Id=tenant["Id"],+ IfMatch=created["ETag"],+ ConnectionGroupId=cg["Id"],+ Customizations={"WebAcl": {"Action": "bogus-action"}},+ )+ assert exc.value.response["Error"]["Code"] == "InvalidArgument"+ got = cloudfront.get_distribution_tenant(Identifier=tenant["Id"])["DistributionTenant"]+ assert got["ConnectionGroupId"] != cg["Id"]+++def test_cf_saas_metadata_only_updates_accept_empty_body(cloudfront):+ """boto3 serializes UpdateConnectionGroup/UpdateDistributionTenant with only+ Id + IfMatch as an empty request body; real AWS accepts it."""+ sfx = _uuid_mod.uuid4().hex[:8]+ created = cloudfront.create_connection_group(Name=f"cg-empty-{sfx}")+ upd = cloudfront.update_connection_group(Id=created["ConnectionGroup"]["Id"], IfMatch=created["ETag"])+ assert upd["ETag"] != created["ETag"]+ assert upd["ConnectionGroup"]["Enabled"] is True+ assert upd["ConnectionGroup"]["Ipv6Enabled"] is True++ dist = _create_tenant_only_distribution(cloudfront, sfx)+ t_created = cloudfront.create_distribution_tenant(+ DistributionId=dist["Id"],+ Name=f"tenant-empty-{sfx}",+ Domains=[{"Domain": f"empty-{sfx}.example.com"}],+ )+ t_upd = cloudfront.update_distribution_tenant(+ Id=t_created["DistributionTenant"]["Id"], IfMatch=t_created["ETag"]+ )+ assert t_upd["ETag"] != t_created["ETag"]+ assert [d["Domain"] for d in t_upd["DistributionTenant"]["Domains"]] == [f"empty-{sfx}.example.com"]+++def test_cf_saas_create_tenant_rejects_duplicate_domains_in_request(cloudfront):+ sfx = _uuid_mod.uuid4().hex[:8]+ dist = _create_tenant_only_distribution(cloudfront, sfx)+ with pytest.raises(ClientError) as exc:+ cloudfront.create_distribution_tenant(+ DistributionId=dist["Id"],+ Name=f"tenant-dupdom-{sfx}",+ Domains=[{"Domain": f"dup-{sfx}.example.com"}, {"Domain": f"DUP-{sfx}.example.com"}],+ )+ assert exc.value.response["Error"]["Code"] == "InvalidArgument"+ with pytest.raises(ClientError) as exc:+ cloudfront.get_distribution_tenant(Identifier=f"tenant-dupdom-{sfx}")+ assert exc.value.response["Error"]["Code"] == "EntityNotFound"+++def test_cf_saas_delete_distribution_with_tenants_refused(cloudfront):+ """Deleting a multi-tenant distribution that still has tenants must fail+ with ResourceInUse instead of orphaning the tenants."""+ sfx = _uuid_mod.uuid4().hex[:8]+ cfg = _tenant_only_distribution_config(f"cf-deldist-{sfx}")+ created = cloudfront.create_distribution(DistributionConfig=cfg)+ dist_id = created["Distribution"]["Id"]+ t_created = cloudfront.create_distribution_tenant(+ DistributionId=dist_id,+ Name=f"tenant-deldist-{sfx}",+ Domains=[{"Domain": f"deldist-{sfx}.example.com"}],+ )++ disabled_cfg = dict(cfg, Enabled=False)+ upd = cloudfront.update_distribution(DistributionConfig=disabled_cfg, Id=dist_id, IfMatch=created["ETag"])+ with pytest.raises(ClientError) as exc:+ cloudfront.delete_distribution(Id=dist_id, IfMatch=upd["ETag"])+ assert exc.value.response["Error"]["Code"] == "ResourceInUse"++ t_upd = cloudfront.update_distribution_tenant(+ Id=t_created["DistributionTenant"]["Id"], IfMatch=t_created["ETag"], Enabled=False+ )+ cloudfront.delete_distribution_tenant(Id=t_created["DistributionTenant"]["Id"], IfMatch=t_upd["ETag"])+ cloudfront.delete_distribution(Id=dist_id, IfMatch=upd["ETag"])+++def test_cf_saas_by_customization_result_root_element(cloudfront):+ """The by-customization list uses its own result root element; boto3+ tolerates a wrong root, so assert on the raw wire bytes."""+ req = urllib.request.Request(+ f"{ENDPOINT}/2020-05-31/distribution-tenants-by-customization",+ data=b"",+ method="POST",+ )+ with urllib.request.urlopen(req) as resp:+ body = resp.read()+ assert b"<ListDistributionTenantsByCustomizationResult" in body+++def test_cf_saas_tenant_ops_survive_cfn_created_distribution(cfn, cloudfront):+ """CloudFormation provisions distribution records with an empty config_xml;+ tenant-side scans over all distributions must not choke on them."""+ sfx = _uuid_mod.uuid4().hex[:8]+ stack_name = f"cf-saas-cfn-{sfx}"+ template = {+ "AWSTemplateFormatVersion": "2010-09-09",+ "Resources": {+ "Distribution": {+ "Type": "AWS::CloudFront::Distribution",+ "Properties": {"DistributionConfig": {"Enabled": True}},+ },+ },+ "Outputs": {"DistributionId": {"Value": {"Ref": "Distribution"}}},+ }+ cfn.create_stack(StackName=stack_name, TemplateBody=json.dumps(template))+ for _ in range(30):+ stack = cfn.describe_stacks(StackName=stack_name)["Stacks"][0]+ if not stack["StackStatus"].endswith("_IN_PROGRESS"):+ break+ time.sleep(0.5)+ assert stack["StackStatus"] == "CREATE_COMPLETE"+ cfn_dist_id = {o["OutputKey"]: o["OutputValue"] for o in stack["Outputs"]}["DistributionId"]++ try:+ dist = _create_tenant_only_distribution(cloudfront, sfx)+ tenant = cloudfront.create_distribution_tenant(+ DistributionId=dist["Id"],+ Name=f"tenant-cfn-{sfx}",+ Domains=[{"Domain": f"cfn-{sfx}.example.com"}],+ )["DistributionTenant"]+ assert tenant["Id"].startswith("dt_")++ direct = cloudfront.list_distributions_by_connection_mode(ConnectionMode="direct")[+ "DistributionList"+ ]+ assert cfn_dist_id in [d["Id"] for d in direct.get("Items", [])]+ finally:+ cfn.delete_stack(StackName=stack_name)+++def test_cf_saas_list_domain_conflicts_wildcard_overlap(cloudfront):+ """ListDomainConflicts reports single-level wildcard overlaps, not just+ exact matches."""+ sfx = _uuid_mod.uuid4().hex[:8]+ dist = _create_tenant_only_distribution(cloudfront, sfx)+ t_wild = cloudfront.create_distribution_tenant(+ DistributionId=dist["Id"],+ Name=f"tenant-wild-{sfx}",+ Domains=[{"Domain": f"*.wc-{sfx}.example.com"}],+ )["DistributionTenant"]+ t_other = cloudfront.create_distribution_tenant(+ DistributionId=dist["Id"],+ Name=f"tenant-wildb-{sfx}",+ Domains=[{"Domain": f"wildb-{sfx}.example.com"}],+ )["DistributionTenant"]++ conflicts = cloudfront.list_domain_conflicts(+ Domain=f"app.wc-{sfx}.example.com",+ DomainControlValidationResource={"DistributionTenantId": t_other["Id"]},+ )["DomainConflicts"]+ assert [c["ResourceId"] for c in conflicts] == [t_wild["Id"]]++ # A wildcard covers exactly one label.+ deep = cloudfront.list_domain_conflicts(+ Domain=f"a.b.wc-{sfx}.example.com",+ DomainControlValidationResource={"DistributionTenantId": t_other["Id"]},+ )["DomainConflicts"]+ assert deep == []+++def test_cf_saas_resourcegroupstagging_tags_new_families(cloudfront, tagging):+ """The Resource Groups Tagging API can tag a distribution tenant and a+ connection group that have no tags yet."""+ sfx = _uuid_mod.uuid4().hex[:8]+ dist = _create_tenant_only_distribution(cloudfront, sfx)+ tenant = cloudfront.create_distribution_tenant(+ DistributionId=dist["Id"],+ Name=f"tenant-rgt-{sfx}",+ Domains=[{"Domain": f"rgt-{sfx}.example.com"}],+ )["DistributionTenant"]+ cg = cloudfront.create_connection_group(Name=f"cg-rgt-{sfx}")["ConnectionGroup"]++ result = tagging.tag_resources(+ ResourceARNList=[tenant["Arn"], cg["Arn"]], Tags={"team": "edge"}+ )+ assert result["FailedResourcesMap"] == {}+ for arn in (tenant["Arn"], cg["Arn"]):+ tags = cloudfront.list_tags_for_resource(Resource=arn)["Tags"]["Items"]+ assert {"Key": "team", "Value": "edge"} in tags+++def test_cf_saas_connection_group_tagging(cloudfront):+ sfx = _uuid_mod.uuid4().hex[:8]+ cg = cloudfront.create_connection_group(+ Name=f"cg-tag-{sfx}", Tags={"Items": [{"Key": "team", "Value": "edge"}]}+ )["ConnectionGroup"]+ tags = cloudfront.list_tags_for_resource(Resource=cg["Arn"])["Tags"]["Items"]+ assert {"Key": "team", "Value": "edge"} in tags++ cloudfront.tag_resource(+ Resource=cg["Arn"], Tags={"Items": [{"Key": "env", "Value": "test"}]}+ )+ tags = cloudfront.list_tags_for_resource(Resource=cg["Arn"])["Tags"]["Items"]+ assert {"Key": "env", "Value": "test"} in tags