Initial Commit
All checks were successful
Locusworks Team/hash-validator/pipeline/head This commit looks good

This commit is contained in:
2020-06-11 12:33:45 -05:00
commit c7334711f9
9 changed files with 546 additions and 0 deletions

6
.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
.classpath
.project
.settings/
target/
*.pdf*
*.txt

186
Jenkinsfile vendored Normal file
View File

@ -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

42
README.md Normal file
View File

@ -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 ###
```
<dependency>
<groupId>net.locusworks</groupId>
<artifactId>hash-validator<artifactId>
<version>CURRENT_VERSION</version>
</dependency>
```
### 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)

98
pom.xml Normal file
View File

@ -0,0 +1,98 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>net.locusworks</groupId>
<artifactId>hash-validator</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hash-validator</name>
<description>Validates Hashes</description>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<nexus.repo>https://nexus.locusworks.net</nexus.repo>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>net.locusworks.hashvalidator.Entry</mainClass>
</transformer>
</transformers>
<finalName>HashValidator</finalName>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.14</version>
</dependency>
<dependency>
<groupId>com.beust</groupId>
<artifactId>jcommander</artifactId>
<version>1.78</version>
</dependency>
</dependencies>
<distributionManagement>
<snapshotRepository>
<id>nexus-snapshot</id>
<url>${nexus.repo}/repository/locusworks-snapshot/</url>
</snapshotRepository>
<repository>
<id>nexus-release</id>
<url>${nexus.repo}/repository/locusworks-release/</url>
</repository>
</distributionManagement>
<repositories>
<repository>
<id>locusworks-public</id>
<name>locusworks-public</name>
<url>${nexus.repo}/repository/locusworks-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>locusworks-public</id>
<url>${nexus.repo}/repository/locusworks-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>

View File

@ -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<Path> files;
@Parameter(names = {"-h", "--hashes"}, description = "List of hashes to go along with the files", order = 3, variableArity = true)
public List<String> 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<Path> getFiles() {
return files;
}
/**
* @return the files
*/
public final List<String> getHashes() {
return hashes;
}
/**
* @return the string
*/
public final String getString() {
return string;
}
/**
* @return the help
*/
public boolean isHelp() {
return help;
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1,7 @@
package net.locusworks.hashvalidator.enums;
public enum HashType {
MD5,
SHA1,
SHA512
}

View File

@ -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<HashType>{
@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");
}
}
}

View File

@ -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<Path> {
@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;
}
}