-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
270 lines (226 loc) · 7.75 KB
/
main.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
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
package main
import (
"context"
"encoding/json"
"fmt"
"html/template"
"io/ioutil"
"log"
"net/http"
"os"
"reflect"
"strings"
"gopkg.in/yaml.v2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
// type WorkloadMap struct {
// Workload string `yaml:"Component Name"`
// WorkloadLabels []string `yaml:"Workload Labels"`
// SensitiveAssetLocations []string `yaml:"Sensitive Asset Locations"`
// Egress []string `yaml:"Egress,omitempty"`
// Ingress []string `yaml:"Ingress,omitempty"`
// RiskDescription string `yaml:"Risk Description,omitempty"`
// Severity string `yaml:"Severity,omitempty"`
// Checkpoints []string `yaml:"Checkpoints,omitempty"`
// RiskID string `yaml:"Risk ID,omitempty"`
// }
type Risk struct {
RiskID string `json:"risk_id" yaml:"risk_id"`
Workload []string `json:"workload" yaml:"workload"`
RiskDescription string `json:"risk_description" yaml:"risk_description"`
Severity string `json:"severity" yaml:"severity"`
Checkpoints CheckpointMap `json:"checkpoints" yaml:"checkpoints"`
}
// Define the structure for the workload
type Workload struct {
WorkloadName string `json:"workload_name" yaml:"workload_name"`
Labels []string `json:"labels" yaml:"labels"` // Labels are key-value pairs
SensitiveAssetLocations []string `json:"sensitive_asset_locations" yaml:"sensitive_asset_locations"`
Egress []string `json:"egress,omitempty" yaml:"egress,omitempty"`
Ingress []string `json:"ingress,omitempty" yaml:"ingress,omitempty"`
Checkpoint Checkpoint `json:"checkpoints" yaml:"checkpoints"`
}
// Define the top-level structure
type workloadConfig struct {
Workloads []Workload `json:"workloads" yaml:"workloads"`
}
type Checkpoint struct {
Description string `json:"description" yaml:"description"`
Status bool `json:"status" yaml:"status"`
}
type CheckpointMap struct {
CHK_TLS []Checkpoint `json:"CHK_TLS" yaml:"CHK_TLS"`
CHK_POLP_EGRESS []Checkpoint `json:"CHK_POLP_EGRESS" yaml:"CHK_POLP_EGRESS"`
CHK_SENSITIVE_ASSETS []Checkpoint `json:"CHK_SENSITIVE_ASSETS" yaml:"CHK_SENSITIVE_ASSETS"`
}
type WorkloadRisks struct {
WorkloadName string `json:"workload_name" yaml:"workload_name"`
Risks []Risk `json:"risks" yaml:"risks"`
}
func main() {
edgeconfig, err := clientcmd.BuildConfigFromFlags("", "/home/ubuntu/.kube/edge-kubeconfig")
if err != nil {
panic(err.Error())
}
coreconfig, err := clientcmd.BuildConfigFromFlags("", "/home/ubuntu/.kube/core-kubeconfig")
if err != nil {
panic(err.Error())
}
edgeclientset, err := kubernetes.NewForConfig(edgeconfig)
if err != nil {
panic(err.Error())
}
coreclientset, err := kubernetes.NewForConfig(coreconfig)
if err != nil {
panic(err.Error())
}
oaiConfig, err := ioutil.ReadFile("oai-workload-map.yaml")
if err != nil {
panic(err)
}
var entries workloadConfig
err = yaml.Unmarshal(oaiConfig, &entries)
if err != nil {
panic(err)
}
risks, err := loadRisks("risk_config.yaml")
if err != nil {
log.Fatalf("Failed to load risks: %v", err)
}
// tmpl := template.Must(template.New("accordion").Parse(tmplStr))
workloadRisks := groupRisksByWorkload(risks)
// Create a map with workload_name as the key
workloadMap := make(map[string]Workload)
for _, workload := range entries.Workloads {
workloadMap[workload.WorkloadName] = workload
}
for i, risk := range workloadRisks {
for _, info := range workloadMap {
exists, err := verifyWorkloads(edgeclientset, coreclientset, info)
if err != nil {
panic(err.Error())
}
if exists {
edgeLabel, edgekspCheck, err := checkSensitiveDirs(edgeconfig, info.SensitiveAssetLocations, info.Labels)
if err != nil {
panic(err.Error())
}
// coreLabel, coreKSPCheck, er := checkSensitiveDirs(coreconfig. info.SensitiveAssetLocations, info.Labels)
if reflect.DeepEqual(info.Labels, edgeLabel) && risk.WorkloadName == info.WorkloadName && edgekspCheck {
for j, riskList := range risk.Risks {
for k := range riskList.Checkpoints.CHK_SENSITIVE_ASSETS {
workloadRisks[i].Risks[j].Checkpoints.CHK_SENSITIVE_ASSETS[k].Status = true
}
}
}
coreNetwork, coreLabel, err := verifyNetworkPolicy(coreclientset, info, workloadMap)
fmt.Println("CORE Network Policy there?:", coreNetwork, " For Label: ", coreLabel)
if reflect.DeepEqual(info.Labels, coreLabel) && risk.WorkloadName == info.WorkloadName && coreNetwork {
for j, _ := range risk.Risks {
// for k := range riskList.Checkpoints.CHK_SENSITIVE_ASSETS {
workloadRisks[i].Risks[j].Checkpoints.CHK_POLP_EGRESS[0].Status = true
// }
}
}
// fmt.Println("Network policy does indeed exist for: ", name)
// }
}
}
}
// Convert to JSON with pretty formatting
jsonData, err := json.MarshalIndent(workloadRisks, "", " ")
if err != nil {
fmt.Printf("Error marshaling to JSON: %v\n", err)
return
}
// Write to file
fileName := "workload_risks.json"
err = os.WriteFile(fileName, jsonData, 0644)
if err != nil {
fmt.Printf("Error writing to file: %v\n", err)
return
}
tmpl := template.Must(template.New("accordion").Parse(tmplStr))
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
workloadRisks := groupRisksByWorkload(risks)
err := tmpl.Execute(w, workloadRisks)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
})
log.Println("Server starting on :8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}
func verifyWorkloads(edgeCientset *kubernetes.Clientset, coreClientset *kubernetes.Clientset, workload Workload) (bool, error) {
// Create label selector from workload labels
var labelSelector string
for _, v := range workload.Labels {
if labelSelector != "" {
labelSelector += ","
}
labelSelector += v
}
// Check if pods with these labels exist in the namespace
edgePods, err := edgeCientset.CoreV1().Pods("").List(context.TODO(), metav1.ListOptions{
LabelSelector: labelSelector,
})
if err != nil {
return false, fmt.Errorf("error checking pods in EDGE cluster: %v", err)
}
corePods, err := coreClientset.CoreV1().Pods("").List(context.TODO(), metav1.ListOptions{
LabelSelector: labelSelector,
})
if err != nil {
return false, fmt.Errorf("error checking pods in CORE cluster: %v", err)
}
if len(edgePods.Items) == 0 && len(corePods.Items) == 0 {
return false, nil
}
return true, nil
}
func verifyNetworkPolicy(clientset *kubernetes.Clientset, w Workload, workload map[string]Workload) (bool, []string, error) {
networkPolicies, err := clientset.NetworkingV1().NetworkPolicies("").List(context.TODO(), metav1.ListOptions{}) // add label selector filter
if err != nil {
return false, nil, fmt.Errorf("failed to list network policies: %v", err)
}
var rLabel []string
match := false
for _, np := range networkPolicies.Items {
for _, egresscheck := range w.Egress {
component, exists := workload[egresscheck]
if !exists {
continue
}
for _, egress := range np.Spec.Egress {
for _, to := range egress.To {
for _, lab := range component.Labels {
if matchesLabelSelector(to.PodSelector.MatchLabels, lab) {
match = true
rLabel = w.Labels
break
}
}
}
if match {
break
}
}
if !match {
return false, nil, nil
}
}
}
return match, rLabel, err
}
func matchesLabelSelector(matchLabels map[string]string, targetLabel string) bool {
for key, value := range matchLabels {
label := fmt.Sprintf("%s=%s", key, value)
if strings.Contains(targetLabel, label) {
return true
}
}
return false
}