From d145476d10e0b08d471f36bddaa660230f7fbec5 Mon Sep 17 00:00:00 2001 From: kooomix Date: Wed, 3 Jan 2024 09:44:05 +0200 Subject: [PATCH] parser --- armometadata/parser.go | 107 ++++++++++++++++++++++++++++++++++++ armometadata/parser_test.go | 76 +++++++++++++++++++++++++ 2 files changed, 183 insertions(+) create mode 100644 armometadata/parser.go create mode 100644 armometadata/parser_test.go diff --git a/armometadata/parser.go b/armometadata/parser.go new file mode 100644 index 0000000..c7ac2e3 --- /dev/null +++ b/armometadata/parser.go @@ -0,0 +1,107 @@ +package armometadata + +import ( + "time" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +const ( + AnnotationKeyStatus = "kubescape.io/status" + AnnotationValueIncomplete = "incomplete" + + MetadataKeyResourceVersion = "resourceVersion" +) + +type KubernetesObjectParser struct { + resourceVersion string + labels map[string]string + annotations map[string]string + creationStamp time.Time + ownerReferences metav1.OwnerReference + kind string + apiVersion string + podSelectorMatchLabels map[string]string +} + +func NewKubernetesResourceParser(input []byte) (*KubernetesObjectParser, error) { + err, annotations, labels, ownerReferences, creationStamp, resourceVersion, kind, apiVersion, podSelectorMatchLabels := ExtractMetadataFromJsonBytes(input) + + if err != nil { + return nil, err + } + + creationStampTime, err := time.Parse(time.RFC3339, creationStamp) + if err != nil { + return nil, err + } + + newOwnerReferences := metav1.OwnerReference{} + + if len(ownerReferences) > 0 { + if value, ok := ownerReferences["name"]; ok { + newOwnerReferences.Name = value + } + + if value, ok := ownerReferences["kind"]; ok { + newOwnerReferences.Kind = value + } + + } + + newKubernetesResourceParser := &KubernetesObjectParser{} + newKubernetesResourceParser.resourceVersion = resourceVersion + newKubernetesResourceParser.labels = labels + newKubernetesResourceParser.annotations = annotations + newKubernetesResourceParser.creationStamp = creationStampTime + newKubernetesResourceParser.ownerReferences = newOwnerReferences + newKubernetesResourceParser.kind = kind + newKubernetesResourceParser.apiVersion = apiVersion + newKubernetesResourceParser.podSelectorMatchLabels = podSelectorMatchLabels + + return newKubernetesResourceParser, nil +} + +func (k *KubernetesObjectParser) GetLabels() map[string]string { + return k.labels +} + +func (k *KubernetesObjectParser) GetLabel(label string) string { + return k.labels[label] +} + +func (k *KubernetesObjectParser) GetAnnotation(annotation string) string { + return k.annotations[annotation] +} + +func (k *KubernetesObjectParser) GetCreationTimestamp() time.Time { + return k.creationStamp +} + +func (k *KubernetesObjectParser) GetResourceVersion() string { + return k.resourceVersion +} + +func (k *KubernetesObjectParser) GetOwnerReferencesKind() string { + return k.ownerReferences.Kind +} + +func (k *KubernetesObjectParser) GetOwnerReferencesName() string { + return k.ownerReferences.Name +} + +func (k *KubernetesObjectParser) GetStatus() string { + return k.annotations[AnnotationKeyStatus] +} + +func (k *KubernetesObjectParser) GetKind() string { + return k.kind +} + +func (k *KubernetesObjectParser) GetApiVersion() string { + return k.apiVersion +} + +func (k *KubernetesObjectParser) GetPodSelectorMatchLabels() map[string]string { + return k.podSelectorMatchLabels +} diff --git a/armometadata/parser_test.go b/armometadata/parser_test.go new file mode 100644 index 0000000..e6b90c2 --- /dev/null +++ b/armometadata/parser_test.go @@ -0,0 +1,76 @@ +package armometadata + +import ( + "reflect" + "testing" + "time" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +func TestNewKubernetesResourceParser(t *testing.T) { + // Test case with valid JSON input + t.Run("valid input", func(t *testing.T) { + validInput := []byte(`{ + "metadata": { + "annotations": {"kubescape.io/status": "active"}, + "labels": {"kubescape.io/workload-name": "example"}, + "ownerReferences": [{"name": "ownerName", "kind": "ownerKind"}], + "creationTimestamp": "2023-03-15T08:00:00Z", + "resourceVersion": "12345" + } + }`) + + expectedCreationTimestamp, _ := time.Parse(time.RFC3339, "2023-03-15T08:00:00Z") + expectedParser := &KubernetesObjectParser{ + resourceVersion: "12345", + labels: map[string]string{"kubescape.io/workload-name": "example"}, + annotations: map[string]string{"kubescape.io/status": "active"}, + creationStamp: expectedCreationTimestamp, + ownerReferences: metav1.OwnerReference{Name: "ownerName", Kind: "ownerKind"}, + podSelectorMatchLabels: map[string]string{}, + } + + parser, err := NewKubernetesResourceParser(validInput) + if err != nil { + t.Fatalf("Expected no error, got %v", err) + } + if !reflect.DeepEqual(parser, expectedParser) { + t.Errorf("Expected parser %+v, got %+v", expectedParser, parser) + } + }) + + // Test case with invalid JSON input + t.Run("invalid json input", func(t *testing.T) { + invalidInput := []byte(`invalid json`) + + _, err := NewKubernetesResourceParser(invalidInput) + if err == nil { + t.Errorf("Expected error, got nil") + } + }) + + // Test case with invalid date format + t.Run("invalid date format", func(t *testing.T) { + invalidDateInput := []byte(`{ + "metadata": { + "creationTimestamp": "invalid-date-format" + } + }`) + + _, err := NewKubernetesResourceParser(invalidDateInput) + if err == nil { + t.Errorf("Expected error parsing date, got nil") + } + }) + + // Test case with empty JSON + t.Run("empty json", func(t *testing.T) { + emptyJSON := []byte(`{}`) + + _, err := NewKubernetesResourceParser(emptyJSON) + if err == nil { + t.Errorf("Expected error due to missing metadata, got nil") + } + }) +}