mirror of
https://github.com/nsacyber/HIRS.git
synced 2024-12-19 04:58:00 +00:00
Validate file arguments with FileArgumentValidator
This commit is contained in:
parent
d7823b6b0b
commit
3fbc0e743c
@ -55,22 +55,14 @@ public class Main {
|
|||||||
String rimel = commander.getRimEventLog();
|
String rimel = commander.getRimEventLog();
|
||||||
String certificateFile = commander.getPublicCertificate();
|
String certificateFile = commander.getPublicCertificate();
|
||||||
String trustStore = commander.getTruststoreFile();
|
String trustStore = commander.getTruststoreFile();
|
||||||
if (!verifyFile.isEmpty()) {
|
|
||||||
validator.setRim(verifyFile);
|
validator.setRim(verifyFile);
|
||||||
if (!rimel.isEmpty()) {
|
|
||||||
validator.setRimEventLog(rimel);
|
validator.setRimEventLog(rimel);
|
||||||
}
|
|
||||||
if (!trustStore.isEmpty()) {
|
|
||||||
validator.setTrustStoreFile(trustStore);
|
validator.setTrustStoreFile(trustStore);
|
||||||
}
|
|
||||||
if (!certificateFile.isEmpty()) {
|
if (!certificateFile.isEmpty()) {
|
||||||
System.out.println("A single cert cannot be used for verification. " +
|
System.out.println("A single cert cannot be used for verification. " +
|
||||||
"The signing cert will be searched for in the trust store.");
|
"The signing cert will be searched for in the trust store.");
|
||||||
}
|
}
|
||||||
validator.validateSwidtagFile(verifyFile);
|
validator.validateSwidtagFile(verifyFile);
|
||||||
} else {
|
|
||||||
exitWithErrorCode("A RIM file was not found for validation.");
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
gateway = new SwidTagGateway();
|
gateway = new SwidTagGateway();
|
||||||
if (commander.isVerbose()) {
|
if (commander.isVerbose()) {
|
||||||
|
@ -19,12 +19,12 @@ public class Commander {
|
|||||||
@Parameter(names = {"-c", "--create \"base\""}, order = 0,
|
@Parameter(names = {"-c", "--create \"base\""}, order = 0,
|
||||||
description = "The type of RIM to create. A base RIM will be created by default.")
|
description = "The type of RIM to create. A base RIM will be created by default.")
|
||||||
private String createType = "";
|
private String createType = "";
|
||||||
@Parameter(names = {"-v", "--verify <path>"}, order = 3,
|
@Parameter(names = {"-v", "--verify <path>"}, validateWith = FileArgumentValidator.class,
|
||||||
description = "Specify a RIM file to verify.")
|
description = "Specify a RIM file to verify.")
|
||||||
private String verifyFile = "";
|
private String verifyFile = "";
|
||||||
@Parameter(names = {"-V", "--version"}, description = "Output the current version.")
|
@Parameter(names = {"-V", "--version"}, description = "Output the current version.")
|
||||||
private boolean version = false;
|
private boolean version = false;
|
||||||
@Parameter(names = {"-a", "--attributes <path>"}, order = 1,
|
@Parameter(names = {"-a", "--attributes <path>"}, validateWith = FileArgumentValidator.class,
|
||||||
description = "The configuration file holding attributes "
|
description = "The configuration file holding attributes "
|
||||||
+ "to populate the base RIM with.")
|
+ "to populate the base RIM with.")
|
||||||
private String attributesFile = "";
|
private String attributesFile = "";
|
||||||
@ -34,14 +34,16 @@ public class Commander {
|
|||||||
private String outFile = "";
|
private String outFile = "";
|
||||||
@Parameter(names = {"--verbose"}, description = "Control output verbosity.")
|
@Parameter(names = {"--verbose"}, description = "Control output verbosity.")
|
||||||
private boolean verbose = false;
|
private boolean verbose = false;
|
||||||
@Parameter(names = {"-t", "--truststore <path>"}, order = 4,
|
@Parameter(names = {"-t", "--truststore <path>"}, validateWith = FileArgumentValidator.class,
|
||||||
description = "The truststore to sign the base RIM created "
|
description = "The truststore to sign the base RIM created "
|
||||||
+ "or to validate the signed base RIM.")
|
+ "or to validate the signed base RIM.")
|
||||||
private String truststoreFile = "";
|
private String truststoreFile = "";
|
||||||
@Parameter(names = {"-k", "--privateKeyFile <path>"}, order = 5,
|
@Parameter(names = {"-k", "--privateKeyFile <path>"},
|
||||||
|
validateWith = FileArgumentValidator.class,
|
||||||
description = "The private key used to sign the base RIM created by this tool.")
|
description = "The private key used to sign the base RIM created by this tool.")
|
||||||
private String privateKeyFile = "";
|
private String privateKeyFile = "";
|
||||||
@Parameter(names = {"-p", "--publicCertificate <path>"}, order = 6,
|
@Parameter(names = {"-p", "--publicCertificate <path>"},
|
||||||
|
validateWith = FileArgumentValidator.class,
|
||||||
description = "The public key certificate to embed in the base RIM created by "
|
description = "The public key certificate to embed in the base RIM created by "
|
||||||
+ "this tool.")
|
+ "this tool.")
|
||||||
private String publicCertificate = "";
|
private String publicCertificate = "";
|
||||||
@ -51,7 +53,7 @@ public class Commander {
|
|||||||
@Parameter(names = {"-d", "--default-key"}, order = 8,
|
@Parameter(names = {"-d", "--default-key"}, order = 8,
|
||||||
description = "Use default signing credentials.")
|
description = "Use default signing credentials.")
|
||||||
private boolean defaultKey = false;
|
private boolean defaultKey = false;
|
||||||
@Parameter(names = {"-l", "--rimel <path>"}, order = 9,
|
@Parameter(names = {"-l", "--rimel <path>"}, validateWith = FileArgumentValidator.class,
|
||||||
description = "The TCG eventlog file to use as a support RIM.")
|
description = "The TCG eventlog file to use as a support RIM.")
|
||||||
private String rimEventLog = "";
|
private String rimEventLog = "";
|
||||||
@Parameter(names = {"--timestamp"}, order = 10, variableArity = true,
|
@Parameter(names = {"--timestamp"}, order = 10, variableArity = true,
|
||||||
|
@ -0,0 +1,33 @@
|
|||||||
|
package hirs.swid.utils;
|
||||||
|
|
||||||
|
import com.beust.jcommander.IParameterValidator;
|
||||||
|
import com.beust.jcommander.ParameterException;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import lombok.extern.log4j.Log4j2;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This class validates arguments that take a String path to a file.
|
||||||
|
* The file path is checked for null, and if the file is found it is checked
|
||||||
|
* for validity, emptiness, and read permissions.
|
||||||
|
*/
|
||||||
|
@Log4j2
|
||||||
|
public class FileArgumentValidator implements IParameterValidator {
|
||||||
|
public void validate(String name, String value) throws ParameterException {
|
||||||
|
try {
|
||||||
|
File file = new File(value);
|
||||||
|
if (!file.isFile()) {
|
||||||
|
throw new ParameterException("Invalid file path: " + value +
|
||||||
|
". Please verify file path.");
|
||||||
|
}
|
||||||
|
if (file.length() == 0) {
|
||||||
|
throw new ParameterException("File " + value + " is empty.");
|
||||||
|
}
|
||||||
|
} catch (NullPointerException e) {
|
||||||
|
throw new ParameterException("File path cannot be null: " + e.getMessage());
|
||||||
|
} catch (SecurityException e) {
|
||||||
|
throw new ParameterException("Read access denied for " + value +
|
||||||
|
", please verify permissions.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user