-
Notifications
You must be signed in to change notification settings - Fork 5
/
inject.go
187 lines (164 loc) · 5.14 KB
/
inject.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
//
// Copyright 2020 IBM Corporation
//
// 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 proxy
import (
"fmt"
"log"
"strings"
promlabels "github.com/prometheus/prometheus/pkg/labels"
"github.com/prometheus/prometheus/promql/parser"
promparser "github.com/prometheus/prometheus/promql/parser"
)
func setRecursive(node promparser.Node, nsLabelname string, namespaces []string) (err error) {
switch n := node.(type) {
case *parser.EvalStmt:
if err := setRecursive(n.Expr, nsLabelname, namespaces); err != nil {
return err
}
case parser.Expressions:
for _, e := range n {
if err := setRecursive(e, nsLabelname, namespaces); err != nil {
return err
}
}
case *parser.AggregateExpr:
if err := setRecursive(n.Expr, nsLabelname, namespaces); err != nil {
return err
}
case *parser.BinaryExpr:
if err := setRecursive(n.LHS, nsLabelname, namespaces); err != nil {
return err
}
if err := setRecursive(n.RHS, nsLabelname, namespaces); err != nil {
return err
}
case *parser.Call:
if err := setRecursive(n.Args, nsLabelname, namespaces); err != nil {
return err
}
case *parser.ParenExpr:
if err := setRecursive(n.Expr, nsLabelname, namespaces); err != nil {
return err
}
case *parser.UnaryExpr:
if err := setRecursive(n.Expr, nsLabelname, namespaces); err != nil {
return err
}
case *parser.SubqueryExpr:
if err := setRecursive(n.Expr, nsLabelname, namespaces); err != nil {
return err
}
case *parser.NumberLiteral, *parser.StringLiteral:
// nothing to do
case *parser.MatrixSelector:
// inject labelselector
if vs, ok := n.VectorSelector.(*parser.VectorSelector); ok {
vs.LabelMatchers = enforceLabelMatcher(vs.LabelMatchers, nsLabelname, namespaces)
}
case *parser.VectorSelector:
// inject labelselector
n.LabelMatchers = enforceLabelMatcher(n.LabelMatchers, nsLabelname, namespaces)
default:
panic(fmt.Errorf("promql.Walk: unhandled node type %T", node))
}
return err
}
//This is where really injection is done.
//It limits original query's namespace matcher
//1. no namespace mather at all
//2. use Equal matcher
//3. use MatchRegexp but can only be simple pattern "name1|name2|name3" or "(name1|name2|name3)" or ".*".
// there should not be any kind of wildcard in the names, otherwise it will return empty data by injecting noDataMatcher
func enforceLabelMatcher(matchers []*promlabels.Matcher, nsLabelname string, namespaces []string) []*promlabels.Matcher {
res := []*promlabels.Matcher{}
var nsMatcher *promlabels.Matcher
noDataMatcher := &promlabels.Matcher{
Type: promlabels.MatchEqual,
Name: nsLabelname,
Value: "__ibm-ocpthanos-proxy-no-data-namespace__",
}
for _, m := range matchers {
if m.Name == nsLabelname {
nsMatcher = m
continue
}
res = append(res, m)
}
if nsMatcher == nil {
//create namespace matcher if raw expression does not containe one
nsMatcher = &promlabels.Matcher{
Type: promlabels.MatchRegexp,
Name: nsLabelname,
Value: generateRegExpr(namespaces),
}
return append(res, nsMatcher)
}
//combine to existing namespace selector
if nsMatcher.Type == promlabels.MatchEqual {
allowed := false
for _, ns := range namespaces {
if ns == nsMatcher.Value {
allowed = true
}
}
if allowed {
return append(res, nsMatcher)
}
}
//namespace matcher value in query string is assumed in the format: '.*' or 'name1|name2|name3' or '(name1|name2|name3)'
//'.*' will be replaced with namespace matcher, otherwise:
//if any one (name1, name2, name3) is not accessible to user, noDataMatcher will be injected
if nsMatcher.Type == promlabels.MatchRegexp {
allowed := true
regexp := strings.TrimSuffix(strings.TrimPrefix(nsMatcher.Value, "("), ")")
if regexp == ".*" {
//create namespace matcher if raw expression tries to match any namespace
nsMatcher = &promlabels.Matcher{
Type: promlabels.MatchRegexp,
Name: nsLabelname,
Value: generateRegExpr(namespaces),
}
return append(res, nsMatcher)
}
//check all namespaces from 'name1|name2|name3' pattern
origNamespaces := strings.Split(regexp, "|")
for _, ons := range origNamespaces {
found := false
for _, ns := range namespaces {
if ons == ns {
found = true
break
}
}
if !found {
allowed = false
break
}
}
if allowed {
return append(res, nsMatcher)
}
}
log.Printf("no data matcher is injected query. namespace matcher in query: %s. allowed namespaces: %s",
nsMatcher.Value, strings.Join(namespaces, ","))
return append(res, noDataMatcher)
}
func generateRegExpr(namespaces []string) string {
for i, ns := range namespaces {
namespaces[i] = "^" + ns + "$"
}
return strings.Join(namespaces, "|")
}