From c7334711f9006aaec61474cdea21fe7ae906b2c2 Mon Sep 17 00:00:00 2001 From: Isaac Parenteau Date: Thu, 11 Jun 2020 12:33:45 -0500 Subject: [PATCH] Initial Commit --- .gitignore | 6 + Jenkinsfile | 186 ++++++++++++++++++ README.md | 42 ++++ pom.xml | 98 +++++++++ .../net/locusworks/hashvalidator/Args.java | 64 ++++++ .../net/locusworks/hashvalidator/Entry.java | 90 +++++++++ .../hashvalidator/enums/HashType.java | 7 + .../utils/HashTypeConverter.java | 27 +++ .../hashvalidator/utils/PathConverter.java | 26 +++ 9 files changed, 546 insertions(+) create mode 100644 .gitignore create mode 100644 Jenkinsfile create mode 100644 README.md create mode 100644 pom.xml create mode 100644 src/main/java/net/locusworks/hashvalidator/Args.java create mode 100644 src/main/java/net/locusworks/hashvalidator/Entry.java create mode 100644 src/main/java/net/locusworks/hashvalidator/enums/HashType.java create mode 100644 src/main/java/net/locusworks/hashvalidator/utils/HashTypeConverter.java create mode 100644 src/main/java/net/locusworks/hashvalidator/utils/PathConverter.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e658ba1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +.classpath +.project +.settings/ +target/ +*.pdf* +*.txt diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..f7e1655 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,186 @@ +#!groovy + +// Required Jenkins plugins: +// https://wiki.jenkins-ci.org/display/JENKINS/Timestamper +// https://wiki.jenkins-ci.org/display/JENKINS/Static+Code+Analysis+Plug-ins +// https://wiki.jenkins-ci.org/display/JENKINS/Checkstyle+Plugin ? +// https://wiki.jenkins-ci.org/display/JENKINS/FindBugs+Plugin +// https://wiki.jenkins-ci.org/display/JENKINS/PMD+Plugin ? +// https://wiki.jenkins-ci.org/display/JENKINS/DRY+Plugin ? +// https://wiki.jenkins-ci.org/display/JENKINS/Task+Scanner+Plugin +// https://wiki.jenkins-ci.org/display/JENKINS/Javadoc+Plugin +// https://wiki.jenkins-ci.org/display/JENKINS/JaCoCo+Plugin ? + + +init() + +def branch_name +def branch_name_base +def build_number +def build_url +def git_commit +def job_name +def tag +def version +def build_type +def display_name + +def init() { + + // Keep the 5 most recent builds + properties([[$class: 'BuildDiscarderProperty', strategy: [$class: 'LogRotator', numToKeepStr: '5']]]) + + build_number = env.BUILD_NUMBER + build_url = env.BUILD_URL + job_name = "${env.JOB_NAME}" + branch_name = env.BRANCH_NAME + branch_name_docker = branch_name.replaceAll(/\//,'.') + persist = "/var/lib/jenkins/PERSIST/${branch_name_docker}" + + // execute the branch type specific pipeline code + try { + + if (branch_name.indexOf('release/')==0) build_type='release' + if (branch_name.indexOf('feature/')==0) build_type='feature' + if (branch_name.indexOf('develop')==0) build_type='develop' + if (branch_name.indexOf('hotfix')==0) build_type='hotfix' + if (branch_name.indexOf('bugfix')==0) build_type='bugfix' + if (branch_name.indexOf('master')==0) build_type='master' + + // common pipeline elements + node('master') { + Initialize() + SetVersion(build_type) + print_vars() // after SetVersion - all variables now defined + set_result('INPROGRESS') + Build() // builds database via flyway migration + } + + if (branch_name.indexOf('develop')==0) { + node('master') { + Deploy(); + } + } else if (branch_name.indexOf('release/')==0) { + node('master') { + Deploy(); + } + } + + node('master') { + set_result('SUCCESS') + } + + } catch (err) { + node() { + set_result('FAILURE') + } + throw err + } +} + +def Build() { + stage ('build') { + mvn "install -DskipTests=true -Dbuild.revision=${git_commit}" + step([$class: 'ArtifactArchiver', artifacts: '**/target/*.jar', fingerprint: true]) + } +} + +def Initialize() { + stage ('initialize') { + + // get new code + checkout scm + + git_commit = getSha1() + } +} + +def Deploy() { + stage ('deploy') { + mvn "deploy -DskipTests=true -Dbuild.number=${build_number} -Dbuild.revision=${git_commit}" + } +} + +def getSha1() { + sha1 = sh(script: 'git rev-parse HEAD', returnStdout: true).trim() + echo "sha1 is ${sha1}" + return sha1 +} + +def mvn(args) { + withMaven( + maven: 'maven-3.6.1', + globalMavenSettingsConfig: 'locusworks-settings' + ) { + + sh "mvn ${args}" + + } +} + +def mvn_initial(args) { + mvn(args) +} + +def set_result(status) { + if ( status == 'SUCCESS' ) { + currentBuild.result = status + notify_bitbucket('SUCCESSFUL') + } else if ( status == 'FAILURE' ) { + currentBuild.result = status + notify_bitbucket('FAILED') + } else if ( status == 'INPROGRESS' ) { + notify_bitbucket('INPROGRESS') + } else { + error ("unknown status") + } + + // save in persistence file for access the status page + // make sure the directory exists first + sh "mkdir -p $persist && echo $status > $persist/build.result" +} + +def notify_bitbucket(state) { +} + +def print_vars() { + echo "build_number = ${build_number}" + echo "build_url = ${build_url}" + echo "job_name = ${job_name}" + echo "branch_name = ${branch_name}" + echo "branch_name_base = ${branch_name_base}" + echo "build_type = ${build_type}" + echo "display_name = ${currentBuild.displayName}" + echo "version = ${version}" + echo "git_commit = ${git_commit}" + +} + +def SetVersion( v ) { + stage ('set version') { + echo "set version ${v}" + branch_name_base = (branch_name =~ /([^\/]+$)/)[0][0] + if ( v == 'release' ) { + // for release branches, where the branch is named "release/1.2.3", + // derive the version and display name derive from the numeric suffix and append the build number + // 3.2.1.100 + version = branch_name_base + "." + build_number + "-RELEASE"; + //version = branch_name.substring('release/'.length()) + "." + build_number + currentBuild.displayName = version + } else if (v == 'develop') { + version = branch_name_base + "." + build_number + "-SNAPSHOT"; + currentBuild.displayName = version + } else { + // for all other branches the version number is 0 with an appended build number + // and for the display name use the jenkins default #n and add the branch name + // #101 - feature/user/foo + //version = '0.' + build_number + version = branch_name_base + "." + build_number + currentBuild.displayName = "#" + build_number + " - " + branch_name_base + } + display_name = currentBuild.displayName + mvn_initial "versions:set -DnewVersion=${version}" + } +} + +return this \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..9dcf900 --- /dev/null +++ b/README.md @@ -0,0 +1,42 @@ +# README # + +Hash Validator will spit out a files hash either md5, sha1 or sha512 + +### What is this repository for? ### + +* Hash Validator +* 1.0.1 + +### How do I get set up? ### +### Maven ### +``` + + net.locusworks + hash-validator + CURRENT_VERSION + +``` + +### Command Line ### + +``` +java -jar HashValidator.jar +``` +``` +Usage: Hash Validator [options] + Options: + * -t, --type + Hash Type + Possible Values: [MD5, SHA1, SHA512] + -f, --files + List of files to check + -h, --hashes + List of hashes to go along with the files + -s, --string + String value to hash + --help + +``` +### Who do I talk to? ### + +* Isaac Parenteau (iparenteau@locusworks.net) \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..8e92d32 --- /dev/null +++ b/pom.xml @@ -0,0 +1,98 @@ + + 4.0.0 + net.locusworks + hash-validator + 0.0.1-SNAPSHOT + hash-validator + Validates Hashes + + 1.8 + 1.8 + https://nexus.locusworks.net + + + + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.2 + + + package + + shade + + + + + net.locusworks.hashvalidator.Entry + + + HashValidator + + + + + + + + + + + + commons-codec + commons-codec + 1.14 + + + + com.beust + jcommander + 1.78 + + + + + + + + nexus-snapshot + ${nexus.repo}/repository/locusworks-snapshot/ + + + nexus-release + ${nexus.repo}/repository/locusworks-release/ + + + + + + locusworks-public + locusworks-public + ${nexus.repo}/repository/locusworks-public/ + + true + + + true + + + + + + + locusworks-public + ${nexus.repo}/repository/locusworks-public/ + + true + + + true + + + + \ No newline at end of file diff --git a/src/main/java/net/locusworks/hashvalidator/Args.java b/src/main/java/net/locusworks/hashvalidator/Args.java new file mode 100644 index 0000000..97e55d1 --- /dev/null +++ b/src/main/java/net/locusworks/hashvalidator/Args.java @@ -0,0 +1,64 @@ +package net.locusworks.hashvalidator; + +import java.nio.file.Path; +import java.util.List; + +import com.beust.jcommander.Parameter; + +import net.locusworks.hashvalidator.enums.HashType; +import net.locusworks.hashvalidator.utils.HashTypeConverter; +import net.locusworks.hashvalidator.utils.PathConverter; + +public class Args { + + @Parameter(names = {"-t", "--type"}, description = "Hash Type", order = 1, required = true, converter = HashTypeConverter.class) + public HashType hashType; + + @Parameter(names = {"-f", "--files"}, description = "List of files to check", order = 2, converter = PathConverter.class, variableArity = true) + public List files; + + @Parameter(names = {"-h", "--hashes"}, description = "List of hashes to go along with the files", order = 3, variableArity = true) + public List hashes; + + @Parameter(names = {"-s", "--string"}, description = "String value to hash", order = 4) + public String string; + + @Parameter(names = {"--help"}, help = true, order = 99) + private boolean help; + + /** + * @return the hashType + */ + public final HashType getHashType() { + return hashType; + } + + /** + * @return the files + */ + public final List getFiles() { + return files; + } + + /** + * @return the files + */ + public final List getHashes() { + return hashes; + } + + /** + * @return the string + */ + public final String getString() { + return string; + } + + /** + * @return the help + */ + public boolean isHelp() { + return help; + } + +} diff --git a/src/main/java/net/locusworks/hashvalidator/Entry.java b/src/main/java/net/locusworks/hashvalidator/Entry.java new file mode 100644 index 0000000..6d63651 --- /dev/null +++ b/src/main/java/net/locusworks/hashvalidator/Entry.java @@ -0,0 +1,90 @@ +package net.locusworks.hashvalidator; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; + +import org.apache.commons.codec.digest.DigestUtils; + +import com.beust.jcommander.JCommander; + +import net.locusworks.hashvalidator.enums.HashType; + +public class Entry { + + public static void main(String[] argv) throws IOException { + Args args = new Args(); + JCommander commander = JCommander.newBuilder().addObject(args).programName("Hash Validator").build(); + try { + commander.parse(argv); + } catch (Exception ex) { + System.err.println(ex.getMessage()); + commander.usage(); + System.exit(-1); + } + + if (args.getString() != null && !args.getString().trim().isEmpty()) { + String val = args.getString().trim(); + String hash = ""; + switch (args.getHashType()) { + case MD5: + hash = DigestUtils.md5Hex(val); + break; + case SHA1: + hash = DigestUtils.sha1Hex(val); + break; + case SHA512: + hash = DigestUtils.sha512Hex(val); + break; + default: + break; + } + System.out.println(String.format("The %s hash for %s is %s", args.getHashType(), val, hash)); + } + + if (args.getFiles() == null || args.getFiles().size() == 0) return; + + if(args.getHashes() != null && (args.getHashes().size() != args.getFiles().size())) + throw new IllegalArgumentException("The number of entered hashes do not match the number of files entered"); + + if (args.getHashes() != null) { + for (int i = 0; i < args.getFiles().size(); i++) { + Path file = args.getFiles().get(i); + String expectedHash = args.getHashes().get(i).trim(); + String actualHash = getHash(file, args.getHashType()); + if (actualHash.equalsIgnoreCase(expectedHash)) { + System.out.println(String.format("%s matches the %s hash of %s", file.getFileName(), args.getHashType(), actualHash)); + } else { + System.out.println(String.format("%s %s hash do not match. Expected: %s -> Actual: %s", + file.getFileName(), args.getHashType(), expectedHash, actualHash)); + } + } + return; + } + + for(Path file : args.getFiles()) { + String hash = getHash(file, args.getHashType()); + System.out.println(String.format("%s %s hash is %s", file.getFileName(), args.getHashType(), hash)); + } + + } + + private static String getHash(Path file, HashType hashType) throws IOException { + String hash = null; + switch(hashType) { + case MD5: + hash = DigestUtils.md5Hex(Files.newInputStream(file)); + break; + case SHA1: + hash = DigestUtils.sha1Hex(Files.newInputStream(file)); + break; + case SHA512: + hash = DigestUtils.sha512Hex(Files.newInputStream(file)); + break; + default: + break; + } + return hash; + } + +} diff --git a/src/main/java/net/locusworks/hashvalidator/enums/HashType.java b/src/main/java/net/locusworks/hashvalidator/enums/HashType.java new file mode 100644 index 0000000..5868d3d --- /dev/null +++ b/src/main/java/net/locusworks/hashvalidator/enums/HashType.java @@ -0,0 +1,7 @@ +package net.locusworks.hashvalidator.enums; + +public enum HashType { + MD5, + SHA1, + SHA512 +} diff --git a/src/main/java/net/locusworks/hashvalidator/utils/HashTypeConverter.java b/src/main/java/net/locusworks/hashvalidator/utils/HashTypeConverter.java new file mode 100644 index 0000000..da2b5cb --- /dev/null +++ b/src/main/java/net/locusworks/hashvalidator/utils/HashTypeConverter.java @@ -0,0 +1,27 @@ +package net.locusworks.hashvalidator.utils; + +import com.beust.jcommander.IStringConverter; + +import net.locusworks.hashvalidator.enums.HashType; + +public class HashTypeConverter implements IStringConverter{ + + @Override + public HashType convert(String value) { + if (value == null || value.trim().isEmpty()) throw new IllegalArgumentException("Hash Type cannot be blank"); + + switch(value.trim().toLowerCase()) { + case "md5": + return HashType.MD5; + case "sha1": + case "sha-1": + return HashType.SHA1; + case "sha512": + case "sha-512": + return HashType.SHA512; + default: + throw new IllegalArgumentException("Hash Type cannot be blank"); + } + } + +} diff --git a/src/main/java/net/locusworks/hashvalidator/utils/PathConverter.java b/src/main/java/net/locusworks/hashvalidator/utils/PathConverter.java new file mode 100644 index 0000000..57a704f --- /dev/null +++ b/src/main/java/net/locusworks/hashvalidator/utils/PathConverter.java @@ -0,0 +1,26 @@ +package net.locusworks.hashvalidator.utils; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +import com.beust.jcommander.IStringConverter; + +public class PathConverter implements IStringConverter { + + @Override + public Path convert(String value) { + Path path = Paths.get(value); + + if (Files.notExists(path)) { + throw new IllegalArgumentException(value + " does not exist. Please check entry"); + } + + if (!Files.isRegularFile(path)) { + throw new IllegalArgumentException(value + " is not a file. Please check entry"); + } + + return path; + } + +}