diff --git a/docs/services/cloudformation.md b/docs/services/cloudformation.md index e534c2d1e4..f4328a4a73 100644 --- a/docs/services/cloudformation.md +++ b/docs/services/cloudformation.md @@ -544,8 +544,14 @@ A function whose code is hot-reloaded publishes a version for every new id. Like SAM, floci gives the version the function's `DeletionPolicy`, else its `VersionDeletionPolicy` (`Delete` or `Retain`), else `Delete` for a function with a `CapacityProviderConfig` and `Retain` for any other. So by default the version a new id replaces stays published, and the update reports -it `DELETE_SKIPPED`. With `Delete` floci reports the old version `DELETE_COMPLETE` but never deletes -it, where AWS does. A `VersionDeletionPolicy` written as an intrinsic function, such as a `Ref` to +it `DELETE_SKIPPED`. With `Delete`, physical version deletion uses Lambda's qualified +`DeleteFunction`: that version is removed while `$LATEST` and other versions remain. An alias +whose primary target is the version makes deletion fail; removing that alias permits a retry. +This uses the native service's existing primary-target check; weighted alias references are not covered. +The same deletion applies to a raw `AWS::Lambda::Version`, template removal and create rollback, +including `RetainExceptOnCreate`. A version whose parent is already absent can be deleted again. +This does not add replacement updates for a version that retains the same logical id. +A `VersionDeletionPolicy` written as an intrinsic function, such as a `Ref` to a parameter, is copied onto the version unresolved, as SAM copies it. CloudFormation takes intrinsic functions only in resource properties, outputs, metadata attributes and update policy attributes, unless a transform such as `AWS::LanguageExtensions` resolves them first, and floci diff --git a/src/main/java/io/floci/aws/services/cloudformation/provisioners/LambdaVersionAliasCfnProvisioner.java b/src/main/java/io/floci/aws/services/cloudformation/provisioners/LambdaVersionAliasCfnProvisioner.java index 40b6a04a1a..09914ae4ad 100644 --- a/src/main/java/io/floci/aws/services/cloudformation/provisioners/LambdaVersionAliasCfnProvisioner.java +++ b/src/main/java/io/floci/aws/services/cloudformation/provisioners/LambdaVersionAliasCfnProvisioner.java @@ -56,9 +56,11 @@ public class LambdaVersionAliasCfnProvisioner implements CfnResourceProvisioner @Override public void delete(String resourceType, String physicalId, String region) { + if ("AWS::Lambda::Version".equals(resourceType)) { + deleteVersion(physicalId, region); + return; + } if (!"AWS::Lambda::Alias".equals(resourceType)) { - // AWS::Lambda::Version has no backing delete: Lambda exposes no DeleteVersion, and - // deleting the function drops its versions with it. return; } // The physical id is the alias ARN (arn:…:function::), so the function name @@ -77,6 +79,18 @@ public class LambdaVersionAliasCfnProvisioner implements CfnResourceProvisioner } } + private void deleteVersion(String physicalId, String region) { + LambdaArnUtils.ResolvedFunctionRef ref = LambdaArnUtils.resolveWithRegion(region, physicalId, null); + String version = ref.qualifier(); + if (version == null || !version.chars().allMatch(value -> value >= '0' && value <= '9')) { + // Older stacks can carry a generic placeholder. Never turn that into a whole-function delete. + LOG.debugv("Not a published version ARN, nothing to delete: {0}", physicalId); + return; + } + CfnDeletes.safeDelete("Lambda version", physicalId, + () -> lambdaService.deleteFunction(region, physicalId, version), "ResourceNotFoundException"); + } + private void provisionVersion(StackResource r, JsonNode props, ProvisionContext ctx) { String functionName = ctx.resolveOptional(props, "FunctionName"); if (functionName == null || functionName.isBlank()) { diff --git a/src/test/java/io/floci/aws/services/cloudformation/CloudFormationLambdaVersionDeletionSdkTest.java b/src/test/java/io/floci/aws/services/cloudformation/CloudFormationLambdaVersionDeletionSdkTest.java new file mode 100644 index 0000000000..a55316494c --- /dev/null +++ b/src/test/java/io/floci/aws/services/cloudformation/CloudFormationLambdaVersionDeletionSdkTest.java @@ -0,0 +1,616 @@ +package io.floci.aws.services.cloudformation; + +import io.floci.aws.testing.AwsTestClients; +import io.floci.aws.testing.CfnStackWaits; +import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; +import software.amazon.awssdk.core.SdkBytes; +import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration; +import software.amazon.awssdk.core.retry.RetryPolicy; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.cloudformation.CloudFormationClient; +import software.amazon.awssdk.services.cloudformation.model.CloudFormationException; +import software.amazon.awssdk.services.cloudformation.model.DescribeStackEventsResponse; +import software.amazon.awssdk.services.cloudformation.model.StackEvent; +import software.amazon.awssdk.services.iam.IamClient; +import software.amazon.awssdk.services.iam.model.IamException; +import software.amazon.awssdk.services.lambda.LambdaClient; +import software.amazon.awssdk.services.lambda.model.FunctionConfiguration; +import software.amazon.awssdk.services.lambda.model.LambdaException; +import software.amazon.awssdk.services.lambda.model.ListVersionsByFunctionResponse; +import software.amazon.awssdk.services.lambda.model.Runtime; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.charset.StandardCharsets; +import java.time.Duration; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; +import java.util.UUID; +import java.util.stream.Collectors; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; + +import static io.floci.aws.testing.AwsSdkErrors.assertAwsError; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** CFN version deletion reaches native qualified deletion while preserving unrelated functions and versions. */ +@QuarkusTest +@Timeout(120) +class CloudFormationLambdaVersionDeletionSdkTest { + private static final String ACCOUNT = "815670000031"; + + @ParameterizedTest + @ValueSource(strings = {"", "\"DeletionPolicy\":\"Delete\",\"UpdateReplacePolicy\":\"Retain\","}) + void deletionRemovesOnlyTheStackVersionFromAnExternalFunction(String policy) { + try (Owned owned = new Owned(ACCOUNT, Region.US_EAST_1)) { + String other = owned.seedVersion; + owned.alias(owned.function, "keep", other); + owned.create(owned.versionTemplate(policy, false), "CREATE_COMPLETE"); + String version = owned.version("Version"); + assertNotEquals(other, version); + FunctionConfiguration latest = owned.configuration(owned.function, "$LATEST"); + FunctionConfiguration kept = owned.configuration(owned.function, other); + + owned.delete("DELETE_COMPLETE"); + + owned.assertVersionMissing(owned.function, version); + assertEquals(latest, owned.configuration(owned.function, "$LATEST")); + assertEquals(kept, owned.configuration(owned.function, other)); + assertEquals(other, owned.lambda.getAlias(request -> request.functionName(owned.function) + .name("keep")).functionVersion()); + } + } + + @Test + void retainedParentFunctionSurvivesWhileItsDefaultDeleteVersionIsRemoved() { + try (Owned owned = new Owned(ACCOUNT, Region.US_EAST_1)) { + String child = owned.marker + "-child"; + owned.functions.add(child); + String template = """ + { + "Resources": { + "Function": { + "Type": "AWS::Lambda::Function", + "DeletionPolicy": "Retain", + "Properties": { + "FunctionName": "%s", + "Runtime": "python3.12", + "Handler": "index.handler", + "Role": "%s", + "Code": {"ZipFile": "def handler(event, context):\\n return 'owned'\\n"} + } + }, + "Version": { + "Type": "AWS::Lambda::Version", + "Properties": {"FunctionName": {"Ref": "Function"}} + } + } + } + """.formatted(child, owned.roleArn); + owned.create(template, "CREATE_COMPLETE"); + String version = owned.version("Version"); + FunctionConfiguration latest = owned.configuration(child, "$LATEST"); + + List deleted = owned.delete("DELETE_COMPLETE"); + + assertTrue(deleted.stream().anyMatch(event -> "Function".equals(event.logicalResourceId()) + && "DELETE_SKIPPED".equals(event.resourceStatusAsString()))); + owned.assertVersionMissing(child, version); + assertEquals(latest, owned.configuration(child, "$LATEST")); + } + } + + @Test + void retainedVersionStaysPublishedAfterStackDeletion() { + try (Owned owned = new Owned(ACCOUNT, Region.US_EAST_1)) { + owned.create(owned.versionTemplate("\"DeletionPolicy\":\"Retain\",", false), "CREATE_COMPLETE"); + String version = owned.version("Version"); + FunctionConfiguration before = owned.configuration(owned.function, version); + + List deleted = owned.delete("DELETE_COMPLETE"); + + assertEquals(before, owned.configuration(owned.function, version)); + assertTrue(deleted.stream().anyMatch(event -> "Version".equals(event.logicalResourceId()) + && "DELETE_SKIPPED".equals(event.resourceStatusAsString()))); + } + } + + @ParameterizedTest + @ValueSource(strings = {"", "\"DeletionPolicy\":\"Retain\","}) + void removingTheVersionFromTheTemplateHonorsItsDeletionPolicy(String policy) { + try (Owned owned = new Owned(ACCOUNT, Region.US_EAST_1)) { + owned.create(owned.versionTemplate(policy, true), "CREATE_COMPLETE"); + String version = owned.version("Version"); + FunctionConfiguration before = owned.configuration(owned.function, version); + + List updated = owned.update(owned.anchorTemplate()); + + if (policy.isEmpty()) { + owned.assertVersionMissing(owned.function, version); + } else { + assertEquals(before, owned.configuration(owned.function, version)); + } + assertTrue(updated.stream().anyMatch(event -> "Version".equals(event.logicalResourceId()) + && (policy.isEmpty() ? "DELETE_COMPLETE" : "DELETE_SKIPPED") + .equals(event.resourceStatusAsString()))); + assertEquals(Set.of("Anchor"), owned.cfn.listStackResources(request -> request.stackName(owned.stackId)) + .stackResourceSummaries().stream().map(value -> value.logicalResourceId()) + .collect(Collectors.toSet())); + } + } + + @ParameterizedTest + @ValueSource(strings = {"", "\"DeletionPolicy\":\"RetainExceptOnCreate\","}) + void failedCreateRollsBackThePublishedVersion(String policy) { + try (Owned owned = new Owned(ACCOUNT, Region.US_EAST_1)) { + Set versions = owned.versions(owned.function); + String template = """ + { + "Resources": { + "Version": { + "Type": "AWS::Lambda::Version", + %s"Properties": {"FunctionName": "%s"} + }, + "Failure": { + "Type": "AWS::Logs::LogStream", + "DependsOn": "Version", + "Properties": { + "LogGroupName": "/%s-missing", + "LogStreamName": "missing" + } + } + } + } + """.formatted(policy, owned.function, owned.marker); + + List events = owned.create(template, "ROLLBACK_COMPLETE"); + + assertTrue(events.stream().anyMatch(event -> "Version".equals(event.logicalResourceId()) + && "CREATE_COMPLETE".equals(event.resourceStatusAsString()))); + assertTrue(events.stream().anyMatch(event -> "Failure".equals(event.logicalResourceId()) + && "CREATE_FAILED".equals(event.resourceStatusAsString()))); + assertTrue(events.stream().anyMatch(event -> "Version".equals(event.logicalResourceId()) + && "DELETE_COMPLETE".equals(event.resourceStatusAsString()))); + assertEquals(versions, owned.versions(owned.function), "Rollback must remove its published version"); + } + } + + @Test + void stackAliasIsDeletedBeforeTheVersionItReferences() { + try (Owned owned = new Owned(ACCOUNT, Region.US_EAST_1)) { + owned.aliases.add("stack-alias"); + String template = """ + { + "Resources": { + "Version": { + "Type": "AWS::Lambda::Version", + "Properties": {"FunctionName": "%s"} + }, + "Alias": { + "Type": "AWS::Lambda::Alias", + "Properties": { + "FunctionName": "%s", + "Name": "stack-alias", + "FunctionVersion": {"Fn::GetAtt": ["Version", "Version"]} + } + } + } + } + """.formatted(owned.function, owned.function); + owned.create(template, "CREATE_COMPLETE"); + String version = owned.version("Version"); + assertEquals(version, owned.lambda.getAlias(request -> request.functionName(owned.function) + .name("stack-alias")).functionVersion()); + + List deleted = new ArrayList<>(owned.delete("DELETE_COMPLETE").reversed()); + + int aliasComplete = eventIndex(deleted, "Alias", "DELETE_COMPLETE"); + int versionStarted = eventIndex(deleted, "Version", "DELETE_IN_PROGRESS"); + assertTrue(aliasComplete < versionStarted, deleted.toString()); + assertAwsError("ResourceNotFoundException", 404, () -> owned.lambda.getAlias(request -> + request.functionName(owned.function).name("stack-alias"))); + owned.assertVersionMissing(owned.function, version); + owned.configuration(owned.function, owned.seedVersion); + } + } + + @Test + void externalAliasBlocksNativeAndStackDeletionUntilTheAliasIsRemoved() { + try (Owned owned = new Owned(ACCOUNT, Region.US_EAST_1)) { + owned.create(owned.versionTemplate("", false), "CREATE_COMPLETE"); + String version = owned.version("Version"); + owned.alias(owned.function, "external", version); + FunctionConfiguration before = owned.configuration(owned.function, version); + LambdaException conflict = assertThrows(LambdaException.class, () -> + owned.lambda.deleteFunction(request -> request.functionName(owned.function).qualifier(version))); + assertEquals(409, conflict.statusCode()); + assertEquals("ResourceConflictException", conflict.awsErrorDetails().errorCode()); + + List failed = owned.delete("DELETE_FAILED"); + + assertTrue(failed.stream().anyMatch(event -> "Version".equals(event.logicalResourceId()) + && "DELETE_FAILED".equals(event.resourceStatusAsString()))); + assertEquals(before, owned.configuration(owned.function, version)); + assertEquals(version, owned.lambda.getAlias(request -> request.functionName(owned.function) + .name("external")).functionVersion()); + owned.lambda.deleteAlias(request -> request.functionName(owned.function).name("external")); + owned.delete("DELETE_COMPLETE"); + owned.assertVersionMissing(owned.function, version); + } + } + + @Test + void anAlreadyDeletedVersionAllowsIdempotentStackDeletion() { + try (Owned owned = new Owned(ACCOUNT, Region.US_EAST_1)) { + owned.create(owned.versionTemplate("", false), "CREATE_COMPLETE"); + String version = owned.version("Version"); + owned.lambda.deleteFunction(request -> request.functionName(owned.function).qualifier(version)); + owned.assertVersionMissing(owned.function, version); + + owned.delete("DELETE_COMPLETE"); + + assertEquals(Set.of("$LATEST", owned.seedVersion), owned.versions(owned.function)); + } + } + + @Test + void sameNamedFunctionsAndVersionsInOtherAccountsAndRegionsRemainUnchanged() { + String shared = "cfn-version-scope-" + UUID.randomUUID().toString().replace("-", ""); + try (Owned owner = new Owned(ACCOUNT, Region.US_EAST_1, shared); + Owned account = new Owned("815670000032", Region.US_EAST_1, shared); + Owned region = new Owned(ACCOUNT, Region.US_WEST_2, shared)) { + for (Owned owned : List.of(owner, account, region)) { + owned.create(owned.versionTemplate("", false), "CREATE_COMPLETE"); + } + String version = owner.version("Version"); + assertEquals(version, account.version("Version")); + assertEquals(version, region.version("Version")); + FunctionConfiguration accountVersion = account.configuration(shared, version); + FunctionConfiguration regionVersion = region.configuration(shared, version); + + owner.delete("DELETE_COMPLETE"); + + owner.assertVersionMissing(shared, version); + assertEquals(accountVersion, account.configuration(shared, version)); + assertEquals(regionVersion, region.configuration(shared, version)); + } + } + + private static int eventIndex(List events, String logical, String status) { + for (int index = 0; index < events.size(); index++) { + StackEvent event = events.get(index); + if (logical.equals(event.logicalResourceId()) && status.equals(event.resourceStatusAsString())) { + return index; + } + } + throw new AssertionError("Missing " + logical + " " + status + ": " + events); + } + + private static Set ids(List events) { + return events.stream().map(StackEvent::eventId).collect(Collectors.toSet()); + } + + private static byte[] zip(String marker) { + try { + ByteArrayOutputStream output = new ByteArrayOutputStream(); + try (ZipOutputStream zip = new ZipOutputStream(output)) { + zip.putNextEntry(new ZipEntry("handler.py")); + zip.write(("def handler(event, context):\n return '" + marker + "'\n") + .getBytes(StandardCharsets.UTF_8)); + zip.closeEntry(); + } + return output.toByteArray(); + } catch (IOException failure) { + throw new UncheckedIOException(failure); + } + } + + private static final class Owned implements AutoCloseable { + private final String marker = "cfn-version-" + UUID.randomUUID().toString().replace("-", ""); + private final Set functions = new LinkedHashSet<>(); + private final Set aliases = new LinkedHashSet<>(); + private final String function; + private CloudFormationClient cfn; + private LambdaClient lambda; + private IamClient iam; + private String roleArn; + private String seedVersion; + private String stackId; + private boolean roleAttempted; + private boolean stackAttempted; + private boolean stackDeleted; + + private Owned(String account, Region region) { + this(account, region, null); + } + + private Owned(String account, Region region, String sharedFunction) { + function = sharedFunction == null ? marker : sharedFunction; + try { + ClientOverrideConfiguration clientConfiguration = ClientOverrideConfiguration.builder() + .apiCallTimeout(Duration.ofSeconds(15)) + .apiCallAttemptTimeout(Duration.ofSeconds(10)) + .retryPolicy(RetryPolicy.none()) + .build(); + cfn = AwsTestClients.create(CloudFormationClient.builder() + .overrideConfiguration(clientConfiguration), account, region); + lambda = AwsTestClients.create(LambdaClient.builder() + .overrideConfiguration(clientConfiguration), account, region); + iam = AwsTestClients.create(IamClient.builder() + .overrideConfiguration(clientConfiguration), account, region); + roleAttempted = true; + roleArn = iam.createRole(request -> request.roleName(marker).assumeRolePolicyDocument(""" + { + "Version": "2012-10-17", + "Statement": [{ + "Effect": "Allow", + "Principal": {"Service": "lambda.amazonaws.com"}, + "Action": "sts:AssumeRole" + }] + } + """)).role().arn(); + functions.add(function); + lambda.createFunction(request -> request.functionName(function).runtime(Runtime.PYTHON3_12) + .handler("handler.handler").role(roleArn) + .code(code -> code.zipFile(SdkBytes.fromByteArray(zip("seed"))))); + awaitFunctionReady(); + seedVersion = lambda.publishVersion(request -> request.functionName(function)).version(); + lambda.updateFunctionCode(request -> request.functionName(function) + .zipFile(SdkBytes.fromByteArray(zip("stack")))); + awaitFunctionReady(); + } catch (RuntimeException | Error failure) { + try { + close(); + } catch (RuntimeException | Error cleanupFailure) { + if (cleanupFailure != failure) { + failure.addSuppressed(cleanupFailure); + } + } + throw failure; + } + } + + private String versionTemplate(String policy, boolean anchor) { + return """ + { + "Resources": { + "Version": { + "Type": "AWS::Lambda::Version", + %s"Properties": {"FunctionName": "%s"} + }%s + } + } + """.formatted(policy, function, anchor ? "," + anchorResource() : ""); + } + + private String anchorResource() { + aliases.add("anchor"); + return """ + "Anchor": { + "Type": "AWS::Lambda::Alias", + "Properties": { + "FunctionName": "%s", + "Name": "anchor", + "FunctionVersion": "%s" + } + } + """.formatted(function, seedVersion); + } + + private String anchorTemplate() { + return "{\"Resources\":{" + anchorResource() + "}}"; + } + + private void alias(String functionName, String name, String version) { + assertEquals(function, functionName); + aliases.add(name); + lambda.createAlias(request -> request.functionName(functionName).name(name).functionVersion(version)); + } + + private FunctionConfiguration configuration(String name, String version) { + return lambda.getFunction(request -> request.functionName(name).qualifier(version)).configuration(); + } + + private void awaitFunctionReady() { + long deadline = System.nanoTime() + Duration.ofSeconds(10).toNanos(); + while (System.nanoTime() < deadline) { + FunctionConfiguration current = configuration(function, "$LATEST"); + assertNotEquals("Failed", current.stateAsString(), current.toString()); + assertNotEquals("Failed", current.lastUpdateStatusAsString(), current.toString()); + if ("Active".equals(current.stateAsString()) + && (current.lastUpdateStatusAsString() == null + || "Successful".equals(current.lastUpdateStatusAsString()))) { + return; + } + pause(); + } + throw new AssertionError("Owned function did not become ready before publishing"); + } + + private Set versions(String name) { + Set result = new HashSet<>(); + Set cursors = new HashSet<>(); + String cursor = null; + for (int page = 0; page < 20; page++) { + String next = cursor; + ListVersionsByFunctionResponse response = lambda.listVersionsByFunction(request -> + request.functionName(name).marker(next)); + for (FunctionConfiguration version : response.versions()) { + assertTrue(result.add(version.version()), "Duplicate version"); + } + cursor = response.nextMarker(); + if (cursor == null) { + return result; + } + assertTrue(cursors.add(cursor), "Repeated version cursor"); + } + throw new AssertionError("Owned version inventory exceeded twenty pages"); + } + + private void assertVersionMissing(String name, String version) { + assertAwsError("ResourceNotFoundException", 404, () -> configuration(name, version)); + assertFalse(versions(name).contains(version)); + } + + private String version(String logical) { + String physical = cfn.describeStackResource(request -> request.stackName(stackId) + .logicalResourceId(logical)).stackResourceDetail().physicalResourceId(); + assertTrue(physical.startsWith("arn:") && physical.contains(":lambda:")); + String version = physical.substring(physical.lastIndexOf(':') + 1); + assertTrue(version.matches("[0-9]+")); + return version; + } + + private List create(String template, String expected) { + stackAttempted = true; + stackId = CfnStackWaits.createStack(cfn, marker, template, expected).stackId(); + return events(); + } + + private List update(String template) { + Set before = ids(events()); + CfnStackWaits.updateStack(cfn, stackId, template, "UPDATE_COMPLETE"); + return eventsAfter(before); + } + + private List delete(String expected) { + Set before = ids(events()); + cfn.deleteStack(request -> request.stackName(stackId)); + CfnStackWaits.awaitStatus(cfn, stackId, expected); + if ("DELETE_COMPLETE".equals(expected)) { + assertEquals("Stack with id " + marker + " does not exist", + assertAwsError("ValidationError", 400, () -> + cfn.describeStacks(request -> request.stackName(marker))) + .awsErrorDetails().errorMessage()); + stackDeleted = true; + } + return eventsAfter(before); + } + + private List events() { + List result = new ArrayList<>(); + Set cursors = new HashSet<>(); + String cursor = null; + for (int page = 0; page < 20; page++) { + String next = cursor; + DescribeStackEventsResponse response = cfn.describeStackEvents(request -> + request.stackName(stackId).nextToken(next)); + result.addAll(response.stackEvents()); + cursor = response.nextToken(); + if (cursor == null) { + assertEquals(result.size(), ids(result).size(), "Duplicate event ids"); + return result; + } + assertTrue(cursors.add(cursor), "Repeated event cursor"); + } + throw new AssertionError("Owned event history exceeded twenty pages"); + } + + private List eventsAfter(Set before) { + return events().stream().filter(event -> !before.contains(event.eventId())).toList(); + } + + private void cleanupStack() { + if (!stackAttempted || stackDeleted) { + return; + } + String lookup = stackId == null ? marker : stackId; + try { + CfnStackWaits.deleteStack(cfn, lookup); + } catch (CloudFormationException missing) { + assertAwsError("ValidationError", 400, missing); + assertEquals("Stack with id " + lookup + " does not exist", missing.awsErrorDetails().errorMessage()); + } + assertEquals("Stack with id " + marker + " does not exist", + assertAwsError("ValidationError", 400, () -> + cfn.describeStacks(request -> request.stackName(marker))).awsErrorDetails().errorMessage()); + stackDeleted = true; + } + + @Override + public void close() { + IllegalStateException failures = new IllegalStateException("Owned Lambda version fixture cleanup failed"); + if (lambda != null) { + for (String alias : aliases) { + cleanup(() -> { + try { + lambda.deleteAlias(request -> request.functionName(function).name(alias)); + } catch (LambdaException missing) { + assertAwsError("ResourceNotFoundException", 404, missing); + } + }, failures); + } + } + if (cfn != null) { + cleanup(this::cleanupStack, failures); + } + if (lambda != null) { + for (String name : functions) { + cleanup(() -> { + try { + lambda.deleteFunction(request -> request.functionName(name)); + } catch (LambdaException missing) { + assertAwsError("ResourceNotFoundException", 404, missing); + } + assertAwsError("ResourceNotFoundException", 404, () -> + lambda.getFunction(request -> request.functionName(name))); + }, failures); + } + } + if (iam != null && roleAttempted) { + cleanup(() -> { + try { + iam.deleteRole(request -> request.roleName(marker)); + } catch (IamException missing) { + assertAwsError("NoSuchEntity", 404, missing); + } + assertAwsError("NoSuchEntity", 404, () -> iam.getRole(request -> request.roleName(marker))); + }, failures); + } + if (cfn != null) { + cleanup(cfn::close, failures); + } + if (lambda != null) { + cleanup(lambda::close, failures); + } + if (iam != null) { + cleanup(iam::close, failures); + } + if (failures.getSuppressed().length > 0) { + throw failures; + } + } + } + + private static void cleanup(Runnable action, IllegalStateException failures) { + try { + action.run(); + } catch (RuntimeException | Error failure) { + if (failure != failures) { + failures.addSuppressed(failure); + } + } + } + + private static void pause() { + try { + Thread.sleep(25); + } catch (InterruptedException failure) { + Thread.currentThread().interrupt(); + throw new IllegalStateException(failure); + } + } + +} diff --git a/src/test/java/io/floci/aws/services/cloudformation/SamTransformIntegrationTest.java b/src/test/java/io/floci/aws/services/cloudformation/SamTransformIntegrationTest.java index 933e6ea5be..dd04f38c6f 100644 --- a/src/test/java/io/floci/aws/services/cloudformation/SamTransformIntegrationTest.java +++ b/src/test/java/io/floci/aws/services/cloudformation/SamTransformIntegrationTest.java @@ -483,9 +483,9 @@ class SamTransformIntegrationTest { .toList(); assertThat(oldVersionStatuses, hasItem("DELETE_COMPLETE")); assertThat(oldVersionStatuses, not(hasItem("DELETE_SKIPPED"))); - // floci reports the version deleted but keeps every version it publishes; AWS deletes it. + // VersionDeletionPolicy: Delete removes the replaced version from the native inventory. given().when().get("/2015-03-31/functions/" + function + "/versions") - .then().statusCode(200).body("Versions.Version", equalTo(List.of("$LATEST", "1", "2"))); + .then().statusCode(200).body("Versions.Version", equalTo(List.of("$LATEST", "2"))); } /** diff --git a/src/test/java/io/floci/aws/services/cloudformation/provisioners/LambdaVersionAliasCfnProvisionerTest.java b/src/test/java/io/floci/aws/services/cloudformation/provisioners/LambdaVersionAliasCfnProvisionerTest.java index b877cf7a10..dadaf37af6 100644 --- a/src/test/java/io/floci/aws/services/cloudformation/provisioners/LambdaVersionAliasCfnProvisionerTest.java +++ b/src/test/java/io/floci/aws/services/cloudformation/provisioners/LambdaVersionAliasCfnProvisionerTest.java @@ -177,10 +177,69 @@ class LambdaVersionAliasCfnProvisionerTest { } @Test - void deleteVersionIsANoOp() { - // Lambda exposes no DeleteVersion; deleting the function drops its versions with it. - provisioner.delete("AWS::Lambda::Version", - "arn:aws:lambda:us-east-1:000000000000:function:my-fn:3", "us-east-1"); + void deleteVersionUsesTheQualifiedNativeDelete() { + String physical = "arn:aws:lambda:us-east-1:000000000000:function:my-fn:3"; + + provisioner.delete("AWS::Lambda::Version", physical, "us-east-1"); + + verify(lambda).deleteFunction("us-east-1", physical, "3"); + } + + @Test + void deleteVersionWhoseParentIsAlreadyAbsentIsIdempotent() { + String physical = "arn:aws:lambda:us-east-1:000000000000:function:my-fn:3"; + Mockito.doThrow(new AwsException("ResourceNotFoundException", "Function not found", 404)) + .when(lambda).deleteFunction("us-east-1", physical, "3"); + + provisioner.delete("AWS::Lambda::Version", physical, "us-east-1"); + + verify(lambda).deleteFunction("us-east-1", physical, "3"); + } + + @Test + void deleteVersionPropagatesAliasConflictAndOtherFailures() { + String physical = "arn:aws:lambda:us-east-1:000000000000:function:my-fn:3"; + for (AwsException expected : new AwsException[] { + new AwsException("ResourceConflictException", "Alias references version", 409), + new AwsException("AccessDeniedException", "Denied", 403), + new AwsException("UnexpectedNotFound", "Unexpected missing response", 404)}) { + Mockito.doThrow(expected).when(lambda).deleteFunction("us-east-1", physical, "3"); + + assertEquals(expected, assertThrows(AwsException.class, () -> + provisioner.delete("AWS::Lambda::Version", physical, "us-east-1"))); + } + } + + @Test + void deleteVersionNeverTurnsAnUnqualifiedOrAliasIdentityIntoAFunctionDelete() { + for (String physical : new String[] { + "LegacyVersion-1a2b3c4d", + "arn:aws:lambda:us-east-1:000000000000:function:my-fn", + "arn:aws:lambda:us-east-1:000000000000:function:my-fn:$LATEST", + "arn:aws:lambda:us-east-1:000000000000:function:my-fn:production"}) { + provisioner.delete("AWS::Lambda::Version", physical, "us-east-1"); + } + verifyNoInteractions(lambda); + } + + @Test + void deleteVersionRejectsAnEmptyQualifierBeforeNativeDeletion() { + AwsException failure = assertThrows(AwsException.class, () -> + provisioner.delete("AWS::Lambda::Version", + "arn:aws:lambda:us-east-1:000000000000:function:my-fn:", "us-east-1")); + + assertEquals("InvalidParameterValueException", failure.getErrorCode()); + assertEquals("Qualifier segment is empty", failure.getMessage()); + verifyNoInteractions(lambda); + } + + @Test + void deleteVersionRejectsAnArnFromAnotherRegionBeforeNativeDeletion() { + AwsException failure = assertThrows(AwsException.class, () -> + provisioner.delete("AWS::Lambda::Version", + "arn:aws:lambda:us-west-2:000000000000:function:my-fn:3", "us-east-1")); + + assertEquals("InvalidParameterValueException", failure.getErrorCode()); verifyNoInteractions(lambda); } diff --git a/src/test/java/io/floci/aws/testing/AwsSdkErrors.java b/src/test/java/io/floci/aws/testing/AwsSdkErrors.java index 21a2d95074..b445033fb0 100644 --- a/src/test/java/io/floci/aws/testing/AwsSdkErrors.java +++ b/src/test/java/io/floci/aws/testing/AwsSdkErrors.java @@ -15,6 +15,11 @@ public final class AwsSdkErrors { /** Asserts that {@code call} fails with the AWS error {@code code} at HTTP {@code status}, and returns it. */ public static AwsServiceException assertAwsError(String code, int status, Executable call) { AwsServiceException error = assertThrows(AwsServiceException.class, call); + return assertAwsError(code, status, error); + } + + /** Checks an already caught SDK error, including errors tolerated during resource cleanup. */ + public static AwsServiceException assertAwsError(String code, int status, AwsServiceException error) { assertEquals(code, error.awsErrorDetails().errorCode()); assertEquals(status, error.statusCode()); return error; diff --git a/src/test/java/io/floci/aws/testing/CfnStackWaits.java b/src/test/java/io/floci/aws/testing/CfnStackWaits.java index 39f2acfa91..851cbbecd7 100644 --- a/src/test/java/io/floci/aws/testing/CfnStackWaits.java +++ b/src/test/java/io/floci/aws/testing/CfnStackWaits.java @@ -5,6 +5,7 @@ import io.restassured.path.xml.element.Node; import io.restassured.response.Response; import io.restassured.specification.RequestSpecification; import software.amazon.awssdk.services.cloudformation.CloudFormationClient; +import software.amazon.awssdk.services.cloudformation.model.DeleteStackRequest; import software.amazon.awssdk.services.cloudformation.model.DescribeStacksRequest; import software.amazon.awssdk.services.cloudformation.model.Stack; @@ -128,13 +129,9 @@ public final class CfnStackWaits { public static Stack awaitStatus(CloudFormationClient client, String stackName, String expectedStatus) { DescribeStacksRequest request = DescribeStacksRequest.builder().stackName(stackName).build(); return await("Stack " + stackName + " to reach " + expectedStatus) - .atMost(Duration.ofSeconds(10)).pollInterval(Duration.ofMillis(50)).until(() -> { - List stacks = client.describeStacks(request).stacks(); - if (stacks.isEmpty()) { - fail("DescribeStacks " + stackName + " returned no stacks"); - } - return stacks.getFirst(); - }, stack -> hasExpectedStatus(stackName, expectedStatus, + .atMost(Duration.ofSeconds(10)).pollInterval(Duration.ofMillis(50)) + .until(() -> describeStack(client, request), + stack -> hasExpectedStatus(stackName, expectedStatus, new StackState(stack.stackStatusAsString(), stack.stackStatusReason() == null ? "" : stack.stackStatusReason()))); } @@ -153,6 +150,29 @@ public final class CfnStackWaits { return awaitStatus(client, stackName, expectedStatus); } + /** Waits for prior work, deletes through the SDK, and follows the retained stack id until deletion completes. */ + public static void deleteStack(CloudFormationClient client, String stackName) { + DescribeStacksRequest request = DescribeStacksRequest.builder().stackName(stackName).build(); + Stack stack = await("Stack " + stackName + " to finish before cleanup") + .atMost(Duration.ofSeconds(10)).pollInterval(Duration.ofMillis(50)) + .until(() -> describeStack(client, request), + current -> TERMINAL.contains(current.stackStatusAsString())); + if ("DELETE_COMPLETE".equals(stack.stackStatusAsString())) { + return; + } + String stackId = stack.stackId(); + client.deleteStack(DeleteStackRequest.builder().stackName(stackId).build()); + awaitStatus(client, stackId, "DELETE_COMPLETE"); + } + + private static Stack describeStack(CloudFormationClient client, DescribeStacksRequest request) { + List stacks = client.describeStacks(request).stacks(); + if (stacks.isEmpty()) { + fail("DescribeStacks " + request.stackName() + " returned no stacks"); + } + return stacks.getFirst(); + } + /** * A resource to append to a template's {@code Resources}, after the one named {@code logicalId}: * an instance that names a launch template that does not exist, so it fails once that resource diff --git a/src/test/java/io/floci/aws/testing/CfnStackWaitsTest.java b/src/test/java/io/floci/aws/testing/CfnStackWaitsTest.java index 140bc8d651..3e142acfae 100644 --- a/src/test/java/io/floci/aws/testing/CfnStackWaitsTest.java +++ b/src/test/java/io/floci/aws/testing/CfnStackWaitsTest.java @@ -6,6 +6,7 @@ import org.junit.jupiter.params.provider.ValueSource; import software.amazon.awssdk.awscore.exception.AwsServiceException; import software.amazon.awssdk.services.cloudformation.CloudFormationClient; import software.amazon.awssdk.services.cloudformation.model.CloudFormationException; +import software.amazon.awssdk.services.cloudformation.model.DeleteStackRequest; import software.amazon.awssdk.services.cloudformation.model.DescribeStacksRequest; import software.amazon.awssdk.services.cloudformation.model.DescribeStacksResponse; import software.amazon.awssdk.services.cloudformation.model.Stack; @@ -78,6 +79,41 @@ class CfnStackWaitsTest { verify(client).describeStacks(REQUEST); } + @Test + void sdkDeleteWaitsForPriorWorkAndFollowsTheRetainedId() { + String stackId = "arn:aws:cloudformation:us-east-1:123456789012:stack/waiter-stack/id"; + DescribeStacksRequest byId = DescribeStacksRequest.builder().stackName(stackId).build(); + when(client.describeStacks(REQUEST)).thenReturn( + response(Stack.builder().stackId(stackId).stackStatus("UPDATE_IN_PROGRESS").build()), + response(Stack.builder().stackId(stackId).stackStatus("UPDATE_COMPLETE").build())); + when(client.describeStacks(byId)).thenReturn( + response(Stack.builder().stackId(stackId).stackStatus("DELETE_IN_PROGRESS").build()), + response(Stack.builder().stackId(stackId).stackStatus("DELETE_COMPLETE").build())); + + CfnStackWaits.deleteStack(client, NAME); + + verify(client, times(2)).describeStacks(REQUEST); + verify(client).deleteStack(DeleteStackRequest.builder().stackName(stackId).build()); + verify(client, times(2)).describeStacks(byId); + } + + @Test + void sdkDeleteReportsTheBackingResourceFailureWithoutPollingAgain() { + String stackId = "arn:aws:cloudformation:us-east-1:123456789012:stack/waiter-stack/id"; + DescribeStacksRequest byId = DescribeStacksRequest.builder().stackName(stackId).build(); + when(client.describeStacks(REQUEST)).thenReturn( + response(Stack.builder().stackId(stackId).stackStatus("CREATE_COMPLETE").build())); + when(client.describeStacks(byId)).thenReturn( + response(Stack.builder().stackId(stackId).stackStatus("DELETE_FAILED") + .stackStatusReason("Version has an alias").build())); + + AssertionError failure = assertThrows(AssertionError.class, () -> CfnStackWaits.deleteStack(client, NAME)); + + assertEquals("Stack " + stackId + " reached DELETE_FAILED instead of DELETE_COMPLETE: Version has an alias", + failure.getMessage()); + verify(client).describeStacks(byId); + } + private static DescribeStacksResponse response(Stack stack) { return DescribeStacksResponse.builder().stacks(stack).build(); }