forked from AdamHowardIRL/sanitizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSanitizer.groovy
710 lines (663 loc) · 18.2 KB
/
Sanitizer.groovy
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
package adam.howard
import java.io.BufferedReader
import java.io.File
import java.io.FileNotFoundException
import java.io.FileReader
import java.io.IOException
import java.sql.Timestamp
import java.util.ArrayList
import java.util.Collection
import java.util.HashMap
import java.util.Iterator
import java.util.Scanner
import java.util.regex.Matcher
import java.util.regex.Pattern
/**
* @author Adam Howard
* @since 4/6/2017
* @version 1.0.0.0
*
*/
public class Sanitizer {
//Locals
public static String jenkinsFile = ""
public static String whiteList = ""
public static String blackList = ""
private static def linesMand = new ArrayList<Integer>()
private static def linesBlack = new ArrayList<Integer>()
private static boolean verboseSwitch = false
private static def mandMap = new HashMap<String, Boolean>()
private static def blackMap = new HashMap<String, Boolean>()
private static def mandLinesFound = new ArrayList<Integer>()
private static def blackLinesFound = new ArrayList<Integer>()
private static def blackNameFound = new ArrayList<String>()
private static def mandNameFound = new ArrayList<String>()
private static final String helpSwitch = "-h"
private static final String mandSwitch = "-m"
private static final String blackSwitch = "-b"
private static final String jenkSwitch = "-j"
private static final String verbSwitch = "-v"
private static boolean wantHelp = false
private static final String whitelistedChars = "[^a-z0-9A-Z-.\\[\\]]"
private static char[] acceptedLeft = [':', '(', '[', ' ', '{', '@','/','\\',';','#','$','*','\"','\'',']','&','!','+','?','|',';',')','.','=','=','='];
private static char[] acceptedRight = [':', ')', ']', ' ', '}', '@','/','\\',';','#','$','*','\"','\'','[','&','!','+','?','|',';','(','.','=','=','='];
private static File jenkFile = null
private static File mand = null
private static File black = null
private static String[] lineNumber = ""
private static Scanner whiteScan = null
private static Scanner blackScan = null
private static Scanner jenkScan = null
private static def white = new ArrayList<String>()
private static def blackArray = new ArrayList<String>()
private static int exitCode = 0
private static boolean isMandatoryThere = false
private static boolean isBlacklistedThere = false
private static int foundLineCounter = 0
private static ArrayList<Integer> commentedLines = new ArrayList<Integer>();
private static boolean whitelistedEmpty = false
private static boolean blacklistedEmpty = false
/**The bulk of the program
* @param args Command line arguments
*/
//@SuppressWarnings("unchecked")
public static void main(String[] args) {
try{
//Parse arguments and fill variables.
for(int i = 0; i < args.length; i++){
//Input Verification. Removes any characters not in whitelistedChars.
args[i] = args[i].replaceAll(whitelistedChars, "");
if (args[i].contains(jenkSwitch))
jenkinsFile = args[i+1]
else if (args[i].equalsIgnoreCase(blackSwitch))
blackList = args[i+1]
else if (args[i].equalsIgnoreCase(mandSwitch) || args[i].equalsIgnoreCase("-w"))
whiteList = args[i+1]
else if (args[i].equalsIgnoreCase(helpSwitch) || args[i].equalsIgnoreCase("/?"))
wantHelp = true
else if (args[i].equalsIgnoreCase(verbSwitch))
verboseSwitch = true
else if (args.length == 0)
printUsage()
}
} catch (Exception ex){
println("Problem reading arguments")
//printUsage()
}
if(wantHelp){
println("This program requires three files as arguments. \n ")
printUsage()
}
//When verbose, print files that have been inputted.
if(verboseSwitch){
println(getTimeStampNow() + " INFO: File to test: " + jenkinsFile)
println(getTimeStampNow() + " INFO: File whitelist: " + whiteList)
println(getTimeStampNow() + " INFO: File blacklist: " + blackList)
}
//Print that file are about to be loaded.
if(verboseSwitch)
println("\n" + getTimeStampNow() + " Loading files\n")
//Load the files
try{
jenkFile = new File(jenkinsFile)
mand = new File(whiteList)
black = new File(blackList)
} catch (Exception e){
printUsage()
return
}
//Mandatory and blacklisted converted to ArrayList
//Scanners
try {
jenkScan = new Scanner(jenkFile)
if(verboseSwitch)
println(getTimeStampNow().toString() + " File to test read")
} catch (Exception e) {
if(!wantHelp){
printUsage()
println("ERROR: Could not load the file to test")
}
//return
}
try{
whiteScan = new Scanner(mand)
if(verboseSwitch)
println(getTimeStampNow().toString() + " Whitelisted file read")
} catch (Exception ex) {
//println("ERROR: Could not load whitelisted file")
//return
}
try{
blackScan = new Scanner(black)
if(verboseSwitch)
println(getTimeStampNow().toString() + " Blacklisted file read")
} catch (Exception ex) {
//println("ERROR: Could not load blacklisted file")
//return
}
//Load files
try{
//Fill Maps
while(whiteScan.hasNextLine()){
String line = whiteScan.nextLine()
String place = null
white.add(line)
mandMap.put(line, false)
}
} catch(Exception e){
//println("Problem loading whitelisted file to program")
}
try{
while(blackScan.hasNextLine()){
String line = blackScan.nextLine()
String place = null
blackArray.add(line)
blackMap.put(line, false)
}
} catch (Exception e){
//println("Problem loading blacklisted file to program");
}
try{
if(isFileEmpty(jenkFile)){
println("File to test is empty")
}
if(isFileEmpty(mand)){
println("Whitelisted file is empty")
whitelistedEmpty = true
}
if(isFileEmpty(black)){
println("Blacklisted file is empty")
blacklistedEmpty = true
}
} catch (Exception e){
}
removeDuplicates()
findCommentedLines()
if(verboseSwitch){
println("\n\t\tBEFORE\n")
println(getTimeStampNow() + " INFO: Whitelisted entries\n ")
printMandMap()
println("\n" + getTimeStampNow() + " INFO: Blacklisted entries\n ")
printBlackMap()
println("\nBeginning search")
println("\n")
}
//Start of search. Whitelisted. For each mandatory word it makes 5 different checks between spaces, between single quotes, between (),
//between [], between {}
if(white.size > 0 && !whitelistedEmpty){
for(String cur : white){
if(checkWordSides(cur, true)){
mandMap.replace(cur, true)
for(String s: lineNumber){
mandNameFound.add(cur)
mandLinesFound.add(s)
}
}
if(findWordInFile(cur, jenkinsFile)){
mandMap.replace(cur, true)
for(String s: lineNumber){
mandNameFound.add(cur)
mandLinesFound.add(s)
}
}
}
}
//Blacklisted search. REGEX and space delimiter test per line.
foundLineCounter = 0
if(blackArray.size > 0 && !blacklistedEmpty){
for(String cur : blackArray){
if(checkWordSides(cur, false)){
blackMap.replace(cur, true)
for(String s: lineNumber){
blackNameFound.add(cur)
blackLinesFound.add(s)
}
}
if(findWordInFile(cur, jenkinsFile)){
blackMap.replace(cur, true)
for(String s: lineNumber){
blackNameFound.add(cur)
blackLinesFound.add(s)
}
}
}
}
//After the file has been searched, output results.
if(verboseSwitch){
println("\n\t\tAFTER\n")
println(getTimeStampNow() + " INFO: Whitelisted entries\n ")
printMandMap()
println("\n" + getTimeStampNow() + " INFO: Blacklisted entries\n ")
printBlackMap()
}
//Print results.
if(testMandatory() && whitelistedEmpty){
isMandatoryThere = true
println("\nINFO: All mandatory values present. SUCCESS.\n")
if(!verboseSwitch)
printMandLines()
} else {
println("\nERROR: " + howManyMandsMissing() + " mandatory values missing. \nFAILURE. Results below.\n")
printMandMap()
if(howManyMandsMissing() == 0)
isMandatoryThere = true
}
if(testBlack()){
isBlacklistedThere = true
println("\nERROR: " + howManyBlacksMissing() + " Blacklisted entries have been found.\nFAILURE. Results below.\n")
if(!verboseSwitch)
printBlackMap()
if(!verboseSwitch)
printBlackLines()
} else {
println("INFO: All blacklisted entries have not been found. SUCCESS.")
}
if(verboseSwitch){
printBlackLines()
printMandLines()
}
//Depending on results, assign exit code.
exitCode = 0
if(!isBlacklistedThere && isMandatoryThere)
exitCode = 1
if(!isMandatoryThere)
exitCode = 2
if(isBlacklistedThere)
exitCode = 3
if(isBlacklistedThere && !isMandatoryThere)
exitCode = 4
//Close
try{
whiteScan.close()
blackScan.close()
jenkScan.close()
} catch (Exception e){
}
//Exit.
System.exit(exitCode)
}
/**
* After test of whitelist values, output where they were found
*/
public static void outputMandatoryLines(){
println("Hit mandatory value at line: ")
for(Integer ing : linesMand){
println(ing.toString())
}
}
/**
* Checks if all whitelisted values are there.
* @return boolean
*/
public static boolean testMandatory(){
boolean answer = true
def mandVals = mandMap.values()
def it = mandVals.iterator()
boolean fal = false
while(it.hasNext()){
if(it.next().equals(fal))
answer = false
}
return answer
}
/**
* Prints to console the lines whitelisted entries were found.
* @author adamhowa
* @since 20/04/2017
*
* {@code}
*
*/
public static void printMandLines(){
if(mandLinesFound.size() > 0){
//println(mandLinesFound.size())
println("\nMandatory values found at line:")
int cnt = 0;
for(int i = 0; i < mandLinesFound.size(); i++){
printf("%-30.30s %-30.30s\n", mandNameFound.get(i), mandLinesFound.get(i))
}
}
}
public static boolean isFileEmpty(File fl){
boolean isEmpty = false
int numLines = 0
Scanner emptyChecker = new Scanner(fl)
while(emptyChecker.hasNextLine()){
emptyChecker.nextLine()
numLines++
}
if(numLines == 0){
isEmpty = true
} else {
isEmpty = false
}
return isEmpty
}
/**
* Prints to console the lines blacklisted entries were found.
* @author adamhowa
* @since 20/04/2017
*
* {@code}
*
*/
public static void printBlackLines(){
if(blackLinesFound.size() > 0){
println("\nBlacklisted values found at line:");
int cnt = 0;
for(int i = 0; i < blackLinesFound.size(); i++){
printf("%-30.30s %-30.30s\n", blackNameFound.get(i), blackLinesFound.get(i))
cnt++
}
}
}
/**
* Prints to the console the usage of this program.
* @author adamhowa
*
*/
public static void printUsage(){
println("The correct usage of this program is\n")
println("sanitizer -j <jenkinsFile> -m <mandatory> -b <blacklisted> [-v] [-h]\n")
println("Where -v is verbose. Giving more detailed output. -h is help.\n")
println("EXIT CODES\n0: Nothing\n1: Pass (All Whitelisted are there AND All blacklisted are not there)\n2: FAIL A whitelisted does not exist\n3: FAIL A blacklisted exists\n4: FAIL A whitelisted does not exist AND A blacklisted exists")
println("--------------------------------------------------------------------\n\n")
}
/**
* Prints blacklisted entries status as columns.
* @author adamhowa
*
*/
public static void printBlackMap(){
def keys = blackMap.keySet()
def it = keys.iterator()
def vals = blackMap.values()
def itt = vals.iterator()
println("\nBlacklisted entry In File?")
while(it.hasNext()){
String next
if(itt.next()){
next = "Yes"
} else {
next = "No"
}
printf("%-30.30s %-30.30s\n", it.next(), next)
}
}
/**
* Prints whitelisted entries status as columns.
* @author adamhowa
*
*/
public static void printMandMap(){
def keys = mandMap.keySet()
def it = keys.iterator()
def vals = mandMap.values()
def itt = vals.iterator()
println("\nWhitelisted entry In File?")
while(it.hasNext()){
String next
if(itt.next()){
next = "Yes"
} else {
next = "No"
}
printf("%-30.30s %-30.30s\n", it.next(), next)
}
}
/**
* Are all of the blacklisted entries there?
* @author adamhowa
* @return boolean
*
*/
public static boolean testBlack(){
boolean answer = false
def mandVals = blackMap.values()
def it = mandVals.iterator()
boolean fal = false
while(it.hasNext()){
if(it.next().equals(true)){
answer = true
}
}
return answer
}
/**
* Gives the number of mandatory or whitelisted values that are not there.
* @author adamhowa
* @return int
*
*/
public static int howManyMandsMissing(){
int howMany = 0
def mandVals = mandMap.values()
def it = mandVals.iterator()
boolean fal = false
while(it.hasNext()){
if(it.next().equals(fal)){
howMany++
}
}
return howMany
}
/**
* Gives the number of blacklisted values that are not there.
* @author adamhowa
* @return int
*
*/
public static int howManyBlacksMissing(){
int howMany = 0
def mandVals = blackMap.values()
def it = mandVals.iterator()
boolean fal = false
while(it.hasNext()){
if(it.next().equals(true)){
howMany++
}
}
return howMany
}
/**
* After test of blacklist values, output where they were found
*/
public static void outputBlacklistedLines(){
println("Hit blacklisted value at line: ")
for(Integer ing : linesBlack){
println(ing.toString())
}
}
/**
* Gets the time(as a String).
* @author adamhowa
* @return Timestamp
*
*/
public static String getTimeStampNow(){
def d = new Date()
String res = d.toString()
return res
}
/**
* Searches both files to see if there are duplicate strings in any line. If there are duplicates, delete.
* @author adamhowa
*
*/
public static void removeDuplicates(){
int indexFirst, indexSecond = 0
def keysMand = mandMap.keySet()
String[] whiteLst = keysMand.toArray()
def keysBlack = blackMap.keySet()
String[] blackLst = keysBlack.toArray()
for(String w : whiteLst){
for(String b : blackLst){
if(w == b){
white.remove(w)
blackArray.remove(b)
mandMap.remove(w)
blackMap.remove(b)
println("\nERROR: Duplicates found in whitelisted and blacklisted files.")
println(w + " was found in both files")
println(w + " removed")
}
}
}
}
/**
* Searches file splitting on space. Ignores lines that are commented.
* @param inputSearch
* @param filePath
* @return boolean true/false
*/
public static boolean findWordInFile(String inputSearch, String filePath){
lineNumber = ""
boolean found = false
double count = 0,countBuffer=0,countLine=0
BufferedReader br
String line = ""
countLine = 0
boolean lineCommented = false
try {
br = new BufferedReader(new FileReader(filePath))
try {
while((line = br.readLine()) != null)
{
for(Integer i : commentedLines){
if(i == countLine){
lineCommented = true
}
}
if(!lineCommented){
String[] words = line.split(" ")
for (String word : words) {
if (word.equals(inputSearch)) {
found =true
count++
countBuffer++
lineNumber += (int)countLine
}
}
}
if(lineCommented)
lineCommented = false
countLine++
}
br.close()
} catch (IOException e) {
e.printStackTrace()
}
} catch (FileNotFoundException e) {
e.printStackTrace()
}
return found
}
/**
* Sifts through the file to test and identifies the commented lines. Both multi line and single.
* Stores line numbers in programs memory for later use.
*/
public static void findCommentedLines(){
boolean isLineCommented = false
String line
int lineCount = 0
boolean hitEnd = false
boolean startOfMulti = false
while(jenkScan.hasNextLine()){
line = jenkScan.nextLine()
if(line.contains("//")){
commentedLines.add(lineCount)
}
if(line.contains("/*") || startOfMulti){
if(!startOfMulti)
startOfMulti = true
if(startOfMulti)
commentedLines.add(lineCount)
if(line.contains("*/")){
hitEnd = true
startOfMulti = false
}
}
lineCount++
}
}
/**
* Iterates through each possible combination of characters to the left and right of the word to search for.
* This is the main SEARCH method that tries to match given values. It uses a huge combination of regex tests per each line in the test file to try and find all
* matches, including complex ones. It ignores commented lines both single and multi line.
* @param inputSearch
* @param filePath
* @return true/false
*/
public static boolean checkWordSides(String inputSearch, boolean whiteBlack){
lineNumber = ""
boolean found = false
double count = 0,countBuffer=0,countLine=0
BufferedReader br
String line = ""
String REGEX_PATTERN = "\\{(" + inputSearch+ ")\\}"
countLine = 0
int leftAcceptedLimit = 3
int acceptedCounter = 0;
int limit = 24;
println("Searching for " + inputSearch + ".")
boolean lineCommented = false
try {
br = new BufferedReader(new FileReader(jenkinsFile))
try {
while((line = br.readLine()) != null)
{
int local = 0;
String left = null;
String right = null;
for(Integer i : commentedLines){
if(i == countLine){
lineCommented = true
}
}
while(acceptedCounter < limit){
if(acceptedCounter == limit-1){
local++;
acceptedCounter = 0;
}
left = acceptedLeft[local];
right = acceptedRight[acceptedCounter]
acceptedCounter++;
REGEX_PATTERN = "\\" + left + "(\\Q" + inputSearch + "\\E)" + "\\" + right;
String s = line
Pattern p = Pattern.compile(REGEX_PATTERN)
Matcher m = p.matcher(s)
//println(REGEX_PATTERN)
while (m.find()) {
found = true
countBuffer++
if(!lineCommented){
lineNumber += (int)countLine
}
if(lineCommented){
String lister = ""
if(whiteBlack){
lister = "Whitelisted"
} else {
lister = "Blacklisted"
}
print("WARNING: line " + (int)countLine + " contains " + lister + " value " + inputSearch + "\n")
}
}
if(acceptedCounter == limit || local == limit){
break;
}
}
if(lineCommented)
lineCommented = false
countLine++;
}
br.close()
} catch (Exception e) {
//e.printStackTrace()
}
} catch (FileNotFoundException e) {
//e.printStackTrace()
}
return found
}
}