-
Notifications
You must be signed in to change notification settings - Fork 24
Use HTTP server for TUF conformance testing #1045
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
aaronlew02
wants to merge
1
commit into
main
Choose a base branch
from
tuf-conformance-server
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+269
−5
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright 2025 The Sigstore Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package dev.sigstore.tuf.cli; | ||
|
||
import java.time.Clock; | ||
|
||
public class TestClock { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not super fond of the idea of a global test clock. I think this should be instantiable . And we can modify the cli to accept a "time" with a --time flag. |
||
private static Clock clock = null; | ||
|
||
public static void set(Clock clock) { | ||
TestClock.clock = clock; | ||
} | ||
|
||
public static Clock get() { | ||
return clock == null ? Clock.systemUTC() : clock; | ||
} | ||
|
||
public static void reset() { | ||
TestClock.clock = null; | ||
} | ||
} |
143 changes: 143 additions & 0 deletions
143
tuf-cli/src/main/java/dev/sigstore/tuf/cli/TufConformanceServer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/* | ||
* Copyright 2025 The Sigstore Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package dev.sigstore.tuf.cli; | ||
|
||
import com.google.gson.Gson; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.io.PrintStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.time.Clock; | ||
import java.time.Instant; | ||
import java.time.ZoneOffset; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.eclipse.jetty.server.Request; | ||
import org.eclipse.jetty.server.Server; | ||
import org.eclipse.jetty.server.handler.AbstractHandler; | ||
|
||
public class TufConformanceServer { | ||
|
||
private static final Gson GSON = new Gson(); | ||
private static boolean debug = false; | ||
|
||
private static class ExecuteRequest { | ||
String[] args; | ||
String faketime; | ||
} | ||
|
||
public static void main(String[] args) throws Exception { | ||
if (args.length > 0 && "--debug".equals(args[0])) { | ||
debug = true; | ||
} | ||
int port = 8080; | ||
Server server = new Server(port); | ||
server.setHandler(new TufConformanceHandler()); | ||
server.start(); | ||
server.join(); | ||
} | ||
|
||
public static class TufConformanceHandler extends AbstractHandler { | ||
@Override | ||
public void handle( | ||
String target, | ||
Request baseRequest, | ||
HttpServletRequest request, | ||
HttpServletResponse response) | ||
throws IOException, ServletException { | ||
if ("/".equals(target)) { | ||
handleHealthCheck(response); | ||
} else if ("/execute".equals(target) && "POST".equals(request.getMethod())) { | ||
handleExecute(request, response); | ||
} | ||
baseRequest.setHandled(true); | ||
} | ||
} | ||
|
||
private static void handleExecute(HttpServletRequest request, HttpServletResponse response) | ||
throws IOException { | ||
ExecuteRequest executeRequest; | ||
try (InputStream is = request.getInputStream()) { | ||
String requestBody = new String(is.readAllBytes(), StandardCharsets.UTF_8); | ||
executeRequest = GSON.fromJson(requestBody, ExecuteRequest.class); | ||
} | ||
|
||
// Tests should not be run in parallel, to ensure orderly input/output | ||
PrintStream originalOut = System.out; | ||
PrintStream originalErr = System.err; | ||
|
||
ByteArrayOutputStream outContent = new ByteArrayOutputStream(); | ||
ByteArrayOutputStream errContent = new ByteArrayOutputStream(); | ||
|
||
try (PrintStream outPs = new PrintStream(outContent, true, StandardCharsets.UTF_8); | ||
PrintStream errPs = new PrintStream(errContent, true, StandardCharsets.UTF_8)) { | ||
if (!debug) { | ||
System.setOut(outPs); | ||
System.setErr(errPs); | ||
} | ||
|
||
try { | ||
Instant fakeNow = Instant.ofEpochSecond(Long.parseLong(executeRequest.faketime)); | ||
TestClock.set(Clock.fixed(fakeNow, ZoneOffset.UTC)); | ||
} catch (NumberFormatException e) { | ||
throw new IllegalArgumentException( | ||
"Invalid 'faketime' format. Must be a long representing epoch seconds, but was: '" | ||
+ executeRequest.faketime | ||
+ "'", | ||
e); | ||
} | ||
|
||
List<String> resolvedArgs = new java.util.ArrayList<>(); | ||
resolvedArgs.addAll(Arrays.asList(executeRequest.args)); | ||
|
||
int exitCode = | ||
new picocli.CommandLine(new Tuf()).execute(resolvedArgs.toArray(new String[0])); | ||
|
||
Map<String, Object> responseMap = | ||
Map.of( | ||
"stdout", outContent.toString(StandardCharsets.UTF_8), | ||
"stderr", errContent.toString(StandardCharsets.UTF_8), | ||
"exitCode", exitCode); | ||
String jsonResponse = GSON.toJson(responseMap); | ||
|
||
response.setStatus(HttpServletResponse.SC_OK); | ||
response.setContentType("application/json"); | ||
byte[] responseBytes = jsonResponse.getBytes(StandardCharsets.UTF_8); | ||
response.setContentLength(responseBytes.length); | ||
|
||
try (OutputStream os = response.getOutputStream()) { | ||
os.write(responseBytes); | ||
} | ||
} finally { | ||
if (!debug) { | ||
System.setOut(originalOut); | ||
System.setErr(originalErr); | ||
} | ||
TestClock.reset(); | ||
} | ||
} | ||
|
||
private static void handleHealthCheck(HttpServletResponse response) throws IOException { | ||
response.setStatus(HttpServletResponse.SC_OK); | ||
response.getWriter().println("OK"); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#!/bin/bash | ||
|
||
set -o pipefail -o errexit -o nounset | ||
|
||
CWD=$PWD | ||
|
||
ARGS_JSON="[" | ||
for arg in "$@"; do | ||
escaped_arg=$(echo -n "$arg" | jq -R -s '.') | ||
ARGS_JSON="$ARGS_JSON$escaped_arg," | ||
done | ||
if [[ $ARGS_JSON == *, ]]; then | ||
ARGS_JSON="${ARGS_JSON%,}" | ||
fi | ||
ARGS_JSON="$ARGS_JSON]" | ||
|
||
DATE_VAL="$(date +%s)" | ||
|
||
JSON_PAYLOAD=$(jq -nc --arg cwd "$CWD" --argjson args "$ARGS_JSON" --arg faketime "$DATE_VAL" '{"cwd": $cwd, "args": $args, "faketime": $faketime}') | ||
|
||
RESPONSE=$(curl -s -X POST --header "Content-Type: application/json" --data-binary "$JSON_PAYLOAD" http://localhost:8080/execute) | ||
|
||
STDOUT=$(echo "$RESPONSE" | jq -r .stdout) | ||
STDERR=$(echo "$RESPONSE" | jq -r .stderr) | ||
EXIT_CODE=$(echo "$RESPONSE" | jq .exitCode) | ||
|
||
echo -n "$STDOUT" | ||
echo -n "$STDERR" >&2 | ||
|
||
exit "$EXIT_CODE" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
test_metadata_bytes_match | ||
test_unusual_role_name[?] | ||
test_unusual_role_name[#] | ||
test_unusual_role_name[/delegatedrole] | ||
test_unusual_role_name[../delegatedrole] | ||
test_snapshot_rollback[basic] | ||
test_snapshot_rollback[with | ||
test_static_repository[tuf-on-ci-0.11] | ||
test_graph_traversal[basic-delegation] | ||
test_graph_traversal[single-level-delegations] | ||
test_graph_traversal[two-level-delegations] | ||
test_graph_traversal[two-level-test-DFS-order-of-traversal] | ||
test_graph_traversal[three-level-delegation-test-DFS-order-of-traversal] | ||
test_graph_traversal[two-level-terminating-ignores-all-but-roles-descendants] | ||
test_graph_traversal[three-level-terminating-ignores-all-but-roles-descendants] | ||
test_graph_traversal[two-level-ignores-all-branches-not-matching-paths] | ||
test_graph_traversal[three-level-ignores-all-branches-not-matching-paths] | ||
test_graph_traversal[cyclic-graph] | ||
test_graph_traversal[two-roles-delegating-to-a-third] | ||
test_graph_traversal[two-roles-delegating-to-a-third-different-paths] | ||
test_targetfile_search[targetpath matches wildcard] | ||
test_targetfile_search[targetpath with separators x] | ||
test_targetfile_search[targetpath with separators y] | ||
test_targetfile_search[targetpath is not delegated by all roles in the chain] | ||
test_snapshot_rollback[with hashes] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
was this not working before?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not having this set triggered a GitHub security warning for #1038.