forked from acronis/go-cti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpression.go
550 lines (472 loc) · 14.8 KB
/
expression.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
/*
Copyright © 2024 Acronis International GmbH.
Released under MIT license.
*/
package cti
import (
"fmt"
"strconv"
"strings"
"github.com/google/uuid"
)
// Vendor is a vendor name.
type Vendor string
// IsWildCard returns true if the vendor name is a wildcard.
func (v Vendor) IsWildCard() bool {
return string(v) == string(Wildcard)
}
// Package is a package name.
type Package string
// IsWildCard returns true if the package name is a wildcard.
func (p Package) IsWildCard() bool {
return string(p) == string(Wildcard)
}
// EntityName is an entity name.
type EntityName string
// EndsWithWildcard returns true if the entity name contains a wildcard in the end.
func (en EntityName) EndsWithWildcard() bool {
return en != "" && en[len(en)-1] == Wildcard
}
// NullVersion is a nullable version value.
type NullVersion struct {
Value uint
Valid bool // Valid is true if Int32 is not NULL
}
// Version is a version.
type Version struct {
Major NullVersion
HasMajorWildcard bool
Minor NullVersion
HasMinorWildcard bool
}
// NewVersion constructs a new Version.
func NewVersion(major, minor uint) Version {
return Version{
Major: NullVersion{major, true},
Minor: NullVersion{minor, true},
}
}
// NewPartialVersion constructs a new Version with only major version part.
func NewPartialVersion(major uint) Version {
return Version{
Major: NullVersion{major, true},
}
}
// HasWildcard returns true if either the major or minor part is a wildcard.
func (v Version) HasWildcard() bool {
return v.HasMajorWildcard || v.HasMinorWildcard
}
// String returns string representation of the Version.
func (v Version) String() string {
var b strings.Builder
v.writeToBuilder(&b)
return b.String()
}
func (v Version) writeToBuilder(b *strings.Builder) {
// Major
if v.HasMajorWildcard {
b.WriteByte(Wildcard)
return
}
if !v.Major.Valid {
return
}
b.WriteString(strconv.FormatUint(uint64(v.Major.Value), 10))
if !v.Minor.Valid && !v.HasMinorWildcard {
return
}
b.WriteByte('.')
// Minor
if v.HasMinorWildcard {
b.WriteByte(Wildcard)
return
}
b.WriteString(strconv.FormatUint(uint64(v.Minor.Value), 10))
}
// Node represents a parsed complete chunk of CTI expression.
type Node struct {
Vendor Vendor
Package Package
EntityName EntityName
Version Version
DynamicParameterName string
Child *Node
}
// AttributeName is a name of the attribute that may be used in CTI query and attribute selector.
type AttributeName string
// QueryAttributeSlice is a slice of QueryAttribute.
type QueryAttributeSlice []QueryAttribute
// Match reports true if QueryAttributeSlice matches with the second QueryAttributeSlice.
func (as QueryAttributeSlice) Match(attrSlice2 QueryAttributeSlice) (bool, error) {
for i := range as {
queryAttr1 := &as[i]
var queryAttr2 *QueryAttribute
for j := range attrSlice2 {
if attrSlice2[j].Name == queryAttr1.Name {
queryAttr2 = &attrSlice2[j]
break
}
}
if queryAttr2 == nil {
return false, nil
}
if !queryAttr1.Value.IsExpression() && !queryAttr2.Value.IsExpression() {
if queryAttr1.Value.Raw != queryAttr2.Value.Raw {
return false, nil
}
continue
}
if !queryAttr1.Value.IsExpression() || !queryAttr2.Value.IsExpression() {
return false, nil
}
queryAttrMatched, queryMatchErr := queryAttr1.Value.Expression.Match(queryAttr2.Value.Expression)
if queryMatchErr != nil {
return false, fmt.Errorf("match query attribute %q: %w", queryAttr1.Name, queryMatchErr)
}
if !queryAttrMatched {
return false, nil
}
}
return true, nil
}
// QueryAttribute is an attribute that is used in CTI query.
type QueryAttribute struct {
Name AttributeName
Value QueryAttributeValue
}
// QueryAttributeValue is value of the attribute that is used in CTI query.
type QueryAttributeValue struct {
Raw string
Expression Expression
}
// IsExpression return true if QueryAttributeValue is expression
func (v QueryAttributeValue) IsExpression() bool {
return v.Expression.Head != nil || len(v.Expression.QueryAttributes) != 0
}
// HasWildcard returns true if Node contains wildcard in any section.
func (n *Node) HasWildcard() bool {
return n.Vendor.IsWildCard() || n.Package.IsWildCard() || n.EntityName.EndsWithWildcard() ||
n.Version.HasMajorWildcard || n.Version.HasMinorWildcard
}
// HasDynamicParameters returns true if the Node contains a dynamic parameter.
func (n *Node) HasDynamicParameters() bool {
return n.DynamicParameterName != ""
}
// String returns string representation of the Node.
func (n *Node) String() string {
b := strings.Builder{}
n.writeToBuilder(&b)
return b.String()
}
func (n *Node) writeToBuilder(b *strings.Builder) {
if n.DynamicParameterName != "" {
b.WriteByte('$')
b.WriteString(n.DynamicParameterName)
return
}
b.WriteString(string(n.Vendor))
if n.Vendor.IsWildCard() {
return
}
b.WriteByte('.')
b.WriteString(string(n.Package))
if n.Package.IsWildCard() {
return
}
b.WriteByte('.')
b.WriteString(string(n.EntityName))
if n.EntityName.EndsWithWildcard() {
return
}
b.WriteByte('.')
b.WriteByte('v')
n.Version.writeToBuilder(b)
}
// Expression represents a parsed CTI expression.
type Expression struct {
parser *Parser
// Head is a head node of the expression.
// Each node contains a complete chunk of the expression (cti.<vendor>.<package>.<entity>.v<major>.<minor>).
Head *Node
// QueryAttributes is a slice of query attributes.
// For example, for the following CTI expression:
// cti.a.p.am.alert.v1.0~a.p.activity.canceled.v1.0[category="cti.a.p.am.category.v1.0~a.p.backup.v1.0",severity="critical"]
// QueryAttributes will contain two attributes ("category" and "severity")
// with values ("cti.a.p.am.category.v1.0~a.p.backup.v1.0" and "critical").
QueryAttributes QueryAttributeSlice
// AttributeSelector is a selector of the attribute.
// For example, for the following CTI expression:
// cti.a.p.am.alert.v1.0~a.p.activity.canceled.v1.0@category
// AttributeSelector will contain "category".
AttributeSelector AttributeName
// AnonymousEntityUUID is an anonymous entity UUID.
// For example, for the following CTI expression:
// cti.a.p.am.alert.v1.0~ba3c448e-55e3-4f7f-ae54-4e87eb8635f6
// AnonymousEntityUUID will contain "ba3c448e-55e3-4f7f-ae54-4e87eb8635f6",
// and will be marked as valid (AnonymousEntityUUID.Valid == true).
AnonymousEntityUUID uuid.NullUUID
}
var emptyExpression = Expression{}
// Tail returns the last node.
func (e *Expression) Tail() *Node {
n := e.Head
for n != nil && n.Child != nil {
n = n.Child
}
return n
}
// HasWildcard returns true if the Expression contains wildcard.
func (e *Expression) HasWildcard() bool {
for n := e.Head; n != nil; n = n.Child {
if n.HasWildcard() {
return true
}
}
return false
}
// HasAnonymousEntity returns true if the Expression contains an anonymous entity.
func (e *Expression) HasAnonymousEntity() bool {
return e.AnonymousEntityUUID.Valid
}
// HasQueryAttributes returns true if the Expression contains any query attributes.
func (e *Expression) HasQueryAttributes() bool {
return len(e.QueryAttributes) != 0
}
// HasDynamicParameters returns true if the Expression contains a dynamic parameter in any node or query attribute.
func (e *Expression) HasDynamicParameters() bool {
for n := e.Head; n != nil; n = n.Child {
if n.HasDynamicParameters() {
return true
}
}
for i := range e.QueryAttributes {
queryAttr := &e.QueryAttributes[i]
if queryAttr.Value.IsExpression() && queryAttr.Value.Expression.HasDynamicParameters() {
return true
}
}
return false
}
// GetQueryAttributeValue returns QueryAttributeValue of the Expression by the attribute name.
func (e *Expression) GetQueryAttributeValue(name AttributeName) (QueryAttributeValue, bool) {
for i := range e.QueryAttributes {
if e.QueryAttributes[i].Name == name {
return e.QueryAttributes[i].Value, true
}
}
return QueryAttributeValue{}, false
}
// String returns the string representation of the whole CTI expression.
func (e *Expression) String() string {
res := strings.Builder{}
for node := e.Head; node != nil; node = node.Child {
if res.Len() == 0 {
res.WriteString("cti.")
} else {
res.WriteByte(InheritanceSeparator)
}
node.writeToBuilder(&res)
}
if e.AnonymousEntityUUID.Valid {
res.WriteByte('~')
res.WriteString(e.AnonymousEntityUUID.UUID.String())
}
if len(e.QueryAttributes) != 0 {
res.WriteByte('[')
for i := range e.QueryAttributes {
if i > 0 {
res.WriteByte(',')
}
res.WriteString(string(e.QueryAttributes[i].Name))
res.WriteByte('=')
res.WriteByte('"')
attrVal := e.QueryAttributes[i].Value.Raw
if e.QueryAttributes[i].Value.IsExpression() {
attrVal = e.QueryAttributes[i].Value.Expression.String()
}
res.WriteString(strings.ReplaceAll(attrVal, "\"", "\\\""))
res.WriteByte('"')
}
res.WriteByte(']')
}
if e.AttributeSelector != "" {
res.WriteByte('@')
res.WriteString(string(e.AttributeSelector))
}
return res.String()
}
// Match reports whether the Expression contains any match of the second expression.
func (e *Expression) Match(secondExpression Expression) (bool, error) {
return e.match(secondExpression, false)
}
// MatchIgnoreQuery reports whether the Expression contains any match of the second expression (ignoring expression query).
func (e *Expression) MatchIgnoreQuery(secondExpression Expression) (bool, error) {
return e.match(secondExpression, true)
}
// nolint:gocyclo // func implements an alg with well-defined concrete purpose, so high cyclomatic complexity is ok here
func (e *Expression) match(secondExpression Expression, ignoreQuery bool) (bool, error) {
if e.AttributeSelector != "" {
return false, fmt.Errorf("matching of CTI with attribute selector is not supported")
}
if secondExpression.AttributeSelector != "" {
return false, fmt.Errorf("matching against CTI with attribute selector is not supported")
}
if secondExpression.HasWildcard() {
return false, fmt.Errorf("matching against CTI with wildcard is not supported")
}
curNode1 := e.Head
curNode2 := secondExpression.Head
for ; curNode1 != nil && curNode2 != nil; curNode1, curNode2 = curNode1.Child, curNode2.Child {
// Vendor matching.
if curNode1.Vendor.IsWildCard() {
return true, nil
}
if curNode1.Vendor != curNode2.Vendor {
return false, nil
}
// Package matching.
if curNode1.Package.IsWildCard() {
return true, nil
}
if curNode1.Package != curNode2.Package {
return false, nil
}
// Entity type matching.
if curNode1.EntityName.EndsWithWildcard() {
entityName1Prefix := string(curNode1.EntityName)
entityName1Prefix = entityName1Prefix[:len(entityName1Prefix)-1] // Remove wildcard from the end.
// Prefix contains dot in the end after wildcard removal,
// so we need to add it to the second entity name as well for matching,
// since the version goes right after entity name.
if !strings.HasPrefix(string(curNode2.EntityName)+".", entityName1Prefix) {
return false, nil
}
return true, nil
}
if curNode1.EntityName != curNode2.EntityName {
return false, nil
}
// Entity version matching.
if curNode1.Version.HasMajorWildcard {
return true, nil
}
if !curNode1.Version.Major.Valid {
continue
}
if curNode1.Version.Major != curNode2.Version.Major {
return false, nil
}
if curNode1.Version.HasMinorWildcard {
return true, nil
}
if !curNode1.Version.Minor.Valid {
continue
}
if curNode1.Version.Minor != curNode2.Version.Minor {
return false, nil
}
}
switch {
case curNode1 == nil && curNode2 == nil:
if e.AnonymousEntityUUID != secondExpression.AnonymousEntityUUID {
return false, nil
}
if !ignoreQuery {
if qaMatched, err := e.QueryAttributes.Match(secondExpression.QueryAttributes); err != nil || !qaMatched {
return false, err
}
}
case curNode1 != nil: // curNode2 == nil
return false, nil
default: // curNode2 != nil && curNode1 == nil
if e.AnonymousEntityUUID.Valid || (!ignoreQuery && e.HasQueryAttributes()) {
return false, nil
}
}
return true, nil
}
// DynamicParameterValues is a container (map) of dynamic parameter values that can be interpolated into the Expression.
type DynamicParameterValues map[string]string
// InterpolateDynamicParameterValues interpolates dynamic parameter values into the Expression.
//
//nolint:funlen // func implements an alg with well-defined concrete purpose, so high cyclomatic complexity is ok here
func (e *Expression) InterpolateDynamicParameterValues(values DynamicParameterValues) (Expression, error) {
var cpHead *Node
var cpPrevNode *Node
initHeadAndPrevNodes := func(n *Node) {
if cpHead == nil {
cpHead = n
cpPrevNode = n
return
}
cpPrevNode.Child = n
cpPrevNode = n
}
for curNode := e.Head; curNode != nil; curNode = curNode.Child {
if curNode.DynamicParameterName == "" {
initHeadAndPrevNodes(&Node{
Vendor: curNode.Vendor,
Package: curNode.Package,
EntityName: curNode.EntityName,
Version: curNode.Version,
})
continue
}
val, ok := values[curNode.DynamicParameterName]
if !ok {
return emptyExpression, fmt.Errorf("dynamic parameter values do not have %q key", curNode.DynamicParameterName)
}
valToParse := val
isCompleteCTI := true
if !strings.HasPrefix(val, "cti.") {
valToParse = "cti." + val
isCompleteCTI = false
}
// Parse value as CTI expression.
p := *e.parser
p.allowedDynamicParameterNames = nil // avoid recursion
parsedExp, parseErr := p.Parse(valToParse)
if parseErr != nil {
return emptyExpression, fmt.Errorf("parse value %q of dynamic parameter %q as CTI: %w",
val, curNode.DynamicParameterName, parseErr)
}
if !isCompleteCTI {
initHeadAndPrevNodes(parsedExp.Head)
continue
}
if cpHead != nil {
prefixExp := Expression{Head: cpHead}
match, matchErr := prefixExp.Match(parsedExp)
if matchErr != nil {
return emptyExpression, fmt.Errorf("match %q and value %q of dynamic parameter %q: %w",
prefixExp.String(), val, curNode.DynamicParameterName, matchErr)
}
if !match {
return emptyExpression, fmt.Errorf("%q and value %q of dynamic parameter %q are not matched",
prefixExp.String(), val, curNode.DynamicParameterName)
}
}
cpHead = parsedExp.Head
cpPrevNode = parsedExp.Tail()
}
cpQueryAttributes := make([]QueryAttribute, len(e.QueryAttributes))
for i := range e.QueryAttributes {
queryAttr := &e.QueryAttributes[i]
var cpExp Expression
if queryAttr.Value.IsExpression() {
var err error
if cpExp, err = queryAttr.Value.Expression.InterpolateDynamicParameterValues(values); err != nil {
return emptyExpression, fmt.Errorf("interpolate dynamic parameters for attribute %q: %w",
queryAttr.Name, err)
}
}
cpQueryAttributes[i] = QueryAttribute{
Name: queryAttr.Name,
Value: QueryAttributeValue{
Raw: queryAttr.Value.Raw,
Expression: cpExp,
},
}
}
return Expression{Head: cpHead, QueryAttributes: cpQueryAttributes}, nil
}