From 4f071135121e919b0442336f3d990886c20fe932 Mon Sep 17 00:00:00 2001 From: Hendrik Ebbers Date: Sun, 3 Dec 2023 20:21:08 +0100 Subject: [PATCH] api keys added --- .../benchscape/jmh/client/JmhRunner.java | 28 +++++------ .../jmh/client/JmhRunnerCommand.java | 16 +++++-- .../benchscape/jmh/client/io/RestHandler.java | 37 ++++++++++----- .../jmh/client/io/RestHandlerConfig.java | 47 +++++++++++++++++++ .../benchscape/jmh/maven/JmhMojo.java | 12 +++++ 5 files changed, 109 insertions(+), 31 deletions(-) create mode 100644 jmh-client/src/main/java/com/openelements/benchscape/jmh/client/io/RestHandlerConfig.java diff --git a/jmh-client/src/main/java/com/openelements/benchscape/jmh/client/JmhRunner.java b/jmh-client/src/main/java/com/openelements/benchscape/jmh/client/JmhRunner.java index 471d32a4..808ea3ce 100644 --- a/jmh-client/src/main/java/com/openelements/benchscape/jmh/client/JmhRunner.java +++ b/jmh-client/src/main/java/com/openelements/benchscape/jmh/client/JmhRunner.java @@ -2,6 +2,7 @@ import com.openelements.benchscape.jmh.client.io.FileHandler; import com.openelements.benchscape.jmh.client.io.RestHandler; +import com.openelements.benchscape.jmh.client.io.RestHandlerConfig; import com.openelements.benchscape.jmh.client.jmh.BenchmarkFactory; import com.openelements.benchscape.jmh.client.jmh.JmhExecutor; import com.openelements.benchscape.jmh.model.BenchmarkExecution; @@ -9,6 +10,7 @@ import java.nio.file.Path; import java.util.Collection; import java.util.Collections; +import java.util.Objects; import java.util.Set; import org.openjdk.jmh.results.RunResult; @@ -27,7 +29,7 @@ public class JmhRunner { private final Path resultsPath; - private final String uploadBaseUrl; + private final RestHandlerConfig restHandlerConfig; private final Collection includes; @@ -45,25 +47,26 @@ public JmhRunner() { * @param writeResults whether to write the results to disc */ public JmhRunner(boolean uploadResults, boolean writeResults) { - this(uploadResults, writeResults, null, null, null); + this(uploadResults, writeResults, null, new RestHandlerConfig(), null); } /** * Constructor. * - * @param uploadResults whether to post the results to a server - * @param writeResults whether to write the results to disc - * @param resultsPath the path of the directory to write the results to. If null, the default directory is used - * @param uploadBaseUrl the base URL of the server to post the results to. If null, the default server is used - * @param includes the names of the benchmarks to run. If empty, all benchmarks are run + * @param uploadResults whether to post the results to a server + * @param writeResults whether to write the results to disc + * @param resultsPath the path of the directory to write the results to. If null, the default directory is + * used + * @param restHandlerConfig TODO + * @param includes the names of the benchmarks to run. If empty, all benchmarks are run */ public JmhRunner(boolean uploadResults, boolean writeResults, @Nullable Path resultsPath, - @Nullable String uploadBaseUrl, + @Nullable RestHandlerConfig restHandlerConfig, @Nullable Collection includes) { this.uploadResults = uploadResults; this.writeResults = writeResults; this.resultsPath = resultsPath; - this.uploadBaseUrl = uploadBaseUrl; + this.restHandlerConfig = Objects.requireNonNull(restHandlerConfig, "restHandlerConfig must not be null"); if (includes == null) { this.includes = Set.of(); } else { @@ -87,12 +90,7 @@ public void execute() throws Exception { final Collection benchmarkExecutions = BenchmarkFactory.convert(runResults); if (uploadResults) { - final RestHandler restHandler; - if (uploadBaseUrl == null) { - restHandler = new RestHandler(); - } else { - restHandler = new RestHandler(uploadBaseUrl); - } + final RestHandler restHandler = new RestHandler(restHandlerConfig); restHandler.post(benchmarkExecutions); } diff --git a/jmh-client/src/main/java/com/openelements/benchscape/jmh/client/JmhRunnerCommand.java b/jmh-client/src/main/java/com/openelements/benchscape/jmh/client/JmhRunnerCommand.java index 80b2df4e..2ed04612 100644 --- a/jmh-client/src/main/java/com/openelements/benchscape/jmh/client/JmhRunnerCommand.java +++ b/jmh-client/src/main/java/com/openelements/benchscape/jmh/client/JmhRunnerCommand.java @@ -1,5 +1,6 @@ package com.openelements.benchscape.jmh.client; +import com.openelements.benchscape.jmh.client.io.RestHandlerConfig; import java.nio.file.Path; import java.util.Collection; import java.util.concurrent.Callable; @@ -22,17 +23,26 @@ public class JmhRunnerCommand implements Callable { "--path"}, description = "The path of the directory to write the results to. If null, the default directory is used") private Path resultsPath; + @Parameters(paramLabel = "INCLUDES", description = "The names of the benchmarks to run. If empty, all benchmarks are run") + private Collection includes; + @Option(names = { "--url"}, description = "The base URL of the server to post the results to. If null, the default server is used") private String uploadBaseUrl; - @Parameters(paramLabel = "INCLUDES", description = "The names of the benchmarks to run. If empty, all benchmarks are run") - private Collection includes; + @Option(names = { + "--apiPrincipal"}, description = "TODO") + private String apiPrincipal; + + @Option(names = { + "--apiKey"}, description = "TODO") + private String apiKey; @Override public Integer call() { try { - new JmhRunner(uploadResults, writeResults, resultsPath, uploadBaseUrl, includes).execute(); + new JmhRunner(uploadResults, writeResults, resultsPath, + new RestHandlerConfig(uploadBaseUrl, apiPrincipal, apiKey), includes).execute(); } catch (Exception e) { e.printStackTrace(); return -1; diff --git a/jmh-client/src/main/java/com/openelements/benchscape/jmh/client/io/RestHandler.java b/jmh-client/src/main/java/com/openelements/benchscape/jmh/client/io/RestHandler.java index 683d4e7a..7677abd9 100644 --- a/jmh-client/src/main/java/com/openelements/benchscape/jmh/client/io/RestHandler.java +++ b/jmh-client/src/main/java/com/openelements/benchscape/jmh/client/io/RestHandler.java @@ -20,25 +20,29 @@ public class RestHandler { public static final String CONTENT_TYPE_HEADER_NAME = "Content-Type"; + public static final String BENCHSCAPE_API_PRINCIPAL_HEADER_NAME = "Benchscape-Api-Principal"; + + public static final String BENCHSCAPE_API_KEY_HEADER_NAME = "Benchscape-Api-Key"; + public static final String CONTENT_TYPE_HEADER_VALUE = "application/json"; - public static final String LOCALHOST = "http://localhost:8080"; private final String baseUrl; + private final String apiPrincipal; + + private final String apiKey; + /** * Creates a new instance. * - * @param baseUrl the base url of the endpoint + * @param config the config to call the endpoint */ - public RestHandler(@NonNull String baseUrl) { - this.baseUrl = Objects.requireNonNull(baseUrl, "baseUrl must not be null"); + public RestHandler(@NonNull RestHandlerConfig config) { + Objects.requireNonNull(config, "config must not be null"); + this.baseUrl = config.getBaseUrl(); + this.apiPrincipal = config.getApiPrincipal(); + this.apiKey = config.getApiKey(); } - /** - * Creates a new instance that uses {@code http://localhost:8080} as base url for the endpoint. - */ - public RestHandler() { - this(LOCALHOST); - } /*** * Posts the benchmark execution to the endpoint. @@ -62,11 +66,18 @@ public void post(@NonNull BenchmarkExecution benchmarkExecution) throws Exceptio */ private void post(@NonNull String json) throws URISyntaxException, IOException, InterruptedException { final BodyPublisher bodyPublisher = HttpRequest.BodyPublishers.ofString(json); - final HttpRequest request = HttpRequest.newBuilder() + + HttpRequest.Builder builder = HttpRequest.newBuilder() .uri(getUrl()) - .header(CONTENT_TYPE_HEADER_NAME, CONTENT_TYPE_HEADER_VALUE) .POST(bodyPublisher) - .build(); + .header(CONTENT_TYPE_HEADER_NAME, CONTENT_TYPE_HEADER_VALUE); + if (apiPrincipal != null) { + builder = builder.header(BENCHSCAPE_API_PRINCIPAL_HEADER_NAME, apiPrincipal); + } + if (apiKey != null) { + builder = builder.header(BENCHSCAPE_API_KEY_HEADER_NAME, apiKey); + } + final HttpRequest request = builder.build(); final HttpClient httpClient = HttpClient.newBuilder().build(); final BodyHandler bodyHandler = HttpResponse.BodyHandlers.discarding(); diff --git a/jmh-client/src/main/java/com/openelements/benchscape/jmh/client/io/RestHandlerConfig.java b/jmh-client/src/main/java/com/openelements/benchscape/jmh/client/io/RestHandlerConfig.java new file mode 100644 index 00000000..99f220ca --- /dev/null +++ b/jmh-client/src/main/java/com/openelements/benchscape/jmh/client/io/RestHandlerConfig.java @@ -0,0 +1,47 @@ +package com.openelements.benchscape.jmh.client.io; + +public class RestHandlerConfig { + + private String baseUrl; + + private String apiPrincipal; + + private String apiKey; + + public RestHandlerConfig() { + } + + public RestHandlerConfig(String baseUrl) { + this.baseUrl = baseUrl; + } + + public RestHandlerConfig(String baseUrl, String apiPrincipal, String apiKey) { + this.baseUrl = baseUrl; + this.apiPrincipal = apiPrincipal; + this.apiKey = apiKey; + } + + public String getBaseUrl() { + return baseUrl; + } + + public void setBaseUrl(String baseUrl) { + this.baseUrl = baseUrl; + } + + public String getApiPrincipal() { + return apiPrincipal; + } + + public void setApiPrincipal(String apiPrincipal) { + this.apiPrincipal = apiPrincipal; + } + + public String getApiKey() { + return apiKey; + } + + public void setApiKey(String apiKey) { + this.apiKey = apiKey; + } +} diff --git a/jmh-maven-plugin/src/main/java/com/openelements/benchscape/jmh/maven/JmhMojo.java b/jmh-maven-plugin/src/main/java/com/openelements/benchscape/jmh/maven/JmhMojo.java index f00b3a20..e2c1699c 100644 --- a/jmh-maven-plugin/src/main/java/com/openelements/benchscape/jmh/maven/JmhMojo.java +++ b/jmh-maven-plugin/src/main/java/com/openelements/benchscape/jmh/maven/JmhMojo.java @@ -81,6 +81,12 @@ public class JmhMojo extends AbstractMojo { @Parameter(defaultValue = "https://backend.benchscape.cloud") private String url; + @Parameter + private String apiPrincipal; + + @Parameter + private String apiKey; + @Component private ArtifactHandlerManager artifactHandlerManager; @@ -170,6 +176,8 @@ private int executeBenchmarkRunInForkedProcess(@NonNull final List class getLog().debug("PARAM 'file'=" + file); getLog().debug("PARAM 'upload'=" + upload); getLog().debug("PARAM 'url'=" + url); + getLog().debug("PARAM 'apiPrincipal'=" + apiPrincipal); + getLog().debug("PARAM 'apiKey'=" + apiKey); final List command = new ArrayList<>(); command.add(javaProcess); @@ -186,6 +194,10 @@ private int executeBenchmarkRunInForkedProcess(@NonNull final List class } command.add("--url"); command.add(url); + command.add("--apiPrincipal"); + command.add(apiPrincipal); + command.add("--apiKey"); + command.add(apiKey); getLog().debug("Running forked execution using: " + command); final ProcessBuilder processBuilder = new ProcessBuilder(command);