...
1 package workloadsiemcfg
2
3 import (
4 "encoding/json"
5 "fmt"
6 "strings"
7
8 v1 "k8s.io/api/core/v1"
9 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10 )
11
12
13 type WorkloadSiemConfigData struct {
14 Configs string
15 }
16
17 type DefaultMessage struct {
18 Default string `json:"Default"`
19 NeedToPanic string `json:"Need to Panic?"`
20 }
21
22
23 func BuildWorkloadSiemConfigMap(classifications []SIEMClassification) (*v1.ConfigMap, error) {
24 defaultMessage := DefaultMessage{
25 Default: "true",
26 NeedToPanic: "This is the default state. if you are expecting values here and a release is active, check the Helm Chart that the SIEM annotation is correctly done.",
27 }
28
29 marshaledClassifications := ""
30 if len(classifications) != 0 {
31 marshaledBytes, err := json.MarshalIndent(classifications, "", " ")
32 if err != nil {
33 return nil, fmt.Errorf("Error marshaling SIEM classifications in the configmap package: %w", err)
34 }
35 marshaledClassifications = string(marshaledBytes)
36 } else {
37 marshaledBytes, err := json.MarshalIndent(defaultMessage, "", " ")
38 if err != nil {
39 return nil, fmt.Errorf("Error marshaling default message in the configmap package: %w", err)
40 }
41 marshaledClassifications = string(marshaledBytes)
42 }
43
44 i := &WorkloadSiemConfigData{
45 Configs: marshaledClassifications,
46 }
47 return i.ToConfigMap(), nil
48 }
49
50
51 func (i WorkloadSiemConfigData) ToConfigMap() *v1.ConfigMap {
52 return &v1.ConfigMap{
53 TypeMeta: metav1.TypeMeta{
54 Kind: "ConfigMap",
55 APIVersion: v1.SchemeGroupVersion.String(),
56 },
57 ObjectMeta: metav1.ObjectMeta{
58 Name: WorkloadSiemConfigMapName,
59 Namespace: WorkloadSiemConfigMapNS,
60 },
61 Data: map[string]string{
62 WorkloadSiemConfigDataFieldName: i.Configs,
63 },
64 }
65 }
66
67
68 func ValidateConfigMap(cfg *v1.ConfigMap) error {
69 var missing []string
70 if cfg.Data[WorkloadSiemConfigDataFieldName] == "" {
71 missing = append(missing, WorkloadSiemConfigDataFieldName)
72 }
73
74 if len(missing) > 0 {
75 return fmt.Errorf("workload-siem configmap invalid, value(s) not provided: %s", strings.Join(missing, ","))
76 }
77 return nil
78 }
79
80
81 func New(cfg *v1.ConfigMap) (*WorkloadSiemConfigData, error) {
82 if err := ValidateConfigMap(cfg); err != nil {
83 return nil, err
84 }
85 return FromConfigMap(cfg), nil
86 }
87
88
89 func FromConfigMap(cfg *v1.ConfigMap) *WorkloadSiemConfigData {
90 return (&WorkloadSiemConfigData{}).FromConfigMap(cfg)
91 }
92
93
94 func (i *WorkloadSiemConfigData) FromConfigMap(cfg *v1.ConfigMap) *WorkloadSiemConfigData {
95 i.Configs = cfg.Data[WorkloadSiemConfigDataFieldName]
96 return i
97 }
98
99
100 func IsSiemConfigMap(name, namespace string) bool {
101 hasRequiredName := name == WorkloadSiemConfigMapName
102 inRequiredNamespace := namespace == WorkloadSiemConfigMapNS
103 return hasRequiredName && inRequiredNamespace
104 }
105
106
107 func ConfigMapToString(configMap *v1.ConfigMap) ([]byte, error) {
108 return json.Marshal(configMap)
109 }
110
View as plain text