-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd.go
More file actions
93 lines (73 loc) · 2.04 KB
/
cmd.go
File metadata and controls
93 lines (73 loc) · 2.04 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"strconv"
"github.com/urfave/cli"
)
func cmdSearch(c *cli.Context) error {
var configs []ConfigurationMap
nlID := c.String("id")
metadata := Metadata{
ID: nlID,
Name: c.String("name"),
}
// Read config file
byteValue := readFileBytes(c.String("map-file"))
err := json.Unmarshal(byteValue, &configs)
if err != nil {
log.Fatal(err)
}
for _, config := range configs {
var cMap ConfigurationMap
log.Printf("Executing search for network list ID %s, Name %s in %s configuration", nlID, c.String("name"), config.ConfigName)
// found counter triggers for rate policies, match targets, security policies
var rpFound, mtFound, spFound int
// Read Security Configuration file
configPath := fmt.Sprintf("%s/%s.json", c.String("source"), strconv.Itoa(config.ConfigID))
cfile := readFileBytes(configPath)
// Rate Policies search
rpFound = ratePolicySearch(nlID, cfile, &cMap)
// Match Targets Search
mtFound = matchTargetSearch(nlID, cfile, &cMap)
// Security Policies Search
spFound = securityPolicySearch(nlID, cfile, &cMap)
if rpFound > 0 || mtFound > 0 || spFound > 0 {
cMap.ConfigID = config.ConfigID
cMap.ConfigName = config.ConfigName
metadata.Usage = append(metadata.Usage, cMap)
}
}
// Output metadata json
if c.String("output") == "json" {
if c.String("destination") == "" {
outputJSON(metadata)
return nil
}
jsonF, err := json.MarshalIndent(metadata, "", " ")
if err != nil {
log.Fatal(err)
}
resultFilePath := fmt.Sprintf("%s/%s.metadata", c.String("destination"), nlID)
err = ioutil.WriteFile(resultFilePath, jsonF, 0644)
if err != nil {
log.Fatal(err)
}
}
// Output dot graphs
if c.String("output") == "dot" {
s := buildGraph(metadata)
if c.String("destination") == "" {
fmt.Println(s)
return nil
}
graphFilePath := fmt.Sprintf("%s/%s.dot", c.String("destination"), nlID)
err = ioutil.WriteFile(graphFilePath, []byte(s), 0644)
if err != nil {
log.Fatal(err)
}
}
return nil
}