Skip to content

Commit

Permalink
Migrate from Commons CLI to picocli
Browse files Browse the repository at this point in the history
  • Loading branch information
amrabed committed Nov 29, 2018
1 parent 6c79cd9 commit d986011
Show file tree
Hide file tree
Showing 17 changed files with 172 additions and 306 deletions.
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,12 @@ addons:

script:
- sonar-scanner

deploy:
provider: releases
api_key:
secure: ebHVnn5fwJZ32s79Df099gM5nsF9QJ/KMpQbBEJo4kwRqDEW6dP0f28MrkJSdq337oOI6/uIdvB+Umko0uZ4wMxyX5NyXXfC4H1FZ+0OpdS+ksueguatAP3Wvhng3bse8UBxeMKekQNwRlK3MS+igLOQJrSxyyzchXgj+vWwm4TiheTIY/0I1JrQv/B/VW5EusVDl6WZosc1q2XF0qhtBT5ytHwVTZN9/4KwtFA8e2dyuX+aHyJdfTjSpXkByJtgBepHdAQ4dL3ZyYifBLChSfYoQlbzxwbl0kNTmsnnvcjO4rqjGwBbZrGufgIigr+w9klMzih/90TRo6MFRQo2vc2wUHhSYmAGc2H+RQjR2gPAsHrHizotJg4mWq/KhXGoT1jp5reKfwKnq4giQ0TWI6eE14y1IDDPygQuQNCY6b7UNdbCLhBfEnjdMmDvOhuyx2FmsP2czeDmlL1U99bwz2AcbWTo/AkiW0b4tzQ+Eml8esPOdSu5Fgf9mcdzfirqYCtbzXxxsJXLXdzJNbfOD8N99xdK/kqNcDaIPM+rBSNTsuGk66k+m+QCIGznAG5goCnbD9RU0T2NT/qewi7LLEV31/KaKVoGzULjxhwLmcbBHxsxGBGNTt+6fJLxTUEfce3G6t8PAv2BeCw5Z1MQ/gw9AWwp6tcnLJXfmcM5rB4=
file: "build/libs/rhids-*.jar"
skip_cleanup: true
on:
tags: true
21 changes: 0 additions & 21 deletions analyzer/BoSC.java

This file was deleted.

106 changes: 0 additions & 106 deletions analyzer/Database.java

This file was deleted.

21 changes: 0 additions & 21 deletions analyzer/aggregate/Aggregator.java

This file was deleted.

20 changes: 0 additions & 20 deletions analyzer/compare/Comparator.java

This file was deleted.

21 changes: 13 additions & 8 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
apply plugin: 'java'
apply plugin: 'application'

mainClassName = 'edu.vt.rhids.main.RHIDS'
mainClassName = 'edu.vt.rhids.Main'
sourceCompatibility = 1.8
version = '0.1'
version = '0.2'

task createJar(type: Jar) {
manifest {
attributes 'Implementation-Title': 'RHIDS',
'Main-Class': mainClassName
manifest {
attributes 'Implementation-Title': 'rhids',
'Main-Class': mainClassName
}
baseName = project.name
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
from { configurations.extralibs.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}

configurations {
extralibs
}

repositories {
mavenCentral()
}

dependencies {
implementation 'commons-cli:commons-cli:1.2'
}
implementation 'info.picocli:picocli:3.8.0'
extralibs 'info.picocli:picocli:3.8.0'
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
107 changes: 104 additions & 3 deletions src/main/java/edu/vt/rhids/Main.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,114 @@
package edu.vt.rhids;

import edu.vt.rhids.common.Database;
import edu.vt.rhids.main.Parameters;
import edu.vt.rhids.main.RHIDS;
import edu.vt.rhids.output.Summary;
import edu.vt.rhids.util.Logger;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Help.Visibility;
import picocli.CommandLine.Option;

public class Main {
import java.util.concurrent.Callable;

public static final RHIDS rhids = RHIDS.getInstance();
@Command(name = "rhids", version = "0.2",
description = "Resilient Host-based Intrusion Detection System for Docker Containers",
subcommands = {
CommandLine.HelpCommand.class,
Main.Classifier.class,
Main.Aggregator.class,
Main.Comparator.class
})
@SuppressWarnings("unused")
public class Main implements Runnable {

@Option(names = {"-V", "--version"}, description = "Print version information and exit", versionHelp = true)
boolean isVersionRequested;

@Option(names = {"-h", "--help"}, description = "Print this help message and exit", usageHelp = true)
boolean isHelpRequested;

public static void main(String[] args) {
Logger.log(rhids.run(args), Logger.Verbosity.NONE);
new CommandLine(new Main()).parseWithHandler(new CommandLine.RunFirst(), args);
}

@Override
public void run() {
CommandLine.usage(this, System.out);
}

@Command(name = "classify", description = "Train & test classifier using an input trace file",
sortOptions = false, mixinStandardHelpOptions = true)
public static class Classifier implements Callable<Summary> {
@Option(names = {"-i", "--input-file"}, paramLabel = "<input file>",
description = "Input file path", required = true)
String inputFile;
@Option(names = {"-b", "--database-file"}, paramLabel = "<database file>",
description = "File to read database from")
String databaseFile;
@Option(names = {"-o", "--output-file"}, description = "Output file path", paramLabel = "<input file>", required = true)
String outputFile;
@Option(names = {"-e", "--epoch-size"}, description = "Range for epoch size",
paramLabel = "<start[[:step]:end]>", converter = Parameters.IntegerRange.class,
defaultValue = "2500:2500:50000", showDefaultValue = Visibility.ALWAYS)
Parameters.IntegerRange epochSizeRange;
@Option(names = {"-t", "--train-threshold"}, description = "Range for training threshold",
paramLabel = "<start[[:step]:end]>", converter = Parameters.FloatRange.class,
defaultValue = "0.9f:0.001f:1f", showDefaultValue = Visibility.ALWAYS)
Parameters.FloatRange trainThresholdRange;
@Option(names = {"-d", "--detection-threshold"}, description = "Range for detection threshold",
paramLabel = "<start[[:step]:end]>", converter = Parameters.IntegerRange.class,
defaultValue = "5:5:200", showDefaultValue = Visibility.ALWAYS)
Parameters.IntegerRange testThresholdRange;
@Option(names = {"-v", "--verbosity"}, description = "Verbose level",
defaultValue = "0", showDefaultValue = Visibility.ALWAYS)
int verbosity;

@Override
public Summary call() throws Exception {
Logger.setHandler(outputFile);
Logger.setLevel(verbosity);
final Parameters parameters = new Parameters(inputFile, databaseFile, epochSizeRange, trainThresholdRange, testThresholdRange);
Summary summary = RHIDS.getInstance().run(parameters);
Logger.log(summary, Logger.Verbosity.LOW);
return summary;
}
}

@Command(name = "aggregate", description = "Aggregate two database files into one",
mixinStandardHelpOptions = true)
public static class Aggregator implements Callable<Void> {
@CommandLine.Parameters(index = "0", description = "dump file for first database")
String file1;
@CommandLine.Parameters(index = "1", description = "dump file for second database")
String file2;
@CommandLine.Parameters(index = "2", description = "output dump file of aggregate database")
String output;

@Override
public Void call() throws Exception {
final Database db1 = new Database(file1);
db1.commit(new Database(file2));
db1.dump(output);
return null;
}
}

@Command(name = "compare", description = "Calculate similarity matrix between two database files", mixinStandardHelpOptions = true)
public static class Comparator implements Callable<Double> {
@CommandLine.Parameters(description = "dump file for first database")
String file1;
@CommandLine.Parameters(description = "dump file for second database")
String file2;

@Override
public Double call() throws Exception {
final Database db1 = new Database(file1);
final Database db2 = new Database(file2);
final double similarity = db1.calculateSimilarity(db2);
Logger.log("Similarity: " + similarity, Logger.Verbosity.LOW);
return similarity;
}
}
}
3 changes: 1 addition & 2 deletions src/main/java/edu/vt/rhids/common/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ public double calculateSimilarity(Database other) {
return dot / (norm1 * norm2);
}

public void dump(String id) throws FileNotFoundException {
final String file = "/var/log/rhids/db-" + id + ".dump";
public void dump(String file) throws FileNotFoundException {
try (PrintStream out = new PrintStream(file)) {
Logger.log("Dumping database to " + file, Verbosity.MEDIUM);
for (Entry<BoSC, Long> entry : entrySet()) {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/edu/vt/rhids/input/BoSC.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package edu.vt.rhids.input;

import edu.vt.rhids.Main;
import edu.vt.rhids.main.RHIDS;

import java.util.ArrayList;
import java.util.Collections;
Expand All @@ -14,9 +14,9 @@ public class BoSC extends ArrayList<Byte> {
private static final long serialVersionUID = 1L;

BoSC(Window window) {
super(Collections.nCopies(Main.rhids.getIndexMap().size() + 1, (byte) 0));
super(Collections.nCopies(RHIDS.getInstance().getIndexMap().size() + 1, (byte) 0));
for (String syscall : window) {
int index = Main.rhids.getIndexMap().get(syscall);
int index = RHIDS.getInstance().getIndexMap().get(syscall);
Byte count = get(index);
set(index, ++count);
}
Expand Down
Loading

0 comments on commit d986011

Please sign in to comment.