-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkubearmor.go
144 lines (122 loc) · 3.42 KB
/
kubearmor.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
package main
import (
"context"
"fmt"
"strings"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/rest"
)
func checkSensitiveDirs(config *rest.Config, sensitiveDirs []string, labelSelectors []string) ([]string, bool, error) {
// Create in-cluster config
var Assets []string
var matched bool
dynamicClient, err := dynamic.NewForConfig(config)
if err != nil {
return nil, false, fmt.Errorf("failed to create dynamic client: %w", err)
}
// Define KubeArmorPolicy GVR
gvr := schema.GroupVersionResource{
Group: "security.kubearmor.com",
Version: "v1",
Resource: "kubearmorpolicies",
}
// List all policies across all namespaces
policies, err := dynamicClient.Resource(gvr).Namespace("").List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, false, fmt.Errorf("failed to list policies: %w", err)
}
// Convert labelSelectors []string to a map for easy matching
labelSelectorMap := make(map[string]string)
for _, selector := range labelSelectors {
parts := strings.Split(selector, "=")
if len(parts) == 2 {
labelSelectorMap[parts[0]] = parts[1]
}
}
for _, policy := range policies.Items {
spec, ok := policy.Object["spec"].(map[string]interface{})
if !ok {
continue
}
// Check if the policy matches the specified labels
selector, ok := spec["selector"].(map[string]interface{})
if ok {
matchLabels, ok := selector["matchLabels"].(map[string]interface{})
if ok {
// Check if the labels match the provided label selectors (app=value)
matches := true
for key, value := range labelSelectorMap {
if policyValue, exists := matchLabels[key]; !exists || policyValue != value {
matches = false
break
}
}
// If the policy matches the label selector, continue checking for sensitive directories
if matches {
fmt.Printf("Policy %s matches label selector: %v\n", policy.GetName(), labelSelectorMap)
} else {
continue
}
}
}
file, ok := spec["file"].(map[string]interface{})
if !ok {
continue
}
matchDirs, ok := file["matchDirectories"].([]interface{})
if !ok {
continue
}
for _, dir := range matchDirs {
dirMap, ok := dir.(map[string]interface{})
if !ok {
continue
}
dirPath, ok := dirMap["dir"].(string)
if !ok {
continue
}
action, ok := dirMap["action"].(string)
if !ok {
continue
}
for _, sensitiveDir := range sensitiveDirs {
if dirPath == sensitiveDir {
fmt.Printf("Found sensitive asset in policy %s:\n Path: %s\n Action: %s\n",
policy.GetName(), dirPath, action)
Assets = append(Assets, dirPath)
matched = true
}
}
}
// Check matchPaths
if matchPaths, ok := file["matchPaths"].([]interface{}); ok {
for _, path := range matchPaths {
pathMap, ok := path.(map[string]interface{})
if !ok {
continue
}
filePath, ok := pathMap["path"].(string)
if !ok {
continue
}
action, ok := pathMap["action"].(string)
if !ok {
continue
}
// readOnly, _ := pathMap["readOnly"].(bool)
for _, sensitiveDir := range sensitiveDirs {
if filePath == sensitiveDir {
fmt.Printf("Found sensitive asset in policy %s:\n Path: %s\n Action: %s\n",
policy.GetName(), filePath, action)
Assets = append(Assets, filePath)
matched = true
}
}
}
}
}
return labelSelectors, matched, nil
}