diff --git a/docs/services/codepipeline.md b/docs/services/codepipeline.md index 1cdd6af52c..cde89209de 100644 --- a/docs/services/codepipeline.md +++ b/docs/services/codepipeline.md @@ -39,6 +39,14 @@ A retried execution counts toward the `QUEUED`/`PARALLEL` active-execution limit the same execution while it is still running, including while sibling actions of a failed action finish, returns `ConflictException`. +S3 source actions resolve `sourceRevisions` from the object actually consumed by the execution. +Unversioned objects report their ETag as the revision ID; versioned objects report the version ID. +`StartPipelineExecution.sourceRevisions` is treated as an override, including supported S3 object-key +and version-ID overrides, rather than being copied directly into execution history. +Resolved revisions appear in `ListPipelineExecutions` summaries, as on AWS. `RollbackStage` reruns +with the object key and version ID the target execution consumed, so rolling back redeploys that +version rather than the latest upload. + The following providers execute against local Floci services: | Category | Provider | Behavior | diff --git a/src/main/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineService.java b/src/main/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineService.java index 94d7f58f2e..c3b5139a6e 100644 --- a/src/main/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineService.java +++ b/src/main/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineService.java @@ -34,6 +34,7 @@ import java.nio.charset.StandardCharsets; import java.time.Instant; import java.util.ArrayList; import java.util.Comparator; +import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -466,7 +467,8 @@ public class CodePipelineService { execution.setStatusSummary("Pipeline execution started."); execution.setStartTime(now()); execution.setLastUpdateTime(execution.getStartTime()); - execution.setSourceRevisions(objectList(request.path("sourceRevisions"))); + execution.setSourceRevisions(new ArrayList<>()); + execution.setSourceRevisionOverrides(sourceRevisionOverrides(request, pipeline)); execution.setVariables(variableList(request.path("variables"))); Map trigger = new LinkedHashMap<>(); trigger.put("triggerType", triggerType); @@ -832,7 +834,9 @@ public class CodePipelineService { private ObjectNode startExecutionFrom(CodePipelineExecution source, String region, String account) { ObjectNode start = mapper.createObjectNode(); start.put("name", source.getPipelineName()); - start.set("sourceRevisions", mapper.valueToTree(source.getSourceRevisions())); + synchronized (source) { + start.set("sourceRevisions", mapper.valueToTree(source.getSourceRevisionOverrides())); + } ArrayNode vars = start.putArray("variables"); source.getVariables().forEach(v -> vars.addObject() .put("name", v.get("name")) @@ -1316,20 +1320,28 @@ public class CodePipelineService { JsonNode config = action.path("configuration"); if ("Source".equals(state.getCategory())) { String bucket = config.path("S3Bucket").asText(config.path("BucketName").asText(null)); - String key = config.path("S3ObjectKey").asText(config.path("ObjectKey").asText(null)); - S3Object object = s3Service.getObject(bucket, key); + String key = effectiveS3SourceKey(execution, state.getActionName(), config); + String versionId = sourceRevisionOverride( + execution, state.getActionName(), "S3_OBJECT_VERSION_ID"); + S3Object object = s3Service.getObject(bucket, key, versionId); for (JsonNode artifact : action.path("outputArtifacts")) { runtimeArtifacts.put(artifactKey(execution, artifact.path("name").asText()), object.getData()); } - Map revision = new LinkedHashMap<>(); - revision.put("name", state.getActionName()); - revision.put("revisionId", object.getVersionId() != null ? object.getVersionId() : object.getETag()); - revision.put("revisionChangeIdentifier", object.getETag()); - revision.put("revisionSummary", bucket + "/" + key); - revision.put("revisionUrl", "s3://" + bucket + "/" + key); - revision.put("created", object.getLastModified().toEpochMilli() / 1000.0); - execution.getArtifactRevisions().add(revision); - state.setExternalExecutionId(revision.get("revisionId").toString()); + String revisionId = object.getVersionId() != null + ? object.getVersionId() : unquoteETag(object.getETag()); + synchronized (execution) { + Map revision = new LinkedHashMap<>(); + revision.put("name", state.getActionName()); + revision.put("revisionId", revisionId); + revision.put("revisionChangeIdentifier", unquoteETag(object.getETag())); + revision.put("revisionSummary", bucket + "/" + key); + revision.put("revisionUrl", "s3://" + bucket + "/" + key); + revision.put("created", object.getLastModified().toEpochMilli() / 1000.0); + execution.getArtifactRevisions().add(revision); + upsertSourceRevision(execution, state.getActionName(), revisionId, bucket, key, object); + pinResolvedS3Version(execution, state.getActionName(), object.getVersionId()); + } + state.setExternalExecutionId(revisionId); return; } String bucket = config.path("BucketName").asText(config.path("S3Bucket").asText(null)); @@ -1762,10 +1774,14 @@ public class CodePipelineService { } private ObjectNode executionNode(CodePipelineExecution execution) { - ObjectNode node = mapper.valueToTree(execution); + ObjectNode node; + synchronized (execution) { + node = mapper.valueToTree(execution); + } node.remove(List.of("accountId", "region", "startTime", "lastUpdateTime", - "sourceRevisions", "actionExecutions", "currentStage", "stopRequested", "abandon", - "rollbackTargetPipelineExecutionId", "stageExecutionStatuses", "artifactsReleased")); + "sourceRevisions", "sourceRevisionOverrides", "actionExecutions", "currentStage", + "stopRequested", "abandon", "rollbackTargetPipelineExecutionId", "stageExecutionStatuses", + "artifactsReleased")); if (execution.getRollbackTargetPipelineExecutionId() != null) { node.putObject("rollbackMetadata").put( "rollbackTargetPipelineExecutionId", execution.getRollbackTargetPipelineExecutionId()); @@ -1782,7 +1798,9 @@ public class CodePipelineService { putIfNotNull(node, "lastUpdateTime", execution.getLastUpdateTime()); node.put("executionMode", execution.getExecutionMode()); node.put("executionType", execution.getExecutionType()); - node.set("sourceRevisions", mapper.valueToTree(execution.getSourceRevisions())); + synchronized (execution) { + node.set("sourceRevisions", mapper.valueToTree(execution.getSourceRevisions())); + } node.set("trigger", mapper.valueToTree(execution.getTrigger())); if (execution.getRollbackTargetPipelineExecutionId() != null) { node.putObject("rollbackMetadata").put( @@ -1875,11 +1893,107 @@ public class CodePipelineService { return "Failed"; } - private List> objectList(JsonNode node) { - if (node == null || !node.isArray()) { + private List> sourceRevisionOverrides(JsonNode request, CodePipelinePipeline pipeline) { + JsonNode overridesNode = request.get("sourceRevisions"); + if (overridesNode == null || overridesNode.isNull()) { return new ArrayList<>(); } - return mapper.convertValue(node, new TypeReference>>() {}); + if (!overridesNode.isArray()) { + throw new AwsException("ValidationException", "sourceRevisions must be an array", 400); + } + List> overrides = new ArrayList<>(); + Set seenOverrides = new HashSet<>(); + for (JsonNode override : overridesNode) { + if (!override.isObject()) { + throw new AwsException("ValidationException", "sourceRevisions entries must be objects", 400); + } + requireOnlyMembers(override, "sourceRevisions entry", + Set.of("actionName", "revisionType", "revisionValue")); + String actionName = text(override, "actionName"); + String revisionType = text(override, "revisionType"); + String revisionValue = text(override, "revisionValue"); + if (!seenOverrides.add(actionName + "\0" + revisionType)) { + throw new AwsException("ValidationException", + "Duplicate " + revisionType + " override for source action " + actionName, 400); + } + JsonNode action = sourceActionByName(pipeline, actionName); + String provider = action.path("actionTypeId").path("provider").asText(); + if (!"S3".equals(provider)) { + throw new AwsException("ValidationException", + "Source revision overrides are not supported for provider " + provider, 400); + } + if (!List.of("S3_OBJECT_KEY", "S3_OBJECT_VERSION_ID").contains(revisionType)) { + throw new AwsException("ValidationException", + "Invalid S3 source revision type: " + revisionType, 400); + } + if ("S3_OBJECT_KEY".equals(revisionType) + && !Boolean.parseBoolean(action.path("configuration") + .path("AllowOverrideForS3ObjectKey").asText("false"))) { + throw new AwsException("ValidationException", + "S3_OBJECT_KEY override requires AllowOverrideForS3ObjectKey=true", 400); + } + Map value = new LinkedHashMap<>(); + value.put("actionName", actionName); + value.put("revisionType", revisionType); + value.put("revisionValue", revisionValue); + overrides.add(value); + } + return overrides; + } + + private JsonNode sourceActionByName(CodePipelinePipeline pipeline, String actionName) { + for (JsonNode stage : pipeline.getDeclaration().path("stages")) { + for (JsonNode action : stage.path("actions")) { + if ("Source".equals(action.path("actionTypeId").path("category").asText()) + && actionName.equals(action.path("name").asText())) { + return action; + } + } + } + throw new AwsException("ValidationException", "Source action not found: " + actionName, 400); + } + + private String effectiveS3SourceKey(CodePipelineExecution execution, String actionName, JsonNode config) { + String override = sourceRevisionOverride(execution, actionName, "S3_OBJECT_KEY"); + return override != null + ? override : config.path("S3ObjectKey").asText(config.path("ObjectKey").asText(null)); + } + + private String sourceRevisionOverride(CodePipelineExecution execution, String actionName, String revisionType) { + synchronized (execution) { + for (Map override : execution.getSourceRevisionOverrides()) { + if (actionName.equals(override.get("actionName")) + && revisionType.equals(override.get("revisionType"))) { + return Objects.toString(override.get("revisionValue"), null); + } + } + return null; + } + } + + private void pinResolvedS3Version(CodePipelineExecution execution, String actionName, String versionId) { + if (versionId == null || sourceRevisionOverride(execution, actionName, "S3_OBJECT_VERSION_ID") != null) { + return; + } + Map pin = new LinkedHashMap<>(); + pin.put("actionName", actionName); + pin.put("revisionType", "S3_OBJECT_VERSION_ID"); + pin.put("revisionValue", versionId); + execution.getSourceRevisionOverrides().add(pin); + } + + private void upsertSourceRevision(CodePipelineExecution execution, String actionName, String revisionId, + String bucket, String key, S3Object object) { + Map sourceRevision = new LinkedHashMap<>(); + sourceRevision.put("actionName", actionName); + sourceRevision.put("revisionId", revisionId); + String summary = object.getMetadata().get("codepipeline-artifact-revision-summary"); + if (summary != null && !summary.isBlank()) { + sourceRevision.put("revisionSummary", summary); + } + sourceRevision.put("revisionUrl", "s3://" + bucket + "/" + key); + execution.getSourceRevisions().removeIf(existing -> actionName.equals(existing.get("actionName"))); + execution.getSourceRevisions().add(sourceRevision); } private List> variableList(JsonNode node) { diff --git a/src/main/java/io/github/hectorvent/floci/services/codepipeline/model/CodePipelineExecution.java b/src/main/java/io/github/hectorvent/floci/services/codepipeline/model/CodePipelineExecution.java index 8809b37c57..2978731eaa 100644 --- a/src/main/java/io/github/hectorvent/floci/services/codepipeline/model/CodePipelineExecution.java +++ b/src/main/java/io/github/hectorvent/floci/services/codepipeline/model/CodePipelineExecution.java @@ -27,6 +27,7 @@ public class CodePipelineExecution { private Double lastUpdateTime; private List> artifactRevisions = new ArrayList<>(); private List> sourceRevisions = new ArrayList<>(); + private List> sourceRevisionOverrides = new ArrayList<>(); private List> variables = new ArrayList<>(); private Map trigger = new LinkedHashMap<>(); private List actionExecutions = new ArrayList<>(); @@ -141,6 +142,14 @@ public class CodePipelineExecution { this.sourceRevisions = sourceRevisions; } + public List> getSourceRevisionOverrides() { + return sourceRevisionOverrides; + } + + public void setSourceRevisionOverrides(List> sourceRevisionOverrides) { + this.sourceRevisionOverrides = sourceRevisionOverrides == null ? new ArrayList<>() : sourceRevisionOverrides; + } + public List> getVariables() { return variables; } diff --git a/src/test/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineIntegrationTest.java index 0782fe9a23..63435ff68a 100644 --- a/src/test/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/codepipeline/CodePipelineIntegrationTest.java @@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.codepipeline; import io.github.hectorvent.floci.testing.RestAssuredJsonUtils; import io.quarkus.test.junit.QuarkusTest; import io.restassured.response.Response; +import io.restassured.specification.RequestSpecification; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -175,7 +176,8 @@ class CodePipelineIntegrationTest { void pipelineLifecycleExecutesS3SourceAndDeployActions() throws Exception { createBucket("codepipeline-source"); createBucket("codepipeline-destination"); - putObject("codepipeline-source", "source.zip", "pipeline artifact"); + String sourceETag = putObject( + "codepipeline-source", "source.zip", "pipeline artifact", "release candidate"); String pipelineName = "s3-copy-pipeline"; post("CreatePipeline", pipeline(pipelineName, """ @@ -246,7 +248,9 @@ class CodePipelineIntegrationTest { .body("pipelineExecution.pipelineName", equalTo(pipelineName)) .body("pipelineExecution.status", equalTo("Succeeded")) .body("pipelineExecution.artifactRevisions", hasSize(1)) - .body("pipelineExecution.artifactRevisions[0].name", equalTo("SourceObject")); + .body("pipelineExecution.artifactRevisions[0].name", equalTo("SourceObject")) + .body("pipelineExecution.sourceRevisions", nullValue()) + .body("pipelineExecution.sourceRevisionOverrides", nullValue()); post("ListPipelineExecutions", """ {"pipelineName": "%s"} @@ -255,7 +259,16 @@ class CodePipelineIntegrationTest { .statusCode(200) .body("pipelineExecutionSummaries", hasSize(1)) .body("pipelineExecutionSummaries[0].pipelineExecutionId", equalTo(executionId)) - .body("pipelineExecutionSummaries[0].status", equalTo("Succeeded")); + .body("pipelineExecutionSummaries[0].status", equalTo("Succeeded")) + .body("pipelineExecutionSummaries[0].sourceRevisions", hasSize(1)) + .body("pipelineExecutionSummaries[0].sourceRevisions[0].actionName", equalTo("SourceObject")) + .body("pipelineExecutionSummaries[0].sourceRevisions[0].revisionId", + equalTo(unquote(sourceETag))) + .body("pipelineExecutionSummaries[0].sourceRevisions[0].revisionSummary", + equalTo("release candidate")) + .body("pipelineExecutionSummaries[0].sourceRevisions[0].revisionUrl", + equalTo("s3://codepipeline-source/source.zip")) + .body("pipelineExecutionSummaries[0].sourceRevisionOverrides", nullValue()); post("ListPipelineExecutions", """ { @@ -1143,6 +1156,290 @@ class CodePipelineIntegrationTest { """.formatted(pipelineName)).then().statusCode(200); } + @Test + void startPipelineExecutionS3OverridesSelectEffectiveKeyAndVersion() throws Exception { + createBucket("codepipeline-source-override"); + createBucket("codepipeline-override-destination"); + given() + .body("Enabled") + .when() + .put("/codepipeline-source-override?versioning") + .then() + .statusCode(200); + + putObject("codepipeline-source-override", "source.zip", "default artifact"); + String oldVersion = given() + .contentType("application/octet-stream") + .body("selected old version".getBytes(java.nio.charset.StandardCharsets.UTF_8)) + .when() + .put("/codepipeline-source-override/alternate.zip") + .then() + .statusCode(200) + .header("x-amz-version-id", notNullValue()) + .extract().header("x-amz-version-id"); + putObject("codepipeline-source-override", "alternate.zip", "newer version"); + + String pipelineName = "source-revision-override-pipeline"; + post("CreatePipeline", pipeline(pipelineName, """ + { + "name": "Source", + "actions": [{ + "name": "SourceObject", + "actionTypeId": { + "category": "Source", + "owner": "AWS", + "provider": "S3", + "version": "1" + }, + "configuration": { + "S3Bucket": "codepipeline-source-override", + "S3ObjectKey": "source.zip", + "AllowOverrideForS3ObjectKey": "true" + }, + "outputArtifacts": [{"name": "SourceOutput"}] + }] + }, + { + "name": "Deploy", + "actions": [{ + "name": "DeployObject", + "actionTypeId": { + "category": "Deploy", + "owner": "AWS", + "provider": "S3", + "version": "1" + }, + "configuration": { + "BucketName": "codepipeline-override-destination", + "ObjectKey": "deployed.zip" + }, + "inputArtifacts": [{"name": "SourceOutput"}] + }] + } + """)) + .then().statusCode(200); + + String executionId = post("StartPipelineExecution", """ + { + "name": "%s", + "sourceRevisions": [ + { + "actionName": "SourceObject", + "revisionType": "S3_OBJECT_KEY", + "revisionValue": "alternate.zip" + }, + { + "actionName": "SourceObject", + "revisionType": "S3_OBJECT_VERSION_ID", + "revisionValue": "%s" + } + ] + } + """.formatted(pipelineName, oldVersion)) + .then() + .statusCode(200) + .extract().path("pipelineExecutionId"); + + waitForExecution(pipelineName, executionId, "Succeeded"); + + given() + .get("/codepipeline-override-destination/deployed.zip") + .then() + .statusCode(200) + .body(equalTo("selected old version")); + + post("ListPipelineExecutions", """ + {"pipelineName": "%s"} + """.formatted(pipelineName)) + .then() + .statusCode(200) + .body("pipelineExecutionSummaries[0].pipelineExecutionId", equalTo(executionId)) + .body("pipelineExecutionSummaries[0].sourceRevisions", hasSize(1)) + .body("pipelineExecutionSummaries[0].sourceRevisions[0].actionName", equalTo("SourceObject")) + .body("pipelineExecutionSummaries[0].sourceRevisions[0].revisionId", equalTo(oldVersion)) + .body("pipelineExecutionSummaries[0].sourceRevisions[0].revisionUrl", + equalTo("s3://codepipeline-source-override/alternate.zip")); + + post("DeletePipeline", """ + {"name": "%s"} + """.formatted(pipelineName)).then().statusCode(200); + } + + @Test + void rollbackStageRedeploysTargetExecutionSourceVersion() throws Exception { + createBucket("codepipeline-rollback-source"); + createBucket("codepipeline-rollback-destination"); + given() + .body("Enabled") + .when() + .put("/codepipeline-rollback-source?versioning") + .then() + .statusCode(200); + putObject("codepipeline-rollback-source", "source.zip", "first release"); + + String pipelineName = "rollback-source-version-pipeline"; + post("CreatePipeline", pipeline(pipelineName, """ + { + "name": "Source", + "actions": [{ + "name": "SourceObject", + "actionTypeId": { + "category": "Source", + "owner": "AWS", + "provider": "S3", + "version": "1" + }, + "configuration": { + "S3Bucket": "codepipeline-rollback-source", + "S3ObjectKey": "source.zip", + "PollForSourceChanges": "false" + }, + "outputArtifacts": [{"name": "SourceOutput"}] + }] + }, + { + "name": "Deploy", + "actions": [{ + "name": "DeployObject", + "actionTypeId": { + "category": "Deploy", + "owner": "AWS", + "provider": "S3", + "version": "1" + }, + "configuration": { + "BucketName": "codepipeline-rollback-destination", + "ObjectKey": "deployed.zip" + }, + "inputArtifacts": [{"name": "SourceOutput"}] + }] + } + """)) + .then().statusCode(200); + + String firstExecutionId = post("StartPipelineExecution", """ + {"name": "%s"} + """.formatted(pipelineName)) + .then() + .statusCode(200) + .extract().path("pipelineExecutionId"); + waitForExecution(pipelineName, firstExecutionId, "Succeeded"); + + putObject("codepipeline-rollback-source", "source.zip", "second release"); + String secondExecutionId = post("StartPipelineExecution", """ + {"name": "%s"} + """.formatted(pipelineName)) + .then() + .statusCode(200) + .extract().path("pipelineExecutionId"); + waitForExecution(pipelineName, secondExecutionId, "Succeeded"); + + String rollbackExecutionId = post("RollbackStage", """ + { + "pipelineName": "%s", + "stageName": "Deploy", + "targetPipelineExecutionId": "%s" + } + """.formatted(pipelineName, firstExecutionId)) + .then() + .statusCode(200) + .extract().path("pipelineExecutionId"); + waitForExecution(pipelineName, rollbackExecutionId, "Succeeded"); + + given() + .get("/codepipeline-rollback-destination/deployed.zip") + .then() + .statusCode(200) + .body(equalTo("first release")); + + post("DeletePipeline", """ + {"name": "%s"} + """.formatted(pipelineName)).then().statusCode(200); + } + + @Test + void startPipelineExecutionRejectsS3KeyOverrideUnlessActionAllowsIt() { + String pipelineName = "source-revision-key-override-validation"; + post("CreatePipeline", pipeline(pipelineName, """ + { + "name": "Source", + "actions": [{ + "name": "SourceObject", + "actionTypeId": { + "category": "Source", + "owner": "AWS", + "provider": "S3", + "version": "1" + }, + "configuration": { + "S3Bucket": "codepipeline-source", + "S3ObjectKey": "source.zip" + }, + "outputArtifacts": [{"name": "SourceOutput"}] + }] + }, + { + "name": "Deploy", + "actions": [{ + "name": "DeployObject", + "actionTypeId": { + "category": "Deploy", + "owner": "AWS", + "provider": "S3", + "version": "1" + }, + "configuration": { + "BucketName": "codepipeline-destination", + "ObjectKey": "deployed.zip" + }, + "inputArtifacts": [{"name": "SourceOutput"}] + }] + } + """)) + .then().statusCode(200); + + post("StartPipelineExecution", """ + { + "name": "%s", + "sourceRevisions": [{ + "actionName": "SourceObject", + "revisionType": "S3_OBJECT_KEY", + "revisionValue": "alternate.zip" + }] + } + """.formatted(pipelineName)) + .then() + .statusCode(400) + .body("__type", containsString("ValidationException")) + .body("message", containsString("AllowOverrideForS3ObjectKey")); + + post("StartPipelineExecution", """ + { + "name": "%s", + "sourceRevisions": [ + { + "actionName": "SourceObject", + "revisionType": "S3_OBJECT_VERSION_ID", + "revisionValue": "v1" + }, + { + "actionName": "SourceObject", + "revisionType": "S3_OBJECT_VERSION_ID", + "revisionValue": "v2" + } + ] + } + """.formatted(pipelineName)) + .then() + .statusCode(400) + .body("__type", containsString("ValidationException")) + .body("message", containsString("Duplicate S3_OBJECT_VERSION_ID")); + + post("DeletePipeline", """ + {"name": "%s"} + """.formatted(pipelineName)).then().statusCode(200); + } + @Test void listPipelineExecutionsValidatesFilterAndMaxResults() { String pipelineName = "list-executions-validation-pipeline"; @@ -2478,13 +2775,27 @@ class CodePipelineIntegrationTest { given().when().put("/" + bucket).then().statusCode(200); } - private static void putObject(String bucket, String key, String body) { - given() + private static String putObject(String bucket, String key, String body) { + return putObject(bucket, key, body, null); + } + + private static String putObject(String bucket, String key, String body, String revisionSummary) { + RequestSpecification request = given() .contentType("application/octet-stream") - .body(body.getBytes(java.nio.charset.StandardCharsets.UTF_8)) + .body(body.getBytes(java.nio.charset.StandardCharsets.UTF_8)); + if (revisionSummary != null) { + request.header("x-amz-meta-codepipeline-artifact-revision-summary", revisionSummary); + } + return request .when() .put("/" + bucket + "/" + key) .then() - .statusCode(200); + .statusCode(200) + .header("ETag", notNullValue()) + .extract().header("ETag"); + } + + private static String unquote(String value) { + return value == null ? null : value.replace("\"", ""); } }