-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApp.java
More file actions
190 lines (174 loc) · 8.17 KB
/
Copy pathApp.java
File metadata and controls
190 lines (174 loc) · 8.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import com.trend.cloudone.amaas.AMaasClient;
import com.trend.cloudone.amaas.AMaasException;
import com.trend.cloudone.amaas.AMaasScanOptions;
public final class App {
private static final Logger logger = Logger.getLogger(App.class.getName());
private App() {
}
private static void info(final String msg, final Object... params) {
logger.log(Level.INFO, msg, params);
}
private static String[] listFiles(final String pathName) {
File fObj = new File(pathName);
if (!fObj.isDirectory()) {
return new String[]{pathName};
}
return Stream.of(fObj.listFiles())
.filter(file -> !file.isDirectory())
.map(File::getPath)
.collect(Collectors.toList()).toArray(new String[] {});
}
static void scanFilesInSequential(final AMaasClient client, final String[] fList, final boolean digest, final AMaasScanOptions options) {
for (String fileName: fList) {
try {
info("===============> Scanning file {0}", fileName);
long startTS = System.currentTimeMillis();
String scanResult = client.scanFile(fileName, digest, options);
long endTS = System.currentTimeMillis();
info("{0}", scanResult);
info("===============> File scan time {0}", endTS - startTS);
} catch (AMaasException err) {
info("Exception {0}", err.getMessage());
}
}
}
private static Options getCmdOptions() {
Options optionList = new Options();
optionList.addRequiredOption("f", "filename", true, "File path or folder to be scanned");
optionList.addRequiredOption("k", "apikey", true, "Vision One API key");
optionList.addRequiredOption("r", "region", true, "AMaaS service region to used. Ignore if self hosted.");
optionList.addOption("a", "addr", true, "host ip address of self hosted AMaaS scanner. Ignore if to use Trend AMaaS service");
optionList.addOption("t", "timeout", true, "Per scan timeout in seconds");
optionList.addOption(null, "tags", true, "commas separated string of tags.e.g, sdk,dev");
optionList.addOption(null, "pml", true, "Enable predictive machine language detection");
optionList.addOption(null, "feedback", true, "Enable Trend Smart Protection Network's Smart Feedback");
optionList.addOption("v", "verbose", true, "Enable log verbose mode");
optionList.addOption(null, "ca_cert", true, "CA Certificate of hosted AMaaS Scanner server");
optionList.addOption(null, "digest", true, "Enable/Disable calculation of digests for cache search and result lookup");
optionList.addOption(null, "active_content", true, "Enable active content scanning. Default to false");
return optionList;
}
/**
* The program takes 4 options and respective values to configure the AMaaS SDK client.
* @param args Input options:
* -f a file or a directory to be scanned
* -k the API key or bearer authentication token
* -r region where the Vision One API key was obtained. eg, us-east-1. If host is given, region value will be ignored.
* -a host ip address of self hosted AMaaS scanner. Ignore if to use Trend AMaaS service.
* -t optional client maximum waiting time in seconds for a scan. 0 or missing means default.
* --tags a commas separated string of tags. e.g. dev,sdk
* --pml enable predictive machine language detection. default to false
* --feedback enable Trend Micro Smart Protection Network's Smart Feedback. default to false
* -v enable log verbose mode. default to false
* --ca_cert CA certificate of self hosted AMaaS server
* --digest Enable/Disable calculation of digests for cache search and result lookup
* --active_content Enable active content scanning. Default to false
*/
public static void main(final String[] args) {
String pathname = "";
String apikey = null;
String region = "";
String addr = "";
long timeout = 0;
String tags = null;
boolean pmlFlag = false;
boolean feedbackFlag = false;
boolean verbose = false;
String caCertPath = null;
boolean digest = true;
boolean activeContent = false;
DefaultParser parser = new DefaultParser();
HelpFormatter helper = new HelpFormatter();
Options optionList = getCmdOptions();
try {
CommandLine cmd = parser.parse(optionList, args);
if (cmd.hasOption("f")) {
pathname = cmd.getOptionValue("f");
}
if (cmd.hasOption("k")) {
apikey = cmd.getOptionValue("k");
}
if (cmd.hasOption("r")) {
region = cmd.getOptionValue("r");
}
if (cmd.hasOption("a")) {
addr = cmd.getOptionValue("a");
}
if (cmd.hasOption("t")) {
timeout = Long.parseLong(cmd.getOptionValue("t"));
}
if (cmd.hasOption("tags")) {
tags = cmd.getOptionValue("tags");
}
if (cmd.hasOption("pml")) {
if (cmd.getOptionValue("pml").equals("true")) {
pmlFlag = true;
}
}
if (cmd.hasOption("feedback")) {
if (cmd.getOptionValue("feedback").equals("true")) {
feedbackFlag = true;
}
}
if (cmd.hasOption("v")) {
if (cmd.getOptionValue("v").equals("true")) {
verbose = true;
}
}
if (cmd.hasOption("ca_cert")) {
caCertPath = cmd.getOptionValue("ca_cert");
}
if (cmd.hasOption("digest")) {
if (cmd.getOptionValue("digest").equals("false")) {
digest = false;
}
}
if (cmd.hasOption("active_content")) {
if (cmd.getOptionValue("active_content").equals("true")) {
activeContent = true;
}
}
String[] tagList = null;
if (tags != null) {
info("tags to used {0}", tags);
tagList = tags.split(",");
}
AMaasClient client = new AMaasClient(region, addr, apikey, timeout, true, caCertPath);
try {
String[] listOfFiles = listFiles(pathname);
long totalStartTs = System.currentTimeMillis();
AMaasScanOptions options = AMaasScanOptions.builder()
.pml(pmlFlag)
.feedback(feedbackFlag)
.verbose(verbose)
.tagList(tagList)
.activeContent(activeContent)
.build();
scanFilesInSequential(client, listOfFiles, digest, options);
long totalEndTs = System.currentTimeMillis();
info("*************** Total scan time {0}", totalEndTs - totalStartTs);
} finally {
// Ensure client resources are properly closed
client.close();
}
} catch (ParseException err) {
helper.printHelp("Usage:", optionList);
} catch (NumberFormatException err) {
info("Exception parsing -t value must be a number");
} catch (AMaasException err) {
info("Exception encountered: {0}", err.getMessage());
} catch (Exception err) {
info("Unexpected exception encountered: {0}", err.getMessage());
}
}
}