-
Notifications
You must be signed in to change notification settings - Fork 7
/
process.go
153 lines (128 loc) · 4.62 KB
/
process.go
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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"math"
"strings"
)
func namespaceSeparator() string {
return ":"
}
func processBytes(byteArray []byte, output *string) (string, int, int, int, error) {
//preflight with optional conversion from YAMLs
err := preflightAsset(&byteArray)
if err != nil {
return "", 0, 0, 0, fmt.Errorf("input failed preflight check: %v", err)
}
// make sure config objects are presented as a list
err = makeList(&byteArray)
if err != nil {
return "", 0, 0, 0, err
}
var apiObjectSet APIObjectSet
if err = json.Unmarshal(byteArray, &apiObjectSet); err != nil {
return "", 0, 0, 0, fmt.Errorf("can't unmarshal data: %v", err)
}
// extract compact datastructures
namespacePodMap := make(map[string][]string)
namespaceLabelMap := make(map[string]map[string]string)
podLabelMap := make(map[string]map[string]string)
networkPolicyNamespaces := make(map[string]struct{})
networkPolicies := []APIObject{}
for _, apiObject := range apiObjectSet.APIObjects {
namespace := apiObject.Metadata.Namespace
switch apiObject.Kind {
case "Pod":
if onBlacklist(namespace) {
continue
}
if apiObject.Status != nil &&
len(apiObject.Status.ContainerStatuses) > 0 &&
apiObject.Status.ContainerStatuses[0].Ready == true {
slice := []string{namespace, apiObject.Metadata.Name}
qualifiedPodName := strings.Join(slice, namespaceSeparator())
namespacePodMap[namespace] = append(namespacePodMap[namespace], qualifiedPodName)
podLabelMap[qualifiedPodName] = apiObject.Metadata.Labels
}
case "NetworkPolicy":
if onBlacklist(namespace) {
continue
}
networkPolicies = append(networkPolicies, *apiObject)
networkPolicyNamespaces[namespace] = struct{}{}
case "Namespace":
namespaceLabelMap[apiObject.Metadata.Name] = apiObject.Metadata.Labels
}
}
edgeMap := make(map[string][]string)
initializeEdgeMap(&edgeMap, &namespacePodMap)
deduplicateEdgeMap(&edgeMap)
allEdgesCount := countEdges(&edgeMap)
namespaceEdgeMap := edgeMap
filterIntraNamespace(&namespaceEdgeMap)
allNamespaceEdgesCount := countEdges(&namespaceEdgeMap)
// two passes req'd: isolation, then whitelisting
filterEdgeMap(&edgeMap, &namespacePodMap, &namespaceLabelMap, &podLabelMap, &networkPolicies, FilterIsolation)
filterEdgeMap(&edgeMap, &namespacePodMap, &namespaceLabelMap, &podLabelMap, &networkPolicies, FilterWhitelist)
filteredEdgesCount := countEdges(&edgeMap)
// metric percentage isolated
var percentageIsolatedInt int
percentageIsolatedInt = 100
if allEdgesCount != 0 {
var percentageIsolated float64
percentageIsolated = 100.0 - (float64(filteredEdgesCount)/float64(allEdgesCount))*100.0
percentageIsolatedInt = int(math.Floor(percentageIsolated + 0.5))
}
// metric percentage namespace policy coverage
var percentageNamespaceCoverageInt int
percentageNamespaceCoverageInt = 100
if len(namespacePodMap) != 0 {
var percentageNamespaceCoverage float64
percentageNamespaceCoverage = (float64(len(networkPolicyNamespaces)) / float64(len(namespacePodMap))) * 100.0
percentageNamespaceCoverageInt = int(math.Floor(percentageNamespaceCoverage + 0.5))
}
// metric percentage isolated - ignoring intra-namespace connections
var percentageIsolatedNamespaceInt int
percentageIsolatedNamespaceInt = 100
if allEdgesCount != 0 {
filterIntraNamespace(&edgeMap)
filteredEdgesCount = countEdges(&edgeMap)
var percentageIsolatedNamespace float64
percentageIsolatedNamespace = 100.0 - (float64(filteredEdgesCount)/float64(allNamespaceEdgesCount))*100.0
percentageIsolatedNamespaceInt = int(math.Floor(percentageIsolatedNamespace + 0.5))
}
var buffer bytes.Buffer
switch *output {
case "dot":
writeDot(&namespacePodMap, &edgeMap, &buffer)
case "json":
writeJSON(percentageIsolatedInt, percentageIsolatedNamespaceInt, percentageNamespaceCoverageInt, &buffer)
case "yaml":
writeYaml(percentageIsolatedInt, percentageIsolatedNamespaceInt, percentageNamespaceCoverageInt, &buffer)
case "markdown":
writeMarkdown(percentageIsolatedInt, percentageNamespaceCoverageInt, &buffer)
}
return buffer.String(), percentageIsolatedInt, percentageIsolatedNamespaceInt, percentageNamespaceCoverageInt, nil
}
func processFile(path string, output *string) (string, error) {
byteArray, err := ioutil.ReadFile(path)
if err != nil {
return "", fmt.Errorf("can't read %s: %v", path, err)
}
result, _, _, _, err := processBytes(byteArray, output)
if err != nil {
return "", fmt.Errorf("can't process %s: %s", path, err)
}
return result, nil
}
func countEdges(edgeMap *map[string][]string) int {
count := 0
for _, v := range *edgeMap {
for range v {
count++
}
}
return count
}