|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/base64" |
| 6 | + "encoding/json" |
| 7 | + "log" |
| 8 | + "os" |
| 9 | + "time" |
| 10 | + metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 11 | + "k8s.io/client-go/kubernetes" |
| 12 | + "k8s.io/client-go/rest" |
| 13 | +) |
| 14 | + |
| 15 | +// Alert falco data structure |
| 16 | +type Alert struct { |
| 17 | + Output string `json:"output"` |
| 18 | + Priority string `json:"priority"` |
| 19 | + Rule string `json:"rule"` |
| 20 | + Time time.Time `json:"time"` |
| 21 | + OutputFields struct { |
| 22 | + ContainerID string `json:"container.id"` |
| 23 | + ContainerImageRepository interface{} `json:"container.image.repository"` |
| 24 | + ContainerImageTag interface{} `json:"container.image.tag"` |
| 25 | + EvtTime int64 `json:"evt.time"` |
| 26 | + FdName string `json:"fd.name"` |
| 27 | + K8SNsName string `json:"k8s.ns.name"` |
| 28 | + K8SPodName string `json:"k8s.pod.name"` |
| 29 | + ProcCmdline string `json:"proc.cmdline"` |
| 30 | + } `json:"output_fields"` |
| 31 | +} |
| 32 | + |
| 33 | +func main() { |
| 34 | + criticalNamespaces := map[string]bool{ |
| 35 | + "kube-system": true, |
| 36 | + "kube-public": true, |
| 37 | + "kube-node-lease": true, |
| 38 | + "falco": true, |
| 39 | + } |
| 40 | + |
| 41 | + var falcoEvent Alert |
| 42 | + |
| 43 | + bodyReq := os.Getenv("BODY") |
| 44 | + log.Println("Body", bodyReq) |
| 45 | + var data map[string]interface{} |
| 46 | + _ = json.Unmarshal([]byte(bodyReq), &data) |
| 47 | + |
| 48 | + bodyReqDecoded , _:= base64.StdEncoding.DecodeString(data["data"].(string)) |
| 49 | + if bodyReq == "" { |
| 50 | + log.Fatalf("Need to get environment variable BODY") |
| 51 | + } |
| 52 | + |
| 53 | + log.Println("Decoded:", string(bodyReqDecoded)) |
| 54 | + |
| 55 | + var body map[string]interface{} |
| 56 | + _ = json.Unmarshal(bodyReqDecoded, &body) |
| 57 | + |
| 58 | + |
| 59 | + bodyReqByte, _ := json.Marshal(body["body"]) |
| 60 | + |
| 61 | + err := json.Unmarshal(bodyReqByte, &falcoEvent) |
| 62 | + if err != nil { |
| 63 | + log.Fatalf("The data doesent match the struct %v", err) |
| 64 | + } |
| 65 | + |
| 66 | + kubeClient, err := setupKubeClient() |
| 67 | + if err != nil { |
| 68 | + log.Fatalf("Unable to create in-cluster config: %v", err) |
| 69 | + } |
| 70 | + |
| 71 | + err = deletePod(kubeClient, falcoEvent, criticalNamespaces) |
| 72 | + if err != nil { |
| 73 | + log.Fatalf("Unable to delete pod due to err %v", err) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +// setupKubeClient |
| 78 | +func setupKubeClient() (*kubernetes.Clientset, error) { |
| 79 | + config, err := rest.InClusterConfig() |
| 80 | + if err != nil { |
| 81 | + return nil, err |
| 82 | + } |
| 83 | + |
| 84 | + // creates the clientset |
| 85 | + kubeClient, err := kubernetes.NewForConfig(config) |
| 86 | + if err != nil { |
| 87 | + return nil, err |
| 88 | + } |
| 89 | + return kubeClient, nil |
| 90 | +} |
| 91 | + |
| 92 | +// deletePod, if not part of the criticalNamespaces the pod will be deleted |
| 93 | +func deletePod(kubeClient *kubernetes.Clientset, falcoEvent Alert, criticalNamespaces map[string]bool) error { |
| 94 | + podName := falcoEvent.OutputFields.K8SPodName |
| 95 | + namespace := falcoEvent.OutputFields.K8SNsName |
| 96 | + log.Printf("PodName: %v & Namespace: %v", podName, namespace) |
| 97 | + |
| 98 | + log.Printf("Rule: %v", falcoEvent.Rule) |
| 99 | + if criticalNamespaces[namespace] { |
| 100 | + log.Printf("The pod %v won't be deleted due to it's part of the critical ns list: %v ", podName, namespace) |
| 101 | + return nil |
| 102 | + } |
| 103 | + |
| 104 | + log.Printf("Deleting pod %s from namespace %s", podName, namespace) |
| 105 | + err := kubeClient.CoreV1().Pods(namespace).Delete(context.Background(), podName, metaV1.DeleteOptions{}) |
| 106 | + if err != nil { |
| 107 | + return err |
| 108 | + } |
| 109 | + return nil |
| 110 | +} |
0 commit comments