-
Notifications
You must be signed in to change notification settings - Fork 18
/
parsing.go
178 lines (149 loc) · 5.24 KB
/
parsing.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// Copyright 2020 Red Hat, Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package content
import (
"errors"
"strings"
"time"
"github.com/RedHatInsights/insights-operator-utils/collections"
ctypes "github.com/RedHatInsights/insights-results-types"
"github.com/rs/zerolog/log"
"github.com/RedHatInsights/insights-results-smart-proxy/types"
)
const (
internalRuleStr = "internal"
ocsRuleStr = "ocs"
)
var (
timeParseFormats = []string{
"2006-01-02 15:04:05",
time.RFC3339,
}
)
// TODO: consider moving parsing to content service
// LoadRuleContent loads the parsed rule content into the storage
func LoadRuleContent(contentDir *ctypes.RuleContentDirectory) {
s := getEmptyRulesWithContentMap()
for i, rule := range contentDir.Rules {
ruleID := ctypes.RuleID(rule.Plugin.PythonModule)
for errorKey, errorProperties := range rule.ErrorKeys {
impact := errorProperties.Metadata.Impact
// we allow empty/missing, but not incorrect
active, success, missing := getActiveStatus(errorProperties.Metadata.Status)
if !success {
log.Error().Msgf(`rule ID %v with key %v has invalid status attribute`, ruleID, errorKey)
continue
} else if missing {
log.Warn().Msgf(`rule ID %v with key %v has missing status attribute`, ruleID, errorKey)
}
// we allow empty/missing, but not incorrect format
publishDate, missing, err := timeParse(errorProperties.Metadata.PublishDate)
if err != nil {
log.Error().Err(err).Msgf(`rule ID %v with key %v has improper publish_date attribute`, ruleID, errorKey)
continue
} else if missing {
log.Warn().Msgf(`rule ID %v with key %v has missing publish_date attribute`, ruleID, errorKey)
}
totalRisk := calculateTotalRisk(impact.Impact, errorProperties.Metadata.Likelihood)
ruleTmp := contentDir.Rules[i]
if ruleTmpErrorKey, ok := ruleTmp.ErrorKeys[errorKey]; ok {
ruleTmpErrorKey.TotalRisk = totalRisk
ruleTmp.ErrorKeys[errorKey] = ruleTmpErrorKey
}
// sets "plugin" level, containing usual fields + list of error keys
s.SetRule(ruleID, &ruleTmp)
s.SetRuleWithContent(ruleID, ctypes.ErrorKey(errorKey), &types.RuleWithContent{
Module: ruleID,
Name: rule.Plugin.Name,
Generic: errorProperties.Generic,
Summary: errorProperties.Summary,
Reason: errorProperties.Reason,
Resolution: errorProperties.Resolution,
MoreInfo: errorProperties.MoreInfo,
ErrorKey: ctypes.ErrorKey(errorKey),
Description: errorProperties.Metadata.Description,
TotalRisk: totalRisk,
ResolutionRisk: errorProperties.Metadata.ResolutionRisk,
Impact: impact.Impact,
Likelihood: errorProperties.Metadata.Likelihood,
PublishDate: publishDate,
Active: active,
Internal: IsRuleInternal(ruleID),
Tags: errorProperties.Metadata.Tags,
OSDCustomer: collections.StringInSlice("osd_customer", errorProperties.Metadata.Tags),
})
}
}
rulesWithContentStorage = s
}
// According to rule content specification, it's explicitly defined as floor((impact + likelihood) / 2), which
// is the default behaviour in Go
func calculateTotalRisk(impact, likelihood int) int {
return (impact + likelihood) / 2
}
// TODO: move to utils
func commaSeparatedStrToTags(str string) []string {
str = strings.TrimSpace(str)
if str == "" {
return []string{}
}
return strings.Split(str, ",")
}
func timeParse(value string) (publishDate time.Time, missing bool, err error) {
missing = false
publishDate = time.Time{}
if value == "" {
missing = true
return
}
for _, datetimeLayout := range timeParseFormats {
publishDate, err = time.Parse(datetimeLayout, value)
if err == nil {
return
}
log.Info().Msgf(
`unable to parse time "%v" using layout "%v"`,
value, datetimeLayout,
)
}
log.Error().Msgf("problem parsing publish_date: %v", err)
err = errors.New("invalid format of publish_date")
return
}
// Reads Status string, first returned bool is active status, second bool is a success check
func getActiveStatus(status string) (active, success, missing bool) {
active, success, missing = false, false, false
status = strings.ToLower(strings.TrimSpace(status))
switch status {
case "active":
active = true
success = true
case "inactive":
success = true
case "":
success = true
missing = true
default:
log.Error().Msgf("invalid rule error key status: '%s'", status)
}
return
}
// IsRuleInternal tries to look for the word "internal" in the ruleID / rule module,
// because it's currently not specified anywhere on it's own
func IsRuleInternal(ruleID ctypes.RuleID) bool {
splitRuleID := strings.Split(string(ruleID), ".")
return len(splitRuleID) > 1 &&
(splitRuleID[1] == internalRuleStr ||
splitRuleID[1] == ocsRuleStr)
}