|
| 1 | +/* |
| 2 | + * Copyright 2025 The Sigstore Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package dev.sigstore.cli; |
| 17 | + |
| 18 | +import com.google.gson.Gson; |
| 19 | +import jakarta.servlet.ServletException; |
| 20 | +import jakarta.servlet.http.HttpServletRequest; |
| 21 | +import jakarta.servlet.http.HttpServletResponse; |
| 22 | +import java.io.ByteArrayOutputStream; |
| 23 | +import java.io.IOException; |
| 24 | +import java.io.InputStream; |
| 25 | +import java.io.OutputStream; |
| 26 | +import java.io.PrintStream; |
| 27 | +import java.nio.charset.StandardCharsets; |
| 28 | +import java.nio.file.Path; |
| 29 | +import java.nio.file.Paths; |
| 30 | +import java.util.Map; |
| 31 | +import org.eclipse.jetty.server.Request; |
| 32 | +import org.eclipse.jetty.server.Server; |
| 33 | +import org.eclipse.jetty.server.handler.AbstractHandler; |
| 34 | + |
| 35 | +public class ConformanceServer { |
| 36 | + |
| 37 | + private static final Gson GSON = new Gson(); |
| 38 | + |
| 39 | + private static class ExecuteRequest { |
| 40 | + String cwd; |
| 41 | + String[] args; |
| 42 | + } |
| 43 | + |
| 44 | + public static void main(String[] args) throws Exception { |
| 45 | + int port = 8080; |
| 46 | + Server server = new Server(port); |
| 47 | + server.setHandler(new ConformanceHandler()); |
| 48 | + server.start(); |
| 49 | + server.join(); |
| 50 | + } |
| 51 | + |
| 52 | + public static class ConformanceHandler extends AbstractHandler { |
| 53 | + @Override |
| 54 | + public void handle( |
| 55 | + String target, |
| 56 | + Request baseRequest, |
| 57 | + HttpServletRequest request, |
| 58 | + HttpServletResponse response) |
| 59 | + throws IOException, ServletException { |
| 60 | + if ("/".equals(target)) { |
| 61 | + handleHealthCheck(response); |
| 62 | + baseRequest.setHandled(true); |
| 63 | + } else if ("/execute".equals(target) && "POST".equals(request.getMethod())) { |
| 64 | + handleExecute(request, response); |
| 65 | + baseRequest.setHandled(true); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private static void handleExecute(HttpServletRequest request, HttpServletResponse response) |
| 71 | + throws IOException { |
| 72 | + ExecuteRequest executeRequest; |
| 73 | + try (InputStream is = request.getInputStream()) { |
| 74 | + String requestBody = new String(is.readAllBytes(), StandardCharsets.UTF_8); |
| 75 | + executeRequest = GSON.fromJson(requestBody, ExecuteRequest.class); |
| 76 | + } |
| 77 | + |
| 78 | + // Tests should not be run in parallel, to ensure orderly input/output |
| 79 | + PrintStream originalOut = System.out; |
| 80 | + PrintStream originalErr = System.err; |
| 81 | + |
| 82 | + ByteArrayOutputStream outContent = new ByteArrayOutputStream(); |
| 83 | + ByteArrayOutputStream errContent = new ByteArrayOutputStream(); |
| 84 | + |
| 85 | + try (PrintStream outPs = new PrintStream(outContent, true, StandardCharsets.UTF_8); |
| 86 | + PrintStream errPs = new PrintStream(errContent, true, StandardCharsets.UTF_8)) { |
| 87 | + System.setOut(outPs); |
| 88 | + System.setErr(errPs); |
| 89 | + |
| 90 | + Path cwd = Paths.get(executeRequest.cwd); |
| 91 | + java.util.List<String> resolvedArgs = new java.util.ArrayList<>(); |
| 92 | + |
| 93 | + for (int i = 0; i < executeRequest.args.length; i++) { |
| 94 | + String arg = executeRequest.args[i]; |
| 95 | + |
| 96 | + if (arg.equals("--bundle") && i + 1 < executeRequest.args.length) { |
| 97 | + resolvedArgs.add(arg); |
| 98 | + String filePath = executeRequest.args[++i]; |
| 99 | + resolvedArgs.add(cwd.resolve(filePath).toAbsolutePath().toString()); |
| 100 | + } else if (i == executeRequest.args.length - 1 && !arg.startsWith("-")) { |
| 101 | + if (arg.contains(":")) { |
| 102 | + resolvedArgs.add(arg); |
| 103 | + } else { |
| 104 | + resolvedArgs.add(cwd.resolve(arg).toAbsolutePath().toString()); |
| 105 | + } |
| 106 | + } else { |
| 107 | + resolvedArgs.add(arg); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + int exitCode = |
| 112 | + new picocli.CommandLine(new Sigstore()).execute(resolvedArgs.toArray(new String[0])); |
| 113 | + |
| 114 | + Map<String, Object> responseMap = |
| 115 | + Map.of( |
| 116 | + "stdout", outContent.toString(StandardCharsets.UTF_8), |
| 117 | + "stderr", errContent.toString(StandardCharsets.UTF_8), |
| 118 | + "exitCode", exitCode); |
| 119 | + String jsonResponse = GSON.toJson(responseMap); |
| 120 | + |
| 121 | + response.setStatus(HttpServletResponse.SC_OK); |
| 122 | + response.setContentType("application/json"); |
| 123 | + byte[] responseBytes = jsonResponse.getBytes(StandardCharsets.UTF_8); |
| 124 | + response.setContentLength(responseBytes.length); |
| 125 | + |
| 126 | + try (OutputStream os = response.getOutputStream()) { |
| 127 | + os.write(responseBytes); |
| 128 | + } |
| 129 | + } finally { |
| 130 | + System.setOut(originalOut); |
| 131 | + System.setErr(originalErr); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + private static void handleHealthCheck(HttpServletResponse response) throws IOException { |
| 136 | + response.setStatus(HttpServletResponse.SC_OK); |
| 137 | + response.getWriter().println("OK"); |
| 138 | + } |
| 139 | +} |
0 commit comments