From 9e1a1e35c25cd3014f88311e509f934c2e824f55 Mon Sep 17 00:00:00 2001 From: Frank Jogeleit Date: Wed, 21 Apr 2021 00:57:24 +0200 Subject: [PATCH] fix result.timestamp parsing (#26) Signed-off-by: Frank Jogeleit --- CHANGELOG.md | 8 ++++++ charts/policy-reporter/Chart.yaml | 4 +-- charts/policy-reporter/values.yaml | 2 +- pkg/kubernetes/mapper.go | 25 +++++++++++++----- pkg/kubernetes/mapper_test.go | 42 +++++++++++++++++------------- 5 files changed, 54 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 48069d53..bc31b344 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # Changelog +## 1.2.2 + +* Fix PolicyReportResult.timestamp parsing + +## 1.2.1 + +* Support PolicyReportResult.status as well as PolicyReportResult.result for newer CRD versions + ## 1.2.0 * Support for (Cluster)PolicyReport CRD Properties in Target Output diff --git a/charts/policy-reporter/Chart.yaml b/charts/policy-reporter/Chart.yaml index f96e052d..f0fb742e 100644 --- a/charts/policy-reporter/Chart.yaml +++ b/charts/policy-reporter/Chart.yaml @@ -5,8 +5,8 @@ description: | It creates Prometheus Metrics and can send rule validation events to different targets like Loki, Elasticsearch, Slack or Discord type: application -version: 1.2.1 -appVersion: 1.2.1 +version: 1.2.2 +appVersion: 1.2.2 dependencies: - name: monitoring diff --git a/charts/policy-reporter/values.yaml b/charts/policy-reporter/values.yaml index bba6d12a..df890e97 100644 --- a/charts/policy-reporter/values.yaml +++ b/charts/policy-reporter/values.yaml @@ -1,7 +1,7 @@ image: repository: fjogeleit/policy-reporter pullPolicy: IfNotPresent - tag: 1.2.1 + tag: 1.2.2 imagePullSecrets: [] diff --git a/pkg/kubernetes/mapper.go b/pkg/kubernetes/mapper.go index b83897ad..be412e9a 100644 --- a/pkg/kubernetes/mapper.go +++ b/pkg/kubernetes/mapper.go @@ -172,12 +172,7 @@ func (m *mapper) mapResult(result map[string]interface{}) report.Result { r.Category = category.(string) } - if created, ok := result["timestamp"]; ok { - time, err := time.Parse("2006-01-02T15:04:05Z", created.(string)) - if err == nil { - r.Timestamp = time - } - } + r.Timestamp = convertTimestamp(result) if props, ok := result["properties"]; ok { if properties, ok := props.(map[string]interface{}); ok { @@ -190,6 +185,24 @@ func (m *mapper) mapResult(result map[string]interface{}) report.Result { return r } +func convertTimestamp(result map[string]interface{}) time.Time { + timestamp, ok := result["timestamp"] + if !ok { + return time.Now().UTC() + } + + seconds, ok := timestamp.(map[string]interface{})["seconds"] + + switch s := seconds.(type) { + case int64: + return time.Unix(s, 0).UTC() + case int: + return time.Unix(int64(s), 0).UTC() + default: + return time.Now().UTC() + } +} + func (m *mapper) resolvePriority(policy string, severity report.Severity) report.Priority { if priority, ok := m.priorityMap[policy]; ok { return report.NewPriority(priority) diff --git a/pkg/kubernetes/mapper_test.go b/pkg/kubernetes/mapper_test.go index ffe4b922..c3d88b4d 100644 --- a/pkg/kubernetes/mapper_test.go +++ b/pkg/kubernetes/mapper_test.go @@ -28,14 +28,16 @@ var policyMap = map[string]interface{}{ }, "results": []interface{}{ map[string]interface{}{ - "message": "message", - "status": "fail", - "scored": true, - "policy": "required-label", - "rule": "app-label-required", - "timestamp": "2021-02-23T15:10:00Z", - "category": "test", - "severity": "high", + "message": "message", + "status": "fail", + "scored": true, + "policy": "required-label", + "rule": "app-label-required", + "timestamp": map[string]interface{}{ + "seconds": 1614093000, + }, + "category": "test", + "severity": "high", "resources": []interface{}{ map[string]interface{}{ "apiVersion": "v1", @@ -50,9 +52,12 @@ var policyMap = map[string]interface{}{ }, }, map[string]interface{}{ - "message": "message 2", - "status": "fail", - "scored": true, + "message": "message 2", + "status": "fail", + "scored": true, + "timestamp": map[string]interface{}{ + "seconds": int64(1614093000), + }, "policy": "priority-test", "resources": []interface{}{}, }, @@ -81,13 +86,14 @@ var clusterPolicyMap = map[string]interface{}{ }, "results": []interface{}{ map[string]interface{}{ - "message": "message", - "result": "fail", - "scored": true, - "policy": "required-label", - "rule": "app-label-required", - "category": "test", - "severity": "high", + "message": "message", + "result": "fail", + "scored": true, + "policy": "required-label", + "rule": "app-label-required", + "category": "test", + "severity": "high", + "timestamp": map[string]interface{}{"seconds": ""}, "resources": []interface{}{ map[string]interface{}{ "apiVersion": "v1",