/
text_analyze.go
323 lines (275 loc) · 8.34 KB
/
text_analyze.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package analyzer
import (
"bytes"
"fmt"
"path/filepath"
"regexp"
"strconv"
"strings"
"text/template"
"github.com/pkg/errors"
troubleshootv1beta2 "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2"
)
type AnalyzeTextAnalyze struct {
analyzer *troubleshootv1beta2.TextAnalyze
}
func (a *AnalyzeTextAnalyze) Title() string {
checkName := a.analyzer.CheckName
if checkName == "" {
checkName = a.analyzer.CollectorName
}
return checkName
}
func (a *AnalyzeTextAnalyze) IsExcluded() (bool, error) {
return isExcluded(a.analyzer.Exclude)
}
func (a *AnalyzeTextAnalyze) Analyze(getFile getCollectedFileContents, findFiles getChildCollectedFileContents) ([]*AnalyzeResult, error) {
return analyzeTextAnalyze(a.analyzer, findFiles, a.Title())
}
func analyzeTextAnalyze(
analyzer *troubleshootv1beta2.TextAnalyze, getCollectedFileContents getChildCollectedFileContents, title string,
) ([]*AnalyzeResult, error) {
fullPath := filepath.Join(analyzer.CollectorName, analyzer.FileName)
excludeFiles := []string{}
for _, excludeFile := range analyzer.ExcludeFiles {
excludeFiles = append(excludeFiles, filepath.Join(analyzer.CollectorName, excludeFile))
}
collected, err := getCollectedFileContents(fullPath, excludeFiles)
if err != nil {
return nil, errors.Wrapf(err, "failed to read collected file name: %s", fullPath)
}
if len(collected) == 0 {
if analyzer.IgnoreIfNoFiles {
return nil, nil
}
return []*AnalyzeResult{
{
Title: title,
IconKey: "kubernetes_text_analyze",
IconURI: "https://troubleshoot.sh/images/analyzer-icons/text-analyze.svg",
IsWarn: true,
Message: "No matching files",
},
}, nil
}
results := []*AnalyzeResult{}
if analyzer.RegexPattern != "" {
for _, fileContents := range collected {
result, err := analyzeRegexPattern(analyzer.RegexPattern, fileContents, analyzer.Outcomes, title)
if err != nil {
return nil, err
}
if result != nil {
results = append(results, result)
}
}
}
if analyzer.RegexGroups != "" {
for _, fileContents := range collected {
result, err := analyzeRegexGroups(analyzer.RegexGroups, fileContents, analyzer.Outcomes, title)
if err != nil {
return nil, err
}
if result != nil {
results = append(results, result)
}
}
}
for i := range results {
results[i].Strict = analyzer.Strict.BoolOrDefaultFalse()
}
if len(results) > 0 {
return results, nil
}
return []*AnalyzeResult{
{
Title: title,
IconKey: "kubernetes_text_analyze",
IconURI: "https://troubleshoot.sh/images/analyzer-icons/text-analyze.svg",
IsFail: true,
Message: "Invalid analyzer",
},
}, nil
}
func analyzeRegexPattern(pattern string, collected []byte, outcomes []*troubleshootv1beta2.Outcome, checkName string) (*AnalyzeResult, error) {
re, err := regexp.Compile(pattern)
if err != nil {
return nil, errors.Wrapf(err, "failed to compile regex: %s", pattern)
}
var failOutcome *troubleshootv1beta2.SingleOutcome
var passOutcome *troubleshootv1beta2.SingleOutcome
for _, outcome := range outcomes {
if outcome.Fail != nil {
failOutcome = outcome.Fail
} else if outcome.Pass != nil {
passOutcome = outcome.Pass
}
}
result := AnalyzeResult{
Title: checkName,
IconKey: "kubernetes_text_analyze",
IconURI: "https://troubleshoot.sh/images/analyzer-icons/text-analyze.svg",
}
reMatch := re.MatchString(string(collected))
failWhen := false
if failOutcome != nil && failOutcome.When != "" {
failWhen, err = strconv.ParseBool(failOutcome.When)
if err != nil {
return nil, errors.Wrapf(err, "failed to process when statement: %s", failOutcome.When)
}
}
passWhen := true
if passOutcome != nil && passOutcome.When != "" {
passWhen, err = strconv.ParseBool(passOutcome.When)
if err != nil {
return nil, errors.Wrapf(err, "failed to process when statement: %s", passOutcome.When)
}
}
if passWhen == failWhen {
return nil, errors.Wrap(err, "outcome when conditions for fail and pass are equal")
}
if reMatch == passWhen {
result.IsPass = true
if passOutcome != nil {
result.Message = passOutcome.Message
result.URI = passOutcome.URI
}
return &result, nil
}
result.IsFail = true
if failOutcome != nil {
result.Message = failOutcome.Message
result.URI = failOutcome.URI
}
return &result, nil
}
func analyzeRegexGroups(pattern string, collected []byte, outcomes []*troubleshootv1beta2.Outcome, checkName string) (*AnalyzeResult, error) {
re, err := regexp.Compile(pattern)
if err != nil {
return nil, errors.Wrapf(err, "failed to compile regex: %s", pattern)
}
match := re.FindStringSubmatch(string(collected))
result := &AnalyzeResult{
Title: checkName,
IconKey: "kubernetes_text_analyze",
IconURI: "https://troubleshoot.sh/images/analyzer-icons/text-analyze.svg?w=13&h=16",
}
foundMatches := map[string]string{}
for i, name := range re.SubexpNames() {
if i != 0 && name != "" && len(match) > i {
foundMatches[name] = match[i]
}
}
// allow fallthrough
for _, outcome := range outcomes {
if outcome.Fail != nil {
isMatch, err := compareRegex(outcome.Fail.When, foundMatches)
if err != nil {
return result, errors.Wrap(err, "failed to compare regex fail conditional")
}
if isMatch {
result.IsFail = true
tplMessage, err := templateRegExGroup(outcome.Fail.Message, foundMatches)
if err != nil {
return result, errors.Wrap(err, "failed to template message in outcome.Fail block")
}
result.Message = tplMessage
result.URI = outcome.Fail.URI
return result, nil
}
} else if outcome.Warn != nil {
isMatch, err := compareRegex(outcome.Warn.When, foundMatches)
if err != nil {
return result, errors.Wrap(err, "failed to compare regex warn conditional")
}
if isMatch {
result.IsWarn = true
tplMessage, err := templateRegExGroup(outcome.Warn.Message, foundMatches)
if err != nil {
return result, errors.Wrap(err, "failed to template message in outcome.Warn block")
}
result.Message = tplMessage
result.URI = outcome.Warn.URI
return result, nil
}
} else if outcome.Pass != nil {
isMatch, err := compareRegex(outcome.Pass.When, foundMatches)
if err != nil {
return result, errors.Wrap(err, "failed to compare regex pass conditional")
}
if isMatch {
result.IsPass = true
tplMessage, err := templateRegExGroup(outcome.Pass.Message, foundMatches)
if err != nil {
return result, errors.Wrap(err, "failed to template message in outcome.Pass block")
}
result.Message = tplMessage
result.URI = outcome.Pass.URI
return result, nil
}
}
}
return result, nil
}
// templateRegExGroup takes a tpl and replaces the variables using matches.
func templateRegExGroup(tpl string, matches map[string]string) (string, error) {
t, err := template.New("").Parse(tpl)
if err != nil {
return "", err
}
var msg bytes.Buffer
err = t.Execute(&msg, matches)
if err != nil {
return "", err
}
return msg.String(), nil
}
func compareRegex(conditional string, foundMatches map[string]string) (bool, error) {
if conditional == "" {
return true, nil
}
parts := strings.Split(strings.TrimSpace(conditional), " ")
if len(parts) != 3 {
return false, errors.New("unable to parse regex conditional")
}
lookForMatchName := parts[0]
operator := parts[1]
lookForValue := parts[2]
foundValue, ok := foundMatches[lookForMatchName]
if !ok {
// not an error, just wasn't matched
return false, nil
}
// if the value side of the conditional is an int, we assume it's an int
lookForValueInt, err := strconv.Atoi(lookForValue)
if err == nil {
foundValueInt, err := strconv.Atoi(foundValue)
if err != nil {
// not an error but maybe it should be...
return false, nil
}
switch operator {
case "=":
fallthrough
case "==":
fallthrough
case "===":
return foundValueInt == lookForValueInt, nil
case "<":
return foundValueInt < lookForValueInt, nil
case ">":
return foundValueInt > lookForValueInt, nil
case "<=":
return foundValueInt <= lookForValueInt, nil
case ">=":
return foundValueInt >= lookForValueInt, nil
}
} else {
// all we can support is "=" and "==" and "===" for now
if operator != "=" && operator != "==" && operator != "===" {
return false, fmt.Errorf("unexpected operator %q in regex comparator, cannot compare %q and %q", operator, foundValue, lookForValue)
}
return foundValue == lookForValue, nil
}
return false, nil
}