From eb77981e41adf30d8ae166ae9b69e3b5ce444b41 Mon Sep 17 00:00:00 2001 From: chubtub <43381989+chubtub@users.noreply.github.com> Date: Thu, 1 Feb 2024 17:46:44 -0500 Subject: [PATCH] Unknown options are caught and reported to the user instead of throwing an exception --- .../src/main/java/hirs/swid/Main.java | 32 +++++++++++++------ .../main/java/hirs/swid/utils/Commander.java | 6 ++++ 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/tools/tcg_rim_tool/src/main/java/hirs/swid/Main.java b/tools/tcg_rim_tool/src/main/java/hirs/swid/Main.java index b252b8cc..9b3a2acd 100644 --- a/tools/tcg_rim_tool/src/main/java/hirs/swid/Main.java +++ b/tools/tcg_rim_tool/src/main/java/hirs/swid/Main.java @@ -21,8 +21,15 @@ public class Main { jc.parse(args); SwidTagGateway gateway; ReferenceManifestValidator validator; + List unknownOpts = commander.getUnknownOptions(); - if (commander.isHelp()) { + if (!unknownOpts.isEmpty()) { + StringBuilder sb = new StringBuilder("Unknown options encountered: "); + for (String opt : unknownOpts) { + sb.append(opt + ", "); + } + exitWithErrorCode(sb.substring(0,sb.lastIndexOf(","))); + } else if (commander.isHelp()) { jc.usage(); System.out.println(commander.printHelpExamples()); } else if (commander.isVersion()) { @@ -57,8 +64,7 @@ public class Main { } validator.validateSwidtagFile(verifyFile); } else { - System.out.println("Need a RIM file to validate!"); - System.exit(1); + exitWithErrorCode("A RIM file was not found for validation."); } } else { gateway = new SwidTagGateway(); @@ -92,13 +98,11 @@ public class Main { gateway.setDefaultCredentials(true); gateway.setJksTruststoreFile(SwidTagConstants.DEFAULT_KEYSTORE_FILE); } else { - System.out.println("A private key (-k) and public certificate (-p) " + + exitWithErrorCode("A private key (-k) and public certificate (-p) " + "are required, or the default key (-d) must be indicated."); - System.exit(1); } if (rimEventLog.isEmpty()) { - System.out.println("Error: a support RIM is required!"); - System.exit(1); + exitWithErrorCode("A support RIM is required."); } else { gateway.setRimEventLog(rimEventLog); } @@ -110,18 +114,28 @@ public class Main { gateway.setTimestampArgument(timestampArguments.get(1)); } } else { - System.exit(1); + exitWithErrorCode("The provided timestamp argument(s) " + + "is/are not valid."); } } gateway.generateSwidTag(commander.getOutFile()); break; default: - System.out.println("No create type given, nothing to do"); + exitWithErrorCode("No create type given, nothing to do"); } } } } + /** + * Use cases that exit with an error code are redirected here. + */ + private static void exitWithErrorCode(String errorMessage) { + //TODO: log errorMessage + System.out.println(errorMessage); + System.exit(1); + } + /** * This method parses the version number from the jar filename in the absence of * the VERSION file expected with an rpm installation. diff --git a/tools/tcg_rim_tool/src/main/java/hirs/swid/utils/Commander.java b/tools/tcg_rim_tool/src/main/java/hirs/swid/utils/Commander.java index 4307083d..85adc77b 100644 --- a/tools/tcg_rim_tool/src/main/java/hirs/swid/utils/Commander.java +++ b/tools/tcg_rim_tool/src/main/java/hirs/swid/utils/Commander.java @@ -12,6 +12,8 @@ import java.util.List; */ public class Commander { + @Parameter(description = "This parameter catches all unrecognized arguments.") + private List unknownOptions = new ArrayList<>(); @Parameter(names = {"-h", "--help"}, help = true, description = "Print this help text.") private boolean help; @Parameter(names = {"-c", "--create \"base\""}, order = 0, @@ -58,6 +60,10 @@ public class Commander { "\tRFC3339 [yyyy-MM-ddThh:mm:ssZ]\n\tRFC3852 ") private List timestampArguments = new ArrayList(2); + public List getUnknownOptions() { + return unknownOptions; + } + public boolean isHelp() { return help; }