/
filtering.go
190 lines (162 loc) · 6 KB
/
filtering.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
package dao
import (
"fmt"
"strings"
"time"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/chef/automate/api/external/common/query"
"github.com/chef/automate/components/secrets-service/utils"
"github.com/chef/automate/api/external/lib/errorutils"
"github.com/chef/automate/lib/pgutils"
)
func mergeFilters(mergeableFilters []*query.Filter) ([]query.Filter, error) {
filterMap := make(map[string]query.Filter)
for _, mf := range mergeableFilters {
var values []string
key := mf.Key
if f, found := filterMap[key]; found {
if f.Exclude != mf.Exclude {
return nil, errors.New("Filters are not allowed to be inclusive and exclusive on the same field.")
}
values = append(f.Values, mf.Values...)
} else {
values = mf.Values
}
filterMap[key] = query.Filter{Key: key, Values: values, Exclude: mf.Exclude}
}
filters := make([]query.Filter, len(filterMap))
i := 0
for k := range filterMap {
filters[i] = filterMap[k]
i++
}
return filters, nil
}
// Takes a filter map (should be validated for content) and table abbreviation and returns a wherefilter
func buildWhereFilter(mergeableFilters []*query.Filter, tableAbbrev string, filterField map[string]string) (whereFilter string, err error) {
if len(mergeableFilters) == 0 {
return "", nil
}
filters, err := mergeFilters(mergeableFilters)
if err != nil {
return "", errors.Wrap(err, "buildWhereFilter error")
}
conditions := make([]string, 0, len(filters))
for _, filter := range filters {
var newCondition string
var err error
if strings.HasPrefix(filter.Key, "tags:") {
tagKeyFilter := strings.TrimPrefix(filter.Key, "tags:")
newCondition, err = wherePatternMatchTags(tagKeyFilter, filter.Values, "t")
} else {
switch filterField[filter.Key] {
case "":
continue
case "source_region", "name":
newCondition, err = wherePatternMatch(filterField[filter.Key], filter.Values, tableAbbrev)
case "statechange_timestamp":
newCondition, err = whereFieldBetween(filterField[filter.Key], filter.Values, tableAbbrev)
case "manager_id":
newCondition, err = whereNodeManagerNodeExists(filterField[filter.Key], filter.Values, tableAbbrev)
default:
newCondition, err = whereFieldIn(filterField[filter.Key], filter.Values, tableAbbrev)
}
}
if err != nil {
whereFilter := "WHERE " + strings.Join(conditions, " AND ")
return whereFilter, errors.Wrap(err, "buildWhereFilter error")
}
if filter.Exclude {
newCondition = fmt.Sprintf("NOT (%s)", newCondition)
}
conditions = append(conditions, newCondition)
}
whereFilter = "WHERE " + strings.Join(conditions, " AND ")
logrus.Debugf("buildWhereFilter, whereFilter=%s", whereFilter)
return whereFilter, nil
}
// Builds an IN where condition like: j.parent_id IN ('e57605ed-bb8a-49b8-606c-af0e2b31b139')
func whereFieldIn(field string, arr []string, tableAbbrev string) (condition string, err error) {
if !pgutils.IsSqlSafe(field) {
return "", &errorutils.InvalidError{Msg: fmt.Sprintf("Unsupported character found in field: %s", field)}
}
condition += fmt.Sprintf("%s.%s IN (", tableAbbrev, field)
if len(arr) > 0 {
for index, item := range arr {
condition += fmt.Sprintf("'%s'", pgutils.EscapeLiteralForPG(item))
if index < len(arr)-1 {
condition += ","
}
}
} else {
condition += "''"
}
return condition + ")", nil
}
func whereFieldBetween(field string, arr []string, tableAbbrev string) (condition string, err error) {
if !pgutils.IsSqlSafe(field) {
return "", &errorutils.InvalidError{Msg: fmt.Sprintf("Unsupported character found in field: %s", field)}
}
if len(arr) != 2 {
return "", &errorutils.InvalidError{Msg: fmt.Sprintf("Two params requires for whereFieldBetween %d", len(arr))}
}
for _, elem := range arr {
_, err := time.Parse(time.RFC3339, elem)
if err != nil {
return "", &errorutils.InvalidError{Msg: fmt.Sprintf("Invalid timestamp: %s", elem)}
}
}
condition = fmt.Sprintf("%s.%s BETWEEN SYMMETRIC '%s' AND '%s'", tableAbbrev, field, arr[0], arr[1])
return condition, nil
}
func wherePatternMatchTags(field string, arr []string, tableAbbrev string) (condition string, err error) {
if !pgutils.IsSqlSafe(field) {
return "", &errorutils.InvalidError{Msg: fmt.Sprintf("Unsupported character found in field: %s", field)}
}
condition += fmt.Sprintf("%s.key LIKE '%s' AND %s.value LIKE ", tableAbbrev, field, tableAbbrev)
if len(arr) == 0 {
condition += "''"
return condition, nil
}
for index, item := range arr {
condition += fmt.Sprintf("'%s%%'", pgutils.EscapeLiteralForPGPatternMatch(item))
if index < len(arr)-1 {
condition += fmt.Sprintf(" OR %s.value LIKE ", tableAbbrev)
}
}
return condition, nil
}
func wherePatternMatch(field string, arr []string, tableAbbrev string) (condition string, err error) {
if !pgutils.IsSqlSafe(field) {
return "", &errorutils.InvalidError{Msg: fmt.Sprintf("Unsupported character found in field: %s", field)}
}
condition += fmt.Sprintf("%s.%s LIKE ", tableAbbrev, field)
if len(arr) == 0 {
condition += "''"
return condition, nil
}
for index, item := range arr {
condition += fmt.Sprintf("'%s%%'", pgutils.EscapeLiteralForPGPatternMatch(item))
if index < len(arr)-1 {
condition += fmt.Sprintf(" OR %s.%s LIKE ", tableAbbrev, field)
}
}
return condition, nil
}
func whereNodeManagerNodeExists(field string, arr []string, tableAbbrev string) (condition string, err error) {
if !pgutils.IsSqlSafe(field) {
return "", &errorutils.InvalidError{Msg: fmt.Sprintf("Unsupported character found in field: %s", field)}
}
if len(arr) == 0 {
condition += fmt.Sprintf("not exists (select 1 from node_managers_nodes nmn where %s.id = nmn.node_id)", tableAbbrev)
return condition, nil
}
for _, item := range arr {
if !utils.IsSafeUUID(item) {
return "", &errorutils.InvalidError{Msg: fmt.Sprintf("Unsupported character found in: %s", item)}
}
}
condition += fmt.Sprintf("exists (select 1 from node_managers_nodes nmn where %s.id = nmn.node_id and nmn.%s = ANY('{%s}'::text[]))", tableAbbrev, field, strings.Join(arr, ","))
return condition, nil
}