Skip to content

Ministack: Free, open-source local AWS emulator - 60+ services, Terraform compatible, real databases. Free forever. MIT licensed.

prerelease (#1963)

Nahuel Nucera01f577b5368f
diff --git a/CHANGELOG.md b/CHANGELOG.md
index fef21e9d..5437fc54 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -9,45 +9,42 @@ Versioning follows [Semantic Versioning](https://semver.org/).
### Added
-- **CloudFormation — ECR repositories are replaced** — an `EncryptionConfiguration` change on `AWS::ECR::Repository` replaces the repository under a new generated name (generated names now carry the usual suffix) and is refused for an explicit `RepositoryName`, where it used to be ignored.
-- **CloudFormation — IAM roles and managed policies are replaced** — a `Path` change on `AWS::IAM::Role` and a `Path` or `Description` change on `AWS::IAM::ManagedPolicy` replace the resource under a new generated name, a named role refuses it as a custom-named replacement, a named policy fails with the IAM duplicate-name error as on AWS, also when only its `Path` changes, generated policy names get the suffix other generated names have, and both ARNs include the `Path`.
-- **CloudFormation — stack updates replace resources** — a change to a property the change set reports as `RequiresRecreation: Always` now creates a new resource under a new generated name, points its dependents at it and deletes the old one after the update succeeds (a rollback deletes the new one instead). With an explicit, unchanged name the update fails with the AWS message naming the physical id; this now also covers `AWS::Lambda::Function`. A named SQS queue or SNS topic fails with AWS's already-exists error instead and keeps its messages or subscriptions.
-- **CloudFormation — IAM roles and managed policies are replaced** — a `Path` change on `AWS::IAM::Role` and a `Path` or `Description` change on `AWS::IAM::ManagedPolicy` replace the resource under a new generated name, a named role refuses it as a custom-named replacement, a named policy fails with the IAM duplicate-name error as on AWS, also when only its `Path` changes, generated policy names get the suffix other generated names have, and both ARNs include the `Path`.
-- **CloudFormation — stack updates replace resources** — a change to a property the change set reports as `RequiresRecreation: Always` now creates a new resource under a new generated name, points its dependents at it and deletes the old one after the update succeeds (a rollback deletes the new one instead). With an explicit, unchanged name the update fails with the AWS message naming the physical id; this now also covers `AWS::Lambda::Function`. A named SQS queue or SNS topic fails with AWS's already-exists error instead and keeps its messages or subscriptions.
-- **CloudFormation — `IMPORT` change sets execute** — existing SQS queues, SSM parameters, S3 buckets, DynamoDB tables, IAM roles, log groups, Lambda functions, IoT policies, IoT CA certificates and Cognito user pools are adopted into a new or existing stack without being changed (`IMPORT_IN_PROGRESS` to `IMPORT_COMPLETE`, or a rollback when a resource is gone), and an import that changes `Outputs` or stack tags is refused, as on AWS.
-- **CloudFormation — `Capabilities` check without `AUTH`** — `CFN_ENFORCE_CAPABILITIES=1` refuses a template whose IAM resources or macros the request does not acknowledge also without `AUTH=true`, as AWS always does.
+- **Athena — databases, DDL, and Trino-style table references** — `ListDatabases` and `GetDatabase` read the Glue Data Catalog; `CREATE EXTERNAL TABLE` and `DROP TABLE` apply to it, creating the Glue table Athena would (formats, SerDe, TBLPROPERTIES); `CREATE TABLE` without `EXTERNAL` is rejected as Athena rejects it, and Iceberg tables are not supported; a query may name a table as `"awsdatacatalog"."db"."t"` or `db.t`. Contributed by @sjincho.
+- **CloudFront — distributions serve traffic, plus the AWS-managed policies and monitoring subscriptions** — a request to `<label>.cloudfront.net` (or `<label>.cloudfront.<MINISTACK_HOST>`) is served: ordered cache behaviors, `ViewerProtocolPolicy`, `AllowedMethods`, `DefaultRootObject`, cache and origin request policy forwarding (and legacy `ForwardedValues`), `viewer-request` / `viewer-response` CloudFront Functions, response headers policies, and S3 or custom origins; an S3 origin is read only as the bucket policy allows it (OAC, OAI or public). `DomainName` has the AWS `d` + 13-character shape. `List*Policies` return the AWS-managed cache, origin request and response headers policies, and `Create`/`Get`/`DeleteMonitoringSubscription` are implemented. Not modelled: caching, WAF, logging, geo restrictions, custom error pages, signed URLs and cookies, Lambda@Edge. Contributed by @skialpine.
+- **CloudFormation — EKS cluster and node group updates** — `AWS::EKS::Cluster` applies `Version`, `Logging`, `ResourcesVpcConfig`, `AccessConfig.AuthenticationMode` and `Tags` in place and `AWS::EKS::Nodegroup` applies `ScalingConfig`, `Labels`, `Taints`, `UpdateConfig`, `LaunchTemplate`, `Version`, `ReleaseVersion` and `Tags`, where every such change used to report `UPDATE_COMPLETE` and was dropped. Contributed by @iot-rocket.
+- **CloudFormation — `AWS::Pipes::Pipe` and `AWS::Scheduler::ScheduleGroup` update in place** — a pipe keeps its stream position and `CreationTime` when `Description`, `Target`, `RoleArn`, `DesiredState` or `Tags` change, gets `Description` and `Tags` on create and refuses a create-only source change under an explicit `Name`, and a schedule group takes template and stack tag changes while keeping tags added through `TagResource`. Contributed by @iot-rocket.
+- **CloudFormation — ECR repositories are replaced** — an `EncryptionConfiguration` change on `AWS::ECR::Repository` replaces the repository under a new generated name (generated names now carry the usual suffix) and is refused for an explicit `RepositoryName`, where it used to be ignored. Contributed by @iot-rocket.
+- **CloudFormation — IAM roles and managed policies are replaced** — a `Path` change on `AWS::IAM::Role` and a `Path` or `Description` change on `AWS::IAM::ManagedPolicy` replace the resource under a new generated name, a named role refuses it as a custom-named replacement, a named policy fails with the IAM duplicate-name error as on AWS, also when only its `Path` changes, generated policy names get the suffix other generated names have, and both ARNs include the `Path`. Contributed by @iot-rocket.
+- **CloudFormation — stack updates replace resources** — a change to a property the change set reports as `RequiresRecreation: Always` now creates a new resource under a new generated name, points its dependents at it and deletes the old one after the update succeeds (a rollback deletes the new one instead). With an explicit, unchanged name the update fails with the AWS message naming the physical id; this now also covers `AWS::Lambda::Function`. A named SQS queue or SNS topic fails with AWS's already-exists error instead and keeps its messages or subscriptions. Contributed by @iot-rocket.
+- **CloudFormation — `IMPORT` change sets execute** — existing SQS queues, SSM parameters, S3 buckets, DynamoDB tables, IAM roles, log groups, Lambda functions, IoT policies, IoT CA certificates and Cognito user pools are adopted into a new or existing stack without being changed (`IMPORT_IN_PROGRESS` to `IMPORT_COMPLETE`, or a rollback when a resource is gone), and an import that changes `Outputs` or stack tags is refused, as on AWS. Contributed by @iot-rocket.
### Fixed
- **CloudFormation — nested stack with a `Transform`** — where `Capabilities` are checked, a nested stack whose template declares a `Transform` or calls `Fn::Transform` fails with `Requires capabilities : [CAPABILITY_AUTO_EXPAND]` unless the parent acknowledged `CAPABILITY_AUTO_EXPAND`, as on AWS.
- **CloudFormation — `AWS::RDS::DBCluster` and `AWS::RDS::DBInstance` update in place** — a stack update re-ran the create, which gave the resource a new endpoint, resource id and create time and emptied the cluster's member list; the properties the create stores now change on the existing record, a create-only or `Engine` change replaces the resource or, under a custom identifier, is refused, a cluster `MasterUsername` change leaves the cluster as it is, change sets report which properties replace, and a stack-created cluster can now be described and answers `Fn::GetAtt DBClusterResourceId`. Contributed by @iot-rocket.
-- **CloudFormation — `AWS::Pipes::Pipe` and `AWS::Scheduler::ScheduleGroup` update in place** — a pipe keeps its stream position and `CreationTime` when `Description`, `Target`, `RoleArn`, `DesiredState` or `Tags` change, gets `Description` and `Tags` on create and refuses a create-only source change under an explicit `Name`, and a schedule group takes template and stack tag changes while keeping tags added through `TagResource`.
-- **CloudFormation — EKS cluster and node group updates** — `AWS::EKS::Cluster` applies `Version`, `Logging`, `ResourcesVpcConfig`, `AccessConfig.AuthenticationMode` and `Tags` in place and `AWS::EKS::Nodegroup` applies `ScalingConfig`, `Labels`, `Taints`, `UpdateConfig`, `LaunchTemplate`, `Version`, `ReleaseVersion` and `Tags`, where every such change used to report `UPDATE_COMPLETE` and was dropped.
- **SES — a sandboxed account sends only to verified or simulator recipients** — SES v2 `PutAccountDetails` with `ProductionAccessEnabled=false` puts the account (per region) in the sandbox, as on AWS, and `GetAccount` reports it with the submitted `Details`. A sandboxed send to a recipient that is neither a verified address or domain identity nor a `@simulator.amazonses.com` address fails with `MessageRejected` "Email address is not verified. The following identities failed the check in region …" for v1 `SendEmail`, `SendRawEmail` and `SendTemplatedEmail` and v2 `SendEmail`; bulk sends reject only the affected entries. Accounts stay in production by default. Reported by @skialpine.
+- **CloudFormation — `AWS::ECS::Cluster` and `AWS::ECS::TaskDefinition` update in place** — a cluster change keeps the cluster (settings or configuration dropped from the template stay), and a task definition change registers the next revision of the family and deregisters the old one instead of overwriting revision 1. Contributed by @iot-rocket.
+- **CloudFormation — more `IMPORT` types** — SNS topics, KMS keys and aliases, IoT thing types, Cognito user pool clients, groups, resource servers and identity pools, and API Gateway REST APIs and stages can be imported, including two-key identifiers, and `Fn::GetAtt` on an identity pool's `Id` resolves. Contributed by @iot-rocket.
+- **CloudFormation — `ImportExistingResources`** — a `CREATE` or `UPDATE` change set imports an added resource whose static custom name already exists (it needs `DeletionPolicy` `Retain` or `RetainExceptOnCreate`), and a rollback releases imported resources instead of deleting them. Contributed by @iot-rocket.
### Fixed
+- **ECS — `UpdateService` with `forceNewDeployment` replaces the tasks** — it was ignored when the task definition did not change. Rolling deployments now pin image digests from the first task, honor `versionConsistency` `disabled`, use `repositoryCredentials` for private registries, and report `imageDigest` and the Fargate platform version; a task falls back to the local image when the pull fails. Contributed by @AdrianAcala.
+- **AutoScaling — target tracking alarms** — `PutScalingPolicy` with `TargetTrackingScaling` creates the `TargetTracking-<group>-AlarmHigh-<uuid>` alarm and, unless `DisableScaleIn` is set, the `AlarmLow` one on the tracked metric, lists them in `Alarms` of `PutScalingPolicy` and `DescribePolicies`, replaces them when the policy is updated and deletes them with the policy or its group. An `AWS::AutoScaling::ScalingPolicy` in a template does the same. Contributed by @iot-rocket.
+- **IoT — mTLS trusts every registered device certificate** — the listener no longer fails the TLS handshake for an ACTIVE certificate whose CA was deactivated or deleted before its first connect, or that was registered without a CA; as on AWS, only the certificate's own status refuses it. Contributed by @iot-rocket.
+- **API Gateway — usage plan throttling** — a REST API request carrying an API key from a usage plan on the stage is throttled with `429 Too Many Requests` by the plan's `throttle` and its per-method `apiStages[].throttle`, in addition to the stage's method settings, and a throttled response carries `x-amzn-ErrorType: TooManyRequestsException`. Contributed by @iot-rocket.
+- **API Gateway — request body validation follows JSON Schema draft 4** — a request validator checks the body against the draft 4 keywords of the model (`enum`, `pattern`, length, range and `multipleOf` bounds, `integer` versus `number` versus `boolean`, `items`, `additionalProperties`, `patternProperties`, `dependencies`, `allOf` / `anyOf` / `oneOf` / `not`, formats, and `$ref` to local definitions and to other models of the API); an empty body and a body nested more than 1000 levels deep are refused, and a schema that applies itself to the same value again answers 500. `BAD_REQUEST_BODY` and `BAD_REQUEST_PARAMETERS` read `{"message": "..."}` and carry `x-amzn-ErrorType: BadRequestException`. Contributed by @iot-rocket.
+- **API Gateway v2 (WebSocket API) — `$connect` authorization** — a `CUSTOM` `$connect` route ran no authorizer. It now runs its REQUEST authorizer, refuses the handshake with 401, 403 or 500 and passes `principalId` and the context to `requestContext.authorizer` of the connection's events. `CreateAuthorizer`, `CreateRoute`, `UpdateRoute` and the CloudFormation resources refuse a JWT authorizer, JWT route authorization and authorization on a route other than `$connect` with `BadRequestException`, as AWS does; a JWT `$connect` route kept in saved state refuses the handshake with 500 instead of validating the token. `requestContext.stage` names the stage in the connection URL instead of `$default`. Contributed by @iot-rocket.
+- **Lambda — Docker executor under `USE_SSL=1` when MiniStack runs in a container** — the gateway certificate, CA bundle and Java truststore were bind-mounted into every Lambda container from MiniStack's own filesystem (`MINISTACK_SSL_CERT`, or the generated `ministack-tls/server.crt` under the temp directory), paths the host Docker daemon cannot see, so every invocation failed with `bind source path does not exist`. In a container they are now copied into the Lambda container, as function code already is. Contributed by @skialpine.
+- **RDS — `DescribeDBClusterSnapshotAttributes`** — was unimplemented (`InvalidAction: Unknown RDS action`), so Terraform's `aws_db_cluster_snapshot` resource failed on read (`reading RDS DB Cluster Snapshot … attribute`) after creating the snapshot. Returns the `restore` attribute with no shared accounts (the manual-snapshot default); `ModifyDBClusterSnapshotAttribute` is not implemented. Unknown snapshot ids answer `DBClusterSnapshotNotFoundFault`. Contributed by @skialpine.
- **SES — a send from an unverified sender is rejected** — v1 `SendEmail`, `SendRawEmail`, `SendTemplatedEmail` and `SendBulkTemplatedEmail` and v2 `SendEmail` and `SendBulkEmail` accepted any `Source` / `FromEmailAddress`. AWS requires the sender to be a verified identity in the account and region, in production as well as the sandbox, and now so does MiniStack: the send fails with `MessageRejected` "Email address is not verified. The following identities failed the check in region …". A verified domain covers its addresses and subdomains, domain names compare case-insensitively and email addresses case-sensitively, as AWS documents. Tests that send from an address they never created as an identity need a `VerifyEmailIdentity` / `CreateEmailIdentity` first. Reported by @skialpine.
-- **CloudFront — serves distribution traffic, plus the AWS-managed policy catalog and monitoring subscriptions** — MiniStack's CloudFront was control-plane only: a distribution's `DomainName` stored nothing a client could reach. A viewer request to `<label>.cloudfront.net` (or the resolvable `<label>.cloudfront.<MINISTACK_HOST>` form) is now actually served — ordered cache-behavior path-pattern matching, `ViewerProtocolPolicy`/`AllowedMethods`/`DefaultRootObject`, cache-policy and origin-request-policy header/cookie/query-string forwarding (plus legacy `ForwardedValues`), `viewer-request`/`viewer-response` CloudFront Functions (`cloudfront-js-1.0`/`-2.0`, on a shared Node worker pool also used by AppSync resolvers), a response-headers policy's custom/security/CORS headers, an S3 or custom origin fetch (a custom origin is dialed at its own `DomainName`/port exactly as configured, trusting the gateway's own TLS cert alongside system roots under `USE_SSL`; a stacked distribution — an origin that is itself a CloudFront distribution — is refused with a 403 instead of recursing), and CloudFront's own added headers. `X-Forwarded-For` always carries the viewer's chain plus the client IP regardless of forwarding policy, a function event's `viewer.ip` is the real TCP peer, and a header with several values is forwarded to the origin as one comma-joined line (RFC 9110 §5.3). `DomainName` is now the real `d` + 13-character shape, independent of the distribution's Id. `ListCachePolicies`/`ListOriginRequestPolicies`/`ListResponseHeadersPolicies` seed the AWS-managed catalog (7 cache, 8 origin-request, 5 response-headers policies with a published configuration), so the Terraform data sources that resolve them by name (e.g. `Managed-CachingDisabled`) now find them. `CreateMonitoringSubscription`/`GetMonitoringSubscription`/`DeleteMonitoringSubscription` store a distribution's real-time-metrics subscription status. Not modelled: caching itself, WAF, logging, geo/IP restrictions, custom error pages, signed URLs/cookies, Lambda@Edge, CORS preflight responses, and origins addressed by an AWS hostname (ALB, Function URL, execute-api, S3 website endpoints) — refused with a 502 before a socket opens. Contributed by @skialpine (#1852).
-### Fixed
-- **API Gateway v2 (WebSocket API) — `$connect` authorization** — a `CUSTOM` `$connect` route ran no authorizer. It now runs its REQUEST authorizer, refuses the handshake with 401, 403 or 500 and passes `principalId` and the context to `requestContext.authorizer` of the connection's events. `CreateAuthorizer`, `CreateRoute`, `UpdateRoute` and the CloudFormation resources refuse a JWT authorizer, JWT route authorization and authorization on a route other than `$connect` with `BadRequestException`, as AWS does; a JWT `$connect` route kept in saved state refuses the handshake with 500 instead of validating the token. `requestContext.stage` names the stage in the connection URL instead of `$default`.
-
-
- **CloudFormation — a change set with only new stack tags lists them** — it ended `FAILED` with "didn't contain changes"; it now lists each resource the stack holds, other than a custom resource or wait condition, as a `Modify` with `Scope: Tags`, and stack tags given in another order are no change for a change set or `UpdateStack`. Contributed by @iot-rocket.
- **CloudFormation — change sets list what a change reaches** — a resource that references a replaced resource, an attribute of a modified resource or a changed parameter is now listed as a `Modify` whose detail names the cause (`ChangeSource` `ResourceReference`, `ResourceAttribute` or `ParameterReference`, with `CausingEntity`). Contributed by @iot-rocket.
-- **CloudFormation — change sets list what a change reaches** — a resource that references a replaced resource, an attribute of a modified resource or a changed parameter is now listed as a `Modify` whose detail names the cause (`ChangeSource` `ResourceReference`, `ResourceAttribute` or `ParameterReference`, with `CausingEntity`). Contributed by @iot-rocket.
-- **ECS — `DescribeClusters` statistics** — `include=["STATISTICS"]` returns the sixteen running/pending task and active/draining service counters per launch type instead of an empty list.
-- **ECS — `DescribeClusters` honours `include`** — `settings` and `tags` come back empty and `attachments` and `configuration` are left out unless requested, `CreateCluster` keeps `configuration`, and a CloudFormation cluster reports its tags and the default `containerInsights` setting.
-- **ECS — `DescribeClusters` honours `include`** — `settings` and `tags` come back empty and `attachments` and `configuration` are left out unless requested, `CreateCluster` keeps `configuration`, and a CloudFormation cluster reports its tags and the default `containerInsights` setting.
-- **CloudFormation — `AWS::S3::MultiRegionAccessPoint` and `AWS::AutoScaling::LaunchConfiguration` are replaced on update** — every property of both types is create-only, so a change now creates the resource under a new generated name and deletes the old one after the update (a `Regions` change was dropped and the stack reported the alias as the physical id, a launch configuration was overwritten under its old name), fails with the custom-named-resource error when the name is explicit, and is reported as `Replacement: True` in a change set.
+- **ECS — `DescribeClusters` statistics** — `include=["STATISTICS"]` returns the sixteen running/pending task and active/draining service counters per launch type instead of an empty list. Contributed by @iot-rocket.
+- **ECS — `DescribeClusters` honours `include`** — `settings` and `tags` come back empty and `attachments` and `configuration` are left out unless requested, `CreateCluster` keeps `configuration`, and a CloudFormation cluster reports its tags and the default `containerInsights` setting. Contributed by @iot-rocket.
+- **CloudFormation — `AWS::S3::MultiRegionAccessPoint` and `AWS::AutoScaling::LaunchConfiguration` are replaced on update** — every property of both types is create-only, so a change now creates the resource under a new generated name and deletes the old one after the update (a `Regions` change was dropped and the stack reported the alias as the physical id, a launch configuration was overwritten under its old name), fails with the custom-named-resource error when the name is explicit, and is reported as `Replacement: True` in a change set. Contributed by @iot-rocket.
- **CloudFormation — `AWS::SQS::Queue` with `FifoQueue` gets a generated `.fifo` name** — `FifoQueue: true` without a `QueueName` creates a FIFO queue with a generated `.fifo` name instead of a standard queue, and a `QueueName` whose `.fifo` suffix disagrees with `FifoQueue` fails the resource. Contributed by @iot-rocket.
-- **RDS — `DescribeDBClusterSnapshotAttributes`** — was unimplemented (`InvalidAction: Unknown RDS action`), so Terraform's `aws_db_cluster_snapshot` resource failed on read (`reading RDS DB Cluster Snapshot … attribute`) after creating the snapshot. Returns the `restore` attribute with no shared accounts (the manual-snapshot default); `ModifyDBClusterSnapshotAttribute` is not implemented. Unknown snapshot ids answer `DBClusterSnapshotNotFoundFault`.
-- **Lambda — Docker executor under `USE_SSL=1` when MiniStack runs in a container** — the gateway certificate, CA bundle and Java truststore were bind-mounted into every Lambda container from MiniStack's own filesystem (`MINISTACK_SSL_CERT`, or the generated `ministack-tls/server.crt` under the temp directory), paths the host Docker daemon cannot see, so every invocation failed with `bind source path does not exist`. In a container they are now copied into the Lambda container, as function code already is.
-- **ECS — forced rolling deployments replace tasks and refresh images** — `UpdateService(forceNewDeployment=True)` replaces tasks even when the task definition is unchanged or omitted. Rolling deployments preserve scheduling limits, wait for replacement tasks to stabilize, and retain circuit-breaker rollback; zero-sized deployments drain old tasks and complete. Docker-backed services pin image digests per deployment, honor `versionConsistency: disabled`, and use Secrets Manager credentials for private registries. Tasks report resolved image digests and Fargate platform fields. Cached images remain usable on both launch types, including MiniStack ECR images, preserving MiniStack’s intentional offline fallback.
-- **IoT — mTLS trusts every registered device certificate** — the listener no longer fails the TLS handshake for an ACTIVE certificate whose CA was deactivated or deleted before its first connect, or that was registered without a CA; as on AWS, only the certificate's own status refuses it.
-### Added
-
-- **Athena — databases, DDL, and Trino-style table references** — `ListDatabases` and `GetDatabase` read the Glue Data Catalog; `CREATE EXTERNAL TABLE` and `DROP TABLE` apply to it, creating the Glue table Athena would (formats, SerDe, TBLPROPERTIES); `CREATE TABLE` without `EXTERNAL` is rejected as Athena rejects it, and Iceberg tables are not supported; a query may name a table as `"awsdatacatalog"."db"."t"` or `db.t`.
+- **CloudFormation — nested stack with a `Transform`** — under `AUTH=true`, a nested stack whose template declares a `Transform` or calls `Fn::Transform` fails with `Requires capabilities : [CAPABILITY_AUTO_EXPAND]` unless the parent acknowledged `CAPABILITY_AUTO_EXPAND`, as on AWS. Contributed by @iot-rocket.
## [1.5.20] — 2026-10-01
diff --git a/README.md b/README.md
index 14cd9804..f02856db 100644
--- a/README.md
+++ b/README.md
@@ -442,7 +442,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, CreateRequestValidator, GetRequestValidators, GetRequestValidator, UpdateRequestValidator, DeleteRequestValidator | 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`, `authenticate-oidc` actions; data plane via `{lb-name}.alb.localhost` Host header or `/_alb/{lb-name}/` path prefix |
| **KMS** | CreateKey, ReplicateKey, ListKeys, DescribeKey, GetPublicKey, Sign, Verify, Encrypt, Decrypt, GenerateMac, VerifyMac, GenerateDataKey, GenerateDataKeyWithoutPlaintext, GenerateDataKeyPair, GenerateDataKeyPairWithoutPlaintext, GenerateRandom, CreateAlias, DeleteAlias, ListAliases, UpdateAlias, UpdateKeyDescription, EnableKeyRotation, DisableKeyRotation, GetKeyRotationStatus, GetKeyPolicy, PutKeyPolicy, ListKeyPolicies, EnableKey, DisableKey, ScheduleKeyDeletion, CancelKeyDeletion, TagResource, UntagResource, ListResourceTags, GetParametersForImport, ImportKeyMaterial, DeleteImportedKeyMaterial, CreateGrant, ListGrants, RevokeGrant, RetireGrant | 41 actions; multi-Region keys: `CreateKey` with `MultiRegion` mints an `mrk-` key and `ReplicateKey` creates a same-id replica in another region sharing the key material (ciphertext from the primary decrypts against the replica; a primary with live replicas waits in `PendingReplicaDeletion`); 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; grants (stored and listed, not evaluated for authorization); 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, CreateMonitoringSubscription, GetMonitoringSubscription, DeleteMonitoringSubscription, 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, **Public keys / key groups**: CreatePublicKey, GetPublicKey, GetPublicKeyConfig, UpdatePublicKey, DeletePublicKey, ListPublicKeys, CreateKeyGroup, GetKeyGroup, GetKeyGroupConfig, UpdateKeyGroup, DeleteKeyGroup, ListKeyGroups, **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; `ListCachePolicies`/`ListOriginRequestPolicies`/`ListResponseHeadersPolicies` seed the AWS-managed catalog (7 cache, 8 origin-request, 5 response-headers policies with a published configuration), immutable and unaffected by `/_ministack/reset`; a monitoring subscription is stored on its distribution, `CreateMonitoringSubscription` upserting it; **CloudFront Functions** are API stubs (in-memory code + DEVELOPMENT/LIVE ETags for Terraform `aws_cloudfront_function`) with `KeyValueStoreAssociations` round-tripped — no `TestFunction`; `UpdateFunction` clears the emulated LIVE stage until `PublishFunction` runs again; **data plane**: a viewer request to a distribution's `DomainName` (the real `d` + 13-character shape, or the resolvable `<label>.cloudfront.<MINISTACK_HOST>` form) is actually served — ordered cache-behavior path matching, viewer-protocol/allowed-methods/default-root-object enforcement, cache-policy and origin-request-policy forwarding (plus legacy `ForwardedValues`), `viewer-request`/`viewer-response` CloudFront Functions evaluated on Node (`cloudfront-js-1.0`/`-2.0`, the same worker pool AppSync resolvers use), response-headers-policy custom/security/CORS headers, and an S3 or custom origin fetch (a custom origin is dialed at its own DomainName/port exactly as configured; an explicit `:port` in the DomainName, which MiniStack's own endpoints such as an HTTP API's `apiEndpoint` carry, overrides `HTTPPort`/`HTTPSPort`); not modelled: caching itself, WAF, logging, geo/IP restrictions, custom error pages, signed URLs/cookies, Lambda@Edge, CORS preflight responses, and origins addressed by an AWS hostname (ALB, Function URL, execute-api, S3 website endpoints) — refused with a 502 before a socket opens, since MiniStack never dials a real AWS host; **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); key groups validate `Items` against real public keys, and deleting either while still referenced (`PublicKeyInUse` / `ResourceInUse`) is rejected |
+| **CloudFront** | CreateDistribution, GetDistribution, GetDistributionConfig, ListDistributions, UpdateDistribution, DeleteDistribution, CreateInvalidation, ListInvalidations, GetInvalidation, CreateOriginAccessControl, GetOriginAccessControl, GetOriginAccessControlConfig, ListOriginAccessControls, UpdateOriginAccessControl, DeleteOriginAccessControl, CreateMonitoringSubscription, GetMonitoringSubscription, DeleteMonitoringSubscription, 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, **Public keys / key groups**: CreatePublicKey, GetPublicKey, GetPublicKeyConfig, UpdatePublicKey, DeletePublicKey, ListPublicKeys, CreateKeyGroup, GetKeyGroup, GetKeyGroupConfig, UpdateKeyGroup, DeleteKeyGroup, ListKeyGroups, **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; the AWS-managed cache (7), origin request (8) and response headers (5) policies are listed and immutable; `CreateMonitoringSubscription` upserts; functions keep DEVELOPMENT/LIVE stages and `KeyValueStoreAssociations`, and `UpdateFunction` clears LIVE until `PublishFunction` runs again; **data plane**: a request to a distribution's `DomainName` (`d` + 13 characters, or `<label>.cloudfront.<MINISTACK_HOST>`) is served through its cache behaviors, policies, `viewer-request` / `viewer-response` CloudFront Functions (on Node) and an S3 or custom origin (read only as the bucket policy allows: OAC, OAI or public); not modelled: caching, WAF, logging, geo restrictions, custom error pages, signed URLs, Lambda@Edge, CORS preflight, and origins at AWS hostnames (502); **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); key groups validate `Items` against real public keys, and deleting either while still referenced (`PublicKeyInUse` / `ResourceInUse`) is rejected |
| **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 |
@@ -603,12 +603,12 @@ MiniStack expands AWS SAM transforms (`Transform: AWS::Serverless-2016-10-31`) i
| Service | Operations | Notes |
|---------|-----------|-------|
| **ECS** | CreateCluster, DeleteCluster, DescribeClusters, ListClusters, UpdateCluster, UpdateClusterSettings, PutClusterCapacityProviders, RegisterTaskDefinition, DeregisterTaskDefinition, DescribeTaskDefinition, ListTaskDefinitions, ListTaskDefinitionFamilies, DeleteTaskDefinitions, CreateService, DeleteService, DescribeServices, UpdateService, ListServices, ListServicesByNamespace, RunTask, StopTask, DescribeTasks, ListTasks, ExecuteCommand, UpdateTaskProtection, GetTaskProtection, CreateCapacityProvider, UpdateCapacityProvider, DeleteCapacityProvider, DescribeCapacityProviders, TagResource, UntagResource, ListTagsForResource, ListAccountSettings, PutAccountSetting, PutAccountSettingDefault, DeleteAccountSetting, PutAttributes, DeleteAttributes, ListAttributes, DescribeServiceDeployments, ListServiceDeployments, DescribeServiceRevisions, SubmitTaskStateChange, SubmitContainerStateChange, SubmitAttachmentStateChanges, DiscoverPollEndpoint | 47 actions; `RunTask` starts real Docker containers via Docker socket; full Terraform ECS coverage |
-| **RDS** | CreateDBInstance, DeleteDBInstance, DescribeDBInstances, ModifyDBInstance, StartDBInstance, StopDBInstance, RebootDBInstance, CreateDBInstanceReadReplica, RestoreDBInstanceFromDBSnapshot, CreateDBCluster, DeleteDBCluster, DescribeDBClusters, ModifyDBCluster, StartDBCluster, StopDBCluster, FailoverDBCluster, SwitchoverGlobalCluster, FailoverGlobalCluster, CreateDBSnapshot, DeleteDBSnapshot, DescribeDBSnapshots, CreateDBClusterSnapshot, DescribeDBClusterSnapshots, DeleteDBClusterSnapshot, CreateDBSubnetGroup, DeleteDBSubnetGroup, DescribeDBSubnetGroups, ModifyDBSubnetGroup, CreateDBParameterGroup, DeleteDBParameterGroup, DescribeDBParameterGroups, DescribeDBParameters, ModifyDBParameterGroup, CreateDBClusterParameterGroup, DescribeDBClusterParameterGroups, DeleteDBClusterParameterGroup, DescribeDBClusterParameters, ModifyDBClusterParameterGroup, CreateOptionGroup, DeleteOptionGroup, DescribeOptionGroups, DescribeOptionGroupOptions, ListTagsForResource, AddTagsToResource, RemoveTagsFromResource, DescribeDBEngineVersions, DescribeOrderableDBInstanceOptions, CreateGlobalCluster, DescribeGlobalClusters, DeleteGlobalCluster, RemoveFromGlobalCluster, ModifyGlobalCluster, CreateDBProxy, DescribeDBProxies, ModifyDBProxy, DeleteDBProxy, CreateDBProxyEndpoint, DescribeDBProxyEndpoints, ModifyDBProxyEndpoint, DeleteDBProxyEndpoint, DescribeDBProxyTargetGroups, ModifyDBProxyTargetGroup, RegisterDBProxyTargets, DeregisterDBProxyTargets, DescribeDBProxyTargets | `CreateDBInstance` spins up real Postgres/MySQL Docker container, returns actual `host:port` endpoint; a cluster's endpoint is its stable AWS-shaped name, registered as a Docker network alias on the backing container (on user-defined networks; the default bridge keeps the address form), so a stored endpoint keeps resolving after the container is replaced; Aurora MySQL 8.0/8.4 compatibility objects (`AWSAuthenticationPlugin`, RDS/Aurora stored procedures, predefined S3 roles) install automatically for supported MySQL images so Aurora provisioning workflows run. IAM database authentication on MySQL and Aurora MySQL 8.0/8.4 (not MariaDB): a user created `IDENTIFIED WITH AWSAuthenticationPlugin AS 'RDS'` logs in with an SDK-generated auth token over `mysql_clear_password` when the instance or cluster has `IAMDatabaseAuthenticationEnabled`; with `AUTH=true` the token and the `rds-db:connect` policy are verified too. Planned Aurora MySQL switchover (`SwitchoverGlobalCluster`, and `FailoverGlobalCluster` without data loss allowed) performs a coordinated no-data-loss writer exchange on provisioned two-member MySQL 8 global clusters; other configurations keep the metadata-only path. RDS Proxy is control plane only — proxies, proxy endpoints, the default target group and registered targets are stored and returned with the AWS shapes, but nothing listens on the proxy endpoint hostname; clients keep connecting to the instance or cluster endpoint |
+| **RDS** | CreateDBInstance, DeleteDBInstance, DescribeDBInstances, ModifyDBInstance, StartDBInstance, StopDBInstance, RebootDBInstance, CreateDBInstanceReadReplica, RestoreDBInstanceFromDBSnapshot, CreateDBCluster, DeleteDBCluster, DescribeDBClusters, ModifyDBCluster, StartDBCluster, StopDBCluster, FailoverDBCluster, SwitchoverGlobalCluster, FailoverGlobalCluster, CreateDBSnapshot, DeleteDBSnapshot, DescribeDBSnapshots, CreateDBClusterSnapshot, DescribeDBClusterSnapshots, DescribeDBClusterSnapshotAttributes, DeleteDBClusterSnapshot, CreateDBSubnetGroup, DeleteDBSubnetGroup, DescribeDBSubnetGroups, ModifyDBSubnetGroup, CreateDBParameterGroup, DeleteDBParameterGroup, DescribeDBParameterGroups, DescribeDBParameters, ModifyDBParameterGroup, CreateDBClusterParameterGroup, DescribeDBClusterParameterGroups, DeleteDBClusterParameterGroup, DescribeDBClusterParameters, ModifyDBClusterParameterGroup, CreateOptionGroup, DeleteOptionGroup, DescribeOptionGroups, DescribeOptionGroupOptions, ListTagsForResource, AddTagsToResource, RemoveTagsFromResource, DescribeDBEngineVersions, DescribeOrderableDBInstanceOptions, CreateGlobalCluster, DescribeGlobalClusters, DeleteGlobalCluster, RemoveFromGlobalCluster, ModifyGlobalCluster, CreateDBProxy, DescribeDBProxies, ModifyDBProxy, DeleteDBProxy, CreateDBProxyEndpoint, DescribeDBProxyEndpoints, ModifyDBProxyEndpoint, DeleteDBProxyEndpoint, DescribeDBProxyTargetGroups, ModifyDBProxyTargetGroup, RegisterDBProxyTargets, DeregisterDBProxyTargets, DescribeDBProxyTargets | `CreateDBInstance` spins up real Postgres/MySQL Docker container, returns actual `host:port` endpoint; a cluster's endpoint is its stable AWS-shaped name, registered as a Docker network alias on the backing container (on user-defined networks; the default bridge keeps the address form), so a stored endpoint keeps resolving after the container is replaced; Aurora MySQL 8.0/8.4 compatibility objects (`AWSAuthenticationPlugin`, RDS/Aurora stored procedures, predefined S3 roles) install automatically for supported MySQL images so Aurora provisioning workflows run. IAM database authentication on MySQL and Aurora MySQL 8.0/8.4 (not MariaDB): a user created `IDENTIFIED WITH AWSAuthenticationPlugin AS 'RDS'` logs in with an SDK-generated auth token over `mysql_clear_password` when the instance or cluster has `IAMDatabaseAuthenticationEnabled`; with `AUTH=true` the token and the `rds-db:connect` policy are verified too. Planned Aurora MySQL switchover (`SwitchoverGlobalCluster`, and `FailoverGlobalCluster` without data loss allowed) performs a coordinated no-data-loss writer exchange on provisioned two-member MySQL 8 global clusters; other configurations keep the metadata-only path. RDS Proxy is control plane only — proxies, proxy endpoints, the default target group and registered targets are stored and returned with the AWS shapes, but nothing listens on the proxy endpoint hostname; clients keep connecting to the instance or cluster endpoint |
| **Aurora DSQL** | CreateCluster, GetCluster, UpdateCluster, DeleteCluster, ListClusters, PutClusterPolicy, GetClusterPolicy, DeleteClusterPolicy, CreateStream, GetStream, ListStreams, DeleteStream, GetVpcEndpointServiceName, TagResource, UntagResource, ListTagsForResource | `CreateCluster` spins up a real `postgres:16-alpine` Docker container fronted by a validating PG wire-protocol proxy that **fails like real DSQL** — rejects unsupported SQL (`serial`, extensions, temp tables, ...), accepts `CREATE INDEX ASYNC` + `sys.jobs`; endpoint is `localhost:<port>`; any password accepted |
| **ElastiCache** | CreateCacheCluster, DeleteCacheCluster, DescribeCacheClusters, ModifyCacheCluster, RebootCacheCluster, CreateReplicationGroup, DeleteReplicationGroup, DescribeReplicationGroups, ModifyReplicationGroup, IncreaseReplicaCount, DecreaseReplicaCount, CreateCacheSubnetGroup, DescribeCacheSubnetGroups, ModifyCacheSubnetGroup, DeleteCacheSubnetGroup, CreateCacheParameterGroup, DescribeCacheParameterGroups, ModifyCacheParameterGroup, ResetCacheParameterGroup, DeleteCacheParameterGroup, DescribeCacheParameters, DescribeCacheEngineVersions, CreateUser, DescribeUsers, DeleteUser, ModifyUser, CreateUserGroup, DescribeUserGroups, DeleteUserGroup, ModifyUserGroup, ListTagsForResource, AddTagsToResource, RemoveTagsFromResource, CreateSnapshot, DeleteSnapshot, DescribeSnapshots, CreateServerlessCache, DescribeServerlessCaches, ModifyServerlessCache, DeleteServerlessCache, DescribeEvents | `CreateCacheCluster` spins up a real Redis, Valkey, or Memcached Docker container (`Engine=redis`/`valkey`/`memcached`); a serverless cache gets a TLS-only Valkey/Redis container whose certificate chains to the CA at `GET /_ministack/elasticache/ca.pem` |
| **Glue** | CreateDatabase, DeleteDatabase, GetDatabase, GetDatabases, UpdateDatabase, CreateTable, DeleteTable, GetTable, GetTables, UpdateTable, BatchDeleteTable, CreatePartition, DeletePartition, GetPartition, GetPartitions, BatchCreatePartition, BatchGetPartition, CreatePartitionIndex, GetPartitionIndexes, GetColumnStatisticsForTable, UpdateColumnStatisticsForTable, DeleteColumnStatisticsForTable, GetColumnStatisticsForPartition, UpdateColumnStatisticsForPartition, DeleteColumnStatisticsForPartition, CreateConnection, DeleteConnection, GetConnection, GetConnections, CreateCrawler, DeleteCrawler, GetCrawler, GetCrawlers, UpdateCrawler, StartCrawler, StopCrawler, GetCrawlerMetrics, CreateJob, DeleteJob, GetJob, GetJobs, UpdateJob, StartJobRun, GetJobRun, GetJobRuns, BatchStopJobRun, CreateTrigger, GetTrigger, DeleteTrigger, UpdateTrigger, StartTrigger, StopTrigger, ListTriggers, BatchGetTriggers, GetTriggers, CreateWorkflow, GetWorkflow, DeleteWorkflow, UpdateWorkflow, StartWorkflowRun, CreateUserDefinedFunction, UpdateUserDefinedFunction, DeleteUserDefinedFunction, GetUserDefinedFunction, GetUserDefinedFunctions, CreateSecurityConfiguration, DeleteSecurityConfiguration, GetSecurityConfiguration, GetSecurityConfigurations, CreateClassifier, GetClassifier, GetClassifiers, DeleteClassifier, TagResource, UntagResource, GetTags | Python shell jobs actually execute via subprocess; Spark jobs run on the official `amazon/aws-glue-libs` PySpark image (`GlueVersion: 4.0` / `3.0`) on MiniStack's Docker network; Glue-backed Iceberg REST catalog at `/iceberg` (signed with the `glue` credential scope, prefix `catalogs/{catalog}`) — read path for tables whose `metadata_location` parameter points at a metadata.json on MiniStack S3, e.g. DuckDB `ATTACH ... (TYPE iceberg, ENDPOINT 'localhost:4566/iceberg', AUTHORIZATION_TYPE 'sigv4')` with `USE_SSL=1` |
| **S3 Tables** | CreateTableBucket, ListTableBuckets, GetTableBucket, DeleteTableBucket, CreateNamespace, ListNamespaces, GetNamespace, DeleteNamespace, CreateTable, ListTables, GetTable, DeleteTable, GetTableMetadataLocation, UpdateTableMetadataLocation | Iceberg-format tables; embedded Iceberg REST catalog at `/iceberg` so Spark jobs (`spark.sql.catalog.*.type=rest`) can create / load / commit without an external catalog; data files in S3, metadata in memory |
-| **Athena** | StartQueryExecution, GetQueryExecution, GetQueryResults, StopQueryExecution, ListQueryExecutions, BatchGetQueryExecution, CreateWorkGroup, DeleteWorkGroup, GetWorkGroup, ListWorkGroups, UpdateWorkGroup, CreateNamedQuery, DeleteNamedQuery, GetNamedQuery, ListNamedQueries, BatchGetNamedQuery, CreateDataCatalog, GetDataCatalog, ListDataCatalogs, DeleteDataCatalog, UpdateDataCatalog, CreatePreparedStatement, GetPreparedStatement, DeletePreparedStatement, ListPreparedStatements, GetTableMetadata, ListTableMetadata, TagResource, UntagResource, ListTagsForResource | Real SQL via **DuckDB** when installed (`pip install duckdb`), otherwise returns mock results; result pagination; column type metadata |
+| **Athena** | StartQueryExecution, GetQueryExecution, GetQueryResults, StopQueryExecution, ListQueryExecutions, BatchGetQueryExecution, CreateWorkGroup, DeleteWorkGroup, GetWorkGroup, ListWorkGroups, UpdateWorkGroup, CreateNamedQuery, DeleteNamedQuery, GetNamedQuery, ListNamedQueries, BatchGetNamedQuery, CreateDataCatalog, GetDataCatalog, ListDataCatalogs, DeleteDataCatalog, UpdateDataCatalog, CreatePreparedStatement, GetPreparedStatement, DeletePreparedStatement, ListPreparedStatements, GetDatabase, ListDatabases, GetTableMetadata, ListTableMetadata, TagResource, UntagResource, ListTagsForResource | Real SQL via **DuckDB** when installed (`pip install duckdb`), otherwise returns mock results; databases and tables come from the Glue Data Catalog, and `CREATE EXTERNAL TABLE` / `DROP TABLE` change it; result pagination; column type metadata |
| **Firehose** | CreateDeliveryStream, DeleteDeliveryStream, DescribeDeliveryStream, ListDeliveryStreams, PutRecord, PutRecordBatch, UpdateDestination, TagDeliveryStream, UntagDeliveryStream, ListTagsForDeliveryStream, StartDeliveryStreamEncryption, StopDeliveryStreamEncryption | S3 destinations write records to the local S3 emulator; all other destination types buffer in-memory; concurrency-safe `UpdateDestination` via `VersionId`; `DeliveryStreamType=KinesisStreamAsSource` consumes records from the source Kinesis stream on `PutRecord` / `PutRecords` and forwards them to the configured S3 destination, honoring `Prefix` and `DeliveryStartTimestamp`; `ProcessingConfiguration.Processors[].Type=Lambda` invoked per-batch with the AWS-shape event (`{invocationId, deliveryStreamArn, region, records:[{recordId, approximateArrivalTimestamp, data}]}`) — `Ok` records ship the transformed data downstream, `Dropped`/`ProcessingFailed` are omitted, Lambda failures pass records through unchanged (best-effort per AWS) |
| **Route53** | CreateHostedZone, GetHostedZone, DeleteHostedZone, ListHostedZones, ListHostedZonesByName, UpdateHostedZoneComment, ChangeResourceRecordSets (CREATE/UPSERT/DELETE), ListResourceRecordSets, GetChange, CreateHealthCheck, GetHealthCheck, DeleteHealthCheck, ListHealthChecks, UpdateHealthCheck, ChangeTagsForResource, ListTagsForResource | REST/XML protocol; SOA + NS records auto-created; CallerReference idempotency; alias records, weighted/failover/latency routing; marker-based pagination |
| **EC2** | RunInstances, DescribeInstances, DescribeInstanceAttribute, DescribeInstanceTypes, DescribeVpcAttribute, TerminateInstances, StopInstances, StartInstances, RebootInstances, AssociateIamInstanceProfile, DescribeIamInstanceProfileAssociations, DisassociateIamInstanceProfile, ReplaceIamInstanceProfileAssociation, DescribeImages, RegisterImage, DeregisterImage, ModifyImageAttribute, DescribeImageAttribute, ResetImageAttribute, CreateSecurityGroup, DeleteSecurityGroup, DescribeSecurityGroups, AuthorizeSecurityGroupIngress, RevokeSecurityGroupIngress, AuthorizeSecurityGroupEgress, RevokeSecurityGroupEgress, DescribeSecurityGroupRules, CreateKeyPair, DeleteKeyPair, DescribeKeyPairs, ImportKeyPair, CreatePlacementGroup, DeletePlacementGroup, DescribePlacementGroups, CreateVpc, DeleteVpc, DescribeVpcs, ModifyVpcAttribute, CreateSubnet, DeleteSubnet, DescribeSubnets, ModifySubnetAttribute, CreateInternetGateway, DeleteInternetGateway, DescribeInternetGateways, AttachInternetGateway, DetachInternetGateway, CreateRouteTable, DeleteRouteTable, DescribeRouteTables, AssociateRouteTable, DisassociateRouteTable, ReplaceRouteTableAssociation, CreateRoute, ReplaceRoute, DeleteRoute, CreateNetworkInterface, DeleteNetworkInterface, DescribeNetworkInterfaces, AttachNetworkInterface, DetachNetworkInterface, CreateVpcEndpoint, DeleteVpcEndpoints, DescribeVpcEndpoints, ModifyVpcEndpoint, DescribePrefixLists, DescribeAvailabilityZones, AllocateAddress, ReleaseAddress, AssociateAddress, DisassociateAddress, DescribeAddresses, DescribeAddressesAttribute, CreateTags, DeleteTags, DescribeTags, CreateNatGateway, DescribeNatGateways, DeleteNatGateway, CreateNetworkAcl, DescribeNetworkAcls, DeleteNetworkAcl, CreateNetworkAclEntry, DeleteNetworkAclEntry, ReplaceNetworkAclEntry, ReplaceNetworkAclAssociation, CreateFlowLogs, DescribeFlowLogs, DeleteFlowLogs, CreateVpcPeeringConnection, AcceptVpcPeeringConnection, DescribeVpcPeeringConnections, DeleteVpcPeeringConnection, CreateDhcpOptions, AssociateDhcpOptions, DescribeDhcpOptions, DeleteDhcpOptions, CreateEgressOnlyInternetGateway, DescribeEgressOnlyInternetGateways, DeleteEgressOnlyInternetGateway, CreateManagedPrefixList, DescribeManagedPrefixLists, GetManagedPrefixListEntries, ModifyManagedPrefixList, DeleteManagedPrefixList, CreateVpnGateway, DescribeVpnGateways, AttachVpnGateway, DetachVpnGateway, DeleteVpnGateway, EnableVgwRoutePropagation, DisableVgwRoutePropagation, CreateCustomerGateway, DescribeCustomerGateways, DeleteCustomerGateway, DescribeInstanceCreditSpecifications, DescribeInstanceMaintenanceOptions, DescribeInstanceAutoRecoveryAttribute, ModifyInstanceMaintenanceOptions, DescribeInstanceTopology, DescribeSpotInstanceRequests, DescribeCapacityReservations, DescribeInstanceStatus, DescribeVpcClassicLink, DescribeVpcClassicLinkDnsSupport, CreateLaunchTemplate, CreateLaunchTemplateVersion, DescribeLaunchTemplates, DescribeLaunchTemplateVersions, ModifyLaunchTemplate, DeleteLaunchTemplate, CreateFleet, DescribeFleets | 133 actions; EC2 Fleet (`CreateFleet` / `DescribeFleets`) with `instant`-type synchronous launch and `maintain` / `request` async fulfillment, multi-config × multi-override round-robin capacity distribution, and `DefaultTargetCapacityType`-driven spot/on-demand selection — unblocks Karpenter / Cluster Autoscaler local validation; IAM instance profile association lifecycle is persisted (`Associate*`, `Describe*`, `Replace*`, `Disassociate*`) so Terraform `aws_instance.iam_instance_profile` round-trips without drift; `AuthorizeSecurityGroupIngress` is idempotent on duplicate rules (same behavior as egress; avoids Terraform re-apply failures); metadata-only instance records by default — `RegisterImage` with a container reference makes an AMI whose launches get a real box, so SSM Run Command returns a real exit code (see *Instances with a real box*); CreateVpc provisions per-VPC default route table, network ACL, and security group; cross-account AMI sharing via launch permissions (`ModifyImageAttribute` add/remove, `Group=all` makes the image public, `DescribeImages` shows shared images with the owner's `OwnerId`, `ExecutableUsers` honored, shared AMIs launch); full Terraform VPC module v6.6.0 compatible; VPN/Customer gateways, managed prefix lists, VPC endpoints with modify support; launch templates with versioning ($Latest/$Default) |
@@ -842,27 +842,12 @@ SDKs running inside the task fetch emulated credentials from the new
`/v2/credentials/<uuid>` endpoint and route service calls through MiniStack
end-to-end without any client config.
-For rolling (`ECS`) services, deployments pin tagged images to registry manifest
-digests, and `UpdateService(forceNewDeployment=True)` replaces tasks and resolves
-the tag again. `DescribeTasks` retains the original image URI and reports the
-resolved digest in `containers[].imageDigest`. Linux Fargate tag resolution
-requires platform `1.3.0` or newer; Windows Fargate requires `1.0.0` or newer.
-Fargate tasks report the resolved platform version: omitted or `LATEST` selects
-Linux `1.4.0` or Windows `1.0.0`. Linux tasks report platform family `Linux`.
-Both launch types attempt registry pulls, then use an available local image if
-the pull fails, including images tagged with MiniStack ECR URIs. This preserves
-MiniStack's offline execution; real AWS Fargate requires a registry image. Failed
-manifest lookups do not fail a deployment when every unresolved image is cached,
-even with the deployment circuit breaker enabled. Unresolved images do not
-receive an invented `imageDigest`. Offline fallback may use the original cached
-tag if the resolved digest is unavailable locally. Additional EC2 agent pull
-modes are not implemented.
-
-Private registry credentials for rolling services can be supplied through
-`containerDefinitions[].repositoryCredentials.credentialsParameter`, referencing
-a Secrets Manager secret containing JSON `username` and `password` strings.
-These credentials are used for both manifest resolution and image pulling.
-The registry must be reachable by the Docker daemon running the task containers.
+Rolling (`ECS`) services pin tagged images to manifest digests per deployment, and
+`UpdateService(forceNewDeployment=True)` resolves them again. A task pulls its image
+on start and falls back to the local image when the pull fails, including images
+tagged with MiniStack ECR URIs, so services run offline. Private registry credentials
+come from `repositoryCredentials.credentialsParameter` (a Secrets Manager secret with
+`username` and `password`).
---
@@ -912,7 +897,6 @@ The registry must be reachable by the Docker daemon running the task containers.
| `LAMBDA_WARM_TTL_SECONDS` | `300` | How long an idle warm Lambda container stays in the pool before the reaper evicts it |
| `LAMBDA_ACCOUNT_CONCURRENCY` | `0` | Account-level concurrent-invocation cap (0 = unbounded). Match real AWS by setting to `1000`. Used to simulate `ConcurrentInvocationLimitExceeded` throttles |
| `SFN_MOCK_CONFIG` | _(unset)_ | Path to JSON file for Step Functions mock testing; compatible with AWS SFN Local format. Also accepts `LOCALSTACK_SFN_MOCK_CONFIG` |
-| `CFN_ENFORCE_CAPABILITIES` | _(unset)_ | Set `1` to have CloudFormation refuse a template whose IAM resources or macros the request's `Capabilities` do not acknowledge (`InsufficientCapabilitiesException`, and on a nested stack a failed resource), as AWS always does, also without `AUTH=true`. Under `AUTH=true` the check always runs |
| `ATHENA_ENGINE` | `auto` | SQL engine for Athena: `auto`, `duckdb`, `mock` |
| `SMTP_HOST` | _(unset)_ | SMTP server for SES email relay (e.g. `mailhog:1025`). When set, SES SendEmail/SendRawEmail actually deliver mail. When unset, emails are stored in-memory only |
| `MINISTACK_APIGW_PROXY_TIMEOUT_SECONDS` | `30` | API Gateway v1/v2 HTTP `HTTP_PROXY` / `HTTP` integration: upstream request timeout (seconds) |
diff --git a/ministack/app.py b/ministack/app.py
index 21fe15e2..16315090 100644
--- a/ministack/app.py
+++ b/ministack/app.py
@@ -47,10 +47,7 @@ def _version() -> str:
# Matches host headers like "{apiId}.execute-api.<host>" or "{apiId}.execute-api.<host>:4566"
_EXECUTE_API_RE = re.compile(r"^([a-f0-9]{8})\.execute-api\." + re.escape(_MINISTACK_HOST) + r"(?::\d+)?$")
-# A CloudFront distribution's DomainName is always AWS-shaped
-# ("<label>.cloudfront.net"); matched the same way ALB's own
-# ".elb.amazonaws.com" / ".alb.localhost" pair is — the real AWS host
-# unconditionally, plus the resolvable local form built on MINISTACK_HOST.
+# A distribution's DomainName: <label>.cloudfront.net, or <label>.cloudfront.<MINISTACK_HOST>.
_CLOUDFRONT_HOST_RE = re.compile(
r"^([a-z0-9]+)\.cloudfront\.(?:net|" + re.escape(_MINISTACK_HOST) + r")(?::\d+)?$"
)
@@ -1741,12 +1738,7 @@ def _resolve_custom_domain_request(host: str, path: str):
def _parse_cloudfront_dataplane_host(host: str) -> str | None:
- """The distribution label addressed by ``host``, or None.
-
- Host-based only, like ALB's own ``.elb.amazonaws.com`` / ``.alb.localhost``
- pair and the S3-vhost check: a request whose first label doesn't match
- this shape isn't a CloudFront distribution request at all, so nothing
- existing changes."""
+ """The distribution label addressed by ``host``, or None."""
m = _CLOUDFRONT_HOST_RE.match(host.split(":")[0].lower())
return m.group(1) if m else None
@@ -1755,13 +1747,7 @@ async def _handle_cloudfront_dataplane_request(
host: str, path: str, raw_path: str, raw_query_string: str, method: str,
headers: dict, body: bytes, query_params: dict, client_ip: str,
):
- """Serve a CloudFront distribution's viewer traffic (Host-based:
- ``<label>.cloudfront.net`` or ``<label>.cloudfront.<MINISTACK_HOST>``).
- Checked before execute-api/lambda-url/S3-vhost/ALB host routing so a
- distribution's own viewer-request function — not the origin it fronts —
- sees the request first, exactly as a real CloudFront edge would. See
- ``cloudfront_dataplane.handle_request`` for what ``raw_path``/
- ``raw_query_string`` carry and why."""
+ """Serve a CloudFront distribution's viewer traffic, matched by Host."""
label = _parse_cloudfront_dataplane_host(host)
if label is None:
return None
diff --git a/ministack/core/cloudfront_js.py b/ministack/core/cloudfront_js.py
index 4bb1701d..c4162730 100644
--- a/ministack/core/cloudfront_js.py
+++ b/ministack/core/cloudfront_js.py
@@ -1,28 +1,8 @@
# Copyright (c) 2026 MiniStack Contributors. SPDX-License-Identifier: MIT
# Copies or substantial portions, including AI-assisted ports or rewrites, must retain this notice (see LICENSE).
-"""CloudFront Functions evaluation.
-
-A CloudFront Function is a top-level ``function handler(event) {...}``
-(runtime `cloudfront-js-1.0`) or an ES-module-style function that may use
-`import`/`async` (`cloudfront-js-2.0`; CloudFront Developer Guide, "Writing
-function code for CloudFront Functions"). Evaluation runs on the shared
-`core.node_pool` worker pool (see that module for the process management,
-JSON-line protocol, leasing, recycling, and timeout mechanics); each worker
-caches compiled functions by the hash of their source, the same pattern
-`core/appsync_js.py` uses for resolvers.
-
-Fidelity note: Node is more permissive than the real CloudFront Functions
-runtime, which is a restricted ECMAScript subset (CloudFront Developer
-Guide, "Writing function code for CloudFront Functions" > "Restrictions and
-limitations", and "JavaScript runtime 2.0 features for CloudFront
-Functions"). Function code that runs here can still be refused by real
-CloudFront at publish/associate time. This module reproduces one documented
-restriction that is cheap to enforce and otherwise easy to get wrong: the
-`require()`/`import` surface is limited to `crypto`, `querystring`, and the
-`Buffer` module, not Node's full module graph. Runtime 2.0 also documents a
-fourth module, `cloudfront` (the KeyValueStore, `import cf from
-'cloudfront'`), which is not modelled here — importing it fails the same way
-an unrecognized Node module would.
+"""CloudFront Functions on the shared Node worker pool (``core.node_pool``).
+
+Node is more permissive than the CloudFront runtime; only ``crypto``, ``querystring`` and ``buffer`` can be required.
"""
from __future__ import annotations
@@ -33,11 +13,7 @@ from ministack.core import node_pool
logger = logging.getLogger("cloudfront_functions")
-# Real CloudFront Functions quota execution at ~1ms (CloudFront Developer
-# Guide, "Quotas on functions"), too tight for a subprocess-per-call Node
-# evaluation. This instead guards only against a runaway (infinite-loop)
-# function, borrowing Lambda@Edge's 5s viewer-request/response timeout as
-# the closest documented CloudFront figure.
+# CloudFront Functions run in ~1ms; this timeout only stops a runaway function.
_EVAL_TIMEOUT = 5.0
_MAX_OLD_SPACE_MB = 256
_RECYCLE_AFTER = 1000
@@ -142,10 +118,7 @@ rl.on("line", async (line) => {
class CloudFrontFunctionError(Exception):
- """A function threw, failed to evaluate, or the worker could not run it
- at all — CloudFront answers every one of these with a 503 (CloudFront
- Developer Guide, "HTTP 503 status code" > "Lambda@Edge or CloudFront
- Function execution error")."""
+ """A function threw or could not run; CloudFront answers 503."""
_pool = node_pool.NodeWorkerPool(
diff --git a/ministack/services/apigateway_v1.py b/ministack/services/apigateway_v1.py
index 7ef71f07..b3bd106f 100644
--- a/ministack/services/apigateway_v1.py
+++ b/ministack/services/apigateway_v1.py
@@ -1505,9 +1505,7 @@ def _gateway_error_response(error, request):
# AWS renders the messageString of these types with a leading space (measured).
message_string = " " + message_string
if response_type == "AUTHORIZER_CONFIGURATION_ERROR":
- # $context.error.responseType renders as API_CONFIGURATION_ERROR, while the
- # response entry, its status and x-amzn-ErrorType stay those of
- # AUTHORIZER_CONFIGURATION_ERROR (measured).
+ # Rendered as API_CONFIGURATION_ERROR; entry, status and error type stay AUTHORIZER_* (measured).
rendered_type = "API_CONFIGURATION_ERROR"
variables = {
"context.error.message": "" if message is None else message,
diff --git a/ministack/services/athena.py b/ministack/services/athena.py
index 56f7aeb2..d1182edb 100644
--- a/ministack/services/athena.py
+++ b/ministack/services/athena.py
@@ -341,20 +341,13 @@ async def handle_request(method, path, headers, body, query_params):
# ---- SQL scanning ----
-#
-# Statements are read token by token, so a literal, a comment or a quoted name
-# is never structure: the "," in 'a, b' or the ")" in -- note) ends nothing.
_NAME_RE = re.compile(r"[A-Za-z_][A-Za-z0-9_]*")
_NUMBER_RE = re.compile(r"[0-9]+")
def _sql_tokens(sql):
- """``(kind, text, start, end)`` per token, skipping whitespace and comments.
-
- Kinds: ``name`` (bare, lower-cased), ``qname`` (``"x"`` or Hive's ``\\`x\\```, unquoted),
- ``str`` (``'x'`` as written), ``number``, ``punct`` (one character).
- """
+ """``(kind, text, start, end)`` per token: ``name`` (lower-cased), ``qname``, ``str``, ``number``, ``punct``."""
i, n = 0, len(sql)
while i < n:
ch = sql[i]
@@ -462,9 +455,7 @@ class _Cursor:
return re.sub(r"\\(.)", lambda m: {"t": "\t", "n": "\n"}.get(m.group(1), m.group(1)), value)
def dotted_name(self, max_parts):
- """The dotted name that is next, right-aligned in a ``max_parts`` tuple: with 3,
- ``t`` → ``(None, None, "t")`` and ``a.b.c`` → ``("a", "b", "c")``. None when no name is
- next, a ``.`` has nothing after it, or the name has more than ``max_parts`` parts."""
+ """The next dotted name, right-aligned in a ``max_parts`` tuple (``t`` → ``(None, None, "t")``)."""
parts = []
while True:
part = self.identifier()
@@ -479,7 +470,7 @@ class _Cursor:
@dataclass(frozen=True)
-class TableReference:
+class _TableReference:
"""One ``FROM``/``JOIN`` target and where its text sits in the query."""
database: str | None # None when the reference names only the table
@@ -497,13 +488,7 @@ _CLAUSE_KEYWORDS = frozenset(
def _table_references(query):
- """The ``FROM``/``JOIN`` targets of ``query``, in order, with CTE names left out.
-
- ``SELECT u.id FROM "awsdatacatalog"."acme"."users" u JOIN orders ON ...`` yields
- ``TableReference("acme", "users", 17, 48, aliased=True)`` and
- ``TableReference(None, "orders", 56, 62, aliased=False)``: a catalog qualifier is dropped,
- since Glue is the only catalog here, and a ``WITH users AS (...)`` name shadows a table.
- """
+ """The ``FROM``/``JOIN`` targets of ``query``, in order; CTE names shadow tables."""
cursor = _Cursor(query)
ctes = _cte_names(cursor)
cursor.reset()
@@ -522,7 +507,7 @@ def _table_references(query):
end = cursor.tokens[cursor.i - 1][3]
next_kind, next_text = cursor.peek()[:2]
aliased = next_kind == "qname" or (next_kind == "name" and next_text not in _CLAUSE_KEYWORDS)
- references.append(TableReference(database, table, start, end, aliased))
+ references.append(_TableReference(database, table, start, end, aliased))
return references
@@ -578,7 +563,7 @@ _DELIMITED = {
@dataclass(frozen=True)
-class CreateTable:
+class _CreateTable:
database: str | None # None when the statement names only the table
table: str
external: bool
@@ -601,18 +586,14 @@ class CreateTable:
@dataclass(frozen=True)
-class DropTable:
+class _DropTable:
database: str | None
table: str
if_exists: bool = False
def _parse_ddl(query):
- """The ``CreateTable`` or ``DropTable`` a statement is, or None.
-
- None also covers a CREATE this does not model (CTAS, ``SORTED BY``, an unknown ``STORED AS``)
- and anything followed by a second statement, so those reach the query engine as written.
- """
+ """The ``_CreateTable`` or ``_DropTable`` a statement is; None sends it to the query engine."""
cursor = _Cursor(query)
if cursor.tokens and cursor.tokens[-1][:2] == ("punct", ";"):
cursor.tokens.pop()
@@ -623,7 +604,7 @@ def _parse_ddl(query):
name = cursor.dotted_name(2)
if name is None or not cursor.at_end():
return None
- return DropTable(*name, if_exists=if_exists)
+ return _DropTable(*name, if_exists=if_exists)
if not cursor.keywords("create"):
return None
external = cursor.keywords("external")
@@ -691,7 +672,7 @@ def _parse_ddl(query):
output_format = _SERDE_TEXT_OUTPUT_FORMAT
else:
input_format, output_format, serde = stored_as
- return CreateTable(
+ return _CreateTable(
*name, external=external, columns=columns, input_format=input_format, output_format=output_format,
serde=row_format_serde or serde or _LAZY_SIMPLE_SERDE, serde_parameters=serde_parameters,
if_not_exists=if_not_exists, **clauses,
@@ -773,10 +754,7 @@ def _property_list(cursor):
def _delimited(cursor):
- """The SerDe parameters a ``ROW FORMAT DELIMITED`` clause sets; None when malformed.
-
- ``FIELDS TERMINATED BY ','`` → ``{"field.delim": ",", "serialization.format": ","}``.
- """
+ """The SerDe parameters a ``ROW FORMAT DELIMITED`` clause sets; None when malformed."""
parameters = {}
while True:
for words, parameter in _DELIMITED.items():
@@ -794,8 +772,7 @@ def _delimited(cursor):
def _create_table_input(ddl):
- """The Glue ``TableInput`` Athena writes for ``ddl``: TBLPROPERTIES and the table COMMENT
- become Parameters, STORED AS / ROW FORMAT become the formats and SerDe."""
+ """The Glue ``TableInput`` Athena writes for ``ddl``."""
parameters = {"EXTERNAL": "TRUE", "transient_lastDdlTime": str(int(time.time())), **ddl.table_properties}
if ddl.comment is not None:
parameters["comment"] = ddl.comment
@@ -825,8 +802,7 @@ def _create_table_input(ddl):
def _data_format(table_data):
- """The file format DuckDB reads a table as: its ``classification`` property when set, else the
- one its InputFormat or SerDe names (``MapredParquetInputFormat`` → ``parquet``), else ``csv``."""
+ """``classification`` when set, else the format its InputFormat or SerDe names, else ``csv``."""
classification = (table_data.get("Parameters") or {}).get("classification")
if classification:
return classification
@@ -852,7 +828,7 @@ def _start_query_execution(data):
# Athena rejects any malformed statement here; with no Trino parser, only this DDL rule is
# checked at submission and other errors fail the execution instead.
ddl = _parse_ddl(query)
- if isinstance(ddl, CreateTable) and not ddl.external and not ddl.iceberg:
+ if isinstance(ddl, _CreateTable) and not ddl.external and not ddl.iceberg:
return error_response_json(
"InvalidRequestException", "External keyword required for table type HIVE", 400,
extra={"AthenaErrorCode": "MALFORMED_QUERY"},
@@ -979,21 +955,7 @@ async def _run_duckdb(query, database):
async def _rewrite_data_paths(query, database):
- """Replace each Glue table reference with the DuckDB relation that reads its local S3 data.
-
- With Glue table ``acme.users`` at ``s3://acme-data/users/`` holding CSV::
-
- SELECT u.id FROM "awsdatacatalog"."acme"."users" u WHERE u.id > 1
- SELECT users.id FROM users WHERE users.id > 1
-
- become::
-
- SELECT u.id FROM '<data dir>/<account>/acme-data/users/**/*.csv' u WHERE u.id > 1
- SELECT users.id FROM '<data dir>/<account>/acme-data/users/**/*.csv' AS "users" WHERE users.id > 1
-
- The second gains an alias so ``users.id`` still resolves once the name is a path. A table
- not in Glue is left as written for DuckDB to reject.
- """
+ """Replace each Glue table reference with a DuckDB relation over its local S3 data."""
from ministack.services import glue as glue_svc
account_id = get_account_id()
@@ -1015,7 +977,7 @@ async def _rewrite_data_paths(query, database):
p = urlparse(s3_location)
stripped = f"{p.netloc}{p.path}".rstrip("/")
local_dir = f"{ATHENA_DATA_DIR}/{account_id}/{stripped}"
- if glob.glob(f"{local_dir}/**/*", recursive=True):
+ if next(glob.iglob(f"{local_dir}/**/*", recursive=True), None):
relation = f"'{local_dir}/**/*.{_data_format(table_data)}'" # DuckDB reads the files, or reports why not
else:
relation = _empty_relation(table_data)
@@ -1052,7 +1014,7 @@ def _run_ddl(ddl, database):
from ministack.services import glue as glue_svc
db_name = ddl.database or database or "default"
- if isinstance(ddl, DropTable):
+ if isinstance(ddl, _DropTable):
if f"{db_name}/{ddl.table}" not in glue_svc._tables:
if ddl.if_exists:
return {"columns": [], "rows": [], "column_types": []}
@@ -1646,7 +1608,10 @@ def _list_prepared_statements(data):
def _athena_database(db):
- return {"Name": db["Name"], "Description": db.get("Description", ""), "Parameters": db.get("Parameters") or {}}
+ database = {"Name": db["Name"], "Parameters": db.get("Parameters") or {}}
+ if db.get("Description"):
+ database["Description"] = db["Description"]
+ return database
def _get_database(data):
@@ -1664,7 +1629,10 @@ def _list_databases(data):
if data.get("CatalogName", "AwsDataCatalog") not in _data_catalogs:
return error_response_json("MetadataException", f"Catalog {data.get('CatalogName')} not found", 400)
databases = [_athena_database(db) for db in glue_svc._databases.values()]
- start = int(data.get("NextToken") or 0)
+ token = data.get("NextToken") or "0"
+ if not token.isdigit():
+ return error_response_json("InvalidRequestException", "Invalid NextToken", 400)
+ start = int(token)
end = start + int(data.get("MaxResults") or 50)
response = {"DatabaseList": databases[start:end]}
if end < len(databases):
diff --git a/ministack/services/cloudformation/changesets.py b/ministack/services/cloudformation/changesets.py
index 407a5446..fc41cbdf 100644
--- a/ministack/services/cloudformation/changesets.py
+++ b/ministack/services/cloudformation/changesets.py
@@ -9,11 +9,13 @@ import copy
import json
import logging
+import ministack.services.apigateway_v1 as _apigw_v1
import ministack.services.cloudwatch_logs as _cw_logs
import ministack.services.cognito as _cognito
import ministack.services.dynamodb as _dynamodb
import ministack.services.iam as _iam
import ministack.services.iot as _iot
+import ministack.services.kms as _kms
import ministack.services.lambda_svc as _lambda_svc
import ministack.services.s3 as _s3
import ministack.services.sns as _sns
@@ -48,6 +50,9 @@ from .helpers import (
)
from .provisioners import _RESOURCE_HANDLERS, _import_resource
from .stacks import (
+ _IMPORT_COMPLETED,
+ _IMPORT_STARTED,
+ _IMPORT_TAGS,
_add_event,
_create_stack_task_in_region,
_create_then_delete_on_failure,
@@ -192,24 +197,97 @@ def _user_pool_import_problem(pool_id):
return None
-# The resource types an IMPORT change set looks up before it describes the
-# import: the one ResourceIdentifier key AWS expects, and a lookup in this
-# emulator's own store that returns the StatusReason of the FAILED change set
-# AWS leaves behind when the identifier is invalid or names nothing (measured
-# for a missing value, and for the first five also a blank and an empty one).
-# Types not listed are accepted without a lookup.
+def _kms_key_import_problem(key_id):
+ if key_id not in _kms._keys:
+ arn = f"arn:aws:kms:{get_region()}:{get_account_id()}:key/{key_id}"
+ return _sdk_error(f"Key '{arn}' does not exist", "Kms", 400)
+ return None
+
+
+def _kms_alias_import_problem(name):
+ # AWS fails the change set without a StatusReason.
+ return None if _kms._alias_arn(name) in _kms._aliases else ""
+
+
+def _thing_type_import_problem(name):
+ if name not in _iot._thing_types:
+ return _IMPORT_NOT_FOUND.format(type="AWS::IoT::ThingType", value=name)
+ return None
+
+
+def _cognito_error(message):
+ return _sdk_error(message, "CognitoIdentityProvider", 400)
+
+
+def _user_pool_client_import_problem(pool_id, client_id):
+ if pool_id not in _cognito._user_pools:
+ return _user_pool_import_problem(pool_id)
+ if client_id not in _cognito._user_pools[pool_id]["_clients"]:
+ return _cognito_error("User pool client does not exist.")
+ return None
+
+
+def _user_pool_group_import_problem(pool_id, group_name):
+ if pool_id not in _cognito._user_pools:
+ return _user_pool_import_problem(pool_id)
+ if group_name not in _cognito._user_pools[pool_id]["_groups"]:
+ return _cognito_error("Group not found.")
+ return None
+
+
+def _resource_server_import_problem(pool_id, identifier):
+ if pool_id not in _cognito._user_pools:
+ return _user_pool_import_problem(pool_id)
+ if identifier not in _cognito._pool_resource_servers(_cognito._user_pools[pool_id]):
+ return _cognito_error(f"{identifier} does not exist in user pool {pool_id}.")
+ return None
+
+
+def _identity_pool_import_problem(pool_id):
+ if pool_id not in _cognito._identity_pools:
+ return _sdk_error(f"IdentityPool '{pool_id}' not found.", "CognitoIdentity", 400)
+ return None
+
+
+def _rest_api_import_problem(api_id):
+ if api_id not in _apigw_v1._rest_apis:
+ return _sdk_error(f"Invalid API identifier specified {get_account_id()}:{api_id}",
+ "ApiGateway", 404)
+ return None
+
+
+def _stage_import_problem(api_id, stage_name):
+ if api_id not in _apigw_v1._rest_apis:
+ return _rest_api_import_problem(api_id)
+ if stage_name not in _apigw_v1._stages_v1.get(api_id, {}):
+ return _sdk_error("Invalid stage identifier specified", "ApiGateway", 404)
+ return None
+
+
+# Per type: the ResourceIdentifier keys (schema primaryIdentifier), the last one being the change's
+# physical id, and the FAILED StatusReason lookup (measured).
_IMPORT_LOOKUPS = {
- "AWS::SQS::Queue": ("QueueUrl", _queue_import_problem),
- "AWS::SNS::Topic": ("TopicArn", _topic_import_problem),
- "AWS::S3::Bucket": ("BucketName", _bucket_import_problem),
- "AWS::DynamoDB::Table": ("TableName", _table_import_problem),
- "AWS::SSM::Parameter": ("Name", _parameter_import_problem),
- "AWS::IAM::Role": ("RoleName", _role_import_problem),
- "AWS::Logs::LogGroup": ("LogGroupName", _log_group_import_problem),
- "AWS::Lambda::Function": ("FunctionName", _function_import_problem),
- "AWS::IoT::Policy": ("Id", _iot_policy_import_problem),
- "AWS::IoT::CACertificate": ("Id", _ca_certificate_import_problem),
- "AWS::Cognito::UserPool": ("UserPoolId", _user_pool_import_problem),
+ "AWS::SQS::Queue": (("QueueUrl",), _queue_import_problem),
+ "AWS::SNS::Topic": (("TopicArn",), _topic_import_problem),
+ "AWS::S3::Bucket": (("BucketName",), _bucket_import_problem),
+ "AWS::DynamoDB::Table": (("TableName",), _table_import_problem),
+ "AWS::SSM::Parameter": (("Name",), _parameter_import_problem),
+ "AWS::IAM::Role": (("RoleName",), _role_import_problem),
+ "AWS::Logs::LogGroup": (("LogGroupName",), _log_group_import_problem),
+ "AWS::Lambda::Function": (("FunctionName",), _function_import_problem),
+ "AWS::IoT::Policy": (("Id",), _iot_policy_import_problem),
+ "AWS::IoT::CACertificate": (("Id",), _ca_certificate_import_problem),
+ "AWS::Cognito::UserPool": (("UserPoolId",), _user_pool_import_problem),
+ "AWS::KMS::Key": (("KeyId",), _kms_key_import_problem),
+ "AWS::KMS::Alias": (("AliasName",), _kms_alias_import_problem),
+ "AWS::IoT::ThingType": (("ThingTypeName",), _thing_type_import_problem),
+ "AWS::Cognito::UserPoolClient": (("UserPoolId", "ClientId"), _user_pool_client_import_problem),
+ "AWS::Cognito::UserPoolGroup": (("UserPoolId", "GroupName"), _user_pool_group_import_problem),
+ "AWS::Cognito::UserPoolResourceServer": (
+ ("UserPoolId", "Identifier"), _resource_server_import_problem),
+ "AWS::Cognito::IdentityPool": (("Id",), _identity_pool_import_problem),
+ "AWS::ApiGateway::RestApi": (("RestApiId",), _rest_api_import_problem),
+ "AWS::ApiGateway::Stage": (("RestApiId", "StageName"), _stage_import_problem),
}
@@ -245,9 +323,10 @@ def _import_changes(resources_to_import, template, diff):
for entry in resources_to_import:
logical, rtype = entry["LogicalResourceId"], entry.get("ResourceType")
identifier = entry.get("ResourceIdentifier") or {}
- if rtype in _IMPORT_LOOKUPS and list(identifier) != [_IMPORT_LOOKUPS[rtype][0]]:
+ keys = _IMPORT_LOOKUPS[rtype][0] if rtype in _IMPORT_LOOKUPS else None
+ if keys and sorted(identifier) != sorted(keys):
raise ValueError(f"Invalid resource identifier for resource type {rtype}. "
- f"Expected [{_IMPORT_LOOKUPS[rtype][0]}]")
+ f"Expected [{', '.join(keys)}]")
if rtype != declared[logical].get("Type"):
raise ValueError(
f"Resource type of [{logical}] passed in ResourceToImport does not match with "
@@ -278,23 +357,62 @@ def _import_changes(resources_to_import, template, diff):
raise ValueError(f"The following resources to import {_listed(no_policy)} must have "
"DeletionPolicy attribute specified in the template.")
- changes = []
- for entry in resources_to_import:
- change = {
- "Action": "Import",
- "LogicalResourceId": entry["LogicalResourceId"],
- "ResourceType": entry["ResourceType"],
- "Scope": [],
- "Details": [],
- }
- # AWS reports the identifier value as the physical id (measured for the
- # five types in _IMPORT_LOOKUPS). How it joins a multi-key identifier
- # (AWS::IAM::RolePolicy's PolicyName + RoleName, say) is unmeasured, so
- # none is invented for one.
- if len(entry["ResourceIdentifier"]) == 1:
- change["PhysicalResourceId"] = next(iter(entry["ResourceIdentifier"].values()))
- changes.append({"ResourceChange": change})
- return changes
+ return [_import_change(entry) for entry in resources_to_import]
+
+
+def _import_change(entry):
+ """The Import change of one resource to import."""
+ change = {
+ "Action": "Import",
+ "LogicalResourceId": entry["LogicalResourceId"],
+ "ResourceType": entry["ResourceType"],
+ "Scope": [],
+ "Details": [],
+ }
+ # Key joining for unlisted multi-key types is unmeasured, so none is invented.
+ identifier = entry["ResourceIdentifier"]
+ if entry["ResourceType"] in _IMPORT_LOOKUPS:
+ change["PhysicalResourceId"] = identifier[_IMPORT_LOOKUPS[entry["ResourceType"]][0][-1]]
+ elif len(identifier) == 1:
+ change["PhysicalResourceId"] = next(iter(identifier.values()))
+ return {"ResourceChange": change}
+
+
+def _existing_resources(changes, template):
+ """The added resources ImportExistingResources finds, in the stack's region."""
+ found = []
+ for change in changes:
+ rc = change["ResourceChange"]
+ rtype, logical_id = rc["ResourceType"], rc["LogicalResourceId"]
+ if (rc["Action"] != "Add" or rtype not in _IMPORT_LOOKUPS
+ or "import" not in _RESOURCE_HANDLERS.get(rtype, {})):
+ continue
+ # Only a name written as a literal counts; one from a Ref or Fn::Sub is created.
+ props = template["Resources"][logical_id].get("Properties") or {}
+ keys = _IMPORT_LOOKUPS[rtype][0]
+ if not all(isinstance(props.get(key), str) and props[key] for key in keys):
+ continue
+ entry = {"ResourceType": rtype, "LogicalResourceId": logical_id,
+ "ResourceIdentifier": {key: props[key] for key in keys}}
+ if _import_not_found([entry]) is None:
+ found.append(entry)
+ return found
+
+
+def _unretained_import_reason(entries, template):
+ """AWS's StatusReason when an existing resource to import is not retained, or None."""
+ declared = template.get("Resources", {})
+ unretained = [
+ f"{e['LogicalResourceId']} ({{{', '.join(f'{k}={v}' for k, v in e['ResourceIdentifier'].items())}}})"
+ for e in sorted(entries, key=lambda e: list(declared).index(e["LogicalResourceId"]))
+ if declared[e["LogicalResourceId"]].get("DeletionPolicy") not in ("Retain", "RetainExceptOnCreate")
+ ]
+ if not unretained:
+ return None
+ return ("CloudFormation is attempting to import some resources because they already exist in "
+ "your account. The resources must have the DeletionPolicy attribute set to 'Retain' or "
+ "'RetainExceptOnCreate' in the template for successful import. The affected resources "
+ f"are {', '.join(unretained)}")
def _import_ref(rtype, value):
@@ -313,16 +431,23 @@ def _import_owner(resources_to_import):
from ministack.services.cloudformation import _stacks
for entry in resources_to_import:
rtype, identifier = entry["ResourceType"], entry["ResourceIdentifier"]
- if len(identifier) != 1:
+ if rtype in _IMPORT_LOOKUPS:
+ keys = _IMPORT_LOOKUPS[rtype][0]
+ elif len(identifier) == 1:
+ keys = tuple(identifier)
+ else:
continue
- value = next(iter(identifier.values()))
- ref = _import_ref(rtype, value)
+ # The last key is the physical id; the others are the resource's properties.
+ *parents, last = keys
+ ref = _import_ref(rtype, identifier[last])
for stack in _stacks.values():
if stack.get("StackStatus") == "DELETE_COMPLETE":
continue
for res in stack.get("_resources", {}).values():
- pid = res.get("PhysicalResourceId")
- if res.get("ResourceType") == rtype and pid and _import_ref(rtype, pid) == ref:
+ pid, props = res.get("PhysicalResourceId"), res.get("Properties") or {}
+ if (res.get("ResourceType") == rtype and pid and _import_ref(rtype, pid) == ref
+ and all(props.get(key) == identifier[key] for key in parents)):
+ value = "|".join(identifier[key] for key in keys)
return f"{value} already exists in stack {stack['StackId']}"
return None
@@ -337,9 +462,9 @@ def _import_not_found(resources_to_import):
for entry in resources_to_import:
rtype, identifier = entry["ResourceType"], entry["ResourceIdentifier"]
if rtype in _IMPORT_LOOKUPS:
- key, problem = _IMPORT_LOOKUPS[rtype]
- reason = problem(identifier[key])
- if reason:
+ keys, problem = _IMPORT_LOOKUPS[rtype]
+ reason = problem(*(identifier[key] for key in keys))
+ if reason is not None:
return reason
continue
# A blank name can name nothing, whatever the type.
@@ -403,6 +528,8 @@ def _create_change_set(params):
stack_name = _p(params, "StackName")
cs_name = _p(params, "ChangeSetName")
cs_type = _p(params, "ChangeSetType", "UPDATE")
+ import_existing = (cs_type in ("CREATE", "UPDATE")
+ and _p(params, "ImportExistingResources", "false").lower() == "true")
if not stack_name:
return _error("ValidationError", "StackName is required")
@@ -598,17 +725,27 @@ def _create_change_set(params):
with _stack_region_context(stack, stack_id):
# AWS looks the resource up before it asks whether a stack holds
# it: a stack's queue deleted behind its back is "not found".
- import_failure = (_import_not_found(resources_to_import)
- or _import_owner(resources_to_import))
- if import_failure:
+ import_failure = _import_not_found(resources_to_import)
+ if import_failure is None:
+ import_failure = _import_owner(resources_to_import)
+ if import_failure is not None:
changes = []
+ existing = []
+ if import_existing:
+ with _stack_region_context(stack, stack_id):
+ existing = _existing_resources(changes, template)
+ import_failure = _unretained_import_reason(existing, template) or _import_owner(existing)
+ if import_failure is not None:
+ changes = []
+ adopted = {e["LogicalResourceId"]: _import_change(e) for e in existing}
+ changes = [adopted.get(c["ResourceChange"]["LogicalResourceId"], c) for c in changes]
cs_id = (
f"arn:aws:cloudformation:{_stack_region(stack, stack_id)}:{get_account_id()}:"
f"changeSet/{cs_name}/{new_uuid()}"
)
- if import_failure:
+ if import_failure is not None:
# AWS accepts an import of a resource that does not exist and fails
# the change set, with no changes, once it looks the resource up.
_cs_status, _cs_exec, _cs_reason = "FAILED", "UNAVAILABLE", import_failure
@@ -655,6 +792,8 @@ def _create_change_set(params):
"_template_body": template_body,
"_resolved_params": param_values,
"_resources_to_import": resources_to_import,
+ "_import_existing": {e["LogicalResourceId"]: e["ResourceIdentifier"] for e in existing},
+ "ImportExistingResources": import_existing,
}
_change_sets[cs_id] = change_set
@@ -751,6 +890,10 @@ def _describe_change_set(params):
"</member>"
)
+ import_existing_xml = (
+ "<ImportExistingResources>true</ImportExistingResources>"
+ if cs.get("ImportExistingResources") else ""
+ )
inner = (
f"<ChangeSetId>{_esc(cs['ChangeSetId'])}</ChangeSetId>"
f"<ChangeSetName>{_esc(cs['ChangeSetName'])}</ChangeSetName>"
@@ -762,6 +905,7 @@ def _describe_change_set(params):
f"<CreationTime>{cs['CreationTime']}</CreationTime>"
f"<Description>{_esc(cs.get('Description', ''))}</Description>"
f"<ChangeSetType>{cs.get('ChangeSetType', '')}</ChangeSetType>"
+ f"{import_existing_xml}"
"<Capabilities>"
+ "".join(f"<member>{_esc(c)}</member>" for c in cs.get("Capabilities", []))
+ "</Capabilities>"
@@ -887,7 +1031,8 @@ def _execute_change_set(params):
param_values, disable_rollback, tags,
is_update=is_update,
previous_stack=previous_stack,
- retain_except_on_create=retain_except_on_create)
+ retain_except_on_create=retain_except_on_create,
+ imports=cs.get("_import_existing"))
if on_stack_failure == "DELETE" and not is_update:
deploy = _create_then_delete_on_failure(real_stack_name, stack_id, deploy)
_create_stack_task_in_region(
@@ -949,7 +1094,7 @@ async def _import_resources_async(cs, stack, new_stack):
entry = entries[logical_id]
rtype = entry["ResourceType"]
started.append(logical_id)
- event(logical_id, "IMPORT_IN_PROGRESS", "Resource import started.")
+ event(logical_id, "IMPORT_IN_PROGRESS", _IMPORT_STARTED)
failure = _import_not_found([entry])
if failure is None:
try:
@@ -964,7 +1109,7 @@ async def _import_resources_async(cs, stack, new_stack):
event(logical_id, "IMPORT_FAILED", failure)
break
event(logical_id, "IMPORT_IN_PROGRESS", physical_id=physical_id)
- event(logical_id, "IMPORT_COMPLETE", "Resource import completed.", physical_id)
+ event(logical_id, "IMPORT_COMPLETE", _IMPORT_COMPLETED, physical_id)
resources[logical_id] = {
"PhysicalResourceId": physical_id,
"ResourceType": rtype,
@@ -995,9 +1140,7 @@ async def _import_resources_async(cs, stack, new_stack):
for logical_id in order:
record = resources[logical_id]
- event(logical_id, "UPDATE_IN_PROGRESS",
- "Apply stack-level tags to imported resource if applicable.",
- record["PhysicalResourceId"])
+ event(logical_id, "UPDATE_IN_PROGRESS", _IMPORT_TAGS, record["PhysicalResourceId"])
record["ResourceStatus"] = "UPDATE_COMPLETE"
event(logical_id, "UPDATE_COMPLETE", physical_id=record["PhysicalResourceId"])
stack["_template"] = template
diff --git a/ministack/services/cloudformation/handlers.py b/ministack/services/cloudformation/handlers.py
index e305b3ea..13fbbca5 100644
--- a/ministack/services/cloudformation/handlers.py
+++ b/ministack/services/cloudformation/handlers.py
@@ -7,7 +7,6 @@ CloudFormation handlers — API action handlers for all supported CloudFormation
import copy
import json
import logging
-import os
from ministack.core.responses import get_account_id, get_region, new_uuid, now_iso
from ministack.services.cloudformation import drift as _drift
@@ -207,8 +206,7 @@ def _check_capabilities(sent, template, params, macros=True):
``template``, because a macro may add IAM resources and AWS asks for those
to be acknowledged as well (template-macros-overview.html).
- The check runs under ``AUTH=true``, and without it when
- ``CFN_ENFORCE_CAPABILITIES=1``. ``CAPABILITY_IAM`` is
+ The check runs under ``AUTH=true`` only. ``CAPABILITY_IAM`` is
satisfied by either IAM capability, ``CAPABILITY_NAMED_IAM`` only by
itself. Pass ``macros=False`` for ``CreateChangeSet``: the API reference
says ``CAPABILITY_AUTO_EXPAND`` "doesn't apply to creating change sets".
@@ -226,9 +224,9 @@ def _check_capabilities(sent, template, params, macros=True):
def _capabilities_enforced():
- """Whether Capabilities are checked: under ``AUTH``, or with ``CFN_ENFORCE_CAPABILITIES`` on."""
+ """Capabilities are IAM scope, so they are checked under ``AUTH=true`` only."""
from ministack.app import AUTH
- return AUTH or os.environ.get("CFN_ENFORCE_CAPABILITIES", "").strip().lower() in ("1", "true", "yes", "on")
+ return AUTH
def _required_iam_capabilities(template):
diff --git a/ministack/services/cloudformation/provisioners.py b/ministack/services/cloudformation/provisioners.py
index 58fd252f..0b7adfec 100644
--- a/ministack/services/cloudformation/provisioners.py
+++ b/ministack/services/cloudformation/provisioners.py
@@ -68,9 +68,7 @@ logger = logging.getLogger("cloudformation")
# so AWS::Region / ARNs reflect the caller's request region (#398).
REGION = os.environ.get("MINISTACK_REGION", "us-east-1")
_MINISTACK_HOST = os.environ.get("MINISTACK_HOST", "localhost")
-# Mixed into the suffix of a generated name. A replacement draws a new one so
-# the new resource does not take its predecessor's name; the engine keeps it
-# on the resource record and sets it again for every later update.
+# Mixed into generated names; a replacement draws a new one, kept on the resource record.
_NAME_SEED = contextvars.ContextVar("cfn_name_seed", default="")
@@ -665,10 +663,7 @@ def _requires_replacement_pipes(old_props, new_props):
# custom-named resource (you must rename it first), so MiniStack must fail the
# update instead of silently executing the replacement and destroying data
# (issue #1433).
-# A replacement is a change to a property of the type's _REPLACING_PROPERTIES
-# row, or one ``requires_replacement`` adds. A type with an ``exists`` message
-# is not refused up front: AWS runs the create, which fails with that message
-# because the predecessor still holds the name.
+# An ``exists`` entry is not refused up front: the replacement's create fails on the name.
_CUSTOM_NAME_REPLACEMENT = {
"AWS::DynamoDB::Table": {
"name": "TableName",
@@ -921,14 +916,7 @@ def _delete_predecessor(delete_fn, *args, **kwargs):
retaining set): the engine then records the DELETE_SKIPPED event and the
predecessor stays, as on AWS. Every update handler that creates the
replacement itself removes the old resource through this, so the policy
- cannot be forgotten at one site, with five exceptions. Four have a
- deterministic generated name (the DynamoDB table, the IoT thing type and
- the ElastiCache cache cluster and replication group): the replacement
- takes the name back, so there is nothing left to retain. The fifth is the
- Lambda permission's degenerate ``Id`` branch,
- which removes and re-puts one statement under a Sid that cannot change:
- the physical id is kept, nothing is replaced, and the policy does not
- apply.
+ cannot be forgotten at one site.
"""
if _RETAIN_REPLACED.get():
return
@@ -939,6 +927,12 @@ def _delete_predecessor(delete_fn, *args, **kwargs):
delete_fn(*args, **kwargs)
+def _seeded_create(create_fn, logical_id, props, stack_name):
+ """Create a handler's replacement under a new generated name."""
+ _NAME_SEED.set(new_uuid()[:8])
+ return create_fn(logical_id, props, stack_name)
+
+
def _replace_resource(resource_type, physical_id, old_props, new_props,
stack_name, logical_id):
"""Create the resource anew under a new generated name."""
@@ -1358,7 +1352,7 @@ def _s3_apply_notification(name, notif):
# S3 Multi-Region Access Point
# ---------------------------------------------------------------------------
-def _s3_mrap_create(logical_id, props, stack_name, replacing=""):
+def _s3_mrap_create(logical_id, props, stack_name):
"""Provision an AWS::S3::MultiRegionAccessPoint.
On AWS this is asynchronous — `CreateMultiRegionAccessPoint` returns a
@@ -1370,7 +1364,7 @@ def _s3_mrap_create(logical_id, props, stack_name, replacing=""):
plane can resolve the alias to one of them.
"""
name = props.get("Name") or _physical_name(
- stack_name, logical_id, lowercase=True, max_len=50, replacing=replacing)
+ stack_name, logical_id, lowercase=True, max_len=50)
buckets = [
r.get("Bucket") for r in (props.get("Regions") or [])
if isinstance(r, dict) and r.get("Bucket")
@@ -1672,7 +1666,11 @@ def _sns_create(logical_id, props, stack_name):
_sns._topics[arn]["subscriptions"].append(sub)
_sns._sub_arn_to_topic[sub_arn] = arn
- return arn, {"TopicArn": arn, "TopicName": name}
+ return arn, _sns_topic_attrs(arn)
+
+
+def _sns_topic_attrs(arn):
+ return {"TopicArn": arn, "TopicName": arn.rsplit(":", 1)[-1]}
def _sns_update(physical_id, old_props, new_props, stack_name, logical_id=None):
@@ -1721,7 +1719,11 @@ def _sns_update(physical_id, old_props, new_props, stack_name, logical_id=None):
}
topic["subscriptions"].append(sub)
_sns._sub_arn_to_topic[sub_arn] = physical_id
- return physical_id, {"TopicArn": physical_id, "TopicName": name}
+ return physical_id, _sns_topic_attrs(physical_id)
+
+
+def _sns_import(identifier):
+ return identifier["TopicArn"], _sns_topic_attrs(identifier["TopicArn"])
def _sns_delete(physical_id, props):
@@ -2194,10 +2196,8 @@ def _lambda_update(physical_id, old_props, new_props, stack_name, logical_id=Non
published versions, aliases, the resource policy, tags, event invoke
configs. Going through the Lambda module's own update paths keeps them.
- FunctionName, PackageType and TenancyConfig changes are replaced in
- ``_update_resource`` before this runs. A DurableConfig change may require
- replacement on AWS; under the same physical name the closest local
- equivalent is the full re-provision the create fallback always did.
+ FunctionName, PackageType and TenancyConfig are replaced in ``_update_resource``;
+ a DurableConfig change re-provisions under the same name.
"""
name = new_props.get("FunctionName") or _physical_name(
stack_name, logical_id or physical_id, max_len=64
@@ -2404,11 +2404,8 @@ def _iam_role_create(logical_id, props, stack_name):
def _iam_role_update(physical_id, old_props, new_props, stack_name, logical_id=None):
"""Update a role in place, keeping its ARN and RoleId.
- RoleName and Path are create-only: ``_update_resource`` replaces the
- role before this runs. Everything else — assume-role document, inline
- Policies, ManagedPolicyArns, Description, MaxSessionDuration, Tags —
- updates in place, so policies attached from outside the template survive
- (the create fallback used to rebuild the record and drop them).
+ RoleName and Path are replaced in ``_update_resource``; the rest updates
+ in place, so policies attached from outside the template survive.
"""
name = new_props.get("RoleName") or _physical_name(
stack_name, logical_id or physical_id, max_len=64
@@ -2700,10 +2697,7 @@ def _iam_ip_roles(props):
def _iam_ip_create(logical_id, props, stack_name):
name = props.get("InstanceProfileName") or _physical_name(stack_name, logical_id, max_len=128)
path = props.get("Path", "/")
- # A generated name belongs to this stack resource, so a profile left under
- # it is taken over; a custom name goes through CreateInstanceProfile as it
- # is, and its EntityAlreadyExists keeps one stack from writing over a
- # profile another stack or the API owns.
+ # A generated name is this stack's to take over; a custom one meets EntityAlreadyExists.
if not props.get("InstanceProfileName"):
_iam._instance_profiles.pop(name, None)
resp = _iam._create_instance_profile({"InstanceProfileName": [name], "Path": [path]})
@@ -3676,16 +3670,14 @@ def _eks_cluster_update(physical_id, old_props, new_props, stack_name, logical_i
if replaced is not None:
return replaced
if _requires_replacement("AWS::EKS::Cluster", old_props, new_props):
- raise ValueError(
- f"AWS::EKS::Cluster {name} requires replacement, which MiniStack does not "
- "perform under a generated name; set a Name to create the replacement."
- )
+ created = _seeded_create(_eks_cluster_create, logical_id or physical_id, new_props, stack_name)
+ _delete_predecessor(_eks_cluster_delete, physical_id, old_props)
+ return created
fields = _eks_cluster_fields(new_props)
if "Logging" not in new_props:
# A rollback to a template without Logging leaves the logging as it is.
del fields["logging"]
- # A template without the endpoint access members or AuthenticationMode
- # leaves them as they are, as with Logging; dropped SecurityGroupIds go.
+ # Omitted endpoint access and AuthenticationMode stay; dropped SecurityGroupIds go.
cluster["resourcesVpcConfig"].update({"securityGroupIds": [], **fields.pop("resourcesVpcConfig")})
cluster.setdefault("accessConfig", {}).update(fields.pop("accessConfig"))
cluster.update(fields)
@@ -3722,7 +3714,8 @@ def _eks_nodegroup_attrs(ng):
def _eks_nodegroup_create(logical_id, props, stack_name):
import ministack.services.eks as _eks
cluster_name = props.get("ClusterName", "")
- ng_name = props.get("NodegroupName") or _physical_name(stack_name, logical_id, max_len=63)
+ ng_name = props.get("NodegroupName") or _physical_name(
+ stack_name, logical_id, max_len=63)
body = {
"nodegroupName": ng_name,
"instanceTypes": props.get("InstanceTypes", ["t3.medium"]),
@@ -3759,10 +3752,10 @@ def _eks_nodegroup_update(physical_id, old_props, new_props, stack_name, logical
"NodeRole", "Subnets", "InstanceTypes", "AmiType", "CapacityType", "DiskSize",
"RemoteAccess")):
if not new_props.get("NodegroupName"):
- raise ValueError(
- f"AWS::EKS::Nodegroup {ng_name} requires replacement, which MiniStack does not "
- "perform under a generated name; set a NodegroupName to create the replacement."
- )
+ created = _seeded_create(
+ _eks_nodegroup_create, logical_id or physical_id, new_props, stack_name)
+ _delete_predecessor(_eks_nodegroup_delete, physical_id, old_props)
+ return created
# The replacement's create meets the name still in use and fails, as on AWS.
return _eks_nodegroup_create(logical_id or physical_id, new_props, stack_name)
fields = _eks_nodegroup_fields(new_props)
@@ -4128,7 +4121,7 @@ def _check_nested_stack_capabilities(parent_stack_name, template):
stacks that contain IAM resources, you must acknowledge IAM capabilities",
using-cfn-nested-stacks), so the set the parent stored covers the child
and, through the child's own record, every level below it. Like the
- parent's check it runs under AUTH=true or CFN_ENFORCE_CAPABILITIES=1. A
+ parent's check it runs under AUTH=true only. A
child template with a macro needs CAPABILITY_AUTO_EXPAND on the parent.
"""
from ministack.services.cloudformation.handlers import (
@@ -4587,13 +4580,13 @@ def _apigw_rest_api_create(logical_id, props, stack_name):
api = json.loads(body) if isinstance(body, bytes) else json.loads(body)
api_id = api.get("id", "")
- # Find root resource id
- root_id = ""
- for rid, res in _apigw_v1._resources.get(api_id, {}).items():
- if res.get("path") == "/":
- root_id = rid
- break
- return api_id, {
+ return api_id, _apigw_rest_api_attrs(api_id)
+
+
+def _apigw_rest_api_attrs(api_id):
+ root_id = next((rid for rid, res in _apigw_v1._resources.get(api_id, {}).items()
+ if res.get("path") == "/"), "")
+ return {
"RestApiId": api_id,
"RootResourceId": root_id,
"Arn": f"arn:aws:apigateway:{get_region()}::/restapis/{api_id}",
@@ -4641,17 +4634,11 @@ def _apigw_rest_api_update(physical_id, old_props, new_props, stack_name, logica
api = _apigw_v1._rest_apis.get(physical_id)
if api is not None:
api["tags"] = {t["Key"]: t["Value"] for t in new_props.get("Tags", [])}
+ return physical_id, _apigw_rest_api_attrs(physical_id)
- root_id = ""
- for rid, res in _apigw_v1._resources.get(physical_id, {}).items():
- if res.get("path") == "/":
- root_id = rid
- break
- return physical_id, {
- "RestApiId": physical_id,
- "RootResourceId": root_id,
- "Arn": f"arn:aws:apigateway:{get_region()}::/restapis/{physical_id}",
- }
+
+def _apigw_rest_api_import(identifier):
+ return identifier["RestApiId"], _apigw_rest_api_attrs(identifier["RestApiId"])
def _apigw_rest_api_delete(physical_id, props):
@@ -5096,6 +5083,10 @@ def _apigw_stage_update(physical_id, old_props, new_props, stack_name):
return stage_name, {"StageName": stage_name}
+def _apigw_stage_import(identifier):
+ return identifier["StageName"], {"StageName": identifier["StageName"]}
+
+
def _apigw_stage_delete(physical_id, props):
api_id = props.get("RestApiId", "")
stage_name = props.get("StageName", "")
@@ -5781,10 +5772,9 @@ def _pipes_pipe_update(physical_id, old_props, new_props, stack_name, logical_id
if replaced is not None:
return replaced
if _requires_replacement("AWS::Pipes::Pipe", old_props, new_props):
- # Not routed through _delete_predecessor, like the Location tracker:
- # the generated name is deterministic, so register_pipe overwrites the
- # predecessor and retaining it is not possible here.
- return _pipes_pipe_create(logical_id or physical_id, new_props, stack_name)
+ created = _seeded_create(_pipes_pipe_create, logical_id or physical_id, new_props, stack_name)
+ _delete_predecessor(_pipes_pipe_delete, physical_id, old_props)
+ return created
fields = _pipes_pipe_fields(new_props)
_pipes.check_same_region(fields["Target"])
status, _headers, body = _pipes._update_pipe(physical_id, fields)
@@ -6667,6 +6657,11 @@ def _cognito_user_pool_client_update(physical_id, old_props, new_props, stack_na
return physical_id, _cognito_user_pool_client_attributes(client)
+def _cognito_user_pool_client_import(identifier):
+ client = _cognito._user_pools[identifier["UserPoolId"]]["_clients"][identifier["ClientId"]]
+ return client["ClientId"], _cognito_user_pool_client_attributes(client)
+
+
def _cognito_user_pool_client_delete(physical_id, props):
pid = props.get("UserPoolId", "")
pool = _cognito._user_pools.get(pid)
@@ -6743,6 +6738,10 @@ def _cognito_user_pool_resource_server_update(physical_id, old_props, new_props,
return identifier, {}
+def _cognito_user_pool_resource_server_import(identifier):
+ return identifier["Identifier"], {}
+
+
def _cognito_user_pool_resource_server_delete(physical_id, props):
pid = props.get("UserPoolId", "")
pool = _cognito._user_pools.get(pid)
@@ -6802,6 +6801,10 @@ def _cognito_user_pool_group_update(physical_id, old_props, new_props, stack_nam
return physical_id, {}
+def _cognito_user_pool_group_import(identifier):
+ return identifier["GroupName"], {}
+
+
def _cognito_user_pool_group_delete(physical_id, props):
pid = props.get("UserPoolId", "")
pool = _cognito._user_pools.get(pid)
@@ -6842,8 +6845,11 @@ def _cognito_identity_pool_create(logical_id, props, stack_name):
_cognito._identity_pools[iid] = pool
# ListTagsForResource reads the pool's tag store, not the record.
_cognito._identity_tags[iid] = _tag_map(props.get("IdentityPoolTags"))
- # The one Fn::GetAtt the resource reference lists is Name.
- return iid, {"Name": name}
+ return iid, _cognito_identity_pool_attributes(iid, name)
+
+
+def _cognito_identity_pool_attributes(iid, name):
+ return {"Id": iid, "Name": name}
# UpdateIdentityPool's parameters, with the value a dropped property reverts
@@ -6882,7 +6888,12 @@ def _cognito_identity_pool_update(physical_id, old_props, new_props, stack_name,
raise ValueError(f"AWS::Cognito::IdentityPool update failed: {body!r}")
_reconcile_tag_map(_cognito._identity_tags.setdefault(physical_id, {}),
old_props, new_props, prop="IdentityPoolTags")
- return physical_id, {"Name": name}
+ return physical_id, _cognito_identity_pool_attributes(physical_id, name)
+
+
+def _cognito_identity_pool_import(identifier):
+ iid = identifier["Id"]
+ return iid, _cognito_identity_pool_attributes(iid, _cognito._identity_pools[iid]["IdentityPoolName"])
def _cognito_identity_pool_delete(physical_id, props):
@@ -7169,7 +7180,11 @@ def _kms_key_create(logical_id, props, stack_name):
)
rec = _kms._resolve_key(json.loads(body)["KeyMetadata"]["KeyId"])
_kms_apply_key_props(rec, props)
- return rec["KeyId"], {"Arn": rec["Arn"], "KeyId": rec["KeyId"]}
+ return rec["KeyId"], _kms_key_attrs(rec)
+
+
+def _kms_key_attrs(rec):
+ return {"Arn": rec["Arn"], "KeyId": rec["KeyId"]}
def _kms_key_update(physical_id, old_props, new_props, stack_name):
@@ -7192,7 +7207,12 @@ def _kms_key_update(physical_id, old_props, new_props, stack_name):
rec["Policy"] = policy if isinstance(policy, str) else json.dumps(policy)
rec["Tags"] = _kms_tags(new_props)
_kms_apply_key_props(rec, new_props)
- return physical_id, {"Arn": rec["Arn"], "KeyId": rec["KeyId"]}
+ return physical_id, _kms_key_attrs(rec)
+
+
+def _kms_key_import(identifier):
+ rec = _kms._keys[identifier["KeyId"]]
+ return rec["KeyId"], _kms_key_attrs(rec)
def _kms_key_delete(physical_id, props):
@@ -7260,6 +7280,10 @@ def _kms_alias_update(physical_id, old_props, new_props, stack_name, logical_id=
return physical_id, {}
+def _kms_alias_import(identifier):
+ return identifier["AliasName"], {}
+
+
def _kms_alias_delete(physical_id, props):
_kms._aliases.pop(_kms._alias_arn(physical_id), None)
_kms._alias_dates.pop(_kms._alias_arn(physical_id), None)
@@ -7889,6 +7913,30 @@ def _ecs_cluster_create(logical_id, props, stack_name):
return name, {"Arn": cluster["clusterArn"], "ClusterName": name}
+def _ecs_cluster_update(physical_id, old_props, new_props, stack_name, logical_id=None):
+ """ClusterName replaces; the rest updates in place (aws-resource-ecs-cluster)."""
+ name = new_props.get("ClusterName", f"{stack_name}-{logical_id or physical_id}")
+ cluster = _ecs._clusters.get(physical_id)
+ replaced = _rename_replacement(
+ physical_id, old_props, new_props, stack_name, logical_id,
+ name, cluster["clusterName"] if cluster else None,
+ _ecs_cluster_create, _ecs_cluster_delete,
+ )
+ if replaced is not None:
+ return replaced
+ cluster["capacityProviders"] = new_props.get("CapacityProviders", [])
+ cluster["defaultCapacityProviderStrategy"] = _pascal_to_camel(
+ new_props.get("DefaultCapacityProviderStrategy") or [])
+ # Settings or configuration dropped from the template stay on the cluster.
+ if new_props.get("ClusterSettings"):
+ cluster["settings"] = _pascal_to_camel(new_props["ClusterSettings"])
+ if new_props.get("Configuration"):
+ cluster["configuration"] = _pascal_to_camel(new_props["Configuration"])
+ _reconcile_tag_list(_ecs._tags.setdefault(cluster["clusterArn"], []), old_props, new_props,
+ key="key", value="value")
+ return physical_id, {"Arn": cluster["clusterArn"], "ClusterName": physical_id}
+
+
def _ecs_cluster_delete(physical_id, props):
_ecs._delete_cluster({"cluster": physical_id})
@@ -7924,7 +7972,7 @@ def _normalize_container_defs(cdefs):
def _ecs_task_def_create(logical_id, props, stack_name):
family = props.get("Family", f"{stack_name}-{logical_id}")
- revision = 1
+ revision = _ecs._next_task_def_revision(family)
td_key = f"{family}:{revision}"
arn = f"arn:aws:ecs:{get_region()}:{get_account_id()}:task-definition/{td_key}"
compat = props.get("RequiresCompatibilities", ["EC2"])
@@ -7950,14 +7998,22 @@ def _ecs_task_def_create(logical_id, props, stack_name):
if prop in props:
td[key] = props[prop]
_ecs._task_defs[td_key] = td
- _ecs._task_def_latest[family] = revision
+ _ecs._tags[arn] = _pascal_to_camel(props.get("Tags") or [])
return arn, {"TaskDefinitionArn": arn}
+def _ecs_task_def_update(physical_id, old_props, new_props, stack_name, logical_id=None):
+ """Tags update in place; any other change registers the next revision."""
+ if ({k: v for k, v in old_props.items() if k != "Tags"}
+ != {k: v for k, v in new_props.items() if k != "Tags"}):
+ return _ecs_task_def_create(logical_id or physical_id, new_props, stack_name)
+ _reconcile_tag_list(_ecs._tags.setdefault(physical_id, []), old_props, new_props,
+ key="key", value="value")
+ return physical_id, {"TaskDefinitionArn": physical_id}
+
+
def _ecs_task_def_delete(physical_id, props):
- # physical_id is the ARN; _task_defs is keyed by "family:revision"
- td_key = physical_id.split("/")[-1] if "/" in physical_id else physical_id
- _ecs._task_defs.pop(td_key, None)
+ _ecs._deregister_task_definition({"taskDefinition": physical_id})
def _ecs_deployment_configuration(props):
@@ -10363,7 +10419,8 @@ def _rds_db_cluster_attrs(cluster):
def _rds_db_cluster_create(logical_id, props, stack_name):
- cluster_id = props.get("DBClusterIdentifier") or _physical_name(stack_name, logical_id, lowercase=True, max_len=63)
+ cluster_id = props.get("DBClusterIdentifier") or _physical_name(
+ stack_name, logical_id, lowercase=True, max_len=63)
arn = f"arn:aws:rds:{get_region()}:{get_account_id()}:cluster:{cluster_id}"
suffix = new_uuid()[:8]
created = now_iso()
@@ -10405,9 +10462,9 @@ def _rds_db_cluster_update(physical_id, old_props, new_props, stack_name, logica
if replaced is not None:
return replaced
if _requires_replacement("AWS::RDS::DBCluster", old_props, new_props):
- # The generated name is deterministic, so the replacement takes it back.
- _rds_db_cluster_delete(physical_id, old_props)
- return _rds_db_cluster_create(logical_id or physical_id, new_props, stack_name)
+ created = _seeded_create(_rds_db_cluster_create, logical_id or physical_id, new_props, stack_name)
+ _delete_predecessor(_rds_db_cluster_delete, physical_id, old_props)
+ return created
# A record saved before the create stored these fields gets them first, so the update cannot fail half-way.
cluster.setdefault("DbClusterResourceId", _rds_cluster_resource_id())
for key in ("EarliestRestorableTime", "LatestRestorableTime"):
@@ -10591,9 +10648,9 @@ def _rds_db_instance_update(physical_id, old_props, new_props, stack_name, logic
if replaced is not None:
return replaced
if _requires_replacement("AWS::RDS::DBInstance", old_props, new_props):
- # The generated name is deterministic, so the replacement takes it back.
- _rds_db_instance_delete(physical_id, old_props)
- return _rds_db_instance_create(logical_id or physical_id, new_props, stack_name)
+ created = _seeded_create(_rds_db_instance_create, logical_id or physical_id, new_props, stack_name)
+ _delete_predecessor(_rds_db_instance_delete, physical_id, old_props)
+ return created
_rds_apply_changed(instance, _rds_db_instance_fields(old_props), _rds_db_instance_fields(new_props))
if _rds_db_instance_port(old_props) != _rds_db_instance_port(new_props):
instance["Endpoint"]["Port"] = _rds_db_instance_port(new_props)
@@ -10733,9 +10790,9 @@ def _asg_delete(physical_id, props):
_asg._delete_group_policies(physical_id)
-def _asg_lc_create(logical_id, props, stack_name, replacing=""):
+def _asg_lc_create(logical_id, props, stack_name):
name = props.get("LaunchConfigurationName") or _physical_name(
- stack_name, logical_id, max_len=255, replacing=replacing)
+ stack_name, logical_id, max_len=255)
arn = f"arn:aws:autoscaling:{get_region()}:{get_account_id()}:launchConfiguration:{new_uuid()}:launchConfigurationName/{name}"
_asg._launch_configs[name] = {
"LaunchConfigurationName": name,
@@ -12004,9 +12061,16 @@ def _iot_thing_type_create(logical_id, props, stack_name):
status, _headers, body = _iot._deprecate_thing_type(name, {"undoDeprecate": False})
if status >= 400:
raise ValueError(f"DeprecateThingType failed: {body}")
+ return name, _iot_thing_type_attrs(name)
+
+
+def _iot_thing_type_attrs(name):
rec = _iot._thing_types.get(name) or {}
- return name, {"Arn": rec.get("thingTypeArn", _iot._thing_type_arn(name)),
- "Id": rec.get("thingTypeId", "")}
+ return {"Arn": rec.get("thingTypeArn", _iot._thing_type_arn(name)), "Id": rec.get("thingTypeId", "")}
+
+
+def _iot_thing_type_import(identifier):
+ return identifier["ThingTypeName"], _iot_thing_type_attrs(identifier["ThingTypeName"])
def _iot_thing_type_delete(physical_id, props):
@@ -12053,10 +12117,7 @@ def _iot_thing_type_update(physical_id, old_props, new_props, stack_name,
status, _headers, body = _iot._deprecate_thing_type(physical_id, {"undoDeprecate": not deprecate})
if status >= 400:
raise ValueError(f"DeprecateThingType failed: {body}")
- return physical_id, {
- "Arn": record.get("thingTypeArn", _iot._thing_type_arn(physical_id)),
- "Id": record.get("thingTypeId", ""),
- }
+ return physical_id, _iot_thing_type_attrs(physical_id)
# --- IoT ThingGroup ---
@@ -12549,9 +12610,7 @@ def _ecr_repo_update(physical_id, old_props, new_props, stack_name, logical_id=N
repo = _ecr._repositories.get(physical_id)
if repo is None:
return _ecr_repo_create(logical_id or physical_id, new_props, stack_name)
- # ImageTagMutability, ImageScanningConfiguration, LifecyclePolicy,
- # RepositoryPolicyText and Tags update in place; RepositoryName and
- # EncryptionConfiguration are replaced in _update_resource.
+ # RepositoryName and EncryptionConfiguration are replaced in _update_resource.
api = _ecr_cfn_to_api(new_props)
repo["imageTagMutability"] = api["imageTagMutability"]
repo["imageScanningConfiguration"] = api["imageScanningConfiguration"]
@@ -12734,14 +12793,7 @@ def _location_tracker_delete(physical_id, props):
_location._delete_tracker(physical_id)
-# CloudFormation replacement rules, checked against DescribeType and change
-# sets. A row lists the schema's createOnlyProperties (Always); the conditional
-# table below lists its conditionalCreateOnlyProperties (Conditionally). Any
-# other property of a listed type is in place (Never), as AWS reports it. A
-# stack update replaces the resource when an Always property changes. Service
-# API immutability is different: an update may fail without being reported as
-# a replacement (for example Cognito sign-in attributes). Types without a row
-# keep the conservative Conditionally answer and their handler's behavior.
+# Per type, the create-only properties (Always): a change replaces the resource.
_REPLACING_PROPERTIES: dict[str, tuple[str, ...]] = {
"AWS::DynamoDB::Table": ("TableName", "ImportSourceSpecification"),
"AWS::StepFunctions::StateMachine": ("StateMachineName", "StateMachineType"),
@@ -12849,8 +12901,6 @@ _RESOURCE_HANDLERS = {
},
"AWS::S3::MultiRegionAccessPoint": {
"create": _s3_mrap_create,
- "update": _replacing_update(_s3_mrap_create, _s3_mrap_delete),
- "update_with_logical_id": True,
"delete": _s3_mrap_delete,
},
"AWS::S3::BucketPolicy": {
@@ -12960,6 +13010,7 @@ _RESOURCE_HANDLERS = {
"update": _sns_update,
"update_with_logical_id": True,
"delete": _sns_delete,
+ "import": _sns_import,
},
"AWS::SNS::Subscription": {
"create": _sns_sub_create,
@@ -13108,6 +13159,7 @@ _RESOURCE_HANDLERS = {
"update": _apigw_rest_api_update,
"update_with_logical_id": True,
"delete": _apigw_rest_api_delete,
+ "import": _apigw_rest_api_import,
},
"AWS::ApiGateway::Resource": {
"create": _apigw_resource_create,
@@ -13139,6 +13191,7 @@ _RESOURCE_HANDLERS = {
"create": _apigw_stage_create,
"update": _apigw_stage_update,
"delete": _apigw_stage_delete,
+ "import": _apigw_stage_import,
},
"AWS::ApiGateway::ApiKey": {
"create": _apigw_api_key_create,
@@ -13256,24 +13309,28 @@ _RESOURCE_HANDLERS = {
"update": _cognito_user_pool_client_update,
"update_with_logical_id": True,
"delete": _cognito_user_pool_client_delete,
+ "import": _cognito_user_pool_client_import,
},
"AWS::Cognito::UserPoolResourceServer": {
"create": _cognito_user_pool_resource_server_create,
"update": _cognito_user_pool_resource_server_update,
"update_with_logical_id": True,
"delete": _cognito_user_pool_resource_server_delete,
+ "import": _cognito_user_pool_resource_server_import,
},
"AWS::Cognito::UserPoolGroup": {
"create": _cognito_user_pool_group_create,
"update": _cognito_user_pool_group_update,
"update_with_logical_id": True,
"delete": _cognito_user_pool_group_delete,
+ "import": _cognito_user_pool_group_import,
},
"AWS::Cognito::IdentityPool": {
"create": _cognito_identity_pool_create,
"update": _cognito_identity_pool_update,
"update_with_logical_id": True,
"delete": _cognito_identity_pool_delete,
+ "import": _cognito_identity_pool_import,
},
"AWS::Cognito::UserPoolDomain": {"create": _cognito_user_pool_domain_create, "delete": _cognito_user_pool_domain_delete},
"AWS::ECR::Repository": {
@@ -13311,12 +13368,14 @@ _RESOURCE_HANDLERS = {
"create": _kms_key_create,
"update": _kms_key_update,
"delete": _kms_key_delete,
+ "import": _kms_key_import,
},
"AWS::KMS::Alias": {
"create": _kms_alias_create,
"update": _kms_alias_update,
"update_with_logical_id": True,
"delete": _kms_alias_delete,
+ "import": _kms_alias_import,
},
"AWS::EC2::VPC": {
"create": _ec2_vpc_create,
@@ -13366,8 +13425,18 @@ _RESOURCE_HANDLERS = {
"delete": _ec2_route_delete,
},
"AWS::EC2::SubnetRouteTableAssociation": {"create": _ec2_subnet_rtb_assoc_create, "delete": _ec2_subnet_rtb_assoc_delete},
- "AWS::ECS::Cluster": {"create": _ecs_cluster_create, "delete": _ecs_cluster_delete},
- "AWS::ECS::TaskDefinition": {"create": _ecs_task_def_create, "delete": _ecs_task_def_delete},
+ "AWS::ECS::Cluster": {
+ "create": _ecs_cluster_create,
+ "update": _ecs_cluster_update,
+ "update_with_logical_id": True,
+ "delete": _ecs_cluster_delete,
+ },
+ "AWS::ECS::TaskDefinition": {
+ "create": _ecs_task_def_create,
+ "update": _ecs_task_def_update,
+ "update_with_logical_id": True,
+ "delete": _ecs_task_def_delete,
+ },
"AWS::ECS::Service": {
"create": _ecs_service_create,
"update": _ecs_service_update,
@@ -13548,6 +13617,7 @@ _RESOURCE_HANDLERS = {
"update": _iot_thing_type_update,
"update_with_logical_id": True,
"delete": _iot_thing_type_delete,
+ "import": _iot_thing_type_import,
},
"AWS::IoT::ThingGroup": {
"create": _iot_thing_group_create,
@@ -13636,8 +13706,6 @@ _RESOURCE_HANDLERS = {
},
"AWS::AutoScaling::LaunchConfiguration": {
"create": _asg_lc_create,
- "update": _replacing_update(_asg_lc_create, _asg_lc_delete),
- "update_with_logical_id": True,
"delete": _asg_lc_delete,
},
"AWS::AutoScaling::ScalingPolicy": {
diff --git a/ministack/services/cloudformation/stacks.py b/ministack/services/cloudformation/stacks.py
index 556edb62..2d136a84 100644
--- a/ministack/services/cloudformation/stacks.py
+++ b/ministack/services/cloudformation/stacks.py
@@ -29,6 +29,7 @@ from .provisioners import (
_RETAINING_POLICIES,
_custom_named_replacement_error,
_delete_resource,
+ _import_resource,
_property_recreation,
_provision_resource,
_replacing_change,
@@ -88,7 +89,7 @@ def _runs_on_worker_thread(resource_type: str) -> bool:
def _update_keeping_seed(*args):
- """Return _update_resource's result and the name seed it left, which a worker thread would lose."""
+ """_update_resource's result and the name seed it left."""
return _update_resource(*args), _NAME_SEED.get()
@@ -167,6 +168,28 @@ def _add_event(stack_id, stack_name, logical_id, resource_type, status,
_stack_events[stack_id].append(event)
+# The reasons AWS gives the events of an imported resource.
+_IMPORT_STARTED = "Resource import started."
+_IMPORT_COMPLETED = "Resource import completed."
+_IMPORT_TAGS = "Apply stack-level tags to imported resource if applicable."
+_IMPORT_UNTAG = "Remove stack-level tags from imported resource if applicable."
+
+
+def _adopt_resource(stack_id, stack_name, logical_id, resource_type, identifier):
+ """Import an existing resource during a create or update, with AWS's events."""
+ from .changesets import _import_not_found
+ _add_event(stack_id, stack_name, logical_id, resource_type, "IMPORT_IN_PROGRESS",
+ _IMPORT_STARTED)
+ missing = _import_not_found([{"ResourceType": resource_type, "ResourceIdentifier": identifier}])
+ if missing is not None:
+ raise ValueError(missing)
+ physical_id, attrs = _import_resource(resource_type, identifier)
+ for status, reason in (("IMPORT_IN_PROGRESS", ""), ("IMPORT_COMPLETE", _IMPORT_COMPLETED),
+ ("UPDATE_IN_PROGRESS", _IMPORT_TAGS), ("UPDATE_COMPLETE", "")):
+ _add_event(stack_id, stack_name, logical_id, resource_type, status, reason, physical_id)
+ return physical_id, attrs
+
+
# ===========================================================================
# Stack Deploy / Delete / Update Logic
# ===========================================================================
@@ -338,7 +361,7 @@ async def _continue_update_rollback_async(stack_name: str, stack_id: str,
def _failed_operation(is_update, previous_stack, created, failed_update,
failed_logical_id, replaced_ids, cancelled, template,
param_values, retain_except_on_create, stack_tags,
- previous_tags) -> dict:
+ previous_tags, imports=None) -> dict:
"""What the rollback of a failed operation needs, kept on the stack record
of a ``*_FAILED`` stack (``DisableRollback``) until ``RollbackStack`` runs
it. Lists only, so the record survives the JSON persistence round trip."""
@@ -355,6 +378,7 @@ def _failed_operation(is_update, previous_stack, created, failed_update,
"retain_except_on_create": bool(retain_except_on_create),
"stack_tags": list(stack_tags or []),
"previous_tags": list(previous_tags or []),
+ "imports": dict(imports or {}),
}
@@ -377,6 +401,7 @@ async def _roll_back_operation(stack_name: str, stack_id: str, stack: dict,
retain_except_on_create = operation.get("retain_except_on_create", False)
stack_tags = operation.get("stack_tags") or []
previous_tags = operation.get("previous_tags") or []
+ imports = operation.get("imports") or {}
mappings = template.get("Mappings", {})
conditions = _evaluate_conditions(template, param_values)
resources_defs = template.get("Resources", {})
@@ -451,12 +476,30 @@ async def _roll_back_operation(stack_name: str, stack_id: str, stack: dict,
# resources" (view-stack-events): a cleanup delete failure does not fail it.
cleanup_phase = bool(is_update and previous_stack
and not rollback_failed_records)
+
+ def release():
+ # An imported resource leaves the stack as it came; one that failed never got tags.
+ if failed_logical_id in imports:
+ for status in ("IMPORT_ROLLBACK_IN_PROGRESS", "IMPORT_ROLLBACK_COMPLETE"):
+ _add_event(stack_id, stack_name, failed_logical_id,
+ resources_defs[failed_logical_id].get("Type", ""), status)
+ for logical_id in reversed([lid for lid in to_delete if lid in imports]):
+ res = provisioned_resources.pop(logical_id, {})
+ for status, reason in (("UPDATE_IN_PROGRESS", _IMPORT_UNTAG), ("UPDATE_COMPLETE", ""),
+ ("IMPORT_ROLLBACK_IN_PROGRESS", ""),
+ ("IMPORT_ROLLBACK_COMPLETE", "")):
+ _add_event(stack_id, stack_name, logical_id, res.get("ResourceType", ""),
+ status, reason, res.get("PhysicalResourceId", ""))
+
+ # An update releases them before its cleanup, a create after its deletes.
+ if is_update and previous_stack:
+ release()
if cleanup_phase:
stack["StackStatus"] = "UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS"
_add_event(stack_id, stack_name, stack_name,
"AWS::CloudFormation::Stack", stack["StackStatus"],
physical_id=stack_id)
- for logical_id in reversed(to_delete):
+ for logical_id in reversed([lid for lid in to_delete if lid not in imports]):
res = provisioned_resources.get(logical_id, {})
rtype = res.get("ResourceType", "")
pid = res.get("PhysicalResourceId", "")
@@ -498,6 +541,8 @@ async def _roll_back_operation(stack_name: str, stack_id: str, stack: dict,
"Properties": res_props,
}
provisioned_resources.pop(logical_id, None)
+ if not (is_update and previous_stack):
+ release()
if is_update and previous_stack:
# Restore previous resources
@@ -543,12 +588,15 @@ async def _deploy_stack_async(stack_name: str, stack_id: str, template: dict,
param_values: dict, disable_rollback: bool,
tags: list, is_update: bool = False,
previous_stack: dict | None = None,
- retain_except_on_create: bool = False):
+ retain_except_on_create: bool = False,
+ imports: dict | None = None):
"""Background task: provision resources and set final stack status.
``retain_except_on_create`` is the API parameter of the same name: a
rollback of this operation then deletes what the operation created even
when the template says ``DeletionPolicy: Retain``."""
+ # The identifiers of the resources ImportExistingResources adopts, by logical id.
+ imports = imports or {}
from ministack.services.cloudformation import _exports, _stacks
status_prefix = "UPDATE" if is_update else "CREATE"
stack = _stacks[stack_name]
@@ -655,8 +703,9 @@ async def _deploy_stack_async(stack_name: str, stack_id: str, template: dict,
reuse_secrets=resource_unchanged,
)
- _add_event(stack_id, stack_name, logical_id, resource_type,
- f"{status_prefix}_IN_PROGRESS")
+ if logical_id not in imports:
+ _add_event(stack_id, stack_name, logical_id, resource_type,
+ f"{status_prefix}_IN_PROGRESS")
# On stack update, route previously-provisioned resources through
# the type's update handler when one exists; otherwise fall back
@@ -722,7 +771,10 @@ async def _deploy_stack_async(stack_name: str, stack_id: str, template: dict,
new_tagged = _with_stack_tags(
resource_type, resolved_props, stack_tags,
stack_name, stack_id, logical_id)
- if _runs_on_worker_thread(resource_type):
+ if logical_id in imports:
+ physical_id, attrs = _adopt_resource(
+ stack_id, stack_name, logical_id, resource_type, imports[logical_id])
+ elif _runs_on_worker_thread(resource_type):
physical_id, attrs = await run_reentrant(
_provision_resource, resource_type, logical_id, new_tagged, stack_name
)
@@ -734,7 +786,8 @@ async def _deploy_stack_async(stack_name: str, stack_id: str, template: dict,
logger.error("Failed to provision %s (%s): %s",
logical_id, resource_type, exc)
_add_event(stack_id, stack_name, logical_id, resource_type,
- f"{status_prefix}_FAILED", str(exc))
+ "IMPORT_FAILED" if logical_id in imports else f"{status_prefix}_FAILED",
+ str(exc))
failed = True
fail_reason = f"Resource {logical_id} failed: {exc}"
failed_logical_id = logical_id
@@ -752,7 +805,8 @@ async def _deploy_stack_async(stack_name: str, stack_id: str, template: dict,
provisioned_resources[logical_id] = {
"PhysicalResourceId": physical_id,
"ResourceType": resource_type,
- "ResourceStatus": f"{status_prefix}_COMPLETE",
+ "ResourceStatus": ("UPDATE_COMPLETE" if logical_id in imports
+ else f"{status_prefix}_COMPLETE"),
"LogicalResourceId": logical_id,
"Properties": resolved_props,
"Attributes": attrs,
@@ -764,8 +818,9 @@ async def _deploy_stack_async(stack_name: str, stack_id: str, template: dict,
provisioned_resources[logical_id]["_name_seed"] = name_seed
created_in_this_run.append(logical_id)
- _add_event(stack_id, stack_name, logical_id, resource_type,
- f"{status_prefix}_COMPLETE", physical_id=physical_id)
+ if logical_id not in imports:
+ _add_event(stack_id, stack_name, logical_id, resource_type,
+ f"{status_prefix}_COMPLETE", physical_id=physical_id)
resolved_outputs: list = []
new_exports: dict = {}
@@ -890,7 +945,7 @@ async def _deploy_stack_async(stack_name: str, stack_id: str, template: dict,
is_update, previous_stack, created_in_this_run, failed_update,
failed_logical_id, [entry[0] for entry in replaced_resources],
cancelled, template, param_values, retain_except_on_create,
- stack_tags, previous_tags)
+ stack_tags, previous_tags, imports)
if disable_rollback:
stack["StackStatus"] = f"{status_prefix}_FAILED"
stack["StackStatusReason"] = fail_reason
@@ -1100,8 +1155,7 @@ _DIFFED_ATTRIBUTES = (
"UpdateReplacePolicy",
)
-# The types a stack-tag change leaves out of a change set; every other
-# resource the stack holds, taggable or not, is a Modify with Scope Tags.
+# Types a stack-tag change leaves out of a change set (measured).
_UNTAGGED_TYPES = (
"AWS::CloudFormation::CustomResource",
"AWS::CloudFormation::WaitCondition",
@@ -1169,9 +1223,7 @@ def _diff_resources(old_template: dict, new_template: dict, resources: dict | No
attributes in ``_DIFFED_ATTRIBUTES`` differs; each changed attribute becomes
a ``Details`` entry (``Target.Attribute``, plus the property name for
``Properties``) and is listed in ``Scope``, as the API reference defines them.
- A property that references a changed parameter, a replaced resource or an
- attribute of a modified resource in ``template`` (the unresolved new
- template, ``new_template`` by default) gets a detail naming that cause.
+ References in ``template`` (unresolved) to changed parameters or resources add their own details.
``resources`` are the stack's provisioned resources, whose physical ids a
``Remove`` or ``Modify`` reports.
Each resource in ``retag`` (the stack's resources when its tags change)
@@ -1211,8 +1263,7 @@ def _diff_resources(old_template: dict, new_template: dict, resources: dict | No
details.extend(refs.get(name, []))
return details + attr_details[key]
- # A replaced or modified resource changes what references it, which can in
- # turn replace or modify the referencing resource.
+ # Repeat until no reference adds a detail: a change can cascade to its referrers.
refs: dict = {}
while True:
details = {key: _modify_details(key, refs.get(key, {})) for key in common}
@@ -1257,8 +1308,7 @@ def _diff_resources(old_template: dict, new_template: dict, resources: dict | No
scope.append(d["Target"]["Attribute"])
replacement = replacements.get(key, "False")
if retagged:
- # Tags comes before the template's own changes in Details, last in
- # Scope, and has no ChangeSource.
+ # First in Details, last in Scope, no ChangeSource (measured).
details[key].insert(0, {
"Target": {"Attribute": "Tags", "RequiresRecreation": "Never"},
"Evaluation": "Static",
diff --git a/ministack/services/cloudfront.py b/ministack/services/cloudfront.py
index 8bdf15a1..f59a3883 100644
--- a/ministack/services/cloudfront.py
+++ b/ministack/services/cloudfront.py
@@ -1097,23 +1097,6 @@ def _build_cache_policy_xml(parent, policy):
# ---------------------------------------------------------------------------
# AWS-managed policies (cache / origin request / response headers).
-#
-# Real CloudFront ships a fixed catalog of these under every account; Terraform
-# modules reference them by name (e.g. "Managed-CachingDisabled") via the
-# aws_cloudfront_cache_policy/aws_cloudfront_origin_request_policy/
-# aws_cloudfront_response_headers_policy data sources, and ListCachePolicies
-# et al. must report them with Type=managed. They are immutable (Update/Delete
-# error) and identical for every account, so they are seeded once as module
-# constants rather than per-account state — nothing resets or persists them
-# because nothing ever mutates them.
-#
-# Evidence: AWS docs — "Use managed cache policies", "Use managed origin
-# request policies", "Use managed response headers policies" (CloudFront
-# Developer Guide) for names/ids/configs. The literal "Managed-" Name prefix
-# (docs show only the short console name) is confirmed by ops-v2's
-# modules/cloudfront-api, which already looks policies up by e.g.
-# "Managed-CachingDisabled". LastModifiedTime below is a stable placeholder,
-# not an AWS-observed value.
# ---------------------------------------------------------------------------
_MANAGED_POLICY_LAST_MODIFIED = "2020-05-20T04:29:32.290Z"
@@ -1581,9 +1564,7 @@ def _orp_cfg(name, header_behavior, headers=(), cookie_behavior="none", qs_behav
}
-# Evidence: AWS docs "Use managed origin request policies" (CloudFront
-# Developer Guide) for names/ids/behaviors; see the catalog note above
-# _MANAGED_CACHE_POLICIES for the "Managed-" Name-prefix evidence.
+# Managed origin request policies (Developer Guide, "Use managed origin request policies").
_MANAGED_ORIGIN_REQUEST_POLICIES = {
pid: _managed_policy(pid, cfg)
for pid, cfg in {
@@ -1860,9 +1841,7 @@ def _rhp_cfg(name, cors=None, security=None):
}
-# Evidence: AWS docs "Use managed response headers policies" (CloudFront
-# Developer Guide) for names/ids/CORS+security settings; see the catalog note
-# above _MANAGED_CACHE_POLICIES for the "Managed-" Name-prefix evidence.
+# Managed response headers policies (Developer Guide, "Use managed response headers policies").
_MANAGED_RESPONSE_HEADERS_POLICIES = {
pid: _managed_policy(pid, cfg)
for pid, cfg in {
@@ -2270,11 +2249,7 @@ def _list_anycast_ip_lists(query_params):
def _list_cache_policies(query_params):
- """CachePolicyList: managed policies plus stored custom ones.
-
- ``Type`` optionally filters to ``managed`` or ``custom``; omitted, AWS
- returns both (ListCachePolicies API reference).
- """
+ """CachePolicyList: managed policies plus stored custom ones."""
max_items = _qval(query_params, "MaxItems", _DEFAULT_MAX_ITEMS) or _DEFAULT_MAX_ITEMS
type_filter = _qval(query_params, "Type", "")
entries = []
@@ -2298,12 +2273,7 @@ def _list_cache_policies(query_params):
def _list_policies(store, spec, query_params):
- """Generic ``*PolicyList``: managed policies plus stored custom ones.
-
- Shared by origin request policies and response headers policies; the
- summary member and resource tag come from ``spec``. ``Type`` optionally
- filters to ``managed`` or ``custom``; omitted, AWS returns both.
- """
+ """Generic ``*PolicyList``: managed policies plus stored custom ones."""
max_items = _qval(query_params, "MaxItems", _DEFAULT_MAX_ITEMS) or _DEFAULT_MAX_ITEMS
type_filter = _qval(query_params, "Type", "")
entries = []
@@ -2335,12 +2305,7 @@ _MONITORING_SUB_STATUSES = {"Enabled", "Disabled"}
def _create_monitoring_subscription(dist_id, body):
- """CreateMonitoringSubscription (POST, 200): NoSuchDistribution when the
- distribution is unknown. terraform-provider-aws's
- aws_cloudfront_monitoring_subscription resource wires its Update to this
- same Create operation (UpdateWithoutTimeout: resourceMonitoringSubscriptionCreate)
- with no AlreadyExists handling, so Create on a distribution that already
- has a subscription must overwrite its status rather than error."""
+ """CreateMonitoringSubscription (POST, 200): NoSuchDistribution when the distribution is unknown."""
dist = _distributions.get(dist_id)
if not dist:
return _error("NoSuchDistribution", "The specified distribution does not exist.", 404)
@@ -4565,14 +4530,7 @@ def _list_distributions_by_connection_mode(mode):
def find_distribution_for_label(label: str):
- """The distribution whose DomainName is ``<label>.cloudfront.net``.
-
- A viewer request carries no credentials, so this scans the ambient
- account's distributions the same way ``alb.find_lb_for_host`` scans
- ``_lbs.values()`` for ALB's own host-routed data plane — not a
- cross-account search, just the same ambient-scoped lookup every other
- host-routed service uses here.
- """
+ """The distribution whose DomainName is ``<label>.cloudfront.net``."""
domain = f"{label}.cloudfront.net"
for dist in _distributions.values():
if dist.get("DomainName", "").lower() == domain:
@@ -4580,11 +4538,7 @@ def find_distribution_for_label(label: str):
return None
-# The regional/legacy REST-endpoint domain shapes an Origin's DomainName
-# takes when the origin is an S3 bucket (CloudFront Developer Guide, "Amazon
-# S3 origin"). A website-endpoint domain (s3-website-*) isn't matched here:
-# AWS itself requires that shape to be configured as a CustomOriginConfig,
-# never S3OriginConfig, so it is correctly left to the custom-origin path.
+# S3 REST-endpoint domain shapes of an origin's DomainName (Developer Guide, "Amazon S3 origin").
_S3_ORIGIN_DOMAIN_RE = re.compile(
r"^(?P<bucket>[a-z0-9][a-z0-9.-]*[a-z0-9])\.s3(?:\.[a-z0-9-]+|-[a-z0-9-]+)?\.amazonaws\.com$"
)
@@ -4596,10 +4550,7 @@ def _s3_origin_bucket(domain_name: str):
def _dataplane_custom_headers(origin_el):
- """Origin.CustomHeaders — wire name "CustomHeaders" (CloudFormation's
- OriginCustomHeaders is a JSON-only rename, see _CFN_DISTRIBUTION_CONFIG_RENAMES
- above); Items/OriginCustomHeader/{HeaderName,HeaderValue} per the botocore
- cloudfront service-2.json (2020-05-31) shape."""
+ """Origin.CustomHeaders as a list of (HeaderName, HeaderValue)."""
headers = []
ch_el = _find(origin_el, "CustomHeaders")
items_el = _find(ch_el, "Items") if ch_el is not None else None
@@ -4621,10 +4572,10 @@ def _dataplane_origin(origin_el):
"domain_name": domain_name,
"origin_path": _opt_text(origin_el, "OriginPath") or "",
"custom_headers": _dataplane_custom_headers(origin_el),
- # Origin carries exactly one of S3OriginConfig or CustomOriginConfig
- # (AWS's schema is mutually exclusive); the bucket name itself is
- # only ever recoverable from DomainName, never a separate field.
+ # An S3 origin's bucket comes only from its DomainName.
"s3_bucket": _s3_origin_bucket(domain_name) if s3cfg is not None else None,
+ "oac_id": _opt_text(origin_el, "OriginAccessControlId") or "",
+ "oai_id": ((_text(s3cfg, "OriginAccessIdentity") or "").rsplit("/", 1)[-1] if s3cfg is not None else ""),
"http_port": int(_text(custom, "HTTPPort") or "80") if custom is not None else 80,
"https_port": int(_text(custom, "HTTPSPort") or "443") if custom is not None else 443,
"protocol_policy": _text(custom, "OriginProtocolPolicy") if custom is not None else "match-viewer",
@@ -4691,10 +4642,7 @@ def _dataplane_behavior(behavior_el, path_pattern=None):
def parse_distribution_dataplane_config(dist: dict) -> dict:
- """A distribution's stored config XML, reduced to what the data plane
- dispatches against: the default root object, origins by id, the default
- behavior, and ordered behaviors in declaration order (first
- ``path_pattern`` match wins)."""
+ """A distribution's config reduced to what the data plane dispatches on."""
config_el = _dist_config_el(dist)
origins = {}
diff --git a/ministack/services/cloudfront_dataplane.py b/ministack/services/cloudfront_dataplane.py
index 8634ac90..26de44b7 100644
--- a/ministack/services/cloudfront_dataplane.py
+++ b/ministack/services/cloudfront_dataplane.py
@@ -1,53 +1,9 @@
# Copyright (c) 2026 MiniStack Contributors. SPDX-License-Identifier: MIT
# Copies or substantial portions, including AI-assisted ports or rewrites, must retain this notice (see LICENSE).
-"""CloudFront data plane — actually serves a distribution's traffic.
-
-``ministack/services/cloudfront.py`` is the CloudFront control plane: it
-persists distributions, functions, and policies and answers the CloudFront
-REST/XML API. It never serves a byte of viewer traffic — MiniStack's
-CloudFront is otherwise control-plane only. This module is the data plane:
-routed in by ``app.py`` whenever a viewer request's Host is
-``<label>.cloudfront.net`` or ``<label>.cloudfront.<MINISTACK_HOST>`` and
-``<label>`` matches a stored distribution's DomainName.
-
-Modelled, at AWS parity for the pieces real deployments use:
- * a custom origin (``CustomOriginConfig``) or an S3 origin
- (``S3OriginConfig``), selected per matched cache behavior —
- ``DefaultCacheBehavior`` plus ordered ``CacheBehaviors``, first
- ``PathPattern`` match wins, CloudFront glob semantics (``*`` any chars
- incl. ``/``, ``?`` one char, case-sensitive);
- * what reaches the origin: the union of a cache policy's
- ParametersInCacheKeyAndForwardedToOrigin and an attached origin request
- policy (CloudFront Developer Guide, "Understand how origin request
- policies and cache policies work together"), or a legacy
- ``ForwardedValues`` block when the behavior has no cache policy at all;
- * ``ViewerProtocolPolicy``, ``AllowedMethods``, ``DefaultRootObject``,
- and a custom origin's ``OriginPath``/``CustomHeaders``;
- * ``viewer-request``/``viewer-response`` CloudFront Functions
- (``cloudfront-js-1.0``/``-2.0``), executed via ``core.cloudfront_js``;
- * a response-headers policy's custom headers, security headers, and
- removed headers;
- * the response headers CloudFront itself adds (``Via``, ``X-Cache``,
- ``X-Amz-Cf-Pop``, ``X-Amz-Cf-Id``);
- * a stacked distribution (an origin that is itself a CloudFront
- distribution) answers 403 instead of recursing.
-
-Not modelled: caching itself (every request reaches the origin —
-``CachingDisabled`` is the simplest fit and nothing here varies by TTL), WAF,
-logging, geo/IP restrictions, custom error pages, signed URLs/cookies,
-Lambda@Edge, CORS preflight responses (CloudFront's response-headers-policy
-CORS docs describe header values added to whatever response already exists;
-nothing documents CloudFront itself answering an OPTIONS preflight instead of
-forwarding it), the two "all viewer headers plus synthetic
-``CloudFront-*``" origin request policies' extra geo/device headers (the
-viewer headers themselves still forward; the ~20 synthesized
-``CloudFront-Viewer-*``/``CloudFront-Is-*-Viewer`` headers do not, since
-nothing here needs them), and origins addressed by an AWS hostname (ALB,
-Function URL, execute-api, S3 website endpoints) — refused with CloudFront's
-502 before a socket opens, since this emulator never dials a real AWS host.
-A custom origin is otherwise dialed at its own DomainName/HTTPPort or
-HTTPSPort exactly as configured; an explicit ``:port`` in its DomainName,
-which MiniStack's own endpoints carry, overrides those.
+"""CloudFront data plane: serves viewer requests to ``<label>.cloudfront.net`` / ``<label>.cloudfront.<MINISTACK_HOST>``.
+
+Not modelled: caching, WAF, logging, geo restrictions, custom error pages, signed URLs and cookies,
+Lambda@Edge, CORS preflight, synthetic ``CloudFront-Viewer-*`` headers, origins at AWS hostnames (502).
"""
import base64
@@ -70,17 +26,10 @@ from ministack.services import cloudfront, s3
logger = logging.getLogger("cloudfront_dataplane")
_HOP_BY_HOP_REQUEST_HEADERS = {"host", "cookie", "content-length", "connection", "transfer-encoding"}
-# A narrower set used only at the actual socket boundary (_forward_to_origin):
-# by that point Host and Cookie have already been deliberately recomputed by
-# the caller (per the forwarding policy and the Host-forwarding rule) and
-# must survive, unlike the other hop-by-hop headers HTTP itself manages.
+# Hop-by-hop headers dropped at the socket boundary (_forward_to_origin).
_CONNECTION_MANAGEMENT_HEADERS = {"content-length", "connection", "transfer-encoding"}
-# Forwarded regardless of any cache-policy/origin-request-policy header
-# configuration (CloudFront Developer Guide, "HTTP request headers and
-# CloudFront behavior (custom and Amazon S3 origins)": Range and the
-# conditional-GET headers are listed as always forwarded, not gated on
-# whether you've configured CloudFront to forward them).
+# Always forwarded, whatever the policies say (Developer Guide, request headers table).
_ALWAYS_FORWARD_REQUEST_HEADERS = {
"range", "if-match", "if-modified-since", "if-none-match", "if-range", "if-unmodified-since",
}
@@ -101,10 +50,7 @@ class CloudFrontFunctionError(Exception):
def _title_case_header_name(name: str) -> str:
- """"example-header-name" -> "Example-Header-Name" — the conversion
- CloudFront Functions applies to every header name when it converts the
- event object back into an HTTP request or response. ASCII letters only;
- left untouched otherwise, matching the documented behavior."""
+ """"example-header-name" -> "Example-Header-Name", as CloudFront Functions title-case header names."""
return "-".join(part[:1].upper() + part[1:] if part[:1].isascii() else part for part in name.split("-"))
@@ -131,10 +77,7 @@ def _event_cookies_from_header(cookie_header: str) -> dict:
def _event_cookies_from_set_cookie(set_cookie) -> dict:
- """The response.cookies event object built from one or more raw
- Set-Cookie header values, grouped by cookie name with a ``multiValue``
- array for repeats and an ``attributes`` string carrying everything after
- the first ``;`` (functions-event-structure.html, "Cookie attributes")."""
+ """The response.cookies event object built from raw Set-Cookie header values."""
values = set_cookie if isinstance(set_cookie, list) else ([set_cookie] if set_cookie else [])
cookies = {}
for raw in values:
@@ -174,12 +117,7 @@ def _event_querystring(query_params: dict) -> dict:
def _merge_function_headers(original_headers_obj: dict, returned_headers_obj: dict) -> dict:
- """A name(lowercase)->value dict built from a function's returned headers
- object. functions-event-structure.html: a header whose ``multiValue``
- field is unchanged from what the event carried is read only for its
- primary ``value`` (the function is assumed to have edited just that),
- and the rest of that header's original values carry through unedited; a
- header whose ``multiValue`` did change is read in full instead."""
+ """A name(lowercase)->value dict built from a function's returned headers object."""
original = original_headers_obj or {}
out = {}
for name, field in (returned_headers_obj or {}).items():
@@ -249,9 +187,7 @@ async def _run_function(function_arn: str, event: dict) -> dict:
if code is None:
raise CloudFrontFunctionError(f"{function_arn} has no published (LIVE) code")
try:
- # A function cannot call back into MiniStack (no network access in
- # its sandbox — see core.cloudfront_js), so this is run_offloop work,
- # same as the S3 origin fetch (core.concurrency).
+ # A function cannot call back into MiniStack, so this is run_offloop work.
return await run_offloop(cloudfront_js.evaluate, code, event)
except cloudfront_js.CloudFrontFunctionError as exc:
raise CloudFrontFunctionError(str(exc)) from exc
@@ -263,11 +199,7 @@ async def _run_function(function_arn: str, event: dict) -> dict:
def _glob_to_regex(pattern: str):
- # "You can optionally include a slash (/) at the beginning of the path
- # pattern... If you don't specify the / at the beginning of the path,
- # this character is automatically implied; CloudFront treats the path
- # the same with or without the leading /" (CloudFront Developer Guide,
- # "Path pattern").
+ # A leading / in a path pattern is optional (Developer Guide, "Path pattern").
if not pattern.startswith("/"):
pattern = "/" + pattern
out = [".*" if ch == "*" else "." if ch == "?" else _re_escape(ch) for ch in pattern]
@@ -282,22 +214,12 @@ def _match_behavior(parsed: dict, path: str):
# ---------------------------------------------------------------------------
-# Origin-request forwarding — CloudFront Developer Guide, "Understand how
-# origin request policies and cache policies work together" (the table of
-# None/All/Allow-list/Block-list combinations this reproduces) plus legacy
-# ``ForwardedValues`` ("Control origin requests with a policy": "With legacy
-# cache settings, CloudFront forwards the headers to your origin by
-# default").
+# Origin-request forwarding: the cache policy / origin request policy combination table (Developer Guide).
# ---------------------------------------------------------------------------
def _behavior_kind(behavior: str, names, lowercase: bool = False):
- """A cache-policy or origin-request-policy behavior value, reduced to a
- (kind, set) pair the combinator below understands. ``allViewer`` and
- ``allViewerAndWhitelistCloudFront`` (origin-request-policy-only header
- behaviors) both forward every viewer header, so both map to "all" —
- this module doesn't synthesize the ~20 CloudFront-added geo/device
- headers the second one also adds (see module docstring)."""
+ """A cache or origin request policy behavior value as a (kind, set) pair."""
names = [n.lower() for n in (names or [])] if lowercase else list(names or [])
if behavior in ("all", "allViewer", "allViewerAndWhitelistCloudFront"):
return ("all", None)
@@ -309,10 +231,7 @@ def _behavior_kind(behavior: str, names, lowercase: bool = False):
def _combine_forward(cache_kind_set, orp_kind_set, name: str) -> bool:
- """Whether ``name`` reaches the origin, per the cache-policy /
- origin-request-policy combination table. ``orp_kind_set`` is None when
- the behavior has no origin request policy at all (same result as an
- origin request policy whose behavior is "none" for every category)."""
+ """Whether ``name`` reaches the origin, per the cache-policy / origin-request-policy combination table."""
cache_kind, cache_set = cache_kind_set
if cache_kind == "all":
return True # a cache policy's "all" always wins, even over an ORP block list
@@ -343,10 +262,7 @@ def _combine_forward(cache_kind_set, orp_kind_set, name: str) -> bool:
def _legacy_predicates(fv: dict):
- """Forwarding predicates for a behavior that has no CachePolicyId — a
- legacy ``ForwardedValues`` block instead (AWS requires a cache policy
- before an origin request policy can be attached, so legacy behaviors
- never carry one)."""
+ """Forwarding predicates for a legacy ``ForwardedValues`` behavior (no CachePolicyId)."""
header_names = {n.lower() for n in (fv.get("headers") or [])}
forward_all_headers = "*" in (fv.get("headers") or [])
cookies_forward = fv.get("cookies_forward", "none")
@@ -415,22 +331,12 @@ def _forwarding_predicates(behavior: dict):
def _cors_origin_allowed(allow_origins: list, viewer_origin: str) -> bool:
- """AccessControlAllowOrigins entries: "*" or an origin value matched the
- way the response-headers-policy CORS doc's wildcard examples show
- (``*`` only ever a glob over the whole value here, not AWS's narrower
- leftmost-subdomain validation rule — that rule constrains what a policy
- may be *created* with, not what this module needs to match against)."""
+ """Whether ``viewer_origin`` matches an AccessControlAllowOrigins entry ("*" or a glob)."""
return any(p == "*" or fnmatch.fnmatchcase(viewer_origin, p) for p in allow_origins or [])
def _apply_cors_headers(headers: dict, cors_cfg: dict, viewer_origin: str, method: str) -> dict:
- """Response-headers-policy CORS config (CloudFront Developer Guide,
- "Understand response headers policies" > "CORS headers"): these are
- headers added to a response to a CORS request, so nothing here applies
- unless the viewer request carried an Origin header. Access-Control-
- Allow-Headers/-Methods/-Max-Age are documented as values for
- *preflight* responses specifically; Allow-Origin/-Credentials/-Expose-
- Headers apply to any CORS response."""
+ """Apply a response headers policy's CORS headers (Developer Guide, "CORS headers")."""
if not cors_cfg or not viewer_origin or not _cors_origin_allowed(cors_cfg.get("AllowOrigins"), viewer_origin):
return headers
result = dict(headers)
@@ -468,9 +374,7 @@ def _apply_response_headers_policy(headers: dict, policy_cfg: dict, viewer_origi
return
result[key] = value
- # CustomHeaders/RemoveHeaders are None (not []) when the create request
- # never supplied that block at all, distinct from supplying an empty one
- # — `or []` covers both "absent" and "empty".
+ # CustomHeaders/RemoveHeaders are None when the policy never supplied that block.
for item in policy_cfg.get("CustomHeaders") or []:
_set(item["Header"], item["Value"], item["Override"])
@@ -535,12 +439,7 @@ def _render_headers(headers: dict, title_case: bool) -> dict:
return {_title_case_header_name(name): value for name, value in headers.items()}
-# Mirrors the fixed body CloudFront's own synthesized error pages use — a
-# "The request could not be satisfied" title/heading across status codes,
-# with only the numeric status and explanation paragraph varying — observed
-# from live responses rather than published in the CloudFront Developer
-# Guide, so the per-status explanation text below is inference, not an
-# AWS-confirmed value.
+# The fixed body of CloudFront's own error pages ("The request could not be satisfied").
_CF_ERROR_TEMPLATE = """<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML><HEAD><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<TITLE>ERROR: The request could not be satisfied</TITLE>
@@ -566,12 +465,7 @@ def _cf_error_response(status: int, explanation: str, x_cache: str) -> tuple:
return status, _render_headers(headers, title_case=True), body
-# CloudFront Developer Guide, http-502-bad-gateway.html: "the SSL/TLS
-# handshake fails, and CloudFront returns an HTTP status code 502 (Bad
-# Gateway) and sets the X-Cache header to Error from cloudfront" — the only
-# documented X-Cache value for a CloudFront-synthesized error; applied here
-# to every such response (unreachable origin, blocked method, protocol
-# policy, function failure), not only the SSL case the doc names.
+# A failed origin TLS handshake is a 502 (Developer Guide, http-502-bad-gateway).
_ERROR_X_CACHE = "Error from cloudfront"
@@ -588,12 +482,7 @@ class OriginTimeout(Exception):
"""The origin didn't respond within OriginReadTimeout."""
-# A custom origin's DomainName can be AWS-owned-shaped — an execute-api/ALB/
-# Lambda-URL/S3 host Terraform computed from a MiniStack-run resource, using
-# AWS's own naming template. It is never dialed: refused with a 502 before a
-# socket opens (module docstring's Not-modelled list). Every other origin,
-# including a plain MiniStack-local host, is dialed at its own DomainName and
-# port exactly as configured (sns.py's HTTP delivery precedent).
+# An origin addressed by an AWS-owned hostname is refused with 502, so no traffic reaches real AWS.
_AWS_OWNED_HOST_SUFFIXES = (".amazonaws.com", ".amazonaws.com.cn", ".on.aws", ".api.aws", ".cloudfront.net")
@@ -611,12 +500,6 @@ def _origin_connect_target(origin: dict, viewer_is_https: bool) -> tuple:
DomainName and HTTPPort/HTTPSPort, dialed exactly as configured."""
policy = origin["protocol_policy"]
use_https = policy == "https-only" or (policy == "match-viewer" and viewer_is_https)
- # MiniStack's own endpoints carry the gateway port (an HTTP API's apiEndpoint
- # is `https://<id>.execute-api.<host>:4566`), so an origin built from one
- # names it; AWS itself takes ports only from HTTPPort/HTTPSPort.
- host, sep, port = origin["domain_name"].rpartition(":")
- if sep and port.isdigit():
- return host, int(port), use_https
port = origin["https_port"] if use_https else origin["http_port"]
return origin["domain_name"], port, use_https
@@ -635,10 +518,7 @@ def _origin_ssl_context() -> ssl.SSLContext:
def _set_header_ci(headers: dict, name: str, value) -> None:
- """Set ``name: value``, dropping any existing key that matches
- case-insensitively first — otherwise a lowercase viewer-forwarded key and
- a freshly Title-Cased one (e.g. a forwarded "host" plus this module's own
- "Host") both survive as distinct dict keys and become two header lines."""
+ """Set ``name: value``, replacing any case-insensitive match."""
for existing in [k for k in headers if k.lower() == name.lower()]:
del headers[existing]
headers[name] = value
@@ -664,15 +544,11 @@ def _forward_to_origin(origin: dict, method: str, uri: str, query_string: str,
fwd_headers = {k: v for k, v in headers.items() if k not in _CONNECTION_MANAGEMENT_HEADERS}
for name, value in origin.get("custom_headers") or []:
_set_header_ci(fwd_headers, name, value)
- # Host defaults to the origin's own configured DomainName — CloudFront's
- # own default Host selection — unless the forwarding policy carries the
- # viewer's.
+ # Host is the origin's DomainName unless the policies forward the viewer's Host.
_set_header_ci(fwd_headers, "Host", headers.get("host", origin["domain_name"]))
_set_header_ci(fwd_headers, "Via", f"1.1 {_cf_via_hash()}.cloudfront.net (CloudFront)")
_set_header_ci(fwd_headers, "X-Amz-Cf-Id", _cf_request_id())
- # Always appended regardless of the forwarding policy (CloudFront
- # Developer Guide, "Client IP addresses"), from the viewer's own XFF, not
- # whatever the policy let through into `headers`.
+ # Always appended (Developer Guide, "Client IP addresses").
_set_header_ci(fwd_headers, "X-Forwarded-For",
_origin_x_forwarded_for(viewer_x_forwarded_for, client_ip))
# RFC 9110 §5.3: a repeated field value is one comma-joined line.
@@ -695,9 +571,7 @@ def _forward_to_origin(origin: dict, method: str, uri: str, query_string: str,
except TimeoutError as e:
raise OriginTimeout(str(e)) from e
except Exception as e:
- # Covers an unreachable origin and a custom origin's untrusted/
- # self-signed certificate failing verification (see
- # http-502-bad-gateway.html's self-signed/expired/invalid-chain case).
+ # An unreachable origin, or an untrusted or self-signed origin certificate.
raise OriginUnreachable(str(e)) from e
finally:
conn.close()
@@ -709,19 +583,15 @@ def _forward_to_origin(origin: dict, method: str, uri: str, query_string: str,
def _s3_object_key(origin: dict, uri: str) -> str:
- """CloudFront concatenates OriginPath directly in front of the request
- URI to build what reaches the origin. S3 keys are stored decoded, so the
- percent-encoding forwarded verbatim to a custom origin is decoded here
- instead, and the leading slash dropped."""
+ """OriginPath prefixed to the request URI, as CloudFront builds the origin path."""
return unquote((origin.get("origin_path") or "") + uri).lstrip("/")
-def _serve_s3_origin(origin: dict, method: str, uri: str, headers: dict) -> tuple:
- """Blocking: run under core.concurrency.run_offloop (an in-process store
- read, never re-entrant). Mirrors _forward_to_origin's calling convention
- (status, headers, body — no reason phrase; the caller derives one from
- the status)."""
- return s3.serve_cloudfront_origin_fetch(origin["s3_bucket"], _s3_object_key(origin, uri), method, headers)
+def _serve_s3_origin(origin: dict, method: str, uri: str, headers: dict, dist_arn: str = "") -> tuple:
+ """Fetch from an S3 origin in-process (blocking; run off the event loop)."""
+ return s3.serve_cloudfront_origin_fetch(
+ origin["s3_bucket"], _s3_object_key(origin, uri), method, headers,
+ source_arn=dist_arn if origin.get("oac_id") else "", oai_id=origin.get("oai_id", ""))
# ---------------------------------------------------------------------------
@@ -758,14 +628,7 @@ def _strip_hop_by_hop_response_headers(headers: dict) -> dict:
def _check_viewer_protocol_policy(behavior: dict, method: str, viewer_is_https: bool, request_uri: str,
query_string: str, headers: dict):
- """A redirect/forbid response, or None to continue. CloudFront Developer
- Guide, "Require HTTPS for communication between viewers and CloudFront" >
- "Require HTTPS for viewers": redirect-to-https sends 301 (GET/HEAD) or
- 307 (other methods, HTTP/1.1+ — "This guarantees that the request is
- sent again to the new location using the same method and body payload")
- with the HTTPS URL; https-only answers plain HTTP with 403. (The same
- page also documents a 403 for a non-idempotent method on pre-HTTP/1.1 —
- out of scope here: this module has no HTTP-version signal.)"""
+ """A redirect/forbid response, or None to continue."""
policy = behavior.get("viewer_protocol_policy", "allow-all")
if viewer_is_https or policy == "allow-all":
return None
@@ -805,11 +668,7 @@ def _apply_default_root_object(default_root_object: str, request_uri: str) -> st
def _check_stacked_distribution(headers: dict):
- """CloudFront Developer Guide, http-403-permission-denied.html, "Stacked
- distributions cause a 403 error": a request that already carries this
- hop's own Via marker (`_forward_to_origin` always stamps one) is refused
- rather than forwarded again, so a distribution fronting itself fails
- fast instead of recursing forever."""
+ """403 for a request that already passed through this hop (Developer Guide, stacked distributions)."""
if "(cloudfront)" in headers.get("via", "").lower():
return _cf_error_response(
403, "This distribution is configured to serve another CloudFront distribution as its origin.",
@@ -825,17 +684,7 @@ def _check_stacked_distribution(headers: dict):
async def handle_request(dist: dict, method: str, path: str, raw_uri: str, raw_query_string: str,
headers: dict, body: bytes, query_params: dict, client_ip: str = "127.0.0.1") -> tuple:
- """Serve one viewer request against ``dist`` (a cloudfront.py distribution
- record). Returns an (status, headers, body) tuple in the shape the ASGI
- gateway sends directly to the client.
-
- ``path`` is the ASGI-decoded path (used only for cache-behavior
- path-pattern matching); ``raw_uri`` and ``raw_query_string`` are the
- request target exactly as the viewer sent it, percent-encoding intact,
- for forwarding byte-exact when nothing rewrites them (CloudFront
- Developer Guide, "Restrictions on all edge functions" > "URI, query
- string, and headers encoding"). ``client_ip`` is the TCP peer's address.
- """
+ """Serve one viewer request against ``dist`` (a cloudfront.py distribution record)."""
stacked_response = _check_stacked_distribution(headers)
if stacked_response is not None:
return stacked_response
@@ -878,10 +727,7 @@ async def handle_request(dist: dict, method: str, path: str, raw_uri: str, raw_q
return _cf_error_response(503, "The Lambda function failed to execute.", _ERROR_X_CACHE)
if isinstance(result, dict) and "statusCode" in result:
- # Function-generated response: CloudFront returns it to the
- # viewer WITHOUT forwarding to the origin, running the
- # viewer-response function, or applying the response-headers
- # policy (functions-event-structure.html).
+ # A function-generated response goes straight to the viewer, skipping the origin.
resp_headers = _merge_function_headers(None, result.get("headers"))
resp_headers = _add_cloudfront_headers(resp_headers, "FunctionGeneratedResponse from cloudfront")
set_cookie_values = _set_cookie_values_from_event(result.get("cookies"))
@@ -889,15 +735,11 @@ async def handle_request(dist: dict, method: str, path: str, raw_uri: str, raw_q
resp_headers["set-cookie"] = (
set_cookie_values if len(set_cookie_values) > 1 else set_cookie_values[0]
)
- # statusDescription has no outlet: ASGI's http.response.start
- # carries only a numeric status, no reason-phrase field, so
- # there is nowhere to forward it to the viewer wire response.
+ # statusDescription is dropped: ASGI carries no reason phrase.
resp_body = _body_from_response_event(result, b"")
return result["statusCode"], _render_headers(resp_headers, title_case=True), resp_body
- # Request-side headers stay lowercase-keyed internally; every
- # viewer-facing response is Title-Cased once, at its single return
- # point, rather than a decision repeated per header.
+ # Request headers stay lowercase internally; responses are Title-Cased once, on return.
req = result if isinstance(result, dict) else event["request"]
request_uri = req.get("uri", request_uri)
request_headers = _merge_function_headers(event["request"]["headers"], req.get("headers"))
@@ -907,11 +749,7 @@ async def handle_request(dist: dict, method: str, path: str, raw_uri: str, raw_q
request_headers["cookie"] = "; ".join(cookie_pairs)
qs_field = req.get("querystring")
if isinstance(qs_field, str):
- # A function rearranged the querystring into a literal string
- # (functions-event-structure.html, "Query strings values or
- # query string objects"). It can no longer be decomposed by
- # parameter name for cache/origin-request-policy filtering, so
- # it is forwarded to the origin exactly as returned.
+ # A function rewrote the querystring as a literal string (functions-event-structure).
literal_query_string = qs_field
request_query = {}
elif qs_field is not None:
@@ -940,11 +778,7 @@ async def handle_request(dist: dict, method: str, path: str, raw_uri: str, raw_q
if header_allowed("user-agent") and "user-agent" in request_headers:
fwd_headers["user-agent"] = request_headers["user-agent"]
else:
- # CloudFront Developer Guide, "User-Agent header": "CloudFront adds
- # this header regardless of whether the request from the viewer
- # includes a User-Agent header. If the request from the viewer
- # includes a User-Agent header, CloudFront removes it" — unless the
- # policy forwards it (handled above), replace it unconditionally.
+ # User-Agent is "Amazon CloudFront" unless forwarded (Developer Guide, "User-Agent header").
fwd_headers["user-agent"] = "Amazon CloudFront"
if forward_host:
fwd_headers["host"] = request_headers.get("host", "")
@@ -965,7 +799,7 @@ async def handle_request(dist: dict, method: str, path: str, raw_uri: str, raw_q
if origin.get("s3_bucket"):
status, origin_headers_list, origin_body = await run_offloop(
- _serve_s3_origin, origin, method, request_uri, fwd_headers,
+ _serve_s3_origin, origin, method, request_uri, fwd_headers, dist.get("ARN", ""),
)
reason = http.client.responses.get(status, "")
origin_headers = _multidict_from_pairs(origin_headers_list.items() if isinstance(origin_headers_list, dict)
@@ -973,9 +807,7 @@ async def handle_request(dist: dict, method: str, path: str, raw_uri: str, raw_q
else:
full_uri = (origin.get("origin_path") or "") + request_uri
try:
- # The origin may be MiniStack's own gateway (e.g. a distribution
- # deliberately fronting another MiniStack-hosted service) — a
- # bounded pool here could deadlock against itself.
+ # The origin may be MiniStack's own gateway, so the call must be reentrant.
status, reason, origin_headers_list, origin_body = await run_reentrant(
_forward_to_origin, origin, method, full_uri, forward_qs, fwd_headers, body, viewer_is_https,
client_ip, request_headers.get("x-forwarded-for", ""),
@@ -996,10 +828,7 @@ async def handle_request(dist: dict, method: str, path: str, raw_uri: str, raw_q
response_body = origin_body
response_function_arn = behavior["functions"].get("viewer-response")
- # CloudFront does not invoke a viewer-response function when the origin
- # answers 400 or above (functions-event-structure.html, "Status code and
- # body" note); the response-headers policy above still applies to that
- # response, only the function is skipped.
+ # No viewer-response function runs when the origin answers 400 or above.
if response_function_arn and status < 400:
event = _build_function_event(
"viewer-response", dist_id, dist_domain, request_id,
@@ -1013,9 +842,7 @@ async def handle_request(dist: dict, method: str, path: str, raw_uri: str, raw_q
return _cf_error_response(503, "The Lambda function failed to execute.", _ERROR_X_CACHE)
response = result if isinstance(result, dict) else event["response"]
status = response.get("statusCode", status)
- # Replace, not merge: the returned headers object is the complete
- # set the function wants sent, so a header it deleted must stay gone
- # rather than survive from the pre-function response.
+ # Replace, not merge: a header the function deleted stays gone.
response_headers = _merge_function_headers(event["response"]["headers"], response.get("headers"))
set_cookie_values = _set_cookie_values_from_event(response.get("cookies"))
if set_cookie_values:
diff --git a/ministack/services/ecs.py b/ministack/services/ecs.py
index a1e43c9b..bd724fc2 100644
--- a/ministack/services/ecs.py
+++ b/ministack/services/ecs.py
@@ -574,6 +574,8 @@ def _cluster_view(cluster, include):
c["tags"] = _tags.get(cluster["clusterArn"], []) if "TAGS" in include else []
if "ATTACHMENTS" in include:
c["attachments"] = cluster.get("attachments", [])
+ if cluster.get("attachmentsStatus"):
+ c["attachmentsStatus"] = cluster["attachmentsStatus"]
if "CONFIGURATIONS" in include and "configuration" in cluster:
c["configuration"] = cluster["configuration"]
return c
@@ -655,8 +657,7 @@ def _register_task_definition(data):
cdef.setdefault("cpu", 0)
cdef.setdefault("essential", True)
- rev = _task_def_latest.get(family, 0) + 1
- _task_def_latest[family] = rev
+ rev = _next_task_def_revision(family)
td_key = f"{family}:{rev}"
arn = f"arn:aws:ecs:{get_region()}:{get_account_id()}:task-definition/{td_key}"
@@ -702,6 +703,13 @@ def _register_task_definition(data):
return json_response({"taskDefinition": td, "tags": req_tags})
+def _next_task_def_revision(family):
+ """Reserve the family's next revision number; numbers are never reused."""
+ rev = _task_def_latest.get(family, 0) + 1
+ _task_def_latest[family] = rev
+ return rev
+
+
def _deregister_task_definition(data):
td_ref = data.get("taskDefinition", "")
key = _resolve_td_key(td_ref)
@@ -1023,8 +1031,7 @@ def _complete_service_deployment(cluster_name, svc_key, expected_deployment_id=N
and primary.get("id") != expected_deployment_id)
or primary.get("runningCount", 0) < svc.get("desiredCount", 0)):
return
- # A callback can start more tasks during reconciliation. Its own
- # startup window cannot certify those newly RUNNING replacements.
+ # Tasks started during reconciliation are not yet past their startup window.
stable_count = sum(
1 for task in _tasks.values()
if task.get("group") == f"service:{svc_name}"
@@ -1114,13 +1121,9 @@ def _schedule_service_deployment_completion(
if (healthy_task and healthy_task.get("lastStatus") == "RUNNING"):
_record_service_task_healthy(svc_key, healthy_task)
_refresh_service_state(cluster_name, group)
- # The first task establishes image digests before the other
- # tasks are started. Also progress capacity-limited rollouts
- # now that this task has survived the startup window.
+ # The first task's digests unblock the remaining tasks and capacity-limited rollouts.
_reconcile_service_tasks(cluster_name, svc_key)
- # A predecessor's startup callback may run after an update,
- # while a crashing replacement is briefly RUNNING. Its grace
- # window cannot establish the replacement's stability.
+ # A predecessor's callback cannot certify a replacement deployment.
_complete_service_deployment(
cluster_name, svc_key, expected_deployment_id=deployment_id)
@@ -1142,8 +1145,7 @@ def _record_service_task_failure(task, digest_resolution_failed=False):
schedule_completion = False
with resource_lock("ecs-service", svc_key):
- # A registry lookup can outlive StopTask/reset. Only digest resolution
- # requires a still-active worker; ordinary task failures arrive stopped.
+ # A registry lookup can outlive StopTask/reset.
if digest_resolution_failed and not _task_is_active(task.get("taskArn"), task):
return
svc = _services.get(svc_key)
@@ -1241,8 +1243,7 @@ def _reconcile_service_tasks(cluster_name, svc_key):
network_cfg = svc.get("networkConfiguration", {})
if desired == 0:
- # No replacement worker will schedule completion for an empty rollout.
- # Stop every deployment's tasks, including predecessors of a force.
+ # Empty rollout: no worker will complete it, so stop every deployment's tasks here.
for task_arn, task in list(_tasks.items()):
if (task.get("group") == f"service:{svc_name}"
and task.get("clusterArn") == cluster_arn
@@ -1261,10 +1262,7 @@ def _reconcile_service_tasks(cluster_name, svc_key):
primary = _primary_deployment(svc)
is_rolling = (svc.get("deploymentController") or {}).get("type", "ECS") == "ECS"
- # Partition tasks by deployment as well as definition: a forced rollout
- # uses the same definition but must replace its predecessor's tasks. A PENDING
- # task already counts toward desired capacity, so reconciliation does not
- # launch duplicates while its worker is pulling the image.
+ # Partition by deployment too (a force reuses the definition); PENDING counts as capacity.
current_tasks = []
stale_tasks = []
for arn, t in _tasks.items():
@@ -1321,8 +1319,7 @@ def _reconcile_service_tasks(cluster_name, svc_key):
if to_spawn > 0:
if (is_rolling and primary and _get_docker()
and _deployment_needs_first_task(primary, td, svc)):
- # Only the first task resolves tags. Its worker releases the
- # remaining capacity after publishing the deployment's digests.
+ # Only the first task resolves tags; its worker releases the rest of the capacity.
to_spawn = 0 if current_tasks else min(to_spawn, 1)
if to_spawn > 0:
_run_task({
@@ -1545,8 +1542,7 @@ def _update_service(data):
is_rolling = (svc.get("deploymentController") or {}).get("type", "ECS") == "ECS"
if (td_arn != svc["taskDefinition"]
or (is_rolling and data.get("forceNewDeployment", False))):
- # Bind legacy persisted tasks before inserting another deployment of
- # the same definition; afterwards its ARN alone is ambiguous.
+ # Bind legacy tasks to their deployment before a same-definition one is added.
for task in _tasks.values():
if (task.get("group") == f"service:{svc_name}"
and task.get("clusterArn") == svc.get("clusterArn")):
@@ -2279,9 +2275,7 @@ def _prepare_service_images(task, td, docker_client):
task.setdefault("_image_resolution_containers", []).append(name)
else:
unresolved = True
- # MiniStack supports offline images for both launch types. A
- # missing registry manifest is not a deployment failure when
- # Docker can still run the requested image from its local cache.
+ # A missing manifest is not a failure while the image is cached locally (offline).
try:
docker_client.images.get(image)
except Exception:
@@ -2294,8 +2288,7 @@ def _prepare_service_images(task, td, docker_client):
return None
deployment["_image_digests"] = digests
if unresolved:
- # Stop resolving after three failed attempts. The circuit breaker
- # still applies to images that cannot execute from local cache.
+ # Stop resolving after three failures; the breaker still covers uncached images.
deployment["_image_resolution_disabled"] = True
if unresolved and not unresolved_images_cached and (svc.get("deploymentConfiguration") or {}).get(
"deploymentCircuitBreaker", {}).get("enable", False):
@@ -2322,16 +2315,14 @@ def _run_docker_container(
repository = _image_repository(cache_image)
tail = cache_image.rsplit("/", 1)[-1]
tag = tail.rsplit(":", 1)[1] if ":" in tail else "latest"
- # The agent tags a successful first-task canonical pull
- # back to its requested URI, preserving the default cache.
+ # Tag the first-task canonical pull back to its requested URI.
if not pulled.tag(repository, tag=tag):
raise RuntimeError("unable to tag the pulled container image")
except Exception as exc:
if not allow_cached:
raise _ImagePullError(str(exc)) from exc
logger.warning("ECS: image pull failed for %s; trying the local cache", cdef["image"])
- # Preserve the resolver's tag fallback. Later tasks prefer the
- # pinned cache entry, then try the original tag offline.
+ # Pull failed: try the pinned cache entry, then the original tag.
from docker.errors import ImageNotFound
for candidate in dict.fromkeys((cache_image or cdef["image"], fallback_image)):
if not candidate:
@@ -2457,9 +2448,7 @@ def _start_task_worker(task, td, container_overrides, docker_client):
digest = cdef["image"].split("@", 1)[1]
if digest:
effective_cdef["image"] = f"{_image_repository(cdef['image'])}@{digest}"
- # ECS reports the resolved manifest digest. Under EC2's default
- # policy it remains that digest even if a pull falls back to the
- # original tag's cache, as in the agent's state-change payload.
+ # Report the resolved digest even when the pull fell back to the cached tag.
with resource_lock("ecs-task", task_arn):
if _task_is_active(task_arn, task):
task["containers"][i]["imageDigest"] = digest
diff --git a/ministack/services/iot.py b/ministack/services/iot.py
index c6bf5528..82048df5 100644
--- a/ministack/services/iot.py
+++ b/ministack/services/iot.py
@@ -7823,10 +7823,8 @@ async def handle_websocket(
# a handshake that sends no CONNECT registers nothing.
#
# The TLS layer adds one constraint of its own: a presented chain is verified,
-# so a certificate the listener does not trust fails the handshake before any
-# of the above. The trusted set is the Local CA plus every registered CA and
-# device certificate, whatever its status: as on AWS, a registered device
-# certificate keeps connecting after its CA is deactivated or deleted.
+# so an untrusted certificate fails the handshake first. Trusted: the Local CA and every
+# registered CA and device certificate, whatever its status.
_MTLS_DEFAULT_PORT = 8883
_MTLS_READ_CHUNK = 65536
diff --git a/ministack/services/lambda_svc.py b/ministack/services/lambda_svc.py
index 0be8d9aa..9c134a78 100644
--- a/ministack/services/lambda_svc.py
+++ b/ministack/services/lambda_svc.py
@@ -3846,9 +3846,7 @@ def _docker_cp_dir(container, src_dir: str, dest_dir: str, arcname: str = "."):
def _docker_cp_file(container, src_path: str, dest_path: str):
- """Copy one local file into a Docker container at ``dest_path``, creating
- its parent directories (``put_archive`` needs an existing destination).
- A symlinked source is followed, as a bind mount would."""
+ """Copy one local file (symlinks followed) into a container at ``dest_path``."""
import tarfile
buf = io.BytesIO()
with tarfile.open(fileobj=buf, mode="w", dereference=True) as tar:
@@ -4134,9 +4132,7 @@ _CONTAINER_TRUSTSTORE_PATH = "/var/ministack/truststore.p12"
def _wire_cognito_issuer_host(run_kwargs, container_env, mounts, runtime="", copies=None):
"""Under USE_SSL=1, resolve the Cognito issuer hosts to the gateway and trust its cert.
- With ``copies`` (DinD: the host daemon cannot see this container's files) the
- trust files are listed there as ``(container_path, local_path)`` for docker cp
- instead of being bind-mounted.
+ With ``copies`` (MiniStack in a container) the trust files are listed for docker cp, not bind-mounted.
"""
from ministack.core import tls as _tls
diff --git a/ministack/services/rds.py b/ministack/services/rds.py
index 33569924..7a1e0f57 100644
--- a/ministack/services/rds.py
+++ b/ministack/services/rds.py
@@ -6789,8 +6789,7 @@ def _describe_db_cluster_snapshot_attributes(p):
if not snap_id or snap_id not in _db_cluster_snapshots:
return _error("DBClusterSnapshotNotFoundFault",
f"DB cluster snapshot {snap_id} not found.", 404)
- # Manual snapshots default to no shared accounts (empty "restore" values);
- # ModifyDBClusterSnapshotAttribute is not implemented, so this is always empty.
+ # Never shared: ModifyDBClusterSnapshotAttribute is not implemented.
result = (
f"<DBClusterSnapshotAttributesResult>"
f"<DBClusterSnapshotIdentifier>{_esc(snap_id)}</DBClusterSnapshotIdentifier>"
diff --git a/ministack/services/s3.py b/ministack/services/s3.py
index 2e013d51..7b105e20 100644
--- a/ministack/services/s3.py
+++ b/ministack/services/s3.py
@@ -4476,22 +4476,38 @@ def _head_object(bucket_name: str, key: str, headers: dict | None = None, query_
return 200, resp_headers, b""
-def serve_cloudfront_origin_fetch(bucket_name: str, key: str, method: str, headers: dict) -> tuple:
- """A CloudFront data plane's origin fetch against an S3 origin, served
- in-process (never a real wire boundary, so no signature to verify —
- calls ``_get_object``/``_head_object`` directly).
-
- A missing key answers 403 AccessDenied, not 404 NoSuchKey: the OAC
- bucket policy CloudFront's console generates grants only
- ``s3:GetObject``, and per the GetObject API reference's "Permissions"
- note, without ``s3:ListBucket`` a missing key can't be distinguished
- from one the caller isn't allowed to see.
- """
+def _cloudfront_origin_allows(owner: str, bucket_name: str, key: str, source_arn: str, oai_id: str) -> bool:
+ """Whether S3 lets CloudFront read the object: OAC as cloudfront.amazonaws.com, OAI as its user, else anonymous."""
+ if _foreign_bucket_allows(owner, bucket_name, key, "GET", {}, ""):
+ return True
+ policy = _bucket_policies.get_scoped(owner, None, bucket_name)
+ if not policy or not (source_arn or oai_id):
+ return False
+ from ministack.core.iam_evaluator import EvalContext, evaluate_resource_policy
+
+ ctx = EvalContext(
+ principal_arn=(f"arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity {oai_id}"
+ if oai_id else "*"),
+ principal_type="User" if oai_id else "Service",
+ principal_account="",
+ action="s3:GetObject",
+ resource_arn=f"arn:aws:s3:::{bucket_name}/{key}",
+ region=get_region(),
+ service_context={"aws:sourcearn": source_arn} if source_arn else {},
+ )
+ service = None if oai_id else "cloudfront.amazonaws.com"
+ return evaluate_resource_policy(policy, ctx, service=service).decision == "Allow"
+
+
+def serve_cloudfront_origin_fetch(bucket_name: str, key: str, method: str, headers: dict,
+ source_arn: str = "", oai_id: str = "") -> tuple:
+ """A CloudFront origin fetch from an S3 bucket, served in-process."""
headers = dict(headers or {})
bucket = _ensure_bucket(bucket_name)
if bucket is None:
return _no_such_bucket(bucket_name)
- if key not in bucket["objects"]:
+ owner = _bucket_owner_account(bucket_name) or get_account_id()
+ if not _cloudfront_origin_allows(owner, bucket_name, key, source_arn, oai_id) or key not in bucket["objects"]:
return _error("AccessDenied", "Access Denied", 403, f"/{bucket_name}/{key}")
if method == "HEAD":
status, resp_headers, _ = _head_object(bucket_name, key, headers, {})
diff --git a/tests/test_athena.py b/tests/test_athena.py
index 6ab4ceae..38d6790d 100644
--- a/tests/test_athena.py
+++ b/tests/test_athena.py
@@ -875,6 +875,12 @@ def test_athena_list_and_get_databases_from_glue(athena, glue):
athena.list_databases(CatalogName="no_such_catalog")
page = athena.list_databases(CatalogName="AwsDataCatalog", MaxResults=1)
assert len(page["DatabaseList"]) == 1 and "NextToken" in page
+ glue.create_database(DatabaseInput={"Name": "ath_db_nodesc"})
+ got = athena.get_database(CatalogName="AwsDataCatalog", DatabaseName="ath_db_nodesc")["Database"]
+ assert "Description" not in got
+ with pytest.raises(ClientError) as err:
+ athena.list_databases(CatalogName="AwsDataCatalog", NextToken="not-a-token")
+ assert err.value.response["Error"]["Code"] == "InvalidRequestException"
def test_athena_workgroup_reports_engine_version(athena):
@@ -1080,13 +1086,13 @@ def test_athena_reads_a_parquet_table_created_without_classification(
def test_parse_create_external_table_with_location_and_comment():
- from ministack.services.athena import CreateTable, _parse_ddl
+ from ministack.services.athena import _CreateTable, _parse_ddl
ddl = _parse_ddl(
"CREATE EXTERNAL TABLE events (id INT, note STRING COMMENT 'free text') "
"LOCATION 's3://bucket/db/events/'"
)
- assert ddl == CreateTable(
+ assert ddl == _CreateTable(
None, "events", external=True,
columns=[{"Name": "id", "Type": "int"}, {"Name": "note", "Type": "string", "Comment": "free text"}],
location="s3://bucket/db/events/",
@@ -1191,20 +1197,20 @@ def test_parse_literals_and_comments_are_not_structure():
def test_parse_drop_table():
- from ministack.services.athena import DropTable, _parse_ddl
+ from ministack.services.athena import _DropTable, _parse_ddl
- assert _parse_ddl("DROP TABLE events") == DropTable(None, "events")
- assert _parse_ddl("drop table if exists db.events;") == DropTable("db", "events", if_exists=True)
+ assert _parse_ddl("DROP TABLE events") == _DropTable(None, "events")
+ assert _parse_ddl("drop table if exists db.events;") == _DropTable("db", "events", if_exists=True)
def test_table_references_qualified_quoted_and_aliased():
- from ministack.services.athena import TableReference, _table_references
+ from ministack.services.athena import _table_references, _TableReference
query = 'SELECT u.id FROM "awsdatacatalog"."acme"."users" u JOIN orders ON u.id = orders.uid'
refs = _table_references(query)
assert refs == [
- TableReference("acme", "users", query.index('"awsdatacatalog"'), query.index(" u JOIN"), aliased=True),
- TableReference(None, "orders", query.index("orders ON"), query.index(" ON u.id"), aliased=False),
+ _TableReference("acme", "users", query.index('"awsdatacatalog"'), query.index(" u JOIN"), aliased=True),
+ _TableReference(None, "orders", query.index("orders ON"), query.index(" ON u.id"), aliased=False),
]
diff --git a/tests/test_cfn.py b/tests/test_cfn.py
index 6085eff2..5cb1050c 100644
--- a/tests/test_cfn.py
+++ b/tests/test_cfn.py
@@ -3178,39 +3178,6 @@ def test_cfn_nested_stack_capabilities_are_rechecked_on_an_update(monkeypatch):
_forget_nested_test_stacks(parent)
[email protected]("auth,setting", [(False, "1"), (True, "0"), (True, "typo")])
-def test_cfn_capabilities_setting_turns_the_check_on(monkeypatch, auth, setting):
- """CFN_ENFORCE_CAPABILITIES turns the check on without AUTH, and no value
- of it turns the check off under AUTH, for CreateStack and nested stacks."""
- import ministack.app as app_mod
- from ministack.services.cloudformation import _stack_events, _stacks
- from ministack.services.cloudformation.handlers import _create_stack
-
- monkeypatch.setattr(app_mod, "AUTH", auth)
- monkeypatch.setenv("CFN_ENFORCE_CAPABILITIES", setting)
- uid = _uuid_mod.uuid4().hex[:8]
- stack_name = f"cfn-caps-setting-{uid}"
- url = "http://localhost:4566/tpl/child.json"
- try:
- status, code, message = _caps_error(
- _create_stack(_caps_params(_CAPS_ROLE_TEMPLATE, StackName=stack_name)))
- assert (status, code, message) == (
- 400, "InsufficientCapabilitiesException",
- "Requires capabilities : [CAPABILITY_IAM]")
- assert stack_name not in _stacks
- with pytest.raises(ValueError, match=r"Requires capabilities : \[CAPABILITY_IAM\]"):
- _deploy_nested_child(monkeypatch, f"{stack_name}-p", [],
- {url: _CAPS_ROLE_TEMPLATE}, url)
- child = _deploy_nested_child(monkeypatch, f"{stack_name}-p2", ["CAPABILITY_IAM"],
- {url: _CAPS_ROLE_TEMPLATE}, url)
- assert _stacks[child]["Capabilities"] == ["CAPABILITY_IAM"]
- finally:
- stack = _stacks.pop(stack_name, None)
- if stack:
- _stack_events.pop(stack["StackId"], None)
- _forget_nested_test_stacks(f"{stack_name}-p")
-
-
def test_cfn_nested_stack_macro_needs_auto_expand_on_the_parent(monkeypatch):
"""A child template with a Transform fails the nested stack unless the
parent acknowledged CAPABILITY_AUTO_EXPAND."""
@@ -18641,6 +18608,100 @@ def test_cfn_ecs_task_definition_keeps_omitted_fields_omitted(cfn, ecs):
_delete_cfn_test_stack(cfn, stack_name)
+def _cfn_ecs_task_definition_template(family, memory=128, stage="v1"):
+ return json.dumps({"Resources": {
+ "Cluster": {"Type": "AWS::ECS::Cluster", "Properties": {"ClusterName": family}},
+ "Td": {"Type": "AWS::ECS::TaskDefinition", "Properties": {
+ "Family": family,
+ "ContainerDefinitions": [{"Name": "app", "Image": "busybox", "Memory": memory}],
+ "Tags": [{"Key": "stage", "Value": stage}]}},
+ "Svc": {"Type": "AWS::ECS::Service", "Properties": {
+ "Cluster": {"Ref": "Cluster"}, "ServiceName": family,
+ "TaskDefinition": {"Ref": "Td"}, "LaunchType": "EC2", "DesiredCount": 0}},
+ }, "Outputs": {"Td": {"Value": {"Ref": "Td"}}}})
+
+
+def _cfn_ecs_task_definition_state(cfn, ecs, stack_name, family):
+ """The stack's Ref, the service's task definition and the status of each revision."""
+ statuses = {}
+ for status in ("ACTIVE", "INACTIVE"):
+ for arn in ecs.list_task_definitions(familyPrefix=family, status=status)[
+ "taskDefinitionArns"]:
+ statuses[int(arn.rsplit(":", 1)[1])] = status
+ service = ecs.describe_services(cluster=family, services=[family])["services"][0]
+ return (_cfn_output(cfn, stack_name, "Td").rsplit(":", 1)[1],
+ service["taskDefinition"].rsplit(":", 1)[1], statuses)
+
+
+def test_cfn_ecs_task_definition_change_registers_the_next_revision(cfn, ecs):
+ """A replacing change registers the next revision and deregisters the old one."""
+ suffix = _uuid_mod.uuid4().hex[:8]
+ stack_name = family = f"cfn-ecs-td-{suffix}"
+ first_arn = ecs.register_task_definition(family=family, containerDefinitions=[
+ {"name": "app", "image": "busybox", "memory": 64}])["taskDefinition"]["taskDefinitionArn"]
+ try:
+ cfn.create_stack(StackName=stack_name,
+ TemplateBody=_cfn_ecs_task_definition_template(family))
+ stack = _wait_stack(cfn, stack_name)
+ assert stack["StackStatus"] == "CREATE_COMPLETE", stack.get("StackStatusReason")
+ assert _cfn_ecs_task_definition_state(cfn, ecs, stack_name, family) == (
+ "2", "2", {1: "ACTIVE", 2: "ACTIVE"})
+ first = ecs.describe_task_definition(taskDefinition=f"{family}:1")["taskDefinition"]
+ assert first["containerDefinitions"][0]["memory"] == 64
+
+ cfn.update_stack(StackName=stack_name,
+ TemplateBody=_cfn_ecs_task_definition_template(family, memory=256))
+ stack = _wait_stack(cfn, stack_name)
+ assert stack["StackStatus"] == "UPDATE_COMPLETE", stack.get("StackStatusReason")
+ assert _cfn_ecs_task_definition_state(cfn, ecs, stack_name, family) == (
+ "3", "3", {1: "ACTIVE", 2: "INACTIVE", 3: "ACTIVE"})
+
+ _delete_cfn_test_stack(cfn, stack_name)
+ third = ecs.describe_task_definition(taskDefinition=f"{family}:3")["taskDefinition"]
+ assert third["status"] == "INACTIVE"
+ finally:
+ _delete_cfn_test_stack(cfn, stack_name)
+ ecs.deregister_task_definition(taskDefinition=first_arn)
+ ecs.delete_task_definitions(taskDefinitions=[first_arn])
+
+
+def test_cfn_ecs_task_definition_tags_update_in_place(cfn, ecs):
+ """A tag change keeps the revision."""
+ suffix = _uuid_mod.uuid4().hex[:8]
+ stack_name = family = f"cfn-ecs-tdtag-{suffix}"
+ try:
+ _cfn_update_roundtrip(cfn, stack_name,
+ json.loads(_cfn_ecs_task_definition_template(family)),
+ json.loads(_cfn_ecs_task_definition_template(family, stage="v2")))
+ assert _cfn_ecs_task_definition_state(cfn, ecs, stack_name, family) == (
+ "1", "1", {1: "ACTIVE"})
+ tags = ecs.list_tags_for_resource(
+ resourceArn=_cfn_output(cfn, stack_name, "Td"))["tags"]
+ assert tags == [{"key": "stage", "value": "v2"}]
+ finally:
+ _delete_cfn_test_stack(cfn, stack_name)
+
+
+def test_cfn_ecs_task_definition_update_is_rolled_back(cfn, ecs):
+ """A rolled-back change keeps the revision and deregisters the one it registered."""
+ suffix = _uuid_mod.uuid4().hex[:8]
+ stack_name = family = f"cfn-ecs-tdrb-{suffix}"
+ try:
+ cfn.create_stack(StackName=stack_name,
+ TemplateBody=_cfn_ecs_task_definition_template(family))
+ stack = _wait_stack(cfn, stack_name)
+ assert stack["StackStatus"] == "CREATE_COMPLETE", stack.get("StackStatusReason")
+
+ cfn.update_stack(StackName=stack_name, TemplateBody=_cfn_with_failing_resource(
+ _cfn_ecs_task_definition_template(family, memory=512), "Td"))
+ stack = _wait_stack(cfn, stack_name)
+ assert stack["StackStatus"] == "UPDATE_ROLLBACK_COMPLETE", stack.get("StackStatusReason")
+ assert _cfn_ecs_task_definition_state(cfn, ecs, stack_name, family) == (
+ "1", "1", {1: "ACTIVE", 2: "INACTIVE"})
+ finally:
+ _delete_cfn_test_stack(cfn, stack_name)
+
+
def test_cfn_ecs_cluster_settings_read_back_in_the_api_shape(cfn, ecs):
"""ClusterSettings, DefaultCapacityProviderStrategy and Configuration read back in camelCase."""
suffix = _uuid_mod.uuid4().hex[:8]
@@ -18768,6 +18829,99 @@ def test_cfn_ecs_cluster_update_keeps_tags_added_outside_the_template(cfn, ecs):
_delete_cfn_test_stack(cfn, stack_name)
+def _cfn_ecs_cluster_template(cluster, stage="v1", insights="disabled", logging="DEFAULT",
+ drop=()):
+ props = {
+ "ClusterName": cluster,
+ "ClusterSettings": [{"Name": "containerInsights", "Value": insights}],
+ "Configuration": {"ExecuteCommandConfiguration": {"Logging": logging}},
+ "CapacityProviders": ["FARGATE"],
+ "DefaultCapacityProviderStrategy": [{"CapacityProvider": "FARGATE", "Weight": 1}],
+ "Tags": [{"Key": "stage", "Value": stage}],
+ }
+ return json.dumps({"Resources": {"Cluster": {
+ "Type": "AWS::ECS::Cluster",
+ "Properties": {k: v for k, v in props.items() if k not in drop}}}})
+
+
+def _cfn_ecs_cluster_state(ecs, cluster):
+ c = ecs.describe_clusters(
+ clusters=[cluster], include=["SETTINGS", "CONFIGURATIONS"])["clusters"][0]
+ tags = ecs.list_tags_for_resource(resourceArn=c["clusterArn"])["tags"]
+ return (c["clusterArn"], c["settings"], c.get("configuration"),
+ c["defaultCapacityProviderStrategy"],
+ {t["key"]: t["value"] for t in tags if not t["key"].startswith("aws:")})
+
+
+def test_cfn_ecs_cluster_tags_update_in_place(cfn, ecs):
+ """Template tags reach the tag store, and a tag change keeps the cluster and its other tags."""
+ suffix = _uuid_mod.uuid4().hex[:8]
+ stack_name = cluster = f"cfn-ecs-tags-{suffix}"
+ try:
+ cfn.create_stack(StackName=stack_name, TemplateBody=_cfn_ecs_cluster_template(cluster))
+ stack = _wait_stack(cfn, stack_name)
+ assert stack["StackStatus"] == "CREATE_COMPLETE", stack.get("StackStatusReason")
+ arn = _cfn_ecs_cluster_state(ecs, cluster)[0]
+ keys = {t["key"] for t in ecs.list_tags_for_resource(resourceArn=arn)["tags"]}
+ assert {"stage", "aws:cloudformation:stack-name"} <= keys
+ ecs.tag_resource(resourceArn=arn, tags=[{"key": "oob", "value": "1"}])
+
+ cfn.update_stack(StackName=stack_name,
+ TemplateBody=_cfn_ecs_cluster_template(cluster, stage="v2"))
+ stack = _wait_stack(cfn, stack_name)
+ assert stack["StackStatus"] == "UPDATE_COMPLETE", stack.get("StackStatusReason")
+ state = _cfn_ecs_cluster_state(ecs, cluster)
+ assert state[0] == arn
+ assert state[4] == {"stage": "v2", "oob": "1"}
+ finally:
+ _delete_cfn_test_stack(cfn, stack_name)
+
+
[email protected]("dropped,kept", [
+ ("ClusterSettings", True), ("Configuration", True),
+ ("DefaultCapacityProviderStrategy", False)])
+def test_cfn_ecs_cluster_property_removed_from_template(cfn, ecs, dropped, kept):
+ """Removed settings and configuration stay on the cluster; a removed strategy is cleared."""
+ suffix = _uuid_mod.uuid4().hex[:8]
+ stack_name = cluster = f"cfn-ecs-drop-{suffix}"
+ index = {"ClusterSettings": 1, "Configuration": 2, "DefaultCapacityProviderStrategy": 3}[dropped]
+ try:
+ cfn.create_stack(StackName=stack_name, TemplateBody=_cfn_ecs_cluster_template(
+ cluster, insights="enabled", logging="NONE"))
+ stack = _wait_stack(cfn, stack_name)
+ assert stack["StackStatus"] == "CREATE_COMPLETE", stack.get("StackStatusReason")
+ before = _cfn_ecs_cluster_state(ecs, cluster)
+
+ cfn.update_stack(StackName=stack_name, TemplateBody=_cfn_ecs_cluster_template(
+ cluster, insights="enabled", logging="NONE", drop=(dropped,)))
+ stack = _wait_stack(cfn, stack_name)
+ assert stack["StackStatus"] == "UPDATE_COMPLETE", stack.get("StackStatusReason")
+ after = _cfn_ecs_cluster_state(ecs, cluster)
+ assert after[index] == (before[index] if kept else [])
+ finally:
+ _delete_cfn_test_stack(cfn, stack_name)
+
+
+def test_cfn_ecs_cluster_update_is_rolled_back(cfn, ecs):
+ """A rolled-back settings and tag change leaves the cluster as it was."""
+ suffix = _uuid_mod.uuid4().hex[:8]
+ stack_name = cluster = f"cfn-ecs-rb-{suffix}"
+ try:
+ cfn.create_stack(StackName=stack_name, TemplateBody=_cfn_ecs_cluster_template(cluster))
+ stack = _wait_stack(cfn, stack_name)
+ assert stack["StackStatus"] == "CREATE_COMPLETE", stack.get("StackStatusReason")
+ before = _cfn_ecs_cluster_state(ecs, cluster)
+ assert before[4] == {"stage": "v1"}
+
+ cfn.update_stack(StackName=stack_name, TemplateBody=_cfn_with_failing_resource(
+ _cfn_ecs_cluster_template(cluster, stage="v2", insights="enabled"), "Cluster"))
+ stack = _wait_stack(cfn, stack_name)
+ assert stack["StackStatus"] == "UPDATE_ROLLBACK_COMPLETE", stack.get("StackStatusReason")
+ assert _cfn_ecs_cluster_state(ecs, cluster) == before
+ finally:
+ _delete_cfn_test_stack(cfn, stack_name)
+
+
def test_cfn_ecs_service_network_and_load_balancers_read_back_in_the_api_shape(cfn, ecs, elbv2):
"""NetworkConfiguration and LoadBalancers read back in camelCase, on create and on update."""
suffix = _uuid_mod.uuid4().hex[:8]
@@ -30701,6 +30855,385 @@ def test_cfn_import_of_a_type_without_an_adopter_is_not_executable(cfn, eb):
eb.delete_event_bus(Name=bus)
+def test_cfn_import_adopts_keys_pool_members_and_apis(cfn, sns, kms_client, iot_client, cognito_idp,
+ cognito_identity, apigw_v1):
+ uid = _uuid_mod.uuid4().hex[:8]
+ name, stack = f"cfn-imp-two-{uid}", f"cfn-imp-two-{uid}"
+ topic = sns.create_topic(Name=name)["TopicArn"]
+ key = kms_client.create_key(Description=name)["KeyMetadata"]
+ alias = f"alias/{name}"
+ kms_client.create_alias(AliasName=alias, TargetKeyId=key["KeyId"])
+ thing_type = iot_client.create_thing_type(thingTypeName=name)
+ pool = cognito_idp.create_user_pool(PoolName=name)["UserPool"]["Id"]
+ client = cognito_idp.create_user_pool_client(UserPoolId=pool, ClientName=name)["UserPoolClient"]["ClientId"]
+ cognito_idp.create_group(UserPoolId=pool, GroupName="admins")
+ cognito_idp.create_resource_server(UserPoolId=pool, Identifier="https://api.example.com", Name=name)
+ identity_pool = cognito_identity.create_identity_pool(
+ IdentityPoolName=name.replace("-", "_"), AllowUnauthenticatedIdentities=False)["IdentityPoolId"]
+ api = apigw_v1.create_rest_api(name=name)["id"]
+ root = next(r["id"] for r in apigw_v1.get_resources(restApiId=api)["items"] if r["path"] == "/")
+ apigw_v1.put_method(restApiId=api, resourceId=root, httpMethod="GET", authorizationType="NONE")
+ apigw_v1.put_integration(restApiId=api, resourceId=root, httpMethod="GET", type="MOCK")
+ deployment = apigw_v1.create_deployment(restApiId=api, stageName="live")["id"]
+ # (type, identifier, properties, physical id, {attribute: live value})
+ cases = {
+ "S": ("AWS::SNS::Topic", {"TopicArn": topic}, {"TopicName": name}, topic,
+ {"TopicArn": topic, "TopicName": name}),
+ "K": ("AWS::KMS::Key", {"KeyId": key["KeyId"]}, {"Description": name}, key["KeyId"],
+ {"Arn": key["Arn"], "KeyId": key["KeyId"]}),
+ "A": ("AWS::KMS::Alias", {"AliasName": alias}, {"AliasName": alias, "TargetKeyId": key["KeyId"]},
+ alias, {}),
+ "TT": ("AWS::IoT::ThingType", {"ThingTypeName": name}, {"ThingTypeName": name}, name,
+ {"Arn": thing_type["thingTypeArn"], "Id": thing_type["thingTypeId"]}),
+ "UC": ("AWS::Cognito::UserPoolClient", {"UserPoolId": pool, "ClientId": client},
+ {"UserPoolId": pool, "ClientName": name}, client, {"ClientId": client, "Name": name}),
+ "UG": ("AWS::Cognito::UserPoolGroup", {"UserPoolId": pool, "GroupName": "admins"},
+ {"UserPoolId": pool, "GroupName": "admins"}, "admins", {}),
+ "RS": ("AWS::Cognito::UserPoolResourceServer", {"UserPoolId": pool, "Identifier": "https://api.example.com"},
+ {"UserPoolId": pool, "Identifier": "https://api.example.com", "Name": name},
+ "https://api.example.com", {}),
+ "IDP": ("AWS::Cognito::IdentityPool", {"Id": identity_pool},
+ {"IdentityPoolName": name.replace("-", "_"), "AllowUnauthenticatedIdentities": False},
+ identity_pool, {"Id": identity_pool, "Name": name.replace("-", "_")}),
+ "API": ("AWS::ApiGateway::RestApi", {"RestApiId": api}, {"Name": name}, api,
+ {"RestApiId": api, "RootResourceId": root}),
+ "STG": ("AWS::ApiGateway::Stage", {"RestApiId": api, "StageName": "live"},
+ {"RestApiId": api, "StageName": "live", "DeploymentId": deployment}, "live", {}),
+ }
+ resources = {lid: {"Type": t, "DeletionPolicy": "Retain", "Properties": props}
+ for lid, (t, _i, props, _p, _a) in cases.items()}
+ to_import = [{"ResourceType": t, "LogicalResourceId": lid, "ResourceIdentifier": ident}
+ for lid, (t, ident, _props, _p, _a) in cases.items()]
+ try:
+ for entry in to_import:
+ if len(entry["ResourceIdentifier"]) == 2:
+ parent, last = entry["ResourceIdentifier"]
+ lid = entry["LogicalResourceId"]
+ with pytest.raises(ClientError) as exc:
+ _cfn_import(cfn, stack, {lid: resources[lid]}, [
+ {**entry, "ResourceIdentifier": {last: entry["ResourceIdentifier"][last]}}])
+ assert exc.value.response["Error"]["Message"] == (
+ f"Invalid resource identifier for resource type {entry['ResourceType']}. "
+ f"Expected [{parent}, {last}]")
+ cs = _cfn_import(cfn, stack, resources, to_import)
+ assert cs["ExecutionStatus"] == "AVAILABLE", cs["StatusReason"]
+ expected = {lid: pid for lid, (_t, _i, _props, pid, _a) in cases.items()}
+ assert {c["ResourceChange"]["LogicalResourceId"]: c["ResourceChange"]["PhysicalResourceId"]
+ for c in cs["Changes"]} == expected
+ final, _events = _cfn_execute_import(cfn, stack)
+ assert final["StackStatus"] == "IMPORT_COMPLETE", final.get("StackStatusReason")
+ assert {r["LogicalResourceId"]: r["PhysicalResourceId"]
+ for r in cfn.describe_stack_resources(StackName=stack)["StackResources"]} == expected
+ outputs = {f"{lid}Ref": {"Value": {"Ref": lid}} for lid in cases}
+ outputs.update({f"{lid}{attr}": {"Value": {"Fn::GetAtt": [lid, attr]}}
+ for lid, (_t, _i, _p, _pid, attrs) in cases.items() for attr in attrs})
+ cfn.update_stack(StackName=stack, TemplateBody=json.dumps({"Resources": resources, "Outputs": outputs}))
+ final = _wait_stack(cfn, stack)
+ assert final["StackStatus"] == "UPDATE_COMPLETE", final.get("StackStatusReason")
+ values = {f"{lid}Ref": pid for lid, pid in expected.items()}
+ values.update({f"{lid}{attr}": value
+ for lid, (_t, _i, _p, _pid, attrs) in cases.items() for attr, value in attrs.items()})
+ assert {o["OutputKey"]: o["OutputValue"] for o in final["Outputs"]} == values
+ finally:
+ _delete_cfn_test_stack(cfn, stack)
+ apigw_v1.delete_rest_api(restApiId=api)
+ cognito_identity.delete_identity_pool(IdentityPoolId=identity_pool)
+ cognito_idp.delete_user_pool(UserPoolId=pool)
+ iot_client.deprecate_thing_type(thingTypeName=name)
+ iot_client.delete_thing_type(thingTypeName=name)
+ kms_client.delete_alias(AliasName=alias)
+ sns.delete_topic(TopicArn=topic)
+
+
+def test_cfn_import_of_a_missing_key_pool_member_or_api_fails_the_change_set(cfn, cognito_idp, apigw_v1):
+ uid = _uuid_mod.uuid4().hex[:8]
+ stack, name = f"cfn-imp-gone-two-{uid}", f"cfn-imp-gone-two-{uid}"
+ pool = cognito_idp.create_user_pool(PoolName=name)["UserPool"]["Id"]
+ api = apigw_v1.create_rest_api(name=name)["id"]
+ ghost = str(_uuid_mod.uuid4())
+ sdk = r" \(Service: {}, Status Code: {}, Request ID: [0-9a-f-]+\) \(SDK Attempt Count: 1\)"
+ cases = [
+ ("AWS::KMS::Key", {"KeyId": ghost},
+ rf"Key 'arn:aws:kms:[a-z0-9-]+:\d{{12}}:key/{ghost}' does not exist" + sdk.format("Kms", 400)),
+ ("AWS::KMS::Alias", {"AliasName": f"alias/{name}"}, ""),
+ ("AWS::IoT::ThingType", {"ThingTypeName": name},
+ rf"Resource of type 'AWS::IoT::ThingType' with identifier '{name}' was not found\."),
+ ("AWS::Cognito::UserPoolClient", {"UserPoolId": pool, "ClientId": "nope"},
+ r"User pool client does not exist\." + sdk.format("CognitoIdentityProvider", 400)),
+ ("AWS::Cognito::UserPoolGroup", {"UserPoolId": pool, "GroupName": "nope"},
+ r"Group not found\." + sdk.format("CognitoIdentityProvider", 400)),
+ ("AWS::Cognito::UserPoolResourceServer", {"UserPoolId": pool, "Identifier": "nope"},
+ rf"nope does not exist in user pool {pool}\." + sdk.format("CognitoIdentityProvider", 400)),
+ ("AWS::Cognito::IdentityPool", {"Id": f"us-east-1:{ghost}"},
+ rf"IdentityPool 'us-east-1:{ghost}' not found\." + sdk.format("CognitoIdentity", 400)),
+ ("AWS::ApiGateway::RestApi", {"RestApiId": "nope"},
+ r"Invalid API identifier specified \d{12}:nope" + sdk.format("ApiGateway", 404)),
+ ("AWS::ApiGateway::Stage", {"RestApiId": api, "StageName": "nope"},
+ r"Invalid stage identifier specified" + sdk.format("ApiGateway", 404)),
+ ]
+ try:
+ for rtype, identifier, reason in cases:
+ cs = _cfn_import(cfn, stack, {"R": {"Type": rtype, "DeletionPolicy": "Retain"}},
+ [{"ResourceType": rtype, "LogicalResourceId": "R", "ResourceIdentifier": identifier}])
+ assert (cs["Status"], cs["ExecutionStatus"]) == ("FAILED", "UNAVAILABLE"), rtype
+ assert re.fullmatch(reason, cs["StatusReason"]), (rtype, cs["StatusReason"])
+ _delete_cfn_test_stack(cfn, stack)
+ finally:
+ _delete_cfn_test_stack(cfn, stack)
+ apigw_v1.delete_rest_api(restApiId=api)
+ cognito_idp.delete_user_pool(UserPoolId=pool)
+
+
+def test_cfn_import_of_a_pool_member_another_stack_holds_fails(cfn, cognito_idp):
+ uid = _uuid_mod.uuid4().hex[:8]
+ held, stack = f"cfn-imp-held-{uid}", f"cfn-imp-take-{uid}"
+ other = cognito_idp.create_user_pool(PoolName=f"cfn-imp-other-{uid}")["UserPool"]["Id"]
+ cognito_idp.create_group(UserPoolId=other, GroupName="admins")
+ cfn.create_stack(StackName=held, TemplateBody=json.dumps({"Resources": {
+ "UP": {"Type": "AWS::Cognito::UserPool", "Properties": {"UserPoolName": held}},
+ "UG": {"Type": "AWS::Cognito::UserPoolGroup",
+ "Properties": {"UserPoolId": {"Ref": "UP"}, "GroupName": "admins"}}}}))
+ try:
+ owner = _wait_stack(cfn, held)
+ assert owner["StackStatus"] == "CREATE_COMPLETE"
+ pool = cfn.describe_stack_resource(StackName=held, LogicalResourceId="UP")[
+ "StackResourceDetail"]["PhysicalResourceId"]
+ for pool_id, status, reason in (
+ (pool, "FAILED", f"{pool}|admins already exists in stack {owner['StackId']}"),
+ (other, "CREATE_COMPLETE", None)):
+ group = {"UserPoolId": pool_id, "GroupName": "admins"}
+ cs = _cfn_import(cfn, stack, {"UG": {"Type": "AWS::Cognito::UserPoolGroup",
+ "DeletionPolicy": "Retain", "Properties": group}},
+ [{"ResourceType": "AWS::Cognito::UserPoolGroup", "LogicalResourceId": "UG",
+ "ResourceIdentifier": group}])
+ assert cs["Status"] == status
+ if reason:
+ assert cs["StatusReason"] == reason
+ cfn.delete_change_set(StackName=stack, ChangeSetName="imp")
+ finally:
+ _delete_cfn_test_stack(cfn, stack)
+ _delete_cfn_test_stack(cfn, held)
+ cognito_idp.delete_user_pool(UserPoolId=other)
+
+
+def _cfn_existing_change_set(cfn, stack, change_set_type, resources):
+ """A change set with ImportExistingResources; returns its description."""
+ cs_id = cfn.create_change_set(
+ StackName=stack, ChangeSetName="cs", ChangeSetType=change_set_type, ImportExistingResources=True,
+ TemplateBody=json.dumps({"Resources": resources}))["Id"]
+ return cfn.describe_change_set(ChangeSetName=cs_id)
+
+
+def _cfn_param(name=None, value="v", policy=None):
+ resource = {"Type": "AWS::SSM::Parameter", "Properties": {"Type": "String", "Value": value}}
+ if name:
+ resource["Properties"]["Name"] = name
+ if policy:
+ resource["DeletionPolicy"] = policy
+ return resource
+
+
+def test_cfn_import_existing_resources_on_create(cfn, ssm, logs):
+ uid = _uuid_mod.uuid4().hex[:8]
+ stack, name = f"cfn-imp-exist-{uid}", f"cfn-imp-exist-{uid}"
+ ssm.put_parameter(Name=name, Type="String", Value="v1")
+ logs.create_log_group(logGroupName=name)
+ try:
+ cs = _cfn_existing_change_set(cfn, stack, "CREATE", {
+ "P": _cfn_param(name, "v2", "Retain"),
+ "L": {"Type": "AWS::Logs::LogGroup", "DeletionPolicy": "RetainExceptOnCreate",
+ "Properties": {"LogGroupName": name}},
+ "N": _cfn_param()})
+ assert cs["ImportExistingResources"] is True
+ assert {c["ResourceChange"]["LogicalResourceId"]: (c["ResourceChange"]["Action"],
+ c["ResourceChange"].get("PhysicalResourceId"))
+ for c in cs["Changes"]} == {"P": ("Import", name), "L": ("Import", name), "N": ("Add", None)}
+ cfn.execute_change_set(ChangeSetName=cs["ChangeSetId"])
+ assert _wait_stack(cfn, stack)["StackStatus"] == "CREATE_COMPLETE"
+ events = [(e["LogicalResourceId"], e["ResourceStatus"], e.get("ResourceStatusReason", ""))
+ for e in reversed(cfn.describe_stack_events(StackName=stack)["StackEvents"])]
+ assert [e for e in events if e[0] == "P"] == [
+ ("P", "IMPORT_IN_PROGRESS", "Resource import started."),
+ ("P", "IMPORT_IN_PROGRESS", ""),
+ ("P", "IMPORT_COMPLETE", "Resource import completed."),
+ ("P", "UPDATE_IN_PROGRESS", "Apply stack-level tags to imported resource if applicable."),
+ ("P", "UPDATE_COMPLETE", ""),
+ ]
+ assert {r["LogicalResourceId"]: r["ResourceStatus"]
+ for r in cfn.describe_stack_resources(StackName=stack)["StackResources"]} == {
+ "P": "UPDATE_COMPLETE", "L": "UPDATE_COMPLETE", "N": "CREATE_COMPLETE"}
+ assert ssm.get_parameter(Name=name)["Parameter"]["Version"] == 1
+ _delete_cfn_test_stack(cfn, stack)
+ # Both were imported, not created: RetainExceptOnCreate keeps the log group too.
+ assert ssm.get_parameter(Name=name)["Parameter"]["Value"] == "v1"
+ assert [g["logGroupName"] for g in logs.describe_log_groups(logGroupNamePrefix=name)["logGroups"]] == [name]
+ finally:
+ _delete_cfn_test_stack(cfn, stack)
+ ssm.delete_parameter(Name=name)
+ logs.delete_log_group(logGroupName=name)
+
+
+def test_cfn_import_existing_resources_needs_a_retaining_policy(cfn, ssm, logs):
+ uid = _uuid_mod.uuid4().hex[:8]
+ stack, name = f"cfn-imp-exist-nopol-{uid}", f"cfn-imp-exist-nopol-{uid}"
+ ssm.put_parameter(Name=name, Type="String", Value="v1")
+ logs.create_log_group(logGroupName=name)
+ try:
+ cs = _cfn_existing_change_set(cfn, stack, "CREATE", {
+ "P": _cfn_param(name), "L": {"Type": "AWS::Logs::LogGroup", "DeletionPolicy": "Delete",
+ "Properties": {"LogGroupName": name}}})
+ assert (cs["Status"], cs["ExecutionStatus"], cs["Changes"]) == ("FAILED", "UNAVAILABLE", [])
+ assert cs["StatusReason"] == (
+ "CloudFormation is attempting to import some resources because they already exist in your "
+ "account. The resources must have the DeletionPolicy attribute set to 'Retain' or "
+ "'RetainExceptOnCreate' in the template for successful import. The affected resources are "
+ f"P ({{Name={name}}}), L ({{LogGroupName={name}}})")
+ finally:
+ _delete_cfn_test_stack(cfn, stack)
+ ssm.delete_parameter(Name=name)
+ logs.delete_log_group(logGroupName=name)
+
+
+def test_cfn_import_existing_resources_needs_a_literal_name(cfn, ssm):
+ """A name from Fn::Sub or a Ref to a parameter is not imported: the change stays Add."""
+ uid = _uuid_mod.uuid4().hex[:8]
+ stack = f"cfn-imp-exist-dyn-{uid}"
+ sub, ref = f"{stack}-sub", f"{stack}-ref"
+ ssm.put_parameter(Name=sub, Type="String", Value="v1")
+ ssm.put_parameter(Name=ref, Type="String", Value="v1")
+ try:
+ cs_id = cfn.create_change_set(
+ StackName=stack, ChangeSetName="cs", ChangeSetType="CREATE", ImportExistingResources=True,
+ TemplateBody=json.dumps({
+ "Parameters": {"N": {"Type": "String", "Default": ref}},
+ "Resources": {
+ "S": {**_cfn_param(None, "v1", "Retain"), "Properties": {
+ "Type": "String", "Value": "v1", "Name": {"Fn::Sub": "${AWS::StackName}-sub"}}},
+ "R": {**_cfn_param(None, "v1", "Retain"), "Properties": {
+ "Type": "String", "Value": "v1", "Name": {"Ref": "N"}}}}}))["Id"]
+ cs = cfn.describe_change_set(ChangeSetName=cs_id)
+ assert (cs["Status"], cs["ExecutionStatus"]) == ("CREATE_COMPLETE", "AVAILABLE")
+ assert {c["ResourceChange"]["LogicalResourceId"]: c["ResourceChange"]["Action"]
+ for c in cs["Changes"]} == {"S": "Add", "R": "Add"}
+ finally:
+ _delete_cfn_test_stack(cfn, stack)
+ ssm.delete_parameter(Name=sub)
+ ssm.delete_parameter(Name=ref)
+
+
+def test_cfn_import_existing_resources_on_update_and_rollback(cfn, ssm, sqs):
+ uid = _uuid_mod.uuid4().hex[:8]
+ stack, name = f"cfn-imp-exist-upd-{uid}", f"cfn-imp-exist-upd-{uid}"
+ ssm.put_parameter(Name=name, Type="String", Value="v1")
+ url = sqs.create_queue(QueueName=name)["QueueUrl"]
+ x = _cfn_param()
+ try:
+ cfn.create_stack(StackName=stack, TemplateBody=json.dumps({"Resources": {"X": x}}))
+ assert _wait_stack(cfn, stack)["StackStatus"] == "CREATE_COMPLETE"
+ # A queue is identified by its URL, which the template does not set: it is not imported.
+ queue = {"Type": "AWS::SQS::Queue", "DeletionPolicy": "Retain", "Properties": {"QueueName": name}}
+ cs = _cfn_existing_change_set(cfn, stack, "UPDATE", {"X": x, "P": _cfn_param(name, "v1", "Retain"),
+ "Q": queue})
+ assert {c["ResourceChange"]["LogicalResourceId"]: c["ResourceChange"]["Action"]
+ for c in cs["Changes"]} == {"P": "Import", "Q": "Add"}
+ cfn.delete_change_set(ChangeSetName=cs["ChangeSetId"])
+ # The update fails after the import; the rollback leaves the parameter alone.
+ failing = {"H": {"Type": "AWS::CloudFormation::WaitConditionHandle"},
+ "W": {"Type": "AWS::CloudFormation::WaitCondition", "DependsOn": "P",
+ "Properties": {"Handle": {"Ref": "H"}, "Timeout": "1"}}}
+ cs = _cfn_existing_change_set(cfn, stack, "UPDATE", {"X": x, "P": _cfn_param(name, "v1", "Retain"),
+ **failing})
+ before = len(cfn.describe_stack_events(StackName=stack)["StackEvents"])
+ cfn.execute_change_set(ChangeSetName=cs["ChangeSetId"])
+ assert _wait_stack(cfn, stack)["StackStatus"] == "UPDATE_ROLLBACK_COMPLETE"
+ events = list(reversed(cfn.describe_stack_events(StackName=stack)["StackEvents"]))[before:]
+ # The parameter is released before the cleanup deletes what the update created.
+ assert [(e["LogicalResourceId"], e["ResourceStatus"]) for e in events
+ if e["LogicalResourceId"] in (stack, "P")] == [
+ (stack, "UPDATE_IN_PROGRESS"), ("P", "IMPORT_IN_PROGRESS"), ("P", "IMPORT_IN_PROGRESS"),
+ ("P", "IMPORT_COMPLETE"), ("P", "UPDATE_IN_PROGRESS"), ("P", "UPDATE_COMPLETE"),
+ (stack, "UPDATE_ROLLBACK_IN_PROGRESS"), ("P", "UPDATE_IN_PROGRESS"), ("P", "UPDATE_COMPLETE"),
+ ("P", "IMPORT_ROLLBACK_IN_PROGRESS"), ("P", "IMPORT_ROLLBACK_COMPLETE"),
+ (stack, "UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS"), (stack, "UPDATE_ROLLBACK_COMPLETE")]
+ resources = cfn.describe_stack_resources(StackName=stack)["StackResources"]
+ assert [r["LogicalResourceId"] for r in resources] == ["X"]
+ assert ssm.get_parameter(Name=name)["Parameter"]["Value"] == "v1"
+ finally:
+ _delete_cfn_test_stack(cfn, stack)
+ ssm.delete_parameter(Name=name)
+ sqs.delete_queue(QueueUrl=url)
+
+
+def test_cfn_import_existing_resources_released_by_rollback_stack(cfn, ssm):
+ """A create that failed with DisableRollback releases its imported resource on RollbackStack."""
+ uid = _uuid_mod.uuid4().hex[:8]
+ stack, name = f"cfn-imp-exist-rbs-{uid}", f"cfn-imp-exist-rbs-{uid}"
+ ssm.put_parameter(Name=name, Type="String", Value="v1")
+ try:
+ cs = _cfn_existing_change_set(cfn, stack, "CREATE", {
+ "P": _cfn_param(name, "v1", "Retain"),
+ "H": {"Type": "AWS::CloudFormation::WaitConditionHandle"},
+ "W": {"Type": "AWS::CloudFormation::WaitCondition", "DependsOn": "P",
+ "Properties": {"Handle": {"Ref": "H"}, "Timeout": "1"}}})
+ assert {c["ResourceChange"]["LogicalResourceId"]: c["ResourceChange"]["Action"]
+ for c in cs["Changes"]} == {"P": "Import", "H": "Add", "W": "Add"}
+ cfn.execute_change_set(ChangeSetName=cs["ChangeSetId"], DisableRollback=True)
+ assert _wait_stack(cfn, stack)["StackStatus"] == "CREATE_FAILED"
+ before = len(cfn.describe_stack_events(StackName=stack)["StackEvents"])
+ cfn.rollback_stack(StackName=stack)
+ assert _wait_stack(cfn, stack)["StackStatus"] == "ROLLBACK_COMPLETE"
+ events = list(reversed(cfn.describe_stack_events(StackName=stack)["StackEvents"]))[before:]
+ assert [(e["ResourceStatus"], e.get("ResourceStatusReason", "")) for e in events
+ if e["LogicalResourceId"] == "P"] == [
+ ("UPDATE_IN_PROGRESS", "Remove stack-level tags from imported resource if applicable."),
+ ("UPDATE_COMPLETE", ""), ("IMPORT_ROLLBACK_IN_PROGRESS", ""), ("IMPORT_ROLLBACK_COMPLETE", "")]
+ resources = cfn.describe_stack_resources(StackName=stack)["StackResources"]
+ assert "P" not in [r["LogicalResourceId"] for r in resources]
+ assert ssm.get_parameter(Name=name)["Parameter"]["Value"] == "v1"
+ finally:
+ _delete_cfn_test_stack(cfn, stack)
+ ssm.delete_parameter(Name=name)
+
+
+def test_cfn_import_existing_resources_another_stack_holds(cfn):
+ uid = _uuid_mod.uuid4().hex[:8]
+ held, stack, name = f"cfn-imp-exist-held-{uid}", f"cfn-imp-exist-take-{uid}", f"cfn-imp-exist-held-{uid}"
+ cfn.create_stack(StackName=held, TemplateBody=json.dumps({"Resources": {"P": _cfn_param(name)}}))
+ try:
+ owner = _wait_stack(cfn, held)
+ assert owner["StackStatus"] == "CREATE_COMPLETE"
+ # A missing retaining policy is reported first.
+ for policy, reason in ((None, "The affected resources are P ({Name=" + name + "})"),
+ ("Retain", f"{name} already exists in stack {owner['StackId']}")):
+ cs = _cfn_existing_change_set(cfn, stack, "CREATE", {"P": _cfn_param(name, "v", policy)})
+ assert (cs["Status"], cs["ExecutionStatus"], cs["Changes"]) == ("FAILED", "UNAVAILABLE", [])
+ assert cs["StatusReason"].endswith(reason)
+ cfn.delete_change_set(ChangeSetName=cs["ChangeSetId"])
+ finally:
+ _delete_cfn_test_stack(cfn, stack)
+ _delete_cfn_test_stack(cfn, held)
+
+
+def test_cfn_import_existing_resource_gone_before_execute(cfn, ssm):
+ uid = _uuid_mod.uuid4().hex[:8]
+ stack, name = f"cfn-imp-exist-gone-{uid}", f"cfn-imp-exist-gone-{uid}"
+ ssm.put_parameter(Name=name, Type="String", Value="v1")
+ try:
+ cs = _cfn_existing_change_set(cfn, stack, "CREATE", {"P": _cfn_param(name, "v1", "Retain")})
+ assert [c["ResourceChange"]["Action"] for c in cs["Changes"]] == ["Import"]
+ ssm.delete_parameter(Name=name)
+ cfn.execute_change_set(ChangeSetName=cs["ChangeSetId"])
+ assert _wait_stack(cfn, stack)["StackStatus"] == "ROLLBACK_COMPLETE"
+ assert [(e["ResourceStatus"], e.get("ResourceStatusReason", ""))
+ for e in reversed(cfn.describe_stack_events(StackName=stack)["StackEvents"])
+ if e["LogicalResourceId"] == "P"] == [
+ ("IMPORT_IN_PROGRESS", "Resource import started."),
+ ("IMPORT_FAILED", f"Resource of type 'AWS::SSM::Parameter' with identifier '{name}' was not found."),
+ ("IMPORT_ROLLBACK_IN_PROGRESS", ""), ("IMPORT_ROLLBACK_COMPLETE", "")]
+ finally:
+ _delete_cfn_test_stack(cfn, stack)
+
+
# ---------------------------------------------------------------------------
# OnFailure, OnStackFailure, DeletionMode, TemplateStage, ClientRequestToken
# ---------------------------------------------------------------------------
diff --git a/tests/test_cloudfront_dataplane.py b/tests/test_cloudfront_dataplane.py
index 7400bc22..646655cd 100644
--- a/tests/test_cloudfront_dataplane.py
+++ b/tests/test_cloudfront_dataplane.py
@@ -867,9 +867,34 @@ def cf_s3_stack(cloudfront, s3):
},
}
dist = cloudfront.create_distribution(DistributionConfig=dist_config)["Distribution"]
+ s3.put_bucket_policy(Bucket=bucket, Policy=json.dumps({"Statement": [{
+ "Effect": "Allow", "Principal": {"Service": "cloudfront.amazonaws.com"},
+ "Action": "s3:GetObject", "Resource": f"arn:aws:s3:::{bucket}/*",
+ "Condition": {"StringEquals": {"AWS:SourceArn": dist["ARN"]}},
+ }]}))
return {"dist_host": dist["DomainName"], "body": body}
+def test_s3_origin_without_a_bucket_policy_grant_is_denied(cloudfront, s3):
+ """An OAC origin whose bucket policy does not grant the distribution answers 403."""
+ suffix = _uuid_mod.uuid4().hex[:10]
+ bucket = f"cf-dp-s3-nogrant-{suffix}"
+ s3.create_bucket(Bucket=bucket)
+ s3.put_object(Bucket=bucket, Key="hello.txt", Body=b"x")
+ dist = cloudfront.create_distribution(DistributionConfig={
+ "CallerReference": f"cf-dp-s3-nogrant-{suffix}", "Comment": "", "Enabled": True,
+ "Origins": {"Quantity": 1, "Items": [{
+ "Id": "s3-origin", "DomainName": f"{bucket}.s3.amazonaws.com",
+ "S3OriginConfig": {"OriginAccessIdentity": ""}, "OriginAccessControlId": "test-oac-id",
+ }]},
+ "DefaultCacheBehavior": {"TargetOriginId": "s3-origin", "ViewerProtocolPolicy": "allow-all",
+ "CachePolicyId": _CACHING_DISABLED},
+ })["Distribution"]
+ status, _headers, body = _get(dist["DomainName"], "/hello.txt")
+ assert status == 403
+ assert b"<Code>AccessDenied</Code>" in body
+
+
def test_s3_origin_serves_bucket_object(cf_s3_stack):
status, headers, body = _get(cf_s3_stack["dist_host"], "/hello.txt")
assert status == 200
@@ -990,30 +1015,6 @@ def cf_raw_origin_stack(cloudfront, path_echo_origin_port):
return dist["DomainName"]
-def test_origin_domain_name_port_overrides_http_port(cloudfront, path_echo_origin_port):
- """An origin built from a MiniStack endpoint names the gateway port in its DomainName."""
- suffix = _uuid_mod.uuid4().hex[:10]
- dist = cloudfront.create_distribution(DistributionConfig={
- "CallerReference": f"cf-dp-port-{suffix}",
- "Comment": "origin DomainName with an explicit port",
- "Enabled": True,
- "Origins": {"Quantity": 1, "Items": [{
- "Id": "port-origin",
- "DomainName": f"127.0.0.1:{path_echo_origin_port}",
- "CustomOriginConfig": {
- "HTTPPort": 1, "HTTPSPort": 443, "OriginProtocolPolicy": "http-only",
- "OriginSslProtocols": {"Quantity": 1, "Items": ["TLSv1.2"]},
- "OriginReadTimeout": 30, "OriginKeepaliveTimeout": 5,
- },
- }]},
- "DefaultCacheBehavior": {
- "TargetOriginId": "port-origin", "ViewerProtocolPolicy": "allow-all",
- "CachePolicyId": _CACHING_DISABLED,
- },
- })["Distribution"]
- status, _body = _raw_get(dist["DomainName"], "/port-check")
- assert status == 200
-
def _raw_get(dist_host: str, raw_path: str):
conn = http.client.HTTPConnection("127.0.0.1", int(GATEWAY_PORT), timeout=10)
try:
diff --git a/tests/test_ecs.py b/tests/test_ecs.py
index b4295552..6df20934 100644
--- a/tests/test_ecs.py
+++ b/tests/test_ecs.py
@@ -1194,8 +1194,8 @@ def test_ecs_service_spawns_tasks(ecs):
taskDefinition="svc-spawn-td",
desiredCount=2,
)
+ _wait_until(lambda: len(ecs.list_tasks(cluster=cluster, serviceName="svc-spawn")["taskArns"]) == 2, timeout=30)
tasks = ecs.list_tasks(cluster=cluster, serviceName="svc-spawn")
- assert len(tasks["taskArns"]) == 2
# Verify describe_tasks returns correct metadata
_wait_until(
@@ -1273,8 +1273,8 @@ def test_ecs_service_scale_up(ecs):
assert len(tasks_before["taskArns"]) == 1
ecs.update_service(cluster=cluster, service="su-svc", desiredCount=3)
+ _wait_until(lambda: len(ecs.list_tasks(cluster=cluster, serviceName="su-svc")["taskArns"]) == 3, timeout=30)
tasks_after = ecs.list_tasks(cluster=cluster, serviceName="su-svc")
- assert len(tasks_after["taskArns"]) == 3
_wait_until(
lambda: ecs.describe_services(
@@ -1297,8 +1297,8 @@ def test_ecs_service_scale_down(ecs):
ecs.create_service(
cluster=cluster, serviceName="sd-svc", taskDefinition="sd-td", desiredCount=3,
)
+ _wait_until(lambda: len(ecs.list_tasks(cluster=cluster, serviceName="sd-svc")["taskArns"]) == 3, timeout=30)
tasks_before = ecs.list_tasks(cluster=cluster, serviceName="sd-svc")
- assert len(tasks_before["taskArns"]) == 3
ecs.update_service(cluster=cluster, service="sd-svc", desiredCount=1)
tasks_after = ecs.list_tasks(cluster=cluster, serviceName="sd-svc")
@@ -1325,8 +1325,8 @@ def test_ecs_service_td_update_replaces_tasks(ecs):
ecs.create_service(
cluster=cluster, serviceName="tdu-svc", taskDefinition="tdu-td:1", desiredCount=2,
)
+ _wait_until(lambda: len(ecs.list_tasks(cluster=cluster, serviceName="tdu-svc")["taskArns"]) == 2, timeout=30)
old_tasks = ecs.list_tasks(cluster=cluster, serviceName="tdu-svc")
- assert len(old_tasks["taskArns"]) == 2
# Register new revision and update service
resp2 = ecs.register_task_definition(
@@ -1535,8 +1535,8 @@ def test_ecs_service_delete_stops_tasks(ecs):
ecs.create_service(
cluster=cluster, serviceName="del-svc", taskDefinition="del-td", desiredCount=2,
)
+ _wait_until(lambda: len(ecs.list_tasks(cluster=cluster, serviceName="del-svc")["taskArns"]) == 2, timeout=30)
tasks = ecs.list_tasks(cluster=cluster, serviceName="del-svc")
- assert len(tasks["taskArns"]) == 2
ecs.delete_service(cluster=cluster, service="del-svc", force=True)
tasks_after = ecs.list_tasks(cluster=cluster, serviceName="del-svc")
@@ -3379,3 +3379,1077 @@ def test_ecs_task_runs_on_its_declared_runtime_platform(cpu_architecture, expect
)
assert kwargs.get("platform") == expected_platform
+
+
+# ---- Forced rolling deployments (simulated workers, no Docker) ----
+def _decoded(response):
+ assert response[0] == 200, response
+ return json.loads(response[2])
+
+
+def service(monkeypatch):
+ monkeypatch.setattr(ecs_service, "_get_docker", lambda: None)
+ cluster = f"force-{_uuid_mod.uuid4().hex[:8]}"
+ td = _decoded(ecs_service._register_task_definition({
+ "family": cluster,
+ "containerDefinitions": [{"name": "app", "image": "example.invalid/app:latest"}],
+ }))["taskDefinition"]
+ _decoded(ecs_service._create_service({
+ "cluster": cluster, "serviceName": "app",
+ "taskDefinition": td["taskDefinitionArn"], "desiredCount": 2,
+ }))
+ svc = ecs_service._services[f"{cluster}/app"]
+ yield cluster, svc
+ for arn, task in list(ecs_service._tasks.items()):
+ if task["clusterArn"] == svc["clusterArn"]:
+ ecs_service._tasks.pop(arn)
+ ecs_service._services.pop(f"{cluster}/app", None)
+ ecs_service._clusters.pop(cluster, None)
+ for key, task_def in list(ecs_service._task_defs.items()):
+ if task_def["family"] == cluster:
+ ecs_service._task_defs.pop(key)
+ ecs_service._task_def_latest.pop(cluster, None)
+
+
+def _tasks(svc, status=None, deployment=None):
+ return {
+ arn: task for arn, task in ecs_service._tasks.items()
+ if task["clusterArn"] == svc["clusterArn"]
+ and (status is None or task["lastStatus"] == status)
+ and (deployment is None or task.get("_deployment_id") == deployment["id"])
+ }
+
+
+def _update(cluster, **kwargs):
+ return _decoded(ecs_service._update_service({"cluster": cluster, "service": "app", **kwargs}))["service"]
+
+
[email protected]("definition", [None, "arn", "family_revision"])
+def test_ecs_force_same_definition_replaces_tasks(service, definition):
+ cluster, svc = service
+ old_id = ecs_service._primary_deployment(svc)["id"]
+ old_tasks = set(_tasks(svc, "RUNNING"))
+ request = {"forceNewDeployment": True}
+ if definition:
+ request["taskDefinition"] = (
+ svc["taskDefinition"] if definition == "arn" else f"{cluster}:1"
+ )
+ response = _update(cluster, **request)
+ assert response["taskDefinition"] == svc["taskDefinition"]
+ assert len(response["deployments"]) == 1
+ primary = response["deployments"][0]
+ assert primary["id"] != old_id
+ assert primary["rolloutState"] == "COMPLETED"
+ assert primary["runningCount"] == response["runningCount"] == 2
+ assert primary["pendingCount"] == 0
+ assert set(_tasks(svc, "RUNNING")).isdisjoint(old_tasks)
+ assert set(_tasks(svc, "STOPPED")) == old_tasks
+
+
[email protected]("explicit_definition", [False, True])
[email protected]("force", [None, False])
+def test_ecs_unchanged_update_does_not_deploy(service, explicit_definition, force):
+ cluster, svc = service
+ deployment = ecs_service._primary_deployment(svc)
+ tasks = set(_tasks(svc))
+ request = {}
+ if explicit_definition:
+ request["taskDefinition"] = f"{cluster}:1"
+ if force is not None:
+ request["forceNewDeployment"] = force
+ _update(cluster, **request)
+ assert ecs_service._primary_deployment(svc) is deployment
+ assert set(_tasks(svc)) == tasks
+
+
[email protected]("force", [False, True])
+def test_ecs_changed_definition_creates_one_deployment(service, force):
+ cluster, svc = service
+ old_tasks = set(_tasks(svc))
+ td = _decoded(ecs_service._register_task_definition({
+ "family": cluster,
+ "containerDefinitions": [{"name": "app", "image": "example.invalid/app:v2"}],
+ }))["taskDefinition"]["taskDefinitionArn"]
+ _update(cluster, taskDefinition=td, forceNewDeployment=force)
+ assert len(svc["deployments"]) == 1
+ assert svc["deployments"][0]["taskDefinition"] == td
+ assert set(_tasks(svc, "RUNNING")).isdisjoint(old_tasks)
+ assert all(task["taskDefinitionArn"] == td for task in _tasks(svc, "RUNNING").values())
+
+
+def test_ecs_repeated_force_uses_distinct_task_and_deployment_identities(service):
+ cluster, svc = service
+ deployment_ids = {ecs_service._primary_deployment(svc)["id"]}
+ seen_tasks = set(_tasks(svc))
+ for _ in range(3):
+ _update(cluster, forceNewDeployment=True)
+ primary = ecs_service._primary_deployment(svc)
+ assert primary["id"] not in deployment_ids
+ deployment_ids.add(primary["id"])
+ current = set(_tasks(svc, "RUNNING"))
+ assert len(current) == 2
+ assert current.isdisjoint(seen_tasks)
+ seen_tasks.update(current)
+
+
+def test_ecs_force_at_zero_desired_and_scale_afterwards(service):
+ cluster, svc = service
+ _update(cluster, desiredCount=0)
+ old_id = ecs_service._primary_deployment(svc)["id"]
+ _update(cluster, forceNewDeployment=True)
+ primary = ecs_service._primary_deployment(svc)
+ assert primary["id"] != old_id
+ assert primary["rolloutState"] == "COMPLETED"
+ assert primary["runningCount"] == primary["pendingCount"] == 0
+ assert not _tasks(svc, "RUNNING")
+ _update(cluster, desiredCount=2)
+ assert ecs_service._primary_deployment(svc) is primary
+ assert len(_tasks(svc, "RUNNING", primary)) == 2
+
+
[email protected]("scale_first", [False, True])
+def test_ecs_docker_force_at_zero_drains_predecessors_without_a_startup_callback(
+ service, pending_launcher, scale_first):
+ cluster, svc = service
+ old_tasks = set(_tasks(svc))
+ if scale_first:
+ _update(cluster, desiredCount=0)
+ _update(cluster, desiredCount=0, forceNewDeployment=True)
+ primary = ecs_service._primary_deployment(svc)
+ assert primary["rolloutState"] == "COMPLETED"
+ assert svc["runningCount"] == svc["pendingCount"] == 0
+ assert svc["deployments"] == [primary]
+ assert set(_tasks(svc, "STOPPED")) == old_tasks
+ assert pending_launcher == []
+ _update(cluster, desiredCount=2)
+ assert ecs_service._primary_deployment(svc) is primary
+ assert len(_tasks(svc, deployment=primary)) == 2
+ assert pending_launcher == [2]
+
+
[email protected]("crash", [False, True])
+def test_ecs_first_task_callback_cannot_approve_a_newly_running_second_task(
+ service, pending_launcher, monkeypatch, crash):
+ from ministack.core.responses import get_account_id, get_region
+
+ cluster, svc = service
+ old = ecs_service._primary_deployment(svc)
+ old_tasks = set(_tasks(svc, "RUNNING"))
+ callbacks = []
+ monkeypatch.setattr(ecs_service, "spawn_background", lambda callback, **kwargs: callbacks.append(callback))
+ monkeypatch.setattr(ecs_service.time, "sleep", lambda delay: None)
+ _update(cluster, forceNewDeployment=True, deploymentConfiguration={
+ "deploymentCircuitBreaker": {
+ "enable": True, "rollback": True,
+ "thresholdConfiguration": {"type": "COUNT", "value": 1},
+ },
+ })
+ primary = ecs_service._primary_deployment(svc)
+ first = next(iter(_tasks(svc, deployment=primary).values()))
+ primary["_image_digests"] = {"app": "sha256:" + "a" * 64}
+ ecs_service._mark_task_running(first["taskArn"], first)
+ ecs_service._schedule_service_deployment_completion(
+ cluster, f"{cluster}/app", get_account_id(), get_region(), healthy_task=first)
+ launch = ecs_service._run_task
+
+ def start_during_reconcile(request):
+ response = launch(request)
+ for task in _decoded(response)["tasks"]:
+ second = ecs_service._tasks[task["taskArn"]]
+ ecs_service._mark_task_running(second["taskArn"], second)
+ ecs_service._schedule_service_deployment_completion(
+ cluster, f"{cluster}/app", get_account_id(), get_region(), healthy_task=second)
+ return response
+
+ monkeypatch.setattr(ecs_service, "_run_task", start_during_reconcile)
+ callbacks[0]()
+ second = next(task for task in _tasks(svc, deployment=primary).values()
+ if task is not first)
+ assert primary["runningCount"] == 2
+ assert primary["rolloutState"] == "IN_PROGRESS"
+ assert old_tasks <= set(_tasks(svc, "RUNNING"))
+ if crash:
+ ecs_service._mark_task_stopped(second["taskArn"], second,
+ "Essential container exited", "EssentialContainerExited", 1)
+ callbacks[1]() # A stopped task's late callback cannot certify a retry.
+ assert primary["rolloutState"] == "FAILED"
+ assert primary["failedTasks"] == 1
+ assert ecs_service._primary_deployment(svc) is old
+ callbacks[-1]()
+ assert old["rolloutState"] == "COMPLETED"
+ assert set(_tasks(svc, "RUNNING")) == old_tasks
+ else:
+ callbacks[1]()
+ assert primary["rolloutState"] == "COMPLETED"
+ assert old_tasks == set(_tasks(svc, "STOPPED"))
+ assert len(_tasks(svc, "RUNNING", primary)) == 2
+
+
+def pending_launcher(monkeypatch):
+ original_run = ecs_service._run_task
+ calls = []
+
+ def launch(request):
+ # Production registration, with only the asynchronous Docker worker
+ # replaced. PROVISIONING is already reserved scheduler capacity.
+ with monkeypatch.context() as patch:
+ patch.setattr(ecs_service, "_get_docker", lambda: None)
+ response = original_run(request)
+ for task in _decoded(response)["tasks"]:
+ ecs_service._tasks[task["taskArn"]]["lastStatus"] = "PROVISIONING"
+ ecs_service._tasks[task["taskArn"]]["_startup_stable"] = False
+ calls.append(request["count"])
+ return response
+
+ monkeypatch.setattr(ecs_service, "_get_docker", lambda: object())
+ monkeypatch.setattr(ecs_service, "_run_task", launch)
+ return calls
+
+
[email protected]("pending_status", ["PROVISIONING", "PENDING", "ACTIVATING"])
+def test_ecs_force_keeps_old_tasks_until_stable_and_reserves_pending_capacity(
+ service, pending_launcher, pending_status):
+ cluster, svc = service
+ old = ecs_service._primary_deployment(svc)
+ old_tasks = set(_tasks(svc, "RUNNING"))
+ _update(cluster, forceNewDeployment=True)
+ primary = ecs_service._primary_deployment(svc)
+ current = _tasks(svc, deployment=primary)
+ assert len(current) == 1
+ assert old["runningCount"] == 2
+ assert primary["runningCount"] == 0
+ for task in current.values():
+ task["lastStatus"] = pending_status
+ for _ in range(3):
+ ecs_service._reconcile_service_tasks(cluster, f"{cluster}/app")
+ assert pending_launcher == [1]
+ assert old_tasks <= set(_tasks(svc, "RUNNING"))
+ assert primary["pendingCount"] == (1 if pending_status == "PENDING" else 0)
+ assert primary["rolloutState"] == "IN_PROGRESS"
+ for task in current.values():
+ td = ecs_service._task_defs[ecs_service._resolve_td_key(svc["taskDefinition"])]
+ ecs_service._prepare_service_images(task, td, SimpleNamespace(images=SimpleNamespace(
+ get_registry_data=lambda image: SimpleNamespace(id="sha256:" + "a" * 64),
+ )))
+ ecs_service._mark_task_running(task["taskArn"], task)
+ ecs_service._refresh_service_state(cluster, "service:app")
+ # RUNNING registration alone does not drain the predecessor. The worker's
+ # steady-state callback invokes completion after its existing grace window.
+ assert old_tasks <= set(_tasks(svc, "RUNNING"))
+ ecs_service._reconcile_service_tasks(cluster, f"{cluster}/app")
+ current = _tasks(svc, deployment=primary)
+ assert len(current) == 2
+ for task in current.values():
+ task["lastStatus"] = "RUNNING"
+ ecs_service._record_service_task_healthy(f"{cluster}/app", task)
+ ecs_service._refresh_service_state(cluster, "service:app")
+ ecs_service._complete_service_deployment(cluster, f"{cluster}/app")
+ assert set(_tasks(svc, "RUNNING")) == set(current)
+ assert old_tasks == set(_tasks(svc, "STOPPED"))
+ assert svc["deployments"] == [primary]
+
+
[email protected]("minimum,maximum,spawned,stopped", [(100, 100, 0, 0), (50, 100, 1, 1), (100, 150, 1, 0)])
+def test_ecs_force_respects_rolling_capacity_limits(
+ service, pending_launcher, minimum, maximum, spawned, stopped):
+ cluster, svc = service
+ old = ecs_service._primary_deployment(svc)
+ _update(cluster, forceNewDeployment=True, deploymentConfiguration={
+ "minimumHealthyPercent": minimum, "maximumPercent": maximum,
+ })
+ primary = ecs_service._primary_deployment(svc)
+ assert len(_tasks(svc, deployment=primary)) == spawned
+ assert len(_tasks(svc, "STOPPED", old)) == stopped
+ assert len(_tasks(svc, "RUNNING")) >= (2 * minimum + 99) // 100
+ live = sum(task["lastStatus"] in ecs_service._PRE_STOP_STATUSES for task in _tasks(svc).values())
+ assert live <= 2 * maximum // 100
+
+
+def test_ecs_force_attributes_legacy_tasks_before_same_definition_is_ambiguous(service, pending_launcher):
+ cluster, svc = service
+ old = ecs_service._primary_deployment(svc)
+ legacy = list(_tasks(svc).values())
+ for task in legacy:
+ task.pop("_deployment_id")
+ _update(cluster, forceNewDeployment=True)
+ primary = ecs_service._primary_deployment(svc)
+ assert all(task["_deployment_id"] == old["id"] for task in legacy)
+ assert old["runningCount"] == 2
+ assert primary["runningCount"] == 0
+ assert len(_tasks(svc, deployment=primary)) == 1
+
+
[email protected]("rollback", [False, True])
+def test_ecs_same_definition_failure_stops_retries_and_rolls_back_by_deployment(
+ service, pending_launcher, monkeypatch, rollback):
+ cluster, svc = service
+ old = ecs_service._primary_deployment(svc)
+ old_tasks = set(_tasks(svc))
+ monkeypatch.setattr(ecs_service, "_schedule_service_deployment_completion", lambda *args, **kwargs: None)
+ _update(cluster, forceNewDeployment=True, deploymentConfiguration={
+ "maximumPercent": 200, "minimumHealthyPercent": 100,
+ "deploymentCircuitBreaker": {"enable": True, "rollback": rollback},
+ })
+ failed = ecs_service._primary_deployment(svc)
+ for _ in range(3):
+ task = next(task for task in _tasks(svc, deployment=failed).values()
+ if task["lastStatus"] in ecs_service._PRE_STOP_STATUSES)
+ task["lastStatus"] = "STOPPED"
+ ecs_service._record_service_task_failure(task)
+ assert failed["rolloutState"] == "FAILED"
+ assert failed["failedTasks"] == 3
+ if rollback:
+ assert ecs_service._primary_deployment(svc) is old
+ ecs_service._refresh_service_state(cluster, "service:app")
+ ecs_service._complete_service_deployment(cluster, f"{cluster}/app")
+ assert set(_tasks(svc, "RUNNING")) == old_tasks
+ assert old["rolloutState"] == "COMPLETED"
+ else:
+ assert ecs_service._primary_deployment(svc) is failed
+ calls_before = list(pending_launcher)
+ ecs_service._reconcile_service_tasks(cluster, f"{cluster}/app")
+ assert pending_launcher == calls_before
+
+
+class _Images:
+ def __init__(self):
+ self.digest = "sha256:" + "a" * 64
+ self.registry_calls = []
+ self.pulls = []
+ self.cached = []
+ self.tags = []
+ self.manifest_error = False
+ self.pull_error = False
+ self.cache_missing = False
+
+ def get_registry_data(self, image, **kwargs):
+ self.registry_calls.append(image)
+ if self.manifest_error:
+ raise RuntimeError("registry unavailable")
+ return SimpleNamespace(id=self.digest)
+
+ def get(self, image):
+ if self.cache_missing:
+ from docker.errors import ImageNotFound
+ raise ImageNotFound("cached image absent")
+ return SimpleNamespace(attrs={"RepoDigests": list(self.cached)})
+
+ def pull(self, image, **kwargs):
+ self.pulls.append((image, kwargs))
+ if self.pull_error:
+ raise RuntimeError("image pull failed")
+ def tag(repository, tag=None):
+ self.tags.append((repository, tag))
+ return True
+ return SimpleNamespace(id="sha256:config-is-not-a-manifest", attrs={}, tag=tag)
+
+
+def docker_worker(monkeypatch):
+ """Real ECS worker + Docker SDK run path; simulated engine/registry only."""
+ from docker.models.containers import ContainerCollection
+
+ images = _Images()
+ created = []
+ client = SimpleNamespace(images=images, api=SimpleNamespace(_version="1.45"))
+ containers = ContainerCollection(client)
+
+ def create(**kwargs):
+ created.append(kwargs)
+ return SimpleNamespace(
+ id=f"container-{len(created)}", status="running",
+ attrs={"NetworkSettings": {"Networks": {}}},
+ start=lambda: None, reload=lambda: None,
+ stop=lambda **kwargs: None, remove=lambda **kwargs: None,
+ )
+
+ containers.create = create
+ containers.get = lambda name: (_ for _ in ()).throw(RuntimeError("not found"))
+ client.containers = containers
+ monkeypatch.setattr(ecs_service, "_start_awslogs_forwarder", lambda *args: None)
+ yield client, created
+ # Workers register metadata even though no containers exist on a daemon.
+ from ministack.services import ecs_metadata
+ for task in list(ecs_service._tasks.values()):
+ for token in task.pop("_metadata_tokens", []):
+ ecs_metadata.unregister_token(token)
+
+
+def _start_worker(svc, task, client):
+ td = ecs_service._task_defs[ecs_service._resolve_td_key(svc["taskDefinition"])]
+ ecs_service._start_task_worker(task, td, [], client)
+ return task
+
+
+def test_ecs_force_refreshes_manifest_and_pins_following_tasks(
+ service, pending_launcher, docker_worker):
+ cluster, svc = service
+ client, created = docker_worker
+ old = ecs_service._primary_deployment(svc)
+ old["_image_digests"] = {"app": "sha256:" + "0" * 64}
+ old_tasks = set(_tasks(svc))
+ client.images.digest = "sha256:" + "b" * 64
+ _update(cluster, forceNewDeployment=True)
+ primary = ecs_service._primary_deployment(svc)
+ first = next(iter(_tasks(svc, deployment=primary).values()))
+ _start_worker(svc, first, client)
+ assert primary["_image_digests"] == {"app": client.images.digest}
+ # Changing the tag again between tasks must not change this deployment.
+ client.images.digest = "sha256:" + "c" * 64
+ ecs_service._reconcile_service_tasks(cluster, f"{cluster}/app")
+ second = next(task for arn, task in _tasks(svc, deployment=primary).items()
+ if arn != first["taskArn"])
+ _start_worker(svc, second, client)
+ assert client.images.registry_calls == ["example.invalid/app:latest"]
+ expected = "example.invalid/app@sha256:" + "b" * 64
+ assert [kwargs["image"] for kwargs in created] == [expected, expected]
+ assert [image for image, kwargs in client.images.pulls] == [expected, expected]
+ assert all(task["containers"][0]["image"] == "example.invalid/app:latest"
+ for task in (first, second))
+ for task in (first, second):
+ ecs_service._record_service_task_healthy(f"{cluster}/app", task)
+ ecs_service._complete_service_deployment(cluster, f"{cluster}/app")
+ assert set(_tasks(svc, "RUNNING")) == {first["taskArn"], second["taskArn"]}
+ assert old_tasks == set(_tasks(svc, "STOPPED"))
+ # Normal scaling keeps the established deployment's manifest, too.
+ _update(cluster, desiredCount=3)
+ third = next(task for task in _tasks(svc, deployment=primary).values()
+ if task["lastStatus"] == "PROVISIONING")
+ _start_worker(svc, third, client)
+ assert created[-1]["image"] == expected
+ assert len(client.images.registry_calls) == 1
+
+
+def test_ecs_ec2_manifest_failure_continues_by_tag_with_cached_execution(service, pending_launcher, docker_worker):
+ cluster, svc = service
+ client, created = docker_worker
+ client.images.manifest_error = True
+ client.images.pull_error = True
+ digest = "sha256:" + "d" * 64
+ client.images.cached = [f"example.invalid/app@{digest}"]
+ _update(cluster, forceNewDeployment=True)
+ primary = ecs_service._primary_deployment(svc)
+ first = next(iter(_tasks(svc, deployment=primary).values()))
+ _start_worker(svc, first, client)
+ assert first["lastStatus"] == "RUNNING"
+ assert not primary["_image_digests"]
+ assert primary["_image_resolution_disabled"]
+ assert created[0]["image"] == "example.invalid/app:latest"
+ assert "imageDigest" not in first["containers"][0]
+ assert len(client.images.registry_calls) == 3
+ assert len(client.images.pulls) == 1
+
+
[email protected]("launch_type", ["EC2", "FARGATE"])
+ "example.invalid/app:latest",
+ "000000000000.dkr.ecr.us-east-1.amazonaws.com/offline-app:latest",
+])
[email protected]("breaker", [False, True])
+def test_ecs_offline_cached_images_complete_for_both_launch_types_and_registries(
+ service, pending_launcher, docker_worker, launch_type, image, breaker):
+ cluster, svc = service
+ client, created = docker_worker
+ svc["launchType"] = launch_type
+ td = ecs_service._task_defs[ecs_service._resolve_td_key(svc["taskDefinition"])]
+ td["containerDefinitions"][0]["image"] = image
+ client.images.manifest_error = True
+ client.images.pull_error = True
+ _update(cluster, forceNewDeployment=True, deploymentConfiguration={
+ "deploymentCircuitBreaker": {"enable": breaker, "rollback": False},
+ })
+ primary = ecs_service._primary_deployment(svc)
+ first = next(iter(_tasks(svc, deployment=primary).values()))
+ _start_worker(svc, first, client)
+ assert first["lastStatus"] == "RUNNING"
+ assert primary["rolloutState"] != "FAILED"
+ assert not primary["_image_digests"]
+ assert "imageDigest" not in first["containers"][0]
+ ecs_service._reconcile_service_tasks(cluster, f"{cluster}/app")
+ second = next(task for task in _tasks(svc, deployment=primary).values()
+ if task["taskArn"] != first["taskArn"])
+ _start_worker(svc, second, client)
+ for task in (first, second):
+ ecs_service._record_service_task_healthy(f"{cluster}/app", task)
+ ecs_service._complete_service_deployment(cluster, f"{cluster}/app")
+ assert primary["rolloutState"] == "COMPLETED"
+ assert all(task["lastStatus"] == "RUNNING"
+ for task in _tasks(svc, deployment=primary).values())
+ assert [kwargs["image"] for kwargs in created] == [image, image]
+ assert len(client.images.registry_calls) == 3
+ assert [uri for uri, _ in client.images.pulls] == [image, image]
+
+
+def test_ecs_offline_cache_does_not_hide_an_uncached_second_container(
+ service, pending_launcher, docker_worker, monkeypatch):
+ from docker.errors import ImageNotFound
+
+ cluster, svc = service
+ client, _ = docker_worker
+ td = ecs_service._task_defs[ecs_service._resolve_td_key(svc["taskDefinition"])]
+ td["containerDefinitions"].append({
+ "name": "sidecar", "image": "example.invalid/sidecar:missing", "essential": True,
+ })
+ client.images.manifest_error = True
+ original_get = client.images.get
+
+ def get(image):
+ if image == "example.invalid/sidecar:missing":
+ raise ImageNotFound("sidecar is not cached")
+ return original_get(image)
+
+ monkeypatch.setattr(client.images, "get", get)
+ _update(cluster, forceNewDeployment=True, deploymentConfiguration={
+ "deploymentCircuitBreaker": {"enable": True, "rollback": False},
+ })
+ primary = ecs_service._primary_deployment(svc)
+ task = next(iter(_tasks(svc, deployment=primary).values()))
+ ecs_service._prepare_service_images(task, td, client)
+ assert primary["rolloutState"] == "FAILED"
+ assert not primary["_image_digests"]
+ assert len(client.images.registry_calls) == 6
+
+
[email protected]("breaker,rollback", [(False, False), (True, False), (True, True)])
+def test_ecs_three_failed_manifest_attempts_continue_or_fail_and_roll_back(
+ service, pending_launcher, docker_worker, monkeypatch, breaker, rollback):
+ cluster, svc = service
+ client, created = docker_worker
+ client.images.manifest_error = True
+ client.images.cache_missing = True
+ # An unrelated RepoDigest, and even an image's local config ID, cannot
+ # establish a deployment's registry manifest.
+ client.images.cached = ["other.invalid/app@sha256:wrong"]
+ old = ecs_service._primary_deployment(svc)
+ monkeypatch.setattr(ecs_service, "_schedule_service_deployment_completion", lambda *args, **kwargs: None)
+ _update(cluster, forceNewDeployment=True, deploymentConfiguration={
+ "maximumPercent": 200, "minimumHealthyPercent": 100,
+ "deploymentCircuitBreaker": {"enable": breaker, "rollback": rollback},
+ })
+ primary = ecs_service._primary_deployment(svc)
+ first = next(iter(_tasks(svc, deployment=primary).values()))
+ _start_worker(svc, first, client)
+ assert len(client.images.registry_calls) == 3
+ assert primary["_image_resolution_disabled"]
+ assert not primary["_image_digests"]
+ if breaker:
+ assert primary["rolloutState"] == "FAILED"
+ assert primary["failedTasks"] == 0 # no task-start failure was invented
+ if rollback:
+ assert ecs_service._primary_deployment(svc) is old
+ ecs_service._complete_service_deployment(cluster, f"{cluster}/app")
+ assert first["lastStatus"] == "STOPPED"
+ else:
+ assert ecs_service._primary_deployment(svc) is primary
+ else:
+ assert primary["rolloutState"] == "IN_PROGRESS"
+ assert created[0]["image"] == "example.invalid/app:latest"
+ ecs_service._reconcile_service_tasks(cluster, f"{cluster}/app")
+ second = next(task for task in _tasks(svc, deployment=primary).values()
+ if task["lastStatus"] == "PROVISIONING")
+ _start_worker(svc, second, client)
+ assert len(client.images.registry_calls) == 3
+ assert all(kwargs["image"] == "example.invalid/app:latest" for kwargs in created)
+
+
[email protected]("launch_type", ["EC2", "FARGATE"])
+def test_ecs_service_pull_failure_cache_policy(
+ service, pending_launcher, docker_worker, launch_type):
+ cluster, svc = service
+ client, created = docker_worker
+ svc["launchType"] = launch_type
+ client.images.pull_error = True
+ _update(cluster, forceNewDeployment=True)
+ primary = ecs_service._primary_deployment(svc)
+ first = next(iter(_tasks(svc, deployment=primary).values()))
+ _start_worker(svc, first, client)
+ assert first["lastStatus"] == "RUNNING"
+ assert len(created) == 1
+ assert primary["launchType"] == launch_type
+ assert len(client.images.pulls) == 1
+
+
+def test_ecs_disabled_version_consistency_pulls_tag_for_each_task(
+ service, pending_launcher, docker_worker):
+ cluster, svc = service
+ client, created = docker_worker
+ td = ecs_service._task_defs[ecs_service._resolve_td_key(svc["taskDefinition"])]
+ td["containerDefinitions"][0]["versionConsistency"] = "disabled"
+ _update(cluster, forceNewDeployment=True)
+ primary = ecs_service._primary_deployment(svc)
+ assert len(_tasks(svc, deployment=primary)) == 2
+ for task in _tasks(svc, deployment=primary).values():
+ _start_worker(svc, task, client)
+ assert client.images.registry_calls == []
+ assert len(client.images.pulls) == 2
+ assert [kwargs["image"] for kwargs in created] == ["example.invalid/app:latest"] * 2
+
+
+def test_ecs_digest_reference_skips_manifest_lookup(service, pending_launcher, docker_worker):
+ cluster, svc = service
+ client, created = docker_worker
+ td = ecs_service._task_defs[ecs_service._resolve_td_key(svc["taskDefinition"])]
+ digest = "sha256:" + "e" * 64
+ td["containerDefinitions"][0]["image"] = f"example.invalid/app:latest@{digest}"
+ _update(cluster, forceNewDeployment=True)
+ assert len(_tasks(svc, deployment=ecs_service._primary_deployment(svc))) == 2
+ assert pending_launcher == [2]
+ task = next(iter(_tasks(svc, deployment=ecs_service._primary_deployment(svc)).values()))
+ _start_worker(svc, task, client)
+ assert client.images.registry_calls == []
+ assert created[0]["image"] == f"example.invalid/app@{digest}"
+
+
+def test_ecs_zero_size_deployment_needs_new_deployment_to_establish_digest(
+ service, pending_launcher, docker_worker):
+ cluster, svc = service
+ client, created = docker_worker
+ _update(cluster, desiredCount=0, forceNewDeployment=True)
+ primary = ecs_service._primary_deployment(svc)
+ assert primary["_image_resolution_disabled"]
+ _update(cluster, desiredCount=2)
+ for task in _tasks(svc, deployment=primary).values():
+ _start_worker(svc, task, client)
+ assert client.images.registry_calls == []
+ _update(cluster, forceNewDeployment=True)
+ primary = ecs_service._primary_deployment(svc)
+ task = next(iter(_tasks(svc, deployment=primary).values()))
+ _start_worker(svc, task, client)
+ assert client.images.registry_calls == ["example.invalid/app:latest"]
+
+
+def test_ecs_pull_preserves_declared_platform_and_existing_fallback(docker_worker, monkeypatch):
+ client, created = docker_worker
+ # The normal run path retains its declared platform during image pull.
+ monkeypatch.setattr(ecs_service.time, "sleep", lambda seconds: None)
+ ecs_service._run_docker_container(client, {"image": "example.invalid/app:latest"},
+ {"detach": True, "platform": "linux/arm64"}, refresh_image=True)
+ assert client.images.pulls == [("example.invalid/app:latest", {"platform": "linux/arm64"})]
+ assert created[0]["platform"] == "linux/arm64"
+ # Host-architecture retry remains available on a platform execution error.
+ original_create = client.containers.create
+
+ def reject_pinned(**kwargs):
+ if kwargs.get("platform"):
+ raise RuntimeError("unsupported platform")
+ return original_create(**kwargs)
+
+ client.containers.create = reject_pinned
+ ecs_service._run_docker_container(client, {"image": "example.invalid/app:latest"},
+ {"detach": True, "platform": "linux/arm64"}, refresh_image=True)
+ assert client.images.pulls[-2:] == [
+ ("example.invalid/app:latest", {"platform": "linux/arm64"}),
+ ("example.invalid/app:latest", {}),
+ ]
+ assert "platform" not in created[-1]
+
+
[email protected]("allow_cached", [False, True])
+def test_ecs_platform_pull_failure_retains_existing_host_architecture_fallback(
+ docker_worker, monkeypatch, allow_cached):
+ from docker.errors import ImageNotFound
+
+ client, created = docker_worker
+ original_pull = client.images.pull
+
+ def pull(image, **kwargs):
+ if kwargs.get("platform") == "linux/arm64":
+ client.images.pulls.append((image, kwargs))
+ raise RuntimeError("no matching manifest for linux/arm64")
+ return original_pull(image, **kwargs)
+
+ def missing(image):
+ raise ImageNotFound("image not cached")
+
+ monkeypatch.setattr(client.images, "pull", pull)
+ monkeypatch.setattr(client.images, "get", missing)
+ ecs_service._run_docker_container(
+ client, {"image": "example.invalid/app:latest"},
+ {"detach": True, "platform": "linux/arm64"},
+ refresh_image=True, allow_cached=allow_cached,
+ )
+ assert client.images.pulls == [
+ ("example.invalid/app:latest", {"platform": "linux/arm64"}),
+ ("example.invalid/app:latest", {}),
+ ]
+ assert len(created) == 1
+ assert "platform" not in created[0]
+
+
[email protected]("controller", ["CODE_DEPLOY", "EXTERNAL"])
+def test_ecs_force_is_scoped_to_supported_rolling_controller(service, controller):
+ cluster, svc = service
+ svc["deploymentController"] = {"type": controller}
+ primary = ecs_service._primary_deployment(svc)
+ tasks = set(_tasks(svc))
+ _update(cluster, forceNewDeployment=True)
+ assert ecs_service._primary_deployment(svc) is primary
+ assert set(_tasks(svc)) == tasks
+
+
+def test_ecs_fargate_manifest_failure_does_not_use_prior_task_cache(
+ service, pending_launcher, docker_worker):
+ cluster, svc = service
+ client, created = docker_worker
+ svc["launchType"] = "FARGATE"
+ client.images.manifest_error = True
+ client.images.cached = ["example.invalid/app@sha256:" + "f" * 64]
+ _update(cluster, forceNewDeployment=True)
+ primary = ecs_service._primary_deployment(svc)
+ task = next(iter(_tasks(svc, deployment=primary).values()))
+ _start_worker(svc, task, client)
+ assert len(client.images.registry_calls) == 3
+ assert not primary["_image_digests"]
+ assert created[0]["image"] == "example.invalid/app:latest"
+ assert client.images.pulls == [("example.invalid/app:latest", {})]
+
+
+def test_ecs_deployment_manifest_survives_persistence_and_scopes(service, pending_launcher, docker_worker):
+ from ministack.core.responses import request_scope
+
+ cluster, svc = service
+ client, created = docker_worker
+ _update(cluster, forceNewDeployment=True)
+ primary = ecs_service._primary_deployment(svc)
+ task = next(iter(_tasks(svc, deployment=primary).values()))
+ _start_worker(svc, task, client)
+ state = ecs_service.get_state()
+ persisted = state["services"][f"{cluster}/app"]["deployments"][0]
+ assert persisted["_image_digests"] == {"app": client.images.digest}
+ # Snapshot and wire output are independent; private digest bookkeeping is
+ # persisted but never becomes a new API member.
+ primary["_image_digests"]["app"] = "sha256:changed"
+ assert persisted["_image_digests"]["app"] == client.images.digest
+ assert "_image_digests" not in ecs_service._sanitize(primary)
+ with request_scope("222222222222", "us-west-2"):
+ assert f"{cluster}/app" not in ecs_service._services
+ assert not _tasks(svc)
+ primary["_image_digests"]["app"] = client.images.digest
+
+
+def test_ecs_initial_deployment_resolution_failure_triggers_enabled_breaker(
+ service, pending_launcher, docker_worker):
+ cluster, svc = service
+ client, created = docker_worker
+ client.images.manifest_error = True
+ client.images.cache_missing = True
+ initial = ecs_service._primary_deployment(svc)
+ svc["deploymentConfiguration"]["deploymentCircuitBreaker"]["enable"] = True
+ task = next(iter(_tasks(svc).values()))
+ task["lastStatus"] = "PROVISIONING"
+ _start_worker(svc, task, client)
+ assert initial["rolloutState"] == "FAILED"
+ assert initial["failedTasks"] == 0
+ assert len(client.images.registry_calls) == 3
+
+
[email protected]("launch_type", ["EC2", "FARGATE"])
[email protected]("architecture", [None, "ARM64"])
+def test_ecs_terminal_service_pull_failure_has_documented_task_error_category(
+ service, pending_launcher, docker_worker, monkeypatch, launch_type, architecture):
+ from docker.errors import ImageNotFound
+
+ from ministack.core.responses import get_account_id, get_region
+
+ cluster, svc = service
+ client, created = docker_worker
+ td = ecs_service._task_defs[ecs_service._resolve_td_key(svc["taskDefinition"])]
+ if architecture:
+ td["runtimePlatform"] = {"cpuArchitecture": architecture}
+ svc["launchType"] = launch_type
+ client.images.pull_error = True
+ def missing(image):
+ raise ImageNotFound("cached image absent")
+ monkeypatch.setattr(client.images, "get", missing)
+ _update(cluster, forceNewDeployment=True)
+ primary = ecs_service._primary_deployment(svc)
+ task = next(iter(_tasks(svc, deployment=primary).values()))
+ ecs_service._run_task_worker(task, td, [], client, get_account_id(), get_region())
+ assert task["lastStatus"] == "STOPPED"
+ assert task["stopCode"] == "TaskFailedToStart"
+ assert task["stoppedReason"] == "CannotPullContainerError: image pull failed"
+ assert not created
+ assert len(client.images.pulls) == (2 if architecture else 1)
+
+
[email protected]("launch_type", ["EC2", "FARGATE"])
+def test_ecs_all_tasks_can_use_cached_tag_after_a_resolved_digest_pull_fails(
+ service, pending_launcher, docker_worker, monkeypatch, launch_type):
+ from docker.errors import ImageNotFound
+
+ cluster, svc = service
+ svc["launchType"] = launch_type
+ client, created = docker_worker
+ client.images.digest = "sha256:" + "b" * 64
+ client.images.pull_error = True
+
+ def only_old_tag(image):
+ if image != "example.invalid/app:latest":
+ raise ImageNotFound("new digest B not cached")
+ return SimpleNamespace(id="old-image-A")
+
+ monkeypatch.setattr(client.images, "get", only_old_tag)
+ _update(cluster, forceNewDeployment=True)
+ primary = ecs_service._primary_deployment(svc)
+ first = next(iter(_tasks(svc, deployment=primary).values()))
+ _start_worker(svc, first, client)
+ assert first["lastStatus"] == "RUNNING"
+ assert primary["_image_digests"] == {"app": client.images.digest}
+ assert client.images.pulls[0][0] == "example.invalid/app@" + client.images.digest
+ assert created[0]["image"] == "example.invalid/app:latest"
+ assert first["containers"][0]["imageDigest"] == client.images.digest
+ assert not client.images.tags
+ # Later tasks retain the resolved digest in their response while also
+ # permitting the requested tag's cached execution for offline use.
+ ecs_service._reconcile_service_tasks(cluster, f"{cluster}/app")
+ second = next(task for task in _tasks(svc, deployment=primary).values()
+ if task["lastStatus"] == "PROVISIONING")
+ _start_worker(svc, second, client)
+ assert second["lastStatus"] == "RUNNING"
+ assert second["containers"][0]["imageDigest"] == client.images.digest
+ assert created[1]["image"] == "example.invalid/app:latest"
+ assert len(created) == 2
+
+
+def test_ecs_successful_first_task_pull_refreshes_original_tag_cache(
+ service, pending_launcher, docker_worker):
+ cluster, svc = service
+ client, created = docker_worker
+ _update(cluster, forceNewDeployment=True)
+ first = next(iter(_tasks(svc, deployment=ecs_service._primary_deployment(svc)).values()))
+ _start_worker(svc, first, client)
+ assert client.images.tags == [("example.invalid/app", "latest")]
+ assert created[0]["image"] == "example.invalid/app@" + client.images.digest
+ described = _decoded(ecs_service._describe_tasks({
+ "cluster": cluster, "tasks": [first["taskArn"]],
+ }))["tasks"][0]["containers"][0]
+ assert described["image"] == "example.invalid/app:latest"
+ assert described["imageDigest"] == client.images.digest
+
+
+def test_ecs_following_tasks_wait_for_first_running_task_even_after_manifest_is_known(
+ service, pending_launcher, docker_worker):
+ cluster, svc = service
+ client, created = docker_worker
+ _update(cluster, forceNewDeployment=True)
+ primary = ecs_service._primary_deployment(svc)
+ first = next(iter(_tasks(svc, deployment=primary).values()))
+ td = ecs_service._task_defs[ecs_service._resolve_td_key(svc["taskDefinition"])]
+ ecs_service._mark_task_activating(first["taskArn"], first)
+ ecs_service._prepare_service_images(first, td, client)
+ assert primary["_image_digests"] == {"app": client.images.digest}
+ _update(cluster, desiredCount=2)
+ assert pending_launcher == [1]
+ assert len(_tasks(svc, deployment=primary)) == 1
+ ecs_service._mark_task_running(first["taskArn"], first)
+ ecs_service._reconcile_service_tasks(cluster, f"{cluster}/app")
+ assert pending_launcher == [1, 1]
+ assert len(_tasks(svc, deployment=primary)) == 2
+
+
+def test_ecs_canceled_manifest_worker_cannot_publish_or_fail_deployment(
+ service, pending_launcher, docker_worker, monkeypatch):
+ cluster, svc = service
+ client, created = docker_worker
+ _update(cluster, forceNewDeployment=True, deploymentConfiguration={
+ "maximumPercent": 200, "minimumHealthyPercent": 100,
+ "deploymentCircuitBreaker": {"enable": True, "rollback": True},
+ })
+ primary = ecs_service._primary_deployment(svc)
+ task = next(iter(_tasks(svc, deployment=primary).values()))
+
+ def cancel_during_lookup(image):
+ client.images.registry_calls.append(image)
+ ecs_service._mark_task_stopped(task["taskArn"], task, "User canceled task", "UserInitiated")
+ raise RuntimeError("registry unavailable after cancellation")
+
+ monkeypatch.setattr(client.images, "get_registry_data", cancel_during_lookup)
+ _start_worker(svc, task, client)
+ assert task["lastStatus"] == "STOPPED"
+ assert task["stopCode"] == "UserInitiated"
+ assert len(client.images.registry_calls) == 1
+ assert "_image_digests" not in primary
+ assert primary["rolloutState"] == "IN_PROGRESS"
+ assert primary["failedTasks"] == 0
+ assert ecs_service._primary_deployment(svc) is primary
+ assert not created
+ # The digest-only transition also rejects a stale completion delivered
+ # after cancellation, while ordinary stopped-task failures stay supported.
+ ecs_service._record_service_task_failure(task, digest_resolution_failed=True)
+ assert primary["rolloutState"] == "IN_PROGRESS"
+
+
[email protected]("force", [False, True])
[email protected]("schedule_after_update", [False, True])
+def test_ecs_predecessor_startup_callback_cannot_complete_crashing_replacement(
+ service, pending_launcher, monkeypatch, force, schedule_after_update):
+ cluster, svc = service
+ _update(cluster, desiredCount=1)
+ old = ecs_service._primary_deployment(svc)
+ old_task = next(iter(_tasks(svc, "RUNNING").values()))
+ callbacks = []
+ monkeypatch.setattr(ecs_service, "spawn_background", lambda callback, **kwargs: callbacks.append(callback))
+ monkeypatch.setattr(ecs_service.time, "sleep", lambda delay: None)
+
+ def schedule_old():
+ from ministack.core.responses import get_account_id, get_region
+ ecs_service._schedule_service_deployment_completion(
+ cluster, f"{cluster}/app", get_account_id(), get_region(), healthy_task=old_task)
+
+ if not schedule_after_update:
+ schedule_old()
+ request = {"forceNewDeployment": True} if force else {
+ "taskDefinition": _decoded(ecs_service._register_task_definition({
+ "family": cluster,
+ "containerDefinitions": [{
+ "name": "app", "image": "example.invalid/app:latest",
+ "command": ["sh", "-c", "exit 1"],
+ }],
+ }))["taskDefinition"]["taskDefinitionArn"],
+ }
+ _update(cluster, **request, deploymentConfiguration={
+ "deploymentCircuitBreaker": {"enable": True, "rollback": True},
+ })
+ primary = ecs_service._primary_deployment(svc)
+ if schedule_after_update:
+ schedule_old()
+ first = next(iter(_tasks(svc, deployment=primary).values()))
+ # The Docker worker publishes RUNNING before its watcher detects exit 1.
+ ecs_service._mark_task_running(first["taskArn"], first)
+ callbacks[0]()
+ assert primary["rolloutState"] == "IN_PROGRESS"
+ assert old_task["lastStatus"] == "RUNNING"
+
+ for _ in range(3):
+ task = next(task for task in _tasks(svc, deployment=primary).values()
+ if task["lastStatus"] in ecs_service._PRE_STOP_STATUSES)
+ ecs_service._mark_task_stopped(task["taskArn"], task,
+ "Essential container in task exited", "EssentialContainerExited", 1)
+ assert primary["rolloutState"] == "FAILED"
+ assert primary["failedTasks"] == 3
+ assert ecs_service._primary_deployment(svc) is old
+ # The rollback's own scheduled callback still completes its deployment.
+ callbacks[-1]()
+ assert old["rolloutState"] == "COMPLETED"
+ assert old_task["lastStatus"] == "RUNNING"
+
+
[email protected]("platform,os_family,resolves,reported_platform,family", [
+ ("1.2.0", "LINUX", False, "1.2.0", "Linux"),
+ ("1.3.0", "LINUX", True, "1.3.0", "Linux"),
+ ("1.4.0", "LINUX", True, "1.4.0", "Linux"),
+ ("LATEST", "LINUX", True, "1.4.0", "Linux"),
+ ("", "LINUX", True, "1.4.0", "Linux"),
+ ("1.0.0", "WINDOWS_SERVER_2022_CORE", True, "1.0.0", ""),
+ ("LATEST", "WINDOWS_SERVER_2022_CORE", True, "1.0.0", ""),
+ ("", "WINDOWS_SERVER_2022_CORE", True, "1.0.0", ""),
+])
+def test_ecs_fargate_platform_controls_resolution_and_first_task_reservation(
+ service, pending_launcher, docker_worker, platform, os_family, resolves,
+ reported_platform, family):
+ cluster, svc = service
+ svc["launchType"] = "FARGATE"
+ svc["platformVersion"] = platform
+ client, created = docker_worker
+ td = ecs_service._task_defs[ecs_service._resolve_td_key(svc["taskDefinition"])]
+ td["runtimePlatform"] = {"operatingSystemFamily": os_family}
+ _update(cluster, forceNewDeployment=True)
+ primary = ecs_service._primary_deployment(svc)
+ current = list(_tasks(svc, deployment=primary).values())
+ assert len(current) == (1 if resolves else 2)
+ assert all(task["platformVersion"] == reported_platform for task in current)
+ assert all(task["platformFamily"] == family for task in current)
+ _start_worker(svc, current[0], client)
+ assert bool(client.images.registry_calls) is resolves
+ assert bool(primary.get("_image_digests")) is resolves
+ container = current[0]["containers"][0]
+ assert ("imageDigest" in container) is resolves
+ assert container["image"] == "example.invalid/app:latest"
+ assert created[0]["image"] == (
+ "example.invalid/app@" + client.images.digest if resolves
+ else "example.invalid/app:latest")
+
+
+def test_ecs_reported_manifest_digest_survives_persistence(
+ service, pending_launcher, docker_worker):
+ cluster, svc = service
+ client, _ = docker_worker
+ _update(cluster, forceNewDeployment=True)
+ task = next(iter(_tasks(svc, deployment=ecs_service._primary_deployment(svc)).values()))
+ _start_worker(svc, task, client)
+ state = ecs_service.get_state()
+ persisted = state["tasks"][task["taskArn"]]["containers"][0]
+ assert persisted["imageDigest"] == client.images.digest
+ task["containers"][0]["imageDigest"] = "sha256:" + "f" * 64
+ assert persisted["imageDigest"] == client.images.digest
+
+
[email protected]("launch_type", ["EC2", "FARGATE"])
[email protected]("consistency", ["enabled", "disabled", "digest"])
+def test_ecs_private_registry_credentials_reach_lookup_and_pull_without_leaking(
+ service, pending_launcher, docker_worker, monkeypatch, launch_type, consistency):
+ cluster, svc = service
+ svc["launchType"] = launch_type
+ client, created = docker_worker
+ td = ecs_service._task_defs[ecs_service._resolve_td_key(svc["taskDefinition"])]
+ cdef = td["containerDefinitions"][0]
+ secret_id = "arn:aws:secretsmanager:us-east-1:000000000000:secret:registry"
+ credentials = {"username": "repro-user", "password": "private-registry-password"}
+ cdef["repositoryCredentials"] = {"credentialsParameter": secret_id}
+ if consistency == "digest":
+ cdef["image"] = "example.invalid/app@" + client.images.digest
+ else:
+ cdef["versionConsistency"] = consistency
+ reads = []
+
+ def resolve(secret):
+ reads.append(secret)
+ return json.dumps(credentials)
+
+ monkeypatch.setattr(ecs_service.secretsmanager, "resolve_secret_string", resolve)
+ lookup = client.images.get_registry_data
+ lookup_auth = []
+
+ def authenticated_lookup(image, **kwargs):
+ lookup_auth.append(kwargs)
+ assert kwargs == {"auth_config": credentials}
+ return lookup(image)
+
+ monkeypatch.setattr(client.images, "get_registry_data", authenticated_lookup)
+ _update(cluster, forceNewDeployment=True)
+ task = next(iter(_tasks(svc, deployment=ecs_service._primary_deployment(svc)).values()))
+ _start_worker(svc, task, client)
+ assert task["lastStatus"] == "RUNNING"
+ assert bool(lookup_auth) is (consistency == "enabled")
+ assert client.images.pulls[-1][1]["auth_config"] == credentials
+ assert set(reads) == {secret_id}
+ assert "auth_config" not in created[0]
+ assert credentials["password"] not in repr(ecs_service.get_state())
+ assert credentials["password"] not in repr(ecs_service._sanitize(task))
+
+
[email protected]("secret", [None, "not-json", "{}", '{"username":"user"}'])
+def test_ecs_unavailable_registry_secret_fails_start_without_exposing_secret(
+ service, pending_launcher, docker_worker, monkeypatch, secret):
+ cluster, svc = service
+ client, created = docker_worker
+ td = ecs_service._task_defs[ecs_service._resolve_td_key(svc["taskDefinition"])]
+ td["containerDefinitions"][0]["repositoryCredentials"] = {"credentialsParameter": "registry"}
+ monkeypatch.setattr(ecs_service.secretsmanager, "resolve_secret_string", lambda secret_id: secret)
+ _update(cluster, forceNewDeployment=True, deploymentConfiguration={
+ "deploymentCircuitBreaker": {"enable": True, "rollback": False},
+ })
+ task = next(iter(_tasks(svc, deployment=ecs_service._primary_deployment(svc)).values()))
+ # Exercise the worker's existing secret-retrieval error category without
+ # starting real Docker watchers or hiding the initialization failure.
+ ecs_service._run_task_worker(task, td, [], client, "000000000000", "us-east-1")
+ assert task["lastStatus"] == "STOPPED"
+ assert task["stopCode"] == "TaskFailedToStart"
+ assert task["stoppedReason"].startswith("ResourceInitializationError: unable to pull secrets or registry auth")
+ assert not created
+ assert not client.images.pulls
+ assert not client.images.registry_calls
+ if secret:
+ assert secret not in task["stoppedReason"]
diff --git a/tests/test_ecs_force_deployment.py b/tests/test_ecs_force_deployment.py
deleted file mode 100644
index 8a430199..00000000
--- a/tests/test_ecs_force_deployment.py
+++ /dev/null
@@ -1,1082 +0,0 @@
-"""Rolling-deployment regressions with simulated workers, without Docker/AWS."""
-
-import json
-import uuid
-from types import SimpleNamespace
-
-import pytest
-
-from ministack.services import ecs
-
-
-def _decoded(response):
- assert response[0] == 200, response
- return json.loads(response[2])
-
-
-def service(monkeypatch):
- monkeypatch.setattr(ecs, "_get_docker", lambda: None)
- cluster = f"force-{uuid.uuid4().hex[:8]}"
- td = _decoded(ecs._register_task_definition({
- "family": cluster,
- "containerDefinitions": [{"name": "app", "image": "example.invalid/app:latest"}],
- }))["taskDefinition"]
- _decoded(ecs._create_service({
- "cluster": cluster, "serviceName": "app",
- "taskDefinition": td["taskDefinitionArn"], "desiredCount": 2,
- }))
- svc = ecs._services[f"{cluster}/app"]
- yield cluster, svc
- for arn, task in list(ecs._tasks.items()):
- if task["clusterArn"] == svc["clusterArn"]:
- ecs._tasks.pop(arn)
- ecs._services.pop(f"{cluster}/app", None)
- ecs._clusters.pop(cluster, None)
- for key, task_def in list(ecs._task_defs.items()):
- if task_def["family"] == cluster:
- ecs._task_defs.pop(key)
- ecs._task_def_latest.pop(cluster, None)
-
-
-def _tasks(svc, status=None, deployment=None):
- return {
- arn: task for arn, task in ecs._tasks.items()
- if task["clusterArn"] == svc["clusterArn"]
- and (status is None or task["lastStatus"] == status)
- and (deployment is None or task.get("_deployment_id") == deployment["id"])
- }
-
-
-def _update(cluster, **kwargs):
- return _decoded(ecs._update_service({"cluster": cluster, "service": "app", **kwargs}))["service"]
-
-
[email protected]("definition", [None, "arn", "family_revision"])
-def test_force_same_definition_replaces_tasks(service, definition):
- cluster, svc = service
- old_id = ecs._primary_deployment(svc)["id"]
- old_tasks = set(_tasks(svc, "RUNNING"))
- request = {"forceNewDeployment": True}
- if definition:
- request["taskDefinition"] = (
- svc["taskDefinition"] if definition == "arn" else f"{cluster}:1"
- )
- response = _update(cluster, **request)
- assert response["taskDefinition"] == svc["taskDefinition"]
- assert len(response["deployments"]) == 1
- primary = response["deployments"][0]
- assert primary["id"] != old_id
- assert primary["rolloutState"] == "COMPLETED"
- assert primary["runningCount"] == response["runningCount"] == 2
- assert primary["pendingCount"] == 0
- assert set(_tasks(svc, "RUNNING")).isdisjoint(old_tasks)
- assert set(_tasks(svc, "STOPPED")) == old_tasks
-
-
[email protected]("explicit_definition", [False, True])
[email protected]("force", [None, False])
-def test_unchanged_update_does_not_deploy(service, explicit_definition, force):
- cluster, svc = service
- deployment = ecs._primary_deployment(svc)
- tasks = set(_tasks(svc))
- request = {}
- if explicit_definition:
- request["taskDefinition"] = f"{cluster}:1"
- if force is not None:
- request["forceNewDeployment"] = force
- _update(cluster, **request)
- assert ecs._primary_deployment(svc) is deployment
- assert set(_tasks(svc)) == tasks
-
-
[email protected]("force", [False, True])
-def test_changed_definition_creates_one_deployment(service, force):
- cluster, svc = service
- old_tasks = set(_tasks(svc))
- td = _decoded(ecs._register_task_definition({
- "family": cluster,
- "containerDefinitions": [{"name": "app", "image": "example.invalid/app:v2"}],
- }))["taskDefinition"]["taskDefinitionArn"]
- _update(cluster, taskDefinition=td, forceNewDeployment=force)
- assert len(svc["deployments"]) == 1
- assert svc["deployments"][0]["taskDefinition"] == td
- assert set(_tasks(svc, "RUNNING")).isdisjoint(old_tasks)
- assert all(task["taskDefinitionArn"] == td for task in _tasks(svc, "RUNNING").values())
-
-
-def test_repeated_force_uses_distinct_task_and_deployment_identities(service):
- cluster, svc = service
- deployment_ids = {ecs._primary_deployment(svc)["id"]}
- seen_tasks = set(_tasks(svc))
- for _ in range(3):
- _update(cluster, forceNewDeployment=True)
- primary = ecs._primary_deployment(svc)
- assert primary["id"] not in deployment_ids
- deployment_ids.add(primary["id"])
- current = set(_tasks(svc, "RUNNING"))
- assert len(current) == 2
- assert current.isdisjoint(seen_tasks)
- seen_tasks.update(current)
-
-
-def test_force_at_zero_desired_and_scale_afterwards(service):
- cluster, svc = service
- _update(cluster, desiredCount=0)
- old_id = ecs._primary_deployment(svc)["id"]
- _update(cluster, forceNewDeployment=True)
- primary = ecs._primary_deployment(svc)
- assert primary["id"] != old_id
- assert primary["rolloutState"] == "COMPLETED"
- assert primary["runningCount"] == primary["pendingCount"] == 0
- assert not _tasks(svc, "RUNNING")
- _update(cluster, desiredCount=2)
- assert ecs._primary_deployment(svc) is primary
- assert len(_tasks(svc, "RUNNING", primary)) == 2
-
-
[email protected]("scale_first", [False, True])
-def test_docker_force_at_zero_drains_predecessors_without_a_startup_callback(
- service, pending_launcher, scale_first):
- cluster, svc = service
- old_tasks = set(_tasks(svc))
- if scale_first:
- _update(cluster, desiredCount=0)
- _update(cluster, desiredCount=0, forceNewDeployment=True)
- primary = ecs._primary_deployment(svc)
- assert primary["rolloutState"] == "COMPLETED"
- assert svc["runningCount"] == svc["pendingCount"] == 0
- assert svc["deployments"] == [primary]
- assert set(_tasks(svc, "STOPPED")) == old_tasks
- assert pending_launcher == []
- _update(cluster, desiredCount=2)
- assert ecs._primary_deployment(svc) is primary
- assert len(_tasks(svc, deployment=primary)) == 2
- assert pending_launcher == [2]
-
-
[email protected]("crash", [False, True])
-def test_first_task_callback_cannot_approve_a_newly_running_second_task(
- service, pending_launcher, monkeypatch, crash):
- from ministack.core.responses import get_account_id, get_region
-
- cluster, svc = service
- old = ecs._primary_deployment(svc)
- old_tasks = set(_tasks(svc, "RUNNING"))
- callbacks = []
- monkeypatch.setattr(ecs, "spawn_background", lambda callback, **kwargs: callbacks.append(callback))
- monkeypatch.setattr(ecs.time, "sleep", lambda delay: None)
- _update(cluster, forceNewDeployment=True, deploymentConfiguration={
- "deploymentCircuitBreaker": {
- "enable": True, "rollback": True,
- "thresholdConfiguration": {"type": "COUNT", "value": 1},
- },
- })
- primary = ecs._primary_deployment(svc)
- first = next(iter(_tasks(svc, deployment=primary).values()))
- primary["_image_digests"] = {"app": "sha256:" + "a" * 64}
- ecs._mark_task_running(first["taskArn"], first)
- ecs._schedule_service_deployment_completion(
- cluster, f"{cluster}/app", get_account_id(), get_region(), healthy_task=first)
- launch = ecs._run_task
-
- def start_during_reconcile(request):
- response = launch(request)
- for task in _decoded(response)["tasks"]:
- second = ecs._tasks[task["taskArn"]]
- ecs._mark_task_running(second["taskArn"], second)
- ecs._schedule_service_deployment_completion(
- cluster, f"{cluster}/app", get_account_id(), get_region(), healthy_task=second)
- return response
-
- monkeypatch.setattr(ecs, "_run_task", start_during_reconcile)
- callbacks[0]()
- second = next(task for task in _tasks(svc, deployment=primary).values()
- if task is not first)
- assert primary["runningCount"] == 2
- assert primary["rolloutState"] == "IN_PROGRESS"
- assert old_tasks <= set(_tasks(svc, "RUNNING"))
- if crash:
- ecs._mark_task_stopped(second["taskArn"], second,
- "Essential container exited", "EssentialContainerExited", 1)
- callbacks[1]() # A stopped task's late callback cannot certify a retry.
- assert primary["rolloutState"] == "FAILED"
- assert primary["failedTasks"] == 1
- assert ecs._primary_deployment(svc) is old
- callbacks[-1]()
- assert old["rolloutState"] == "COMPLETED"
- assert set(_tasks(svc, "RUNNING")) == old_tasks
- else:
- callbacks[1]()
- assert primary["rolloutState"] == "COMPLETED"
- assert old_tasks == set(_tasks(svc, "STOPPED"))
- assert len(_tasks(svc, "RUNNING", primary)) == 2
-
-
-def pending_launcher(monkeypatch):
- original_run = ecs._run_task
- calls = []
-
- def launch(request):
- # Production registration, with only the asynchronous Docker worker
- # replaced. PROVISIONING is already reserved scheduler capacity.
- with monkeypatch.context() as patch:
- patch.setattr(ecs, "_get_docker", lambda: None)
- response = original_run(request)
- for task in _decoded(response)["tasks"]:
- ecs._tasks[task["taskArn"]]["lastStatus"] = "PROVISIONING"
- ecs._tasks[task["taskArn"]]["_startup_stable"] = False
- calls.append(request["count"])
- return response
-
- monkeypatch.setattr(ecs, "_get_docker", lambda: object())
- monkeypatch.setattr(ecs, "_run_task", launch)
- return calls
-
-
[email protected]("pending_status", ["PROVISIONING", "PENDING", "ACTIVATING"])
-def test_force_keeps_old_tasks_until_stable_and_reserves_pending_capacity(
- service, pending_launcher, pending_status):
- cluster, svc = service
- old = ecs._primary_deployment(svc)
- old_tasks = set(_tasks(svc, "RUNNING"))
- _update(cluster, forceNewDeployment=True)
- primary = ecs._primary_deployment(svc)
- current = _tasks(svc, deployment=primary)
- assert len(current) == 1
- assert old["runningCount"] == 2
- assert primary["runningCount"] == 0
- for task in current.values():
- task["lastStatus"] = pending_status
- for _ in range(3):
- ecs._reconcile_service_tasks(cluster, f"{cluster}/app")
- assert pending_launcher == [1]
- assert old_tasks <= set(_tasks(svc, "RUNNING"))
- assert primary["pendingCount"] == (1 if pending_status == "PENDING" else 0)
- assert primary["rolloutState"] == "IN_PROGRESS"
- for task in current.values():
- td = ecs._task_defs[ecs._resolve_td_key(svc["taskDefinition"])]
- ecs._prepare_service_images(task, td, SimpleNamespace(images=SimpleNamespace(
- get_registry_data=lambda image: SimpleNamespace(id="sha256:" + "a" * 64),
- )))
- ecs._mark_task_running(task["taskArn"], task)
- ecs._refresh_service_state(cluster, "service:app")
- # RUNNING registration alone does not drain the predecessor. The worker's
- # steady-state callback invokes completion after its existing grace window.
- assert old_tasks <= set(_tasks(svc, "RUNNING"))
- ecs._reconcile_service_tasks(cluster, f"{cluster}/app")
- current = _tasks(svc, deployment=primary)
- assert len(current) == 2
- for task in current.values():
- task["lastStatus"] = "RUNNING"
- ecs._record_service_task_healthy(f"{cluster}/app", task)
- ecs._refresh_service_state(cluster, "service:app")
- ecs._complete_service_deployment(cluster, f"{cluster}/app")
- assert set(_tasks(svc, "RUNNING")) == set(current)
- assert old_tasks == set(_tasks(svc, "STOPPED"))
- assert svc["deployments"] == [primary]
-
-
[email protected]("minimum,maximum,spawned,stopped", [(100, 100, 0, 0), (50, 100, 1, 1), (100, 150, 1, 0)])
-def test_force_respects_rolling_capacity_limits(
- service, pending_launcher, minimum, maximum, spawned, stopped):
- cluster, svc = service
- old = ecs._primary_deployment(svc)
- _update(cluster, forceNewDeployment=True, deploymentConfiguration={
- "minimumHealthyPercent": minimum, "maximumPercent": maximum,
- })
- primary = ecs._primary_deployment(svc)
- assert len(_tasks(svc, deployment=primary)) == spawned
- assert len(_tasks(svc, "STOPPED", old)) == stopped
- assert len(_tasks(svc, "RUNNING")) >= (2 * minimum + 99) // 100
- live = sum(task["lastStatus"] in ecs._PRE_STOP_STATUSES for task in _tasks(svc).values())
- assert live <= 2 * maximum // 100
-
-
-def test_force_attributes_legacy_tasks_before_same_definition_is_ambiguous(service, pending_launcher):
- cluster, svc = service
- old = ecs._primary_deployment(svc)
- legacy = list(_tasks(svc).values())
- for task in legacy:
- task.pop("_deployment_id")
- _update(cluster, forceNewDeployment=True)
- primary = ecs._primary_deployment(svc)
- assert all(task["_deployment_id"] == old["id"] for task in legacy)
- assert old["runningCount"] == 2
- assert primary["runningCount"] == 0
- assert len(_tasks(svc, deployment=primary)) == 1
-
-
[email protected]("rollback", [False, True])
-def test_same_definition_failure_stops_retries_and_rolls_back_by_deployment(
- service, pending_launcher, monkeypatch, rollback):
- cluster, svc = service
- old = ecs._primary_deployment(svc)
- old_tasks = set(_tasks(svc))
- monkeypatch.setattr(ecs, "_schedule_service_deployment_completion", lambda *args, **kwargs: None)
- _update(cluster, forceNewDeployment=True, deploymentConfiguration={
- "maximumPercent": 200, "minimumHealthyPercent": 100,
- "deploymentCircuitBreaker": {"enable": True, "rollback": rollback},
- })
- failed = ecs._primary_deployment(svc)
- for _ in range(3):
- task = next(task for task in _tasks(svc, deployment=failed).values()
- if task["lastStatus"] in ecs._PRE_STOP_STATUSES)
- task["lastStatus"] = "STOPPED"
- ecs._record_service_task_failure(task)
- assert failed["rolloutState"] == "FAILED"
- assert failed["failedTasks"] == 3
- if rollback:
- assert ecs._primary_deployment(svc) is old
- ecs._refresh_service_state(cluster, "service:app")
- ecs._complete_service_deployment(cluster, f"{cluster}/app")
- assert set(_tasks(svc, "RUNNING")) == old_tasks
- assert old["rolloutState"] == "COMPLETED"
- else:
- assert ecs._primary_deployment(svc) is failed
- calls_before = list(pending_launcher)
- ecs._reconcile_service_tasks(cluster, f"{cluster}/app")
- assert pending_launcher == calls_before
-
-
-class _Images:
- def __init__(self):
- self.digest = "sha256:" + "a" * 64
- self.registry_calls = []
- self.pulls = []
- self.cached = []
- self.tags = []
- self.manifest_error = False
- self.pull_error = False
- self.cache_missing = False
-
- def get_registry_data(self, image, **kwargs):
- self.registry_calls.append(image)
- if self.manifest_error:
- raise RuntimeError("registry unavailable")
- return SimpleNamespace(id=self.digest)
-
- def get(self, image):
- if self.cache_missing:
- from docker.errors import ImageNotFound
- raise ImageNotFound("cached image absent")
- return SimpleNamespace(attrs={"RepoDigests": list(self.cached)})
-
- def pull(self, image, **kwargs):
- self.pulls.append((image, kwargs))
- if self.pull_error:
- raise RuntimeError("image pull failed")
- def tag(repository, tag=None):
- self.tags.append((repository, tag))
- return True
- return SimpleNamespace(id="sha256:config-is-not-a-manifest", attrs={}, tag=tag)
-
-
-def docker_worker(monkeypatch):
- """Real ECS worker + Docker SDK run path; simulated engine/registry only."""
- from docker.models.containers import ContainerCollection
-
- images = _Images()
- created = []
- client = SimpleNamespace(images=images, api=SimpleNamespace(_version="1.45"))
- containers = ContainerCollection(client)
-
- def create(**kwargs):
- created.append(kwargs)
- return SimpleNamespace(
- id=f"container-{len(created)}", status="running",
- attrs={"NetworkSettings": {"Networks": {}}},
- start=lambda: None, reload=lambda: None,
- stop=lambda **kwargs: None, remove=lambda **kwargs: None,
- )
-
- containers.create = create
- containers.get = lambda name: (_ for _ in ()).throw(RuntimeError("not found"))
- client.containers = containers
- monkeypatch.setattr(ecs, "_start_awslogs_forwarder", lambda *args: None)
- yield client, created
- # Workers register metadata even though no containers exist on a daemon.
- from ministack.services import ecs_metadata
- for task in list(ecs._tasks.values()):
- for token in task.pop("_metadata_tokens", []):
- ecs_metadata.unregister_token(token)
-
-
-def _start_worker(svc, task, client):
- td = ecs._task_defs[ecs._resolve_td_key(svc["taskDefinition"])]
- ecs._start_task_worker(task, td, [], client)
- return task
-
-
-def test_force_refreshes_manifest_and_pins_following_tasks(
- service, pending_launcher, docker_worker):
- cluster, svc = service
- client, created = docker_worker
- old = ecs._primary_deployment(svc)
- old["_image_digests"] = {"app": "sha256:" + "0" * 64}
- old_tasks = set(_tasks(svc))
- client.images.digest = "sha256:" + "b" * 64
- _update(cluster, forceNewDeployment=True)
- primary = ecs._primary_deployment(svc)
- first = next(iter(_tasks(svc, deployment=primary).values()))
- _start_worker(svc, first, client)
- assert primary["_image_digests"] == {"app": client.images.digest}
- # Changing the tag again between tasks must not change this deployment.
- client.images.digest = "sha256:" + "c" * 64
- ecs._reconcile_service_tasks(cluster, f"{cluster}/app")
- second = next(task for arn, task in _tasks(svc, deployment=primary).items()
- if arn != first["taskArn"])
- _start_worker(svc, second, client)
- assert client.images.registry_calls == ["example.invalid/app:latest"]
- expected = "example.invalid/app@sha256:" + "b" * 64
- assert [kwargs["image"] for kwargs in created] == [expected, expected]
- assert [image for image, kwargs in client.images.pulls] == [expected, expected]
- assert all(task["containers"][0]["image"] == "example.invalid/app:latest"
- for task in (first, second))
- for task in (first, second):
- ecs._record_service_task_healthy(f"{cluster}/app", task)
- ecs._complete_service_deployment(cluster, f"{cluster}/app")
- assert set(_tasks(svc, "RUNNING")) == {first["taskArn"], second["taskArn"]}
- assert old_tasks == set(_tasks(svc, "STOPPED"))
- # Normal scaling keeps the established deployment's manifest, too.
- _update(cluster, desiredCount=3)
- third = next(task for task in _tasks(svc, deployment=primary).values()
- if task["lastStatus"] == "PROVISIONING")
- _start_worker(svc, third, client)
- assert created[-1]["image"] == expected
- assert len(client.images.registry_calls) == 1
-
-
-def test_ec2_manifest_failure_continues_by_tag_with_cached_execution(service, pending_launcher, docker_worker):
- cluster, svc = service
- client, created = docker_worker
- client.images.manifest_error = True
- client.images.pull_error = True
- digest = "sha256:" + "d" * 64
- client.images.cached = [f"example.invalid/app@{digest}"]
- _update(cluster, forceNewDeployment=True)
- primary = ecs._primary_deployment(svc)
- first = next(iter(_tasks(svc, deployment=primary).values()))
- _start_worker(svc, first, client)
- assert first["lastStatus"] == "RUNNING"
- assert not primary["_image_digests"]
- assert primary["_image_resolution_disabled"]
- assert created[0]["image"] == "example.invalid/app:latest"
- assert "imageDigest" not in first["containers"][0]
- assert len(client.images.registry_calls) == 3
- assert len(client.images.pulls) == 1
-
-
[email protected]("launch_type", ["EC2", "FARGATE"])
- "example.invalid/app:latest",
- "000000000000.dkr.ecr.us-east-1.amazonaws.com/offline-app:latest",
-])
[email protected]("breaker", [False, True])
-def test_offline_cached_images_complete_for_both_launch_types_and_registries(
- service, pending_launcher, docker_worker, launch_type, image, breaker):
- cluster, svc = service
- client, created = docker_worker
- svc["launchType"] = launch_type
- td = ecs._task_defs[ecs._resolve_td_key(svc["taskDefinition"])]
- td["containerDefinitions"][0]["image"] = image
- client.images.manifest_error = True
- client.images.pull_error = True
- _update(cluster, forceNewDeployment=True, deploymentConfiguration={
- "deploymentCircuitBreaker": {"enable": breaker, "rollback": False},
- })
- primary = ecs._primary_deployment(svc)
- first = next(iter(_tasks(svc, deployment=primary).values()))
- _start_worker(svc, first, client)
- assert first["lastStatus"] == "RUNNING"
- assert primary["rolloutState"] != "FAILED"
- assert not primary["_image_digests"]
- assert "imageDigest" not in first["containers"][0]
- ecs._reconcile_service_tasks(cluster, f"{cluster}/app")
- second = next(task for task in _tasks(svc, deployment=primary).values()
- if task["taskArn"] != first["taskArn"])
- _start_worker(svc, second, client)
- for task in (first, second):
- ecs._record_service_task_healthy(f"{cluster}/app", task)
- ecs._complete_service_deployment(cluster, f"{cluster}/app")
- assert primary["rolloutState"] == "COMPLETED"
- assert all(task["lastStatus"] == "RUNNING"
- for task in _tasks(svc, deployment=primary).values())
- assert [kwargs["image"] for kwargs in created] == [image, image]
- assert len(client.images.registry_calls) == 3
- assert [uri for uri, _ in client.images.pulls] == [image, image]
-
-
-def test_offline_cache_does_not_hide_an_uncached_second_container(
- service, pending_launcher, docker_worker, monkeypatch):
- from docker.errors import ImageNotFound
-
- cluster, svc = service
- client, _ = docker_worker
- td = ecs._task_defs[ecs._resolve_td_key(svc["taskDefinition"])]
- td["containerDefinitions"].append({
- "name": "sidecar", "image": "example.invalid/sidecar:missing", "essential": True,
- })
- client.images.manifest_error = True
- original_get = client.images.get
-
- def get(image):
- if image == "example.invalid/sidecar:missing":
- raise ImageNotFound("sidecar is not cached")
- return original_get(image)
-
- monkeypatch.setattr(client.images, "get", get)
- _update(cluster, forceNewDeployment=True, deploymentConfiguration={
- "deploymentCircuitBreaker": {"enable": True, "rollback": False},
- })
- primary = ecs._primary_deployment(svc)
- task = next(iter(_tasks(svc, deployment=primary).values()))
- ecs._prepare_service_images(task, td, client)
- assert primary["rolloutState"] == "FAILED"
- assert not primary["_image_digests"]
- assert len(client.images.registry_calls) == 6
-
-
[email protected]("breaker,rollback", [(False, False), (True, False), (True, True)])
-def test_three_failed_manifest_attempts_continue_or_fail_and_roll_back(
- service, pending_launcher, docker_worker, monkeypatch, breaker, rollback):
- cluster, svc = service
- client, created = docker_worker
- client.images.manifest_error = True
- client.images.cache_missing = True
- # An unrelated RepoDigest, and even an image's local config ID, cannot
- # establish a deployment's registry manifest.
- client.images.cached = ["other.invalid/app@sha256:wrong"]
- old = ecs._primary_deployment(svc)
- monkeypatch.setattr(ecs, "_schedule_service_deployment_completion", lambda *args, **kwargs: None)
- _update(cluster, forceNewDeployment=True, deploymentConfiguration={
- "maximumPercent": 200, "minimumHealthyPercent": 100,
- "deploymentCircuitBreaker": {"enable": breaker, "rollback": rollback},
- })
- primary = ecs._primary_deployment(svc)
- first = next(iter(_tasks(svc, deployment=primary).values()))
- _start_worker(svc, first, client)
- assert len(client.images.registry_calls) == 3
- assert primary["_image_resolution_disabled"]
- assert not primary["_image_digests"]
- if breaker:
- assert primary["rolloutState"] == "FAILED"
- assert primary["failedTasks"] == 0 # no task-start failure was invented
- if rollback:
- assert ecs._primary_deployment(svc) is old
- ecs._complete_service_deployment(cluster, f"{cluster}/app")
- assert first["lastStatus"] == "STOPPED"
- else:
- assert ecs._primary_deployment(svc) is primary
- else:
- assert primary["rolloutState"] == "IN_PROGRESS"
- assert created[0]["image"] == "example.invalid/app:latest"
- ecs._reconcile_service_tasks(cluster, f"{cluster}/app")
- second = next(task for task in _tasks(svc, deployment=primary).values()
- if task["lastStatus"] == "PROVISIONING")
- _start_worker(svc, second, client)
- assert len(client.images.registry_calls) == 3
- assert all(kwargs["image"] == "example.invalid/app:latest" for kwargs in created)
-
-
[email protected]("launch_type", ["EC2", "FARGATE"])
-def test_service_pull_failure_cache_policy(
- service, pending_launcher, docker_worker, launch_type):
- cluster, svc = service
- client, created = docker_worker
- svc["launchType"] = launch_type
- client.images.pull_error = True
- _update(cluster, forceNewDeployment=True)
- primary = ecs._primary_deployment(svc)
- first = next(iter(_tasks(svc, deployment=primary).values()))
- _start_worker(svc, first, client)
- assert first["lastStatus"] == "RUNNING"
- assert len(created) == 1
- assert primary["launchType"] == launch_type
- assert len(client.images.pulls) == 1
-
-
-def test_disabled_version_consistency_pulls_tag_for_each_task(
- service, pending_launcher, docker_worker):
- cluster, svc = service
- client, created = docker_worker
- td = ecs._task_defs[ecs._resolve_td_key(svc["taskDefinition"])]
- td["containerDefinitions"][0]["versionConsistency"] = "disabled"
- _update(cluster, forceNewDeployment=True)
- primary = ecs._primary_deployment(svc)
- assert len(_tasks(svc, deployment=primary)) == 2
- for task in _tasks(svc, deployment=primary).values():
- _start_worker(svc, task, client)
- assert client.images.registry_calls == []
- assert len(client.images.pulls) == 2
- assert [kwargs["image"] for kwargs in created] == ["example.invalid/app:latest"] * 2
-
-
-def test_digest_reference_skips_manifest_lookup(service, pending_launcher, docker_worker):
- cluster, svc = service
- client, created = docker_worker
- td = ecs._task_defs[ecs._resolve_td_key(svc["taskDefinition"])]
- digest = "sha256:" + "e" * 64
- td["containerDefinitions"][0]["image"] = f"example.invalid/app:latest@{digest}"
- _update(cluster, forceNewDeployment=True)
- assert len(_tasks(svc, deployment=ecs._primary_deployment(svc))) == 2
- assert pending_launcher == [2]
- task = next(iter(_tasks(svc, deployment=ecs._primary_deployment(svc)).values()))
- _start_worker(svc, task, client)
- assert client.images.registry_calls == []
- assert created[0]["image"] == f"example.invalid/app@{digest}"
-
-
-def test_zero_size_deployment_needs_new_deployment_to_establish_digest(
- service, pending_launcher, docker_worker):
- cluster, svc = service
- client, created = docker_worker
- _update(cluster, desiredCount=0, forceNewDeployment=True)
- primary = ecs._primary_deployment(svc)
- assert primary["_image_resolution_disabled"]
- _update(cluster, desiredCount=2)
- for task in _tasks(svc, deployment=primary).values():
- _start_worker(svc, task, client)
- assert client.images.registry_calls == []
- _update(cluster, forceNewDeployment=True)
- primary = ecs._primary_deployment(svc)
- task = next(iter(_tasks(svc, deployment=primary).values()))
- _start_worker(svc, task, client)
- assert client.images.registry_calls == ["example.invalid/app:latest"]
-
-
-def test_pull_preserves_declared_platform_and_existing_fallback(docker_worker, monkeypatch):
- client, created = docker_worker
- # The normal run path retains its declared platform during image pull.
- monkeypatch.setattr(ecs.time, "sleep", lambda seconds: None)
- ecs._run_docker_container(client, {"image": "example.invalid/app:latest"},
- {"detach": True, "platform": "linux/arm64"}, refresh_image=True)
- assert client.images.pulls == [("example.invalid/app:latest", {"platform": "linux/arm64"})]
- assert created[0]["platform"] == "linux/arm64"
- # Host-architecture retry remains available on a platform execution error.
- original_create = client.containers.create
-
- def reject_pinned(**kwargs):
- if kwargs.get("platform"):
- raise RuntimeError("unsupported platform")
- return original_create(**kwargs)
-
- client.containers.create = reject_pinned
- ecs._run_docker_container(client, {"image": "example.invalid/app:latest"},
- {"detach": True, "platform": "linux/arm64"}, refresh_image=True)
- assert client.images.pulls[-2:] == [
- ("example.invalid/app:latest", {"platform": "linux/arm64"}),
- ("example.invalid/app:latest", {}),
- ]
- assert "platform" not in created[-1]
-
-
[email protected]("allow_cached", [False, True])
-def test_platform_pull_failure_retains_existing_host_architecture_fallback(
- docker_worker, monkeypatch, allow_cached):
- from docker.errors import ImageNotFound
-
- client, created = docker_worker
- original_pull = client.images.pull
-
- def pull(image, **kwargs):
- if kwargs.get("platform") == "linux/arm64":
- client.images.pulls.append((image, kwargs))
- raise RuntimeError("no matching manifest for linux/arm64")
- return original_pull(image, **kwargs)
-
- def missing(image):
- raise ImageNotFound("image not cached")
-
- monkeypatch.setattr(client.images, "pull", pull)
- monkeypatch.setattr(client.images, "get", missing)
- ecs._run_docker_container(
- client, {"image": "example.invalid/app:latest"},
- {"detach": True, "platform": "linux/arm64"},
- refresh_image=True, allow_cached=allow_cached,
- )
- assert client.images.pulls == [
- ("example.invalid/app:latest", {"platform": "linux/arm64"}),
- ("example.invalid/app:latest", {}),
- ]
- assert len(created) == 1
- assert "platform" not in created[0]
-
-
[email protected]("controller", ["CODE_DEPLOY", "EXTERNAL"])
-def test_force_is_scoped_to_supported_rolling_controller(service, controller):
- cluster, svc = service
- svc["deploymentController"] = {"type": controller}
- primary = ecs._primary_deployment(svc)
- tasks = set(_tasks(svc))
- _update(cluster, forceNewDeployment=True)
- assert ecs._primary_deployment(svc) is primary
- assert set(_tasks(svc)) == tasks
-
-
-def test_fargate_manifest_failure_does_not_use_prior_task_cache(
- service, pending_launcher, docker_worker):
- cluster, svc = service
- client, created = docker_worker
- svc["launchType"] = "FARGATE"
- client.images.manifest_error = True
- client.images.cached = ["example.invalid/app@sha256:" + "f" * 64]
- _update(cluster, forceNewDeployment=True)
- primary = ecs._primary_deployment(svc)
- task = next(iter(_tasks(svc, deployment=primary).values()))
- _start_worker(svc, task, client)
- assert len(client.images.registry_calls) == 3
- assert not primary["_image_digests"]
- assert created[0]["image"] == "example.invalid/app:latest"
- assert client.images.pulls == [("example.invalid/app:latest", {})]
-
-
-def test_deployment_manifest_survives_persistence_and_scopes(service, pending_launcher, docker_worker):
- from ministack.core.responses import request_scope
-
- cluster, svc = service
- client, created = docker_worker
- _update(cluster, forceNewDeployment=True)
- primary = ecs._primary_deployment(svc)
- task = next(iter(_tasks(svc, deployment=primary).values()))
- _start_worker(svc, task, client)
- state = ecs.get_state()
- persisted = state["services"][f"{cluster}/app"]["deployments"][0]
- assert persisted["_image_digests"] == {"app": client.images.digest}
- # Snapshot and wire output are independent; private digest bookkeeping is
- # persisted but never becomes a new API member.
- primary["_image_digests"]["app"] = "sha256:changed"
- assert persisted["_image_digests"]["app"] == client.images.digest
- assert "_image_digests" not in ecs._sanitize(primary)
- with request_scope("222222222222", "us-west-2"):
- assert f"{cluster}/app" not in ecs._services
- assert not _tasks(svc)
- primary["_image_digests"]["app"] = client.images.digest
-
-
-def test_initial_deployment_resolution_failure_triggers_enabled_breaker(
- service, pending_launcher, docker_worker):
- cluster, svc = service
- client, created = docker_worker
- client.images.manifest_error = True
- client.images.cache_missing = True
- initial = ecs._primary_deployment(svc)
- svc["deploymentConfiguration"]["deploymentCircuitBreaker"]["enable"] = True
- task = next(iter(_tasks(svc).values()))
- task["lastStatus"] = "PROVISIONING"
- _start_worker(svc, task, client)
- assert initial["rolloutState"] == "FAILED"
- assert initial["failedTasks"] == 0
- assert len(client.images.registry_calls) == 3
-
-
[email protected]("launch_type", ["EC2", "FARGATE"])
[email protected]("architecture", [None, "ARM64"])
-def test_terminal_service_pull_failure_has_documented_task_error_category(
- service, pending_launcher, docker_worker, monkeypatch, launch_type, architecture):
- from docker.errors import ImageNotFound
-
- from ministack.core.responses import get_account_id, get_region
-
- cluster, svc = service
- client, created = docker_worker
- td = ecs._task_defs[ecs._resolve_td_key(svc["taskDefinition"])]
- if architecture:
- td["runtimePlatform"] = {"cpuArchitecture": architecture}
- svc["launchType"] = launch_type
- client.images.pull_error = True
- def missing(image):
- raise ImageNotFound("cached image absent")
- monkeypatch.setattr(client.images, "get", missing)
- _update(cluster, forceNewDeployment=True)
- primary = ecs._primary_deployment(svc)
- task = next(iter(_tasks(svc, deployment=primary).values()))
- ecs._run_task_worker(task, td, [], client, get_account_id(), get_region())
- assert task["lastStatus"] == "STOPPED"
- assert task["stopCode"] == "TaskFailedToStart"
- assert task["stoppedReason"] == "CannotPullContainerError: image pull failed"
- assert not created
- assert len(client.images.pulls) == (2 if architecture else 1)
-
-
[email protected]("launch_type", ["EC2", "FARGATE"])
-def test_all_tasks_can_use_cached_tag_after_a_resolved_digest_pull_fails(
- service, pending_launcher, docker_worker, monkeypatch, launch_type):
- from docker.errors import ImageNotFound
-
- cluster, svc = service
- svc["launchType"] = launch_type
- client, created = docker_worker
- client.images.digest = "sha256:" + "b" * 64
- client.images.pull_error = True
-
- def only_old_tag(image):
- if image != "example.invalid/app:latest":
- raise ImageNotFound("new digest B not cached")
- return SimpleNamespace(id="old-image-A")
-
- monkeypatch.setattr(client.images, "get", only_old_tag)
- _update(cluster, forceNewDeployment=True)
- primary = ecs._primary_deployment(svc)
- first = next(iter(_tasks(svc, deployment=primary).values()))
- _start_worker(svc, first, client)
- assert first["lastStatus"] == "RUNNING"
- assert primary["_image_digests"] == {"app": client.images.digest}
- assert client.images.pulls[0][0] == "example.invalid/app@" + client.images.digest
- assert created[0]["image"] == "example.invalid/app:latest"
- assert first["containers"][0]["imageDigest"] == client.images.digest
- assert not client.images.tags
- # Later tasks retain the resolved digest in their response while also
- # permitting the requested tag's cached execution for offline use.
- ecs._reconcile_service_tasks(cluster, f"{cluster}/app")
- second = next(task for task in _tasks(svc, deployment=primary).values()
- if task["lastStatus"] == "PROVISIONING")
- _start_worker(svc, second, client)
- assert second["lastStatus"] == "RUNNING"
- assert second["containers"][0]["imageDigest"] == client.images.digest
- assert created[1]["image"] == "example.invalid/app:latest"
- assert len(created) == 2
-
-
-def test_successful_first_task_pull_refreshes_original_tag_cache(
- service, pending_launcher, docker_worker):
- cluster, svc = service
- client, created = docker_worker
- _update(cluster, forceNewDeployment=True)
- first = next(iter(_tasks(svc, deployment=ecs._primary_deployment(svc)).values()))
- _start_worker(svc, first, client)
- assert client.images.tags == [("example.invalid/app", "latest")]
- assert created[0]["image"] == "example.invalid/app@" + client.images.digest
- described = _decoded(ecs._describe_tasks({
- "cluster": cluster, "tasks": [first["taskArn"]],
- }))["tasks"][0]["containers"][0]
- assert described["image"] == "example.invalid/app:latest"
- assert described["imageDigest"] == client.images.digest
-
-
-def test_following_tasks_wait_for_first_running_task_even_after_manifest_is_known(
- service, pending_launcher, docker_worker):
- cluster, svc = service
- client, created = docker_worker
- _update(cluster, forceNewDeployment=True)
- primary = ecs._primary_deployment(svc)
- first = next(iter(_tasks(svc, deployment=primary).values()))
- td = ecs._task_defs[ecs._resolve_td_key(svc["taskDefinition"])]
- ecs._mark_task_activating(first["taskArn"], first)
- ecs._prepare_service_images(first, td, client)
- assert primary["_image_digests"] == {"app": client.images.digest}
- _update(cluster, desiredCount=2)
- assert pending_launcher == [1]
- assert len(_tasks(svc, deployment=primary)) == 1
- ecs._mark_task_running(first["taskArn"], first)
- ecs._reconcile_service_tasks(cluster, f"{cluster}/app")
- assert pending_launcher == [1, 1]
- assert len(_tasks(svc, deployment=primary)) == 2
-
-
-def test_canceled_manifest_worker_cannot_publish_or_fail_deployment(
- service, pending_launcher, docker_worker, monkeypatch):
- cluster, svc = service
- client, created = docker_worker
- _update(cluster, forceNewDeployment=True, deploymentConfiguration={
- "maximumPercent": 200, "minimumHealthyPercent": 100,
- "deploymentCircuitBreaker": {"enable": True, "rollback": True},
- })
- primary = ecs._primary_deployment(svc)
- task = next(iter(_tasks(svc, deployment=primary).values()))
-
- def cancel_during_lookup(image):
- client.images.registry_calls.append(image)
- ecs._mark_task_stopped(task["taskArn"], task, "User canceled task", "UserInitiated")
- raise RuntimeError("registry unavailable after cancellation")
-
- monkeypatch.setattr(client.images, "get_registry_data", cancel_during_lookup)
- _start_worker(svc, task, client)
- assert task["lastStatus"] == "STOPPED"
- assert task["stopCode"] == "UserInitiated"
- assert len(client.images.registry_calls) == 1
- assert "_image_digests" not in primary
- assert primary["rolloutState"] == "IN_PROGRESS"
- assert primary["failedTasks"] == 0
- assert ecs._primary_deployment(svc) is primary
- assert not created
- # The digest-only transition also rejects a stale completion delivered
- # after cancellation, while ordinary stopped-task failures stay supported.
- ecs._record_service_task_failure(task, digest_resolution_failed=True)
- assert primary["rolloutState"] == "IN_PROGRESS"
-
-
[email protected]("force", [False, True])
[email protected]("schedule_after_update", [False, True])
-def test_predecessor_startup_callback_cannot_complete_crashing_replacement(
- service, pending_launcher, monkeypatch, force, schedule_after_update):
- cluster, svc = service
- _update(cluster, desiredCount=1)
- old = ecs._primary_deployment(svc)
- old_task = next(iter(_tasks(svc, "RUNNING").values()))
- callbacks = []
- monkeypatch.setattr(ecs, "spawn_background", lambda callback, **kwargs: callbacks.append(callback))
- monkeypatch.setattr(ecs.time, "sleep", lambda delay: None)
-
- def schedule_old():
- from ministack.core.responses import get_account_id, get_region
- ecs._schedule_service_deployment_completion(
- cluster, f"{cluster}/app", get_account_id(), get_region(), healthy_task=old_task)
-
- if not schedule_after_update:
- schedule_old()
- request = {"forceNewDeployment": True} if force else {
- "taskDefinition": _decoded(ecs._register_task_definition({
- "family": cluster,
- "containerDefinitions": [{
- "name": "app", "image": "example.invalid/app:latest",
- "command": ["sh", "-c", "exit 1"],
- }],
- }))["taskDefinition"]["taskDefinitionArn"],
- }
- _update(cluster, **request, deploymentConfiguration={
- "deploymentCircuitBreaker": {"enable": True, "rollback": True},
- })
- primary = ecs._primary_deployment(svc)
- if schedule_after_update:
- schedule_old()
- first = next(iter(_tasks(svc, deployment=primary).values()))
- # The Docker worker publishes RUNNING before its watcher detects exit 1.
- ecs._mark_task_running(first["taskArn"], first)
- callbacks[0]()
- assert primary["rolloutState"] == "IN_PROGRESS"
- assert old_task["lastStatus"] == "RUNNING"
-
- for _ in range(3):
- task = next(task for task in _tasks(svc, deployment=primary).values()
- if task["lastStatus"] in ecs._PRE_STOP_STATUSES)
- ecs._mark_task_stopped(task["taskArn"], task,
- "Essential container in task exited", "EssentialContainerExited", 1)
- assert primary["rolloutState"] == "FAILED"
- assert primary["failedTasks"] == 3
- assert ecs._primary_deployment(svc) is old
- # The rollback's own scheduled callback still completes its deployment.
- callbacks[-1]()
- assert old["rolloutState"] == "COMPLETED"
- assert old_task["lastStatus"] == "RUNNING"
-
-
[email protected]("platform,os_family,resolves,reported_platform,family", [
- ("1.2.0", "LINUX", False, "1.2.0", "Linux"),
- ("1.3.0", "LINUX", True, "1.3.0", "Linux"),
- ("1.4.0", "LINUX", True, "1.4.0", "Linux"),
- ("LATEST", "LINUX", True, "1.4.0", "Linux"),
- ("", "LINUX", True, "1.4.0", "Linux"),
- ("1.0.0", "WINDOWS_SERVER_2022_CORE", True, "1.0.0", ""),
- ("LATEST", "WINDOWS_SERVER_2022_CORE", True, "1.0.0", ""),
- ("", "WINDOWS_SERVER_2022_CORE", True, "1.0.0", ""),
-])
-def test_fargate_platform_controls_resolution_and_first_task_reservation(
- service, pending_launcher, docker_worker, platform, os_family, resolves,
- reported_platform, family):
- cluster, svc = service
- svc["launchType"] = "FARGATE"
- svc["platformVersion"] = platform
- client, created = docker_worker
- td = ecs._task_defs[ecs._resolve_td_key(svc["taskDefinition"])]
- td["runtimePlatform"] = {"operatingSystemFamily": os_family}
- _update(cluster, forceNewDeployment=True)
- primary = ecs._primary_deployment(svc)
- current = list(_tasks(svc, deployment=primary).values())
- assert len(current) == (1 if resolves else 2)
- assert all(task["platformVersion"] == reported_platform for task in current)
- assert all(task["platformFamily"] == family for task in current)
- _start_worker(svc, current[0], client)
- assert bool(client.images.registry_calls) is resolves
- assert bool(primary.get("_image_digests")) is resolves
- container = current[0]["containers"][0]
- assert ("imageDigest" in container) is resolves
- assert container["image"] == "example.invalid/app:latest"
- assert created[0]["image"] == (
- "example.invalid/app@" + client.images.digest if resolves
- else "example.invalid/app:latest")
-
-
-def test_reported_manifest_digest_survives_persistence(
- service, pending_launcher, docker_worker):
- cluster, svc = service
- client, _ = docker_worker
- _update(cluster, forceNewDeployment=True)
- task = next(iter(_tasks(svc, deployment=ecs._primary_deployment(svc)).values()))
- _start_worker(svc, task, client)
- state = ecs.get_state()
- persisted = state["tasks"][task["taskArn"]]["containers"][0]
- assert persisted["imageDigest"] == client.images.digest
- task["containers"][0]["imageDigest"] = "sha256:" + "f" * 64
- assert persisted["imageDigest"] == client.images.digest
-
-
[email protected]("launch_type", ["EC2", "FARGATE"])
[email protected]("consistency", ["enabled", "disabled", "digest"])
-def test_private_registry_credentials_reach_lookup_and_pull_without_leaking(
- service, pending_launcher, docker_worker, monkeypatch, launch_type, consistency):
- cluster, svc = service
- svc["launchType"] = launch_type
- client, created = docker_worker
- td = ecs._task_defs[ecs._resolve_td_key(svc["taskDefinition"])]
- cdef = td["containerDefinitions"][0]
- secret_id = "arn:aws:secretsmanager:us-east-1:000000000000:secret:registry"
- credentials = {"username": "repro-user", "password": "private-registry-password"}
- cdef["repositoryCredentials"] = {"credentialsParameter": secret_id}
- if consistency == "digest":
- cdef["image"] = "example.invalid/app@" + client.images.digest
- else:
- cdef["versionConsistency"] = consistency
- reads = []
-
- def resolve(secret):
- reads.append(secret)
- return json.dumps(credentials)
-
- monkeypatch.setattr(ecs.secretsmanager, "resolve_secret_string", resolve)
- lookup = client.images.get_registry_data
- lookup_auth = []
-
- def authenticated_lookup(image, **kwargs):
- lookup_auth.append(kwargs)
- assert kwargs == {"auth_config": credentials}
- return lookup(image)
-
- monkeypatch.setattr(client.images, "get_registry_data", authenticated_lookup)
- _update(cluster, forceNewDeployment=True)
- task = next(iter(_tasks(svc, deployment=ecs._primary_deployment(svc)).values()))
- _start_worker(svc, task, client)
- assert task["lastStatus"] == "RUNNING"
- assert bool(lookup_auth) is (consistency == "enabled")
- assert client.images.pulls[-1][1]["auth_config"] == credentials
- assert set(reads) == {secret_id}
- assert "auth_config" not in created[0]
- assert credentials["password"] not in repr(ecs.get_state())
- assert credentials["password"] not in repr(ecs._sanitize(task))
-
-
[email protected]("secret", [None, "not-json", "{}", '{"username":"user"}'])
-def test_unavailable_registry_secret_fails_start_without_exposing_secret(
- service, pending_launcher, docker_worker, monkeypatch, secret):
- cluster, svc = service
- client, created = docker_worker
- td = ecs._task_defs[ecs._resolve_td_key(svc["taskDefinition"])]
- td["containerDefinitions"][0]["repositoryCredentials"] = {"credentialsParameter": "registry"}
- monkeypatch.setattr(ecs.secretsmanager, "resolve_secret_string", lambda secret_id: secret)
- _update(cluster, forceNewDeployment=True, deploymentConfiguration={
- "deploymentCircuitBreaker": {"enable": True, "rollback": False},
- })
- task = next(iter(_tasks(svc, deployment=ecs._primary_deployment(svc)).values()))
- # Exercise the worker's existing secret-retrieval error category without
- # starting real Docker watchers or hiding the initialization failure.
- ecs._run_task_worker(task, td, [], client, "000000000000", "us-east-1")
- assert task["lastStatus"] == "STOPPED"
- assert task["stopCode"] == "TaskFailedToStart"
- assert task["stoppedReason"].startswith("ResourceInitializationError: unable to pull secrets or registry auth")
- assert not created
- assert not client.images.pulls
- assert not client.images.registry_calls
- if secret:
- assert secret not in task["stoppedReason"]
diff --git a/tests/test_lambda.py b/tests/test_lambda.py
index 96463c6d..81e1a7c4 100644
--- a/tests/test_lambda.py
+++ b/tests/test_lambda.py
@@ -13958,9 +13958,7 @@ def test_cognito_issuer_host_resolves_to_the_gateway_under_use_ssl(monkeypatch,
def test_lambda_trust_files_are_copied_not_mounted_when_ministack_runs_in_docker(monkeypatch, tmp_path):
- """In a container, a bind source resolves on the host daemon, where
- MiniStack's certificate does not exist: the trust files are docker cp'd
- instead, for a container-image function too, and a symlink is followed."""
+ """In a container the trust files are docker cp'd, not bind-mounted; a symlink is followed."""
import tarfile
real = tmp_path / "real.crt"