diff --git a/docs/services/codepipeline.md b/docs/services/codepipeline.md index 5e6c10177e..bc19c773c6 100644 --- a/docs/services/codepipeline.md +++ b/docs/services/codepipeline.md @@ -39,6 +39,14 @@ The following providers execute against local Floci services: AWS-managed providers without a corresponding Floci execution adapter fail the action with an AWS-shaped action error. Floci does not call real AWS accounts or third-party SaaS providers. +## Approvals + +`PutApprovalResult` completes waiting Manual approval actions with AWS-shaped validation +and error responses. Floci validates the stage and action names, enforces `result.status` +as `Approved` or `Rejected`, limits `result.summary` to 512 characters, returns +`InvalidApprovalTokenException` for unknown tokens, and returns +`ApprovalAlreadyCompletedException` if the same approval token is reused after completion. + ## Configuration | Variable | Default | Description | 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 bce62efef2..19b1467e16 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 @@ -39,6 +39,7 @@ import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Optional; +import java.util.Set; import java.util.UUID; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentHashMap; @@ -322,12 +323,40 @@ public class CodePipelineService { String pipelineName = text(request, "pipelineName"); requirePipeline(account, region, pipelineName); List executions = executions(account, region, pipelineName); - JsonNode filter = request.path("filter"); - if (filter.hasNonNull("succeededInStage")) { - String stage = filter.path("succeededInStage").asText(); + JsonNode filter = request.get("filter"); + if (filter != null && !filter.isNull() && !filter.isObject()) { + throw new AwsException("ValidationException", "filter must be an object", 400); + } + if (filter != null && filter.isObject()) { + requireOnlyMembers(filter, "filter", Set.of("succeededInStage")); + } + JsonNode succeededInStage = filter == null ? null : filter.get("succeededInStage"); + if (succeededInStage != null && !succeededInStage.isNull()) { + if (!succeededInStage.isObject()) { + throw new AwsException("ValidationException", + "succeededInStage requires stageName", 400); + } + requireOnlyMembers(succeededInStage, "succeededInStage", Set.of("stageName")); + JsonNode stageNameNode = succeededInStage.get("stageName"); + if (stageNameNode == null || stageNameNode.isNull() || !stageNameNode.isTextual() + || stageNameNode.asText().isBlank()) { + throw new AwsException("ValidationException", + "succeededInStage requires stageName", 400); + } + String stage = stageNameNode.asText(); executions = executions.stream().filter(e -> actionExecutionsForStage(e, stage).stream() .allMatch(a -> "Succeeded".equals(a.getStatus()))).toList(); } + if (request.hasNonNull("maxResults")) { + JsonNode maxResultsNode = request.get("maxResults"); + if (!maxResultsNode.isIntegralNumber()) { + throw new AwsException("ValidationException", "maxResults must be an integer", 400); + } + int maxResults = maxResultsNode.asInt(); + if (maxResults < 1 || maxResults > 100) { + throw new AwsException("ValidationException", "maxResults must be between 1 and 100", 400); + } + } Page page = page(request, executions.size(), 100); ObjectNode response = mapper.createObjectNode(); ArrayNode summaries = response.putArray("pipelineExecutionSummaries"); @@ -414,23 +443,60 @@ public class CodePipelineService { String stageName = text(request, "stageName"); String actionName = text(request, "actionName"); String token = text(request, "token"); - String status = request.path("result").path("status").asText(); + JsonNode resultNode = request.get("result"); + if (resultNode == null || resultNode.isNull()) { + throw new AwsException("ValidationException", "result is required", 400); + } + if (!resultNode.isObject()) { + throw new AwsException("ValidationException", "result must be an object", 400); + } + String status = text(resultNode, "status"); + if (!List.of("Approved", "Rejected").contains(status)) { + throw new AwsException("ValidationException", + "result.status must be one of [Approved, Rejected]", 400); + } + JsonNode summaryNode = resultNode.get("summary"); + String summary = ""; + if (summaryNode != null && !summaryNode.isNull()) { + if (!summaryNode.isTextual()) { + throw new AwsException("ValidationException", "result.summary must be a string", 400); + } + summary = summaryNode.asText(); + } + if (summary.length() > 512) { + throw new AwsException("ValidationException", + "result.summary must not exceed 512 characters", 400); + } + + CodePipelinePipeline pipeline = requirePipeline(account, region, pipelineName); + CodePipelineExecution execution = executions(account, region, pipelineName).stream() .filter(e -> e.getActionExecutions().stream() .anyMatch(a -> stageName.equals(a.getStageName()) && actionName.equals(a.getActionName()) - && token.equals(a.getToken()) - && "InProgress".equals(a.getStatus()))) + && token.equals(a.getToken()))) .findFirst() - .orElseThrow(() -> new AwsException( - "InvalidApprovalTokenException", "Approval token is invalid", 400)); - ActionExecution approval = execution.getActionExecutions().stream() + .orElse(null); + + ActionExecution approval = execution == null ? null : execution.getActionExecutions().stream() .filter(a -> stageName.equals(a.getStageName()) && actionName.equals(a.getActionName())) - .filter(a -> token.equals(a.getToken()) && "InProgress".equals(a.getStatus())) + .filter(a -> token.equals(a.getToken())) .findFirst() - .orElseThrow(); + .orElse(null); + + if (approval == null) { + requireStage(pipeline, stageName); + requireAction(pipeline, stageName, actionName); + throw new AwsException("InvalidApprovalTokenException", "Approval token is invalid", 400); + } + + if (!"InProgress".equals(approval.getStatus())) { + throw new AwsException("ApprovalAlreadyCompletedException", + "The approval action has already been approved or rejected.", 400); + } + approval.setStatus("Approved".equals(status) ? "Succeeded" : "Failed"); - approval.setSummary(request.path("result").path("summary").asText(status)); + approval.setSummary(summary); approval.setLastUpdateTime(now()); putExecution(execution); return mapper.createObjectNode().put("approvedAt", now()); @@ -1127,6 +1193,14 @@ public class CodePipelineService { } } + private static void requireOnlyMembers(JsonNode node, String label, Set allowed) { + node.fieldNames().forEachRemaining(field -> { + if (!allowed.contains(field)) { + throw new AwsException("ValidationException", "Unknown " + label + " member: " + field, 400); + } + }); + } + private void requireStage(CodePipelinePipeline pipeline, String stageName) { for (JsonNode stage : pipeline.getDeclaration().path("stages")) { if (stageName.equals(stage.path("name").asText())) { @@ -1136,6 +1210,20 @@ public class CodePipelineService { throw new AwsException("StageNotFoundException", "Stage not found: " + stageName, 400); } + private void requireAction(CodePipelinePipeline pipeline, String stageName, String actionName) { + for (JsonNode stage : pipeline.getDeclaration().path("stages")) { + if (stageName.equals(stage.path("name").asText())) { + for (JsonNode action : stage.path("actions")) { + if (actionName.equals(action.path("name").asText())) { + return; + } + } + throw new AwsException("ActionNotFoundException", "Action not found: " + actionName, 400); + } + } + throw new AwsException("StageNotFoundException", "Stage not found: " + stageName, 400); + } + private ObjectNode executionNode(CodePipelineExecution execution) { ObjectNode node = mapper.valueToTree(execution); node.remove(List.of("accountId", "region", "startTime", "lastUpdateTime", 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 a92b60565f..2e2e12cd7f 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 @@ -27,6 +27,139 @@ class CodePipelineIntegrationTest { RestAssuredJsonUtils.configureAwsContentTypes(); } + @Test + void getPipelineReturnsCurrentStructureAndMetadata() { + String pipelineName = "get-pipeline-it"; + post("CreatePipeline", pipeline(pipelineName, """ + { + "name": "Source", + "actions": [{ + "name": "SourceAction", + "actionTypeId": { + "category": "Source", + "owner": "AWS", + "provider": "S3", + "version": "1" + }, + "runOrder": 1 + }] + }, + { + "name": "Build", + "actions": [{ + "name": "BuildAction", + "actionTypeId": { + "category": "Build", + "owner": "AWS", + "provider": "CodeBuild", + "version": "1" + }, + "runOrder": 1 + }] + } + """)).then().statusCode(200); + + post("GetPipeline", """ + {"name": "%s"} + """.formatted(pipelineName)) + .then() + .statusCode(200) + .body("pipeline.name", equalTo(pipelineName)) + .body("pipeline.version", equalTo(1)) + .body("pipeline.pipelineType", equalTo("V1")) + .body("pipeline.executionMode", equalTo("SUPERSEDED")) + .body("metadata.pipelineArn", equalTo("arn:aws:codepipeline:us-east-1:000000000000:" + pipelineName)) + .body("metadata.created", notNullValue()) + .body("metadata.updated", notNullValue()); + + post("GetPipeline", """ + {"name": "%s", "version": 2} + """.formatted(pipelineName)) + .then() + .statusCode(400) + .body("__type", containsString("PipelineVersionNotFoundException")); + + post("DeletePipeline", """ + {"name": "%s"} + """.formatted(pipelineName)).then().statusCode(200); + } + + @Test + void updatePipelineReplacesStructureAndIncrementsVersion() { + String pipelineName = "update-pipeline-it"; + post("CreatePipeline", pipeline(pipelineName, """ + { + "name": "Source", + "actions": [{ + "name": "SourceAction", + "actionTypeId": { + "category": "Source", + "owner": "AWS", + "provider": "S3", + "version": "1" + } + }] + }, + { + "name": "Build", + "actions": [{ + "name": "BuildAction", + "actionTypeId": { + "category": "Build", + "owner": "AWS", + "provider": "CodeBuild", + "version": "1" + } + }] + } + """)).then().statusCode(200); + + post("UpdatePipeline", pipeline(pipelineName, """ + { + "name": "Source", + "actions": [{ + "name": "SourceActionV2", + "actionTypeId": { + "category": "Source", + "owner": "AWS", + "provider": "S3", + "version": "1" + } + }] + }, + { + "name": "Deploy", + "actions": [{ + "name": "DeployAction", + "actionTypeId": { + "category": "Deploy", + "owner": "AWS", + "provider": "CodeDeploy", + "version": "1" + } + }] + } + """)) + .then() + .statusCode(200) + .body("pipeline.name", equalTo(pipelineName)) + .body("pipeline.version", equalTo(2)) + .body("pipeline.stages[1].name", equalTo("Deploy")); + + post("GetPipeline", """ + {"name": "%s"} + """.formatted(pipelineName)) + .then() + .statusCode(200) + .body("pipeline.version", equalTo(2)) + .body("pipeline.stages[0].actions[0].name", equalTo("SourceActionV2")) + .body("pipeline.stages[1].name", equalTo("Deploy")); + + post("DeletePipeline", """ + {"name": "%s"} + """.formatted(pipelineName)).then().statusCode(200); + } + @Test void pipelineLifecycleExecutesS3SourceAndDeployActions() throws Exception { createBucket("codepipeline-source"); @@ -104,6 +237,25 @@ class CodePipelineIntegrationTest { .body("pipelineExecution.artifactRevisions", hasSize(1)) .body("pipelineExecution.artifactRevisions[0].name", equalTo("SourceObject")); + post("ListPipelineExecutions", """ + {"pipelineName": "%s"} + """.formatted(pipelineName)) + .then() + .statusCode(200) + .body("pipelineExecutionSummaries", hasSize(1)) + .body("pipelineExecutionSummaries[0].pipelineExecutionId", equalTo(executionId)) + .body("pipelineExecutionSummaries[0].status", equalTo("Succeeded")); + + post("ListPipelineExecutions", """ + { + "pipelineName": "%s", + "filter": {"succeededInStage": {"stageName": "Deploy"}} + } + """.formatted(pipelineName)) + .then() + .statusCode(200) + .body("pipelineExecutionSummaries", hasSize(1)); + post("GetPipelineState", """ {"name": "%s"} """.formatted(pipelineName)) @@ -169,6 +321,227 @@ class CodePipelineIntegrationTest { """.formatted(pipelineName)).then().statusCode(200); } + @Test + void listPipelineExecutionsValidatesFilterAndMaxResults() { + String pipelineName = "list-executions-validation-pipeline"; + post("CreatePipeline", pipeline(pipelineName, """ + { + "name": "Source", + "actions": [{ + "name": "SourceAction", + "actionTypeId": { + "category": "Source", + "owner": "AWS", + "provider": "S3", + "version": "1" + }, + "runOrder": 1 + }] + }, + { + "name": "Build", + "actions": [{ + "name": "BuildAction", + "actionTypeId": { + "category": "Build", + "owner": "AWS", + "provider": "CodeBuild", + "version": "1" + }, + "runOrder": 1 + }] + } + """)) + .then() + .statusCode(200); + + post("ListPipelineExecutions", """ + { + "pipelineName": "%s", + "filter": {"succeededInStage": {"stageName": 42}} + } + """.formatted(pipelineName)) + .then() + .statusCode(400) + .body("__type", containsString("ValidationException")) + .body("message", containsString("succeededInStage requires stageName")); + + post("ListPipelineExecutions", """ + { + "pipelineName": "%s", + "filter": {"succeededInStage": {"stageName": "Source", "extra": 1}} + } + """.formatted(pipelineName)) + .then() + .statusCode(400) + .body("__type", containsString("ValidationException")) + .body("message", containsString("Unknown")); + + post("ListPipelineExecutions", """ + { + "pipelineName": "%s", + "filter": {"succeededInStage": {"stageName": "Source"}, "extra": 1} + } + """.formatted(pipelineName)) + .then() + .statusCode(400) + .body("__type", containsString("ValidationException")) + .body("message", containsString("Unknown")); + + post("ListPipelineExecutions", """ + { + "pipelineName": "%s", + "filter": {"succeededInStage": null} + } + """.formatted(pipelineName)) + .then() + .statusCode(200); + + post("ListPipelineExecutions", """ + { + "pipelineName": "%s", + "maxResults": "5" + } + """.formatted(pipelineName)) + .then() + .statusCode(400) + .body("__type", containsString("ValidationException")) + .body("message", containsString("maxResults must be")); + + post("ListPipelineExecutions", """ + { + "pipelineName": "%s", + "maxResults": 5.5 + } + """.formatted(pipelineName)) + .then() + .statusCode(400) + .body("__type", containsString("ValidationException")) + .body("message", containsString("maxResults must be")); + + post("ListPipelineExecutions", """ + { + "pipelineName": "%s", + "maxResults": 0 + } + """.formatted(pipelineName)) + .then() + .statusCode(400) + .body("__type", containsString("ValidationException")) + .body("message", containsString("between 1 and 100")); + + post("ListPipelineExecutions", """ + { + "pipelineName": "%s", + "maxResults": 101 + } + """.formatted(pipelineName)) + .then() + .statusCode(400) + .body("__type", containsString("ValidationException")) + .body("message", containsString("between 1 and 100")); + } + + @Test + void putApprovalResultSurvivesStageRenameForInFlightApproval() throws Exception { + String pipelineName = "approval-rename-pipeline"; + post("CreatePipeline", pipeline(pipelineName, """ + { + "name": "Approve", + "actions": [{ + "name": "ManualApproval", + "actionTypeId": { + "category": "Approval", + "owner": "AWS", + "provider": "Manual", + "version": "1" + }, + "configuration": {}, + "runOrder": 1 + }] + }, + { + "name": "Deploy", + "actions": [{ + "name": "PlaceholderAction", + "actionTypeId": { + "category": "Deploy", + "owner": "AWS", + "provider": "S3", + "version": "1" + }, + "configuration": { + "BucketName": "codepipeline-artifacts", + "ObjectKey": "placeholder" + }, + "runOrder": 1 + }] + } + """)) + .then() + .statusCode(200); + + post("StartPipelineExecution", """ + {"name": "%s"} + """.formatted(pipelineName)) + .then() + .statusCode(200); + + String approvalToken = waitForApprovalToken(pipelineName); + + post("UpdatePipeline", pipeline(pipelineName, """ + { + "name": "ApproveRenamed", + "actions": [{ + "name": "ManualApprovalRenamed", + "actionTypeId": { + "category": "Approval", + "owner": "AWS", + "provider": "Manual", + "version": "1" + }, + "configuration": {}, + "runOrder": 1 + }] + }, + { + "name": "Deploy", + "actions": [{ + "name": "PlaceholderAction", + "actionTypeId": { + "category": "Deploy", + "owner": "AWS", + "provider": "S3", + "version": "1" + }, + "configuration": { + "BucketName": "codepipeline-artifacts", + "ObjectKey": "placeholder" + }, + "runOrder": 1 + }] + } + """)) + .then() + .statusCode(200); + + post("PutApprovalResult", """ + { + "pipelineName": "%s", + "stageName": "Approve", + "actionName": "ManualApproval", + "token": "%s", + "result": { + "status": "Approved", + "summary": "Approved despite rename" + } + } + """.formatted(pipelineName, approvalToken)) + .then() + .statusCode(200) + .body("approvedAt", notNullValue()); + } + @Test void customActionUsesAwsWorkerJobProtocol() throws Exception { createBucket("codepipeline-custom-source"); @@ -255,6 +628,367 @@ class CodePipelineIntegrationTest { """).then().statusCode(200); } + @Test + void putApprovalResultApprovesAndRejectsManualApprovalActions() throws Exception { + String pipelineName = "approval-test-pipeline"; + post("CreatePipeline", pipeline(pipelineName, """ + { + "name": "Approve", + "actions": [{ + "name": "ManualApproval", + "actionTypeId": { + "category": "Approval", + "owner": "AWS", + "provider": "Manual", + "version": "1" + }, + "configuration": {}, + "runOrder": 1 + }] + }, + { + "name": "Deploy", + "actions": [{ + "name": "PlaceholderAction", + "actionTypeId": { + "category": "Deploy", + "owner": "AWS", + "provider": "S3", + "version": "1" + }, + "configuration": { + "BucketName": "codepipeline-artifacts", + "ObjectKey": "placeholder" + }, + "runOrder": 1 + }] + } + """)) + .then() + .statusCode(200); + + String executionId = post("StartPipelineExecution", """ + {"name": "%s"} + """.formatted(pipelineName)) + .then() + .statusCode(200) + .extract().path("pipelineExecutionId"); + + String approvalToken = waitForApprovalToken(pipelineName); + + post("PutApprovalResult", """ + { + "pipelineName": "%s", + "stageName": "Approve", + "actionName": "ManualApproval", + "token": "%s", + "result": { + "status": "Approved", + "summary": "Approved by test" + } + } + """.formatted(pipelineName, approvalToken)) + .then() + .statusCode(200) + .body("approvedAt", notNullValue()); + + waitForApprovalStatus(pipelineName, "Succeeded") + .then() + .statusCode(200) + .body("stageStates[0].actionStates[0].latestExecution.status", equalTo("Succeeded")) + .body("stageStates[0].actionStates[0].latestExecution.summary", equalTo("Approved by test")); + + String pipelineName2 = "rejection-test-pipeline"; + post("CreatePipeline", pipeline(pipelineName2, """ + { + "name": "Approve", + "actions": [{ + "name": "ManualApproval", + "actionTypeId": { + "category": "Approval", + "owner": "AWS", + "provider": "Manual", + "version": "1" + }, + "configuration": {}, + "runOrder": 1 + }] + }, + { + "name": "Deploy", + "actions": [{ + "name": "PlaceholderAction", + "actionTypeId": { + "category": "Deploy", + "owner": "AWS", + "provider": "S3", + "version": "1" + }, + "configuration": { + "BucketName": "codepipeline-artifacts", + "ObjectKey": "placeholder" + }, + "runOrder": 1 + }] + } + """)) + .then() + .statusCode(200); + + String executionId2 = post("StartPipelineExecution", """ + {"name": "%s"} + """.formatted(pipelineName2)) + .then() + .statusCode(200) + .extract().path("pipelineExecutionId"); + + String approvalToken2 = waitForApprovalToken(pipelineName2); + + post("PutApprovalResult", """ + { + "pipelineName": "%s", + "stageName": "Approve", + "actionName": "ManualApproval", + "token": "%s", + "result": { + "status": "Rejected", + "summary": "Rejected by test" + } + } + """.formatted(pipelineName2, approvalToken2)) + .then() + .statusCode(200) + .body("approvedAt", notNullValue()); + + waitForApprovalStatus(pipelineName2, "Failed") + .then() + .statusCode(200) + .body("stageStates[0].actionStates[0].latestExecution.status", equalTo("Failed")) + .body("stageStates[0].actionStates[0].latestExecution.summary", equalTo("Rejected by test")); + } + + @Test + void putApprovalResultValidatesErrors() throws Exception { + String pipelineName = "error-test-pipeline"; + post("CreatePipeline", pipeline(pipelineName, """ + { + "name": "Approve", + "actions": [{ + "name": "ManualApproval", + "actionTypeId": { + "category": "Approval", + "owner": "AWS", + "provider": "Manual", + "version": "1" + }, + "configuration": {}, + "runOrder": 1 + }] + }, + { + "name": "Deploy", + "actions": [{ + "name": "PlaceholderAction", + "actionTypeId": { + "category": "Deploy", + "owner": "AWS", + "provider": "S3", + "version": "1" + }, + "configuration": { + "BucketName": "codepipeline-artifacts", + "ObjectKey": "placeholder" + }, + "runOrder": 1 + }] + } + """)) + .then() + .statusCode(200); + + post("PutApprovalResult", """ + { + "pipelineName": "nonexistent", + "stageName": "Approve", + "actionName": "ManualApproval", + "token": "00000000-0000-0000-0000-000000000000", + "result": { + "status": "Approved", + "summary": "" + } + } + """) + .then() + .statusCode(400) + .body("__type", containsString("PipelineNotFoundException")); + + post("PutApprovalResult", """ + { + "pipelineName": "%s", + "stageName": "NonexistentStage", + "actionName": "ManualApproval", + "token": "00000000-0000-0000-0000-000000000000", + "result": { + "status": "Approved", + "summary": "" + } + } + """.formatted(pipelineName)) + .then() + .statusCode(400) + .body("__type", containsString("StageNotFoundException")); + + post("PutApprovalResult", """ + { + "pipelineName": "%s", + "stageName": "Approve", + "actionName": "NonexistentAction", + "token": "00000000-0000-0000-0000-000000000000", + "result": { + "status": "Approved", + "summary": "" + } + } + """.formatted(pipelineName)) + .then() + .statusCode(400) + .body("__type", containsString("ActionNotFoundException")); + + post("PutApprovalResult", """ + { + "pipelineName": "%s", + "stageName": "Approve", + "actionName": "ManualApproval", + "token": "00000000-0000-0000-0000-000000000000" + } + """.formatted(pipelineName)) + .then() + .statusCode(400) + .body("__type", containsString("ValidationException")) + .body("message", containsString("result is required")); + + post("PutApprovalResult", """ + { + "pipelineName": "%s", + "stageName": "Approve", + "actionName": "ManualApproval", + "token": "00000000-0000-0000-0000-000000000000", + "result": "Approved" + } + """.formatted(pipelineName)) + .then() + .statusCode(400) + .body("__type", containsString("ValidationException")) + .body("message", containsString("result must be an object")); + + post("PutApprovalResult", """ + { + "pipelineName": "%s", + "stageName": "Approve", + "actionName": "ManualApproval", + "token": "00000000-0000-0000-0000-000000000000", + "result": { + "status": "Approved", + "summary": 12345 + } + } + """.formatted(pipelineName)) + .then() + .statusCode(400) + .body("__type", containsString("ValidationException")) + .body("message", containsString("result.summary must be a string")); + + post("PutApprovalResult", """ + { + "pipelineName": "%s", + "stageName": "Approve", + "actionName": "ManualApproval", + "token": "00000000-0000-0000-0000-000000000000", + "result": { + "status": "Invalid", + "summary": "" + } + } + """.formatted(pipelineName)) + .then() + .statusCode(400) + .body("__type", containsString("ValidationException")) + .body("message", containsString("result.status must be one of")); + + post("PutApprovalResult", """ + { + "pipelineName": "%s", + "stageName": "Approve", + "actionName": "ManualApproval", + "token": "00000000-0000-0000-0000-000000000000", + "result": { + "status": "Approved", + "summary": "%s" + } + } + """.formatted(pipelineName, "a".repeat(513))) + .then() + .statusCode(400) + .body("__type", containsString("ValidationException")) + .body("message", containsString("must not exceed 512 characters")); + + String executionId = post("StartPipelineExecution", """ + {"name": "%s"} + """.formatted(pipelineName)) + .then() + .statusCode(200) + .extract().path("pipelineExecutionId"); + + String approvalToken = waitForApprovalToken(pipelineName); + + post("PutApprovalResult", """ + { + "pipelineName": "%s", + "stageName": "Approve", + "actionName": "ManualApproval", + "token": "00000000-0000-0000-0000-000000000000", + "result": { + "status": "Approved", + "summary": "" + } + } + """.formatted(pipelineName)) + .then() + .statusCode(400) + .body("__type", containsString("InvalidApprovalTokenException")); + + post("PutApprovalResult", """ + { + "pipelineName": "%s", + "stageName": "Approve", + "actionName": "ManualApproval", + "token": "%s", + "result": { + "status": "Approved", + "summary": "First approval" + } + } + """.formatted(pipelineName, approvalToken)) + .then() + .statusCode(200); + + post("PutApprovalResult", """ + { + "pipelineName": "%s", + "stageName": "Approve", + "actionName": "ManualApproval", + "token": "%s", + "result": { + "status": "Approved", + "summary": "Second approval attempt" + } + } + """.formatted(pipelineName, approvalToken)) + .then() + .statusCode(400) + .body("__type", containsString("ApprovalAlreadyCompletedException")); + } + @Test void webhookStageTransitionAndValidationErrorsUseAwsShapes() { post("PutWebhook", """ @@ -342,6 +1076,39 @@ class CodePipelineIntegrationTest { throw new AssertionError("Pipeline did not reach " + expected + ", last status: " + status); } + private Response waitForApprovalStatus(String pipelineName, String expectedStatus) throws Exception { + Instant deadline = Instant.now().plus(Duration.ofSeconds(5)); + Response response; + String status; + do { + response = post("GetPipelineState", """ + {"name": "%s"} + """.formatted(pipelineName)); + status = response.jsonPath().getString("stageStates[0].actionStates[0].latestExecution.status"); + if (expectedStatus.equals(status)) { + return response; + } + Thread.sleep(50); + } while (Instant.now().isBefore(deadline)); + throw new AssertionError("Approval action did not reach " + expectedStatus + ", last status: " + status); + } + + private String waitForApprovalToken(String pipelineName) throws Exception { + Instant deadline = Instant.now().plus(Duration.ofSeconds(5)); + String token; + do { + token = post("GetPipelineState", """ + {"name": "%s"} + """.formatted(pipelineName)) + .jsonPath().getString("stageStates[0].actionStates[0].latestExecution.token"); + if (token != null) { + return token; + } + Thread.sleep(50); + } while (Instant.now().isBefore(deadline)); + throw new AssertionError("Approval token was not issued for pipeline " + pipelineName); + } + private static Response post(String action, String body) { return given() .header("X-Amz-Target", TARGET + action)