-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
83 lines (70 loc) · 1.79 KB
/
main.go
File metadata and controls
83 lines (70 loc) · 1.79 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
package main
import (
"fmt"
"log"
"os"
"jcanary/engine"
"jcanary/engine/rules/operators"
"github.com/Jeffail/gabs"
"github.com/fatih/color"
)
var RULES_CONFIG = getEnvVar("RULES_CONFIG", "./rules.json")
func main() {
c := color.New(color.FgCyan).Add(color.Underline)
c.Println("running jcanary...")
successPrinter := color.New(color.FgGreen)
errPrinter := color.New(color.FgRed)
// parse rules file
rawConfig, err := gabs.ParseJSONFile(RULES_CONFIG)
if err != nil {
log.Fatalf("unable to parse config: %v", err)
}
conf, err := engine.New(rawConfig)
if err != nil {
log.Fatalf("unable to create engine instance: %v", err)
}
pipeline := []*operators.Result{}
results := map[string]bool{}
for r, rule := range conf.Rules {
c.Printf("\tprocessing rule #%v\n", r)
for s, step := range rule.Steps {
c.Printf("\t\tprocessing step #%v\n", s)
res := step.Operate(conf.Vars, &pipeline)
if res.HasError() {
errPrinter.Printf("\t\t\tstep #%v failed: %v\n", s, res.Err)
}
pipeline = append(pipeline, res)
}
ruleHasFailure := false
for _, res := range pipeline {
if res.HasError() {
ruleHasFailure = true
}
}
results[rule.Name] = ruleHasFailure
// reset pipeline for new rule
pipeline = []*operators.Result{}
}
fmt.Printf("\n\n----------------------------\n")
failureDetected := false
for ruleName, hasFailure := range results {
verb := successPrinter.Sprintf("passed")
if hasFailure {
verb = errPrinter.Sprintf("failed")
failureDetected = true
}
fmt.Printf("rule \"%v\": %v\n", ruleName, verb)
}
fmt.Printf("----------------------------\n")
fmt.Println("finished running jcanary")
if failureDetected {
os.Exit(1)
}
}
func getEnvVar(key, fallback string) string {
val, ok := os.LookupEnv(key)
if !ok {
return fallback
}
return val
}