mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
feat(codepipeline): validate ListPipelineExecutions filters and PutApprovalResult inputs (#2696)
* feat: tighten CodePipeline P0 operation contracts * fix(codepipeline): reject malformed filters and stop stranding approvals on rename - ListPipelineExecutions: reject non-string succeededInStage.stageName, unknown filter/succeededInStage members, and non-integer maxResults instead of silently coercing them. - PutApprovalResult: reject non-string result.summary instead of coercing it, and split the "result is required" error from a clearer "result must be an object" error for wrong-typed payloads. - PutApprovalResult: look up the pending approval by token across the execution's own captured stage/action names before consulting the pipeline's current declaration, so renaming or removing a stage or action via UpdatePipeline no longer strands an in-flight approval behind a StageNotFoundException/ActionNotFoundException. - Replace fixed Thread.sleep(100) waits in CodePipelineIntegrationTest with polling helpers (waitForApprovalToken/waitForApprovalStatus) matching the existing waitForJob/waitForExecution pattern, removing test flakiness.
This commit is contained in:
1 parent
48e0ecd809
commit
a155e66b15
3 files changed
+875
-12
No files matched your search
+100
-12
@@ -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<CodePipelineExecution> 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<String> 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",
|
||||
|
||||
Reference in new issue
Block a user