From 33c15faf9dfb01c886ac007a28b4b8f4d36318d5 Mon Sep 17 00:00:00 2001 From: Ryan Doherty Date: Sun, 11 Aug 2024 22:14:59 -0400 Subject: [PATCH] Factor secret key file reader into separate class and add cli utility to generate value used --- Model/bin/readSecretKey | 3 ++ .../gusdb/wdk/model/config/ModelConfig.java | 7 ++-- .../wdk/model/config/SecretKeyReader.java | 35 +++++++++++++++++++ 3 files changed, 40 insertions(+), 5 deletions(-) create mode 100755 Model/bin/readSecretKey create mode 100644 Model/src/main/java/org/gusdb/wdk/model/config/SecretKeyReader.java diff --git a/Model/bin/readSecretKey b/Model/bin/readSecretKey new file mode 100755 index 000000000..9e786af42 --- /dev/null +++ b/Model/bin/readSecretKey @@ -0,0 +1,3 @@ +#!/bin/bash + +fgpJava org.gusdb.wdk.model.config.SecretKeyReader $@ diff --git a/Model/src/main/java/org/gusdb/wdk/model/config/ModelConfig.java b/Model/src/main/java/org/gusdb/wdk/model/config/ModelConfig.java index af2b74436..5b9663bf8 100644 --- a/Model/src/main/java/org/gusdb/wdk/model/config/ModelConfig.java +++ b/Model/src/main/java/org/gusdb/wdk/model/config/ModelConfig.java @@ -1,15 +1,12 @@ package org.gusdb.wdk.model.config; -import java.io.FileReader; import java.io.IOException; import java.nio.file.Path; import java.util.List; import java.util.Optional; import org.apache.log4j.Logger; -import org.gusdb.fgputil.EncryptionUtil; import org.gusdb.fgputil.FormatUtil; -import org.gusdb.fgputil.IoUtil; import org.gusdb.fgputil.Named.NamedObject; import org.gusdb.oauth2.client.KeyStoreTrustManager.KeyStoreConfig; import org.gusdb.oauth2.client.OAuthConfig; @@ -302,8 +299,8 @@ public ModelConfigAppDB getAppDB() { */ public String getSecretKey() { if (_secretKey == null && _secretKeyFile.isPresent()) { - try (FileReader in = new FileReader(_secretKeyFile.get().toFile())) { - _secretKey = EncryptionUtil.md5(IoUtil.readAllChars(in).strip()); + try { + _secretKey = SecretKeyReader.readSecretKey(_secretKeyFile); } catch (IOException e) { // log error but otherwise ignore so null is returned; problem may be remedied in the future diff --git a/Model/src/main/java/org/gusdb/wdk/model/config/SecretKeyReader.java b/Model/src/main/java/org/gusdb/wdk/model/config/SecretKeyReader.java new file mode 100644 index 000000000..30af7de87 --- /dev/null +++ b/Model/src/main/java/org/gusdb/wdk/model/config/SecretKeyReader.java @@ -0,0 +1,35 @@ +package org.gusdb.wdk.model.config; + +import java.io.FileReader; +import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Optional; + +import org.gusdb.fgputil.EncryptionUtil; +import org.gusdb.fgputil.IoUtil; + +public class SecretKeyReader { + + public static void main(String[] args) { + if (args.length != 1) { + System.err.println("USAGE: readSecretKey "); + System.exit(1); + } + Path path = Paths.get(args[0]); + try { + System.out.print(readSecretKey(Optional.of(path))); + } + catch (Exception e) { + e.printStackTrace(System.err); + System.exit(2); + } + } + + public static String readSecretKey(Optional secretKeyFile) throws IOException { + try (FileReader in = new FileReader(secretKeyFile.get().toFile())) { + return EncryptionUtil.md5(IoUtil.readAllChars(in).strip()); + } + } + +}