-
Notifications
You must be signed in to change notification settings - Fork 0
/
random.go
223 lines (198 loc) · 5.52 KB
/
random.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
package main
import (
"bufio"
"encoding/json"
"fmt"
"log"
"math/rand"
"os"
"time"
)
// Event represents a Kubernetes audit log event
type Event struct {
Kind string `json:"kind"`
APIVersion string `json:"apiVersion"`
Level string `json:"level"`
AuditID string `json:"auditID"`
Stage string `json:"stage"`
RequestURI string `json:"requestURI"`
Verb string `json:"verb"`
User User `json:"user"`
SourceIPs []string `json:"sourceIPs"`
UserAgent string `json:"userAgent"`
ObjectRef ObjectRef `json:"objectRef"`
ResponseStatus Status `json:"responseStatus"`
RequestReceivedTimestamp time.Time `json:"requestReceivedTimestamp"`
StageTimestamp time.Time `json:"stageTimestamp"`
Annotations Annotations `json:"annotations"`
}
type User struct {
Username string `json:"username"`
Groups []string `json:"groups"`
}
type ObjectRef struct {
Resource string `json:"resource"`
Namespace string `json:"namespace"`
Name string `json:"name"`
APIVersion string `json:"apiVersion"`
}
type Status struct {
Metadata map[string]string `json:"metadata"`
Code int `json:"code"`
}
type Annotations struct {
Decision string `json:"authorization.k8s.io/decision"`
Reason string `json:"authorization.k8s.io/reason"`
}
func RunRandomLogsGenerator() {
const numLogs = 1000000
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
logFile, err := os.Create("k8s_audit_logs.json")
if err != nil {
log.Fatalf("Failed to create log file: %v", err)
}
defer logFile.Close()
writer := bufio.NewWriter(logFile)
for i := 0; i < numLogs; i++ {
event := generateRandomEvent(rng)
data, err := json.Marshal(event)
if err != nil {
log.Fatalf("Failed to marshal event: %v", err)
}
writer.WriteString(string(data) + "\n")
}
writer.Flush()
}
func generateRandomEvent(rng *rand.Rand) Event {
return Event{
Kind: "Event",
APIVersion: getRandomApi(rng),
Level: "Metadata",
AuditID: randomString(rng, 36),
Stage: "ResponseComplete",
RequestURI: randomRequestURI(rng),
Verb: randomVerb(rng),
User: User{
Username: randomUsername(rng),
Groups: []string{"system:masters", "system:authenticated"},
},
SourceIPs: []string{randomIP(rng)},
UserAgent: randomUserAgent(rng),
ObjectRef: ObjectRef{
Resource: randomResources(rng),
Namespace: "default",
Name: randomPodName(rng),
APIVersion: "v1",
},
ResponseStatus: Status{
Metadata: map[string]string{},
Code: 200,
},
RequestReceivedTimestamp: time.Now(),
StageTimestamp: time.Now().Add(time.Millisecond * time.Duration(rng.Intn(1000))),
Annotations: Annotations{
Decision: allowOrblock(rng),
Reason: "",
},
}
}
func getRandomApi(rng *rand.Rand) string {
apiVersions := []string{
"v1",
"apps/v1",
"batch/v1",
"extensions/v1beta1",
"networking.k8s.io/v1",
"rbac.authorization.k8s.io/v1",
"storage.k8s.io/v1",
"apiextensions.k8s.io/v1",
"autoscaling/v1",
"scheduling.k8s.io/v1",
"admissionregistration.k8s.io/v1",
"apiregistration.k8s.io/v1",
"certificates.k8s.io/v1",
"coordination.k8s.io/v1",
"discovery.k8s.io/v1",
"node.k8s.io/v1",
"policy/v1",
"audit.k8s.io/v1",
"authentication.k8s.io/v1",
"authorization.k8s.io/v1",
"flowcontrol.apiserver.k8s.io/v1beta1",
}
return apiVersions[rng.Intn(len(apiVersions))]
}
func allowOrblock(rng *rand.Rand) string {
alow := []string{"allow", "block"}
return alow[rng.Intn(len(alow))]
}
func randomResources(rng *rand.Rand) string {
resources := []string{
"pods",
"services",
"deployments",
"replicasets",
"statefulsets",
"daemonsets",
"jobs",
"cronjobs",
"configmaps",
"secrets",
"namespaces",
"nodes",
"persistentvolumes",
"persistentvolumeclaims",
"ingresses",
"networkpolicies",
"serviceaccounts",
"roles",
"rolebindings",
"clusterroles",
"clusterrolebindings",
"resourcequotas",
"limitranges",
"horizontalpodautoscalers",
"poddisruptionbudgets",
"endpoints",
"events",
"replicationcontrollers",
"leases",
"csidrivers",
"csinodes",
"customresourcedefinitions",
}
return resources[rng.Intn(len(resources))]
}
func randomString(rng *rand.Rand, n int) string {
const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
b := make([]byte, n)
for i := range b {
b[i] = letters[rng.Intn(len(letters))]
}
return string(b)
}
func randomRequestURI(rng *rand.Rand) string {
return "/api/v1/namespaces/default/pods/" + randomPodName(rng)
}
func randomVerb(rng *rand.Rand) string {
verbs := []string{"get", "list", "create", "update", "delete"}
return verbs[rng.Intn(len(verbs))]
}
func randomUsername(rng *rand.Rand) string {
usernames := []string{"kubernetes-admin", "system:serviceaccount:kube-system:default", "system:anonymous"}
return usernames[rng.Intn(len(usernames))]
}
func randomIP(rng *rand.Rand) string {
return fmt.Sprintf("%d.%d.%d.%d", rng.Intn(256), rng.Intn(256), rng.Intn(256), rng.Intn(256))
}
func randomUserAgent(rng *rand.Rand) string {
agents := []string{
"kubectl/v1.26.0 (linux/amd64) kubernetes/b46a3f8",
"kubelet/v1.26.0 (linux/amd64) kubernetes/b46a39",
}
return agents[rng.Intn(len(agents))]
}
func randomPodName(rng *rand.Rand) string {
pods := []string{"test", "nginx", "busybox", "alpine"}
return pods[rng.Intn(len(pods))]
}