1
0
Fork 0
mirror of https://github.com/kyverno/policy-reporter.git synced 2024-12-14 11:57:32 +00:00

fix result.timestamp parsing (#26)

Signed-off-by: Frank Jogeleit <fj@move-elevator.de>
This commit is contained in:
Frank Jogeleit 2021-04-21 00:57:24 +02:00 committed by GitHub
parent 6e33bde8ef
commit 9e1a1e35c2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 54 additions and 27 deletions

View file

@ -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

View file

@ -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

View file

@ -1,7 +1,7 @@
image:
repository: fjogeleit/policy-reporter
pullPolicy: IfNotPresent
tag: 1.2.1
tag: 1.2.2
imagePullSecrets: []

View file

@ -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)

View file

@ -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",