From e82856a53c96c7d704130282f20ccaf115aaf04b Mon Sep 17 00:00:00 2001 From: grapigeau Date: Thu, 12 Sep 2024 11:43:23 -0400 Subject: [PATCH] add api key auth and implement for nabu plugin --- .../gsi/shesmu/nabu/ArchiveCaseAction.java | 19 +++++++++++----- .../gsi/shesmu/nabu/NabuConfiguration.java | 11 ++++++++++ .../on/oicr/gsi/shesmu/nabu/NabuPlugin.java | 8 +++++++ .../AuthenticationConfiguration.java | 8 +++++-- .../AuthenticationConfigurationAPIkey.java | 20 +++++++++++++++++ ...AuthenticationConfigurationFileAPIkey.java | 22 +++++++++++++++++++ 6 files changed, 81 insertions(+), 7 deletions(-) create mode 100644 shesmu-pluginapi/src/main/java/ca/on/oicr/gsi/shesmu/plugin/authentication/AuthenticationConfigurationAPIkey.java create mode 100644 shesmu-pluginapi/src/main/java/ca/on/oicr/gsi/shesmu/plugin/authentication/AuthenticationConfigurationFileAPIkey.java diff --git a/plugin-nabu/src/main/java/ca/on/oicr/gsi/shesmu/nabu/ArchiveCaseAction.java b/plugin-nabu/src/main/java/ca/on/oicr/gsi/shesmu/nabu/ArchiveCaseAction.java index 793a5b18c..f6b2d966a 100644 --- a/plugin-nabu/src/main/java/ca/on/oicr/gsi/shesmu/nabu/ArchiveCaseAction.java +++ b/plugin-nabu/src/main/java/ca/on/oicr/gsi/shesmu/nabu/ArchiveCaseAction.java @@ -32,6 +32,7 @@ public class ArchiveCaseAction extends JsonParameterisedAction { static final HttpClient HTTP_CLIENT = HttpClient.newHttpClient(); private final ObjectNode parameters; private final ObjectNode rootParameters = MAPPER.createObjectNode(); + private Optional authenticationHeader = Optional.empty(); private static final Counter NabuRequestErrors = Counter.build( @@ -172,6 +173,9 @@ public ActionState perform( HttpRequest.BodyPublisher body; try { body = HttpRequest.BodyPublishers.ofString(createRequestBody()); + final var authentication = owner.get().NabuToken(); + authenticationHeader = + authentication == null ? Optional.empty() : Optional.of(authentication); } catch (final Exception e) { e.printStackTrace(); this.errors = Collections.singletonList(e.getMessage()); @@ -179,14 +183,19 @@ public ActionState perform( } final var baseUrl = owner.get().NabuUrl(); + final var builder = HttpRequest.newBuilder(URI.create(baseUrl + "/case")); + + authenticationHeader.ifPresent(header -> builder.header("X-API-KEY", header)); + final var request = - HttpRequest.newBuilder(URI.create(baseUrl + "/case")) - .header("Content-type", "application/json") - .header("Accept", "application/json") - .POST(body) - .build(); + builder + .header("Content-type", "application/json") + .header("Accept", "application/json") + .POST(body) + .build(); owner.log("NABU REQUEST: " + request, LogLevel.DEBUG, null); + try (var timer = NabuRequestTime.start(baseUrl)) { var response = HTTP_CLIENT.send(request, new JsonListBodyHandler<>(MAPPER, NabuCaseArchiveDto.class)); diff --git a/plugin-nabu/src/main/java/ca/on/oicr/gsi/shesmu/nabu/NabuConfiguration.java b/plugin-nabu/src/main/java/ca/on/oicr/gsi/shesmu/nabu/NabuConfiguration.java index 99770137e..41a2feeb2 100644 --- a/plugin-nabu/src/main/java/ca/on/oicr/gsi/shesmu/nabu/NabuConfiguration.java +++ b/plugin-nabu/src/main/java/ca/on/oicr/gsi/shesmu/nabu/NabuConfiguration.java @@ -1,13 +1,24 @@ package ca.on.oicr.gsi.shesmu.nabu; +import ca.on.oicr.gsi.shesmu.plugin.authentication.AuthenticationConfiguration; + public class NabuConfiguration { + private AuthenticationConfiguration authentication; private String url; + public AuthenticationConfiguration getAuthentication() { + return authentication; + } + public String getUrl() { return url; } + public void setAuthentication(AuthenticationConfiguration authentication) { + this.authentication = authentication; + } + public void setUrl(String url) { this.url = url; } diff --git a/plugin-nabu/src/main/java/ca/on/oicr/gsi/shesmu/nabu/NabuPlugin.java b/plugin-nabu/src/main/java/ca/on/oicr/gsi/shesmu/nabu/NabuPlugin.java index 9c9fc9075..c1a699097 100644 --- a/plugin-nabu/src/main/java/ca/on/oicr/gsi/shesmu/nabu/NabuPlugin.java +++ b/plugin-nabu/src/main/java/ca/on/oicr/gsi/shesmu/nabu/NabuPlugin.java @@ -161,4 +161,12 @@ protected Optional update(NabuConfiguration value) { public String NabuUrl() { return config.get().getUrl(); } + + public String NabuToken() throws RuntimeException { + try { + return config.get().getAuthentication().prepareAuthentication(); + } catch (IOException e) { + throw new RuntimeException("Unable to get authentication method"); + } + } } diff --git a/shesmu-pluginapi/src/main/java/ca/on/oicr/gsi/shesmu/plugin/authentication/AuthenticationConfiguration.java b/shesmu-pluginapi/src/main/java/ca/on/oicr/gsi/shesmu/plugin/authentication/AuthenticationConfiguration.java index 0ae47936b..f946fde3c 100644 --- a/shesmu-pluginapi/src/main/java/ca/on/oicr/gsi/shesmu/plugin/authentication/AuthenticationConfiguration.java +++ b/shesmu-pluginapi/src/main/java/ca/on/oicr/gsi/shesmu/plugin/authentication/AuthenticationConfiguration.java @@ -9,14 +9,18 @@ @JsonSubTypes({ @JsonSubTypes.Type(value = AuthenticationConfigurationBasic.class, name = "basic"), @JsonSubTypes.Type(value = AuthenticationConfigurationBearer.class, name = "bearer"), + @JsonSubTypes.Type(value = AuthenticationConfigurationAPIkey.class, name = "apikey"), @JsonSubTypes.Type(value = AuthenticationConfigurationFileBasic.class, name = "basic-file"), - @JsonSubTypes.Type(value = AuthenticationConfigurationFileBearer.class, name = "bearer-file") + @JsonSubTypes.Type(value = AuthenticationConfigurationFileBearer.class, name = "bearer-file"), + @JsonSubTypes.Type(value = AuthenticationConfigurationFileAPIkey.class, name = "apikey-file") }) public abstract sealed class AuthenticationConfiguration permits AuthenticationConfigurationBasic, AuthenticationConfigurationBearer, + AuthenticationConfigurationAPIkey, AuthenticationConfigurationFileBasic, - AuthenticationConfigurationFileBearer { + AuthenticationConfigurationFileBearer, + AuthenticationConfigurationFileAPIkey { public static void addAuthenticationHeader( AuthenticationConfiguration authentication, Builder request) throws IOException { diff --git a/shesmu-pluginapi/src/main/java/ca/on/oicr/gsi/shesmu/plugin/authentication/AuthenticationConfigurationAPIkey.java b/shesmu-pluginapi/src/main/java/ca/on/oicr/gsi/shesmu/plugin/authentication/AuthenticationConfigurationAPIkey.java new file mode 100644 index 000000000..81f69ed71 --- /dev/null +++ b/shesmu-pluginapi/src/main/java/ca/on/oicr/gsi/shesmu/plugin/authentication/AuthenticationConfigurationAPIkey.java @@ -0,0 +1,20 @@ +package ca.on.oicr.gsi.shesmu.plugin.authentication; + +import java.io.IOException; + +public final class AuthenticationConfigurationAPIkey extends AuthenticationConfiguration { + private String apikey; + + public String getAPIkey() { + return apikey; + } + + @Override + public String prepareAuthentication() throws IOException { + return apikey; + } + + public void setAPIkey(String apikey) { + this.apikey = apikey; + } +} diff --git a/shesmu-pluginapi/src/main/java/ca/on/oicr/gsi/shesmu/plugin/authentication/AuthenticationConfigurationFileAPIkey.java b/shesmu-pluginapi/src/main/java/ca/on/oicr/gsi/shesmu/plugin/authentication/AuthenticationConfigurationFileAPIkey.java new file mode 100644 index 000000000..c001980ca --- /dev/null +++ b/shesmu-pluginapi/src/main/java/ca/on/oicr/gsi/shesmu/plugin/authentication/AuthenticationConfigurationFileAPIkey.java @@ -0,0 +1,22 @@ +package ca.on.oicr.gsi.shesmu.plugin.authentication; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; + +public final class AuthenticationConfigurationFileAPIkey extends AuthenticationConfiguration { + private String apikeyFile; + + public String getAPIkeyFile() { + return apikeyFile; + } + + @Override + public String prepareAuthentication() throws IOException { + return Files.readString(Paths.get(apikeyFile)).trim(); + } + + public void setAPIkeyFile(String apikeyFile) { + this.apikeyFile = apikeyFile; + } +}