/
filtering.go
486 lines (435 loc) · 13.7 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
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
// Copyright 2014 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package client
import (
"fmt"
"net"
"path"
"regexp"
"strconv"
"strings"
"github.com/juju/errors"
"github.com/juju/names/v5"
"github.com/juju/juju/core/network"
"github.com/juju/juju/core/status"
"github.com/juju/juju/state"
)
var InvalidFormatErr = errors.Errorf("the given filter did not match any known patterns.")
// UnitChainPredicateFn builds a function which runs the given
// predicate over a unit and all of its subordinates. If one unit in
// the chain matches, the entire chain matches.
func UnitChainPredicateFn(
predicate Predicate,
getUnit func(string) *state.Unit,
) func(*state.Unit) (bool, error) {
considered := make(map[string]bool)
var f func(unit *state.Unit) (bool, error)
f = func(unit *state.Unit) (bool, error) {
// Don't try and filter the same unit 2x.
if matches, ok := considered[unit.Name()]; ok {
logger.Debugf("%s has already been examined and found to be: %t", unit.Name(), matches)
return matches, nil
}
// Check the current unit.
matches, err := predicate(unit)
if err != nil {
return false, errors.Annotate(err, "could not filter units")
}
considered[unit.Name()] = matches
// Now check all of this unit's subordinates.
for _, subName := range unit.SubordinateNames() {
// A master match supercedes any subordinate match.
if matches {
logger.Debugf("%s is a subordinate to a match.", subName)
considered[subName] = true
continue
}
subUnit := getUnit(subName)
if subUnit == nil {
// We have already deleted this unit
matches = false
continue
}
matches, err = f(subUnit)
if err != nil {
return false, err
}
considered[subName] = matches
}
return matches, nil
}
return f
}
// BuildPredicate returns a Predicate which will evaluate a machine,
// service, or unit against the given patterns.
func BuildPredicateFor(patterns []string) Predicate {
or := func(predicates ...closurePredicate) (bool, error) {
// Differentiate between a valid format that eliminated all
// elements, and an invalid query.
oneValidFmt := false
for _, p := range predicates {
if matches, ok, err := p(); err != nil {
return false, err
} else if ok {
oneValidFmt = true
if matches {
return true, nil
}
}
}
if !oneValidFmt && len(predicates) > 0 {
return false, InvalidFormatErr
}
return false, nil
}
return func(i interface{}) (bool, error) {
switch i.(type) {
default:
panic(errors.Errorf("expected a machine or an applications or a unit, got %T", i))
case *state.Machine:
shims, err := buildMachineMatcherShims(i.(*state.Machine), patterns)
if err != nil {
return false, err
}
return or(shims...)
case *state.Unit:
return or(buildUnitMatcherShims(i.(*state.Unit), patterns)...)
case *state.Application:
shims, err := buildApplicationMatcherShims(i.(*state.Application), patterns...)
if err != nil {
return false, err
}
return or(shims...)
}
}
}
// Predicate is a function that when given a unit, machine, or
// service, will determine whether the unit meets some criteria.
type Predicate func(interface{}) (matches bool, _ error)
// closurePredicate is a function which has at some point been closed
// around an element so that it can examine whether this element
// matches some criteria.
type closurePredicate func() (matches bool, formatOK bool, _ error)
func matchMachineId(m *state.Machine, patterns []string) (bool, bool, error) {
var anyValid bool
for _, p := range patterns {
if !names.IsValidMachine(p) {
continue
}
anyValid = true
if m.Id() == p || strings.HasPrefix(m.Id(), p+"/") {
// Pattern matches the machine, or container's
// host machine.
return true, true, nil
}
}
return false, anyValid, nil
}
func unitMatchUnitName(u *state.Unit, patterns []string) (bool, bool, error) {
um, err := NewUnitMatcher(patterns)
if err != nil {
// Currently, the only error possible here is a matching
// error. We don't want this error to hold up further
// matching.
logger.Debugf("ignoring matching error: %v", err)
return false, false, nil
}
return um.matchUnit(u), true, nil
}
func unitMatchAgentStatus(u *state.Unit, patterns []string) (bool, bool, error) {
statusInfo, err := u.AgentStatus()
if err != nil {
return false, false, err
}
return matchAgentStatus(patterns, statusInfo.Status)
}
func unitMatchWorkloadStatus(u *state.Unit, patterns []string) (bool, bool, error) {
workloadStatusInfo, err := u.Status()
if err != nil {
return false, false, err
}
agentStatusInfo, err := u.AgentStatus()
if err != nil {
return false, false, err
}
return matchWorkloadStatus(patterns, workloadStatusInfo.Status, agentStatusInfo.Status)
}
func unitMatchExposure(u *state.Unit, patterns []string) (bool, bool, error) {
s, err := u.Application()
if err != nil {
return false, false, err
}
return matchExposure(patterns, s)
}
func unitMatchPort(u *state.Unit, patterns []string) (bool, bool, error) {
unitPortRanges, err := u.OpenedPortRanges()
if err != nil {
if errors.IsNotAssigned(err) || errors.Is(err, errors.NotSupported) {
return false, false, nil
}
return false, false, err
}
return matchPortRanges(patterns, unitPortRanges.UniquePortRanges()...)
}
// buildApplicationMatcherShims adds matchers for application name, application units and
// whether the application is exposed.
func buildApplicationMatcherShims(a *state.Application, patterns ...string) (shims []closurePredicate, _ error) {
// Match on name.
shims = append(shims, func() (bool, bool, error) {
for _, p := range patterns {
if strings.EqualFold(a.Name(), p) {
return true, true, nil
}
}
return false, true, nil
})
// Match on exposure.
shims = append(shims, func() (bool, bool, error) { return matchExposure(patterns, a) })
// If the service has an unit instance that matches any of the
// given criteria, consider the service a match as well.
unitShims, err := buildShimsForUnit(a.AllUnits, patterns...)
if err != nil {
return nil, err
}
shims = append(shims, unitShims...)
// Units may be able to match the pattern. Ultimately defer to
// that logic, and guard against breaking the predicate-chain.
if len(unitShims) <= 0 {
shims = append(shims, func() (bool, bool, error) { return false, true, nil })
}
return shims, nil
}
func buildShimsForUnit(unitsFn func() ([]*state.Unit, error), patterns ...string) (shims []closurePredicate, _ error) {
units, err := unitsFn()
if err != nil {
return nil, err
}
for _, u := range units {
shims = append(shims, buildUnitMatcherShims(u, patterns)...)
}
return shims, nil
}
func buildMachineMatcherShims(m *state.Machine, patterns []string) (shims []closurePredicate, _ error) {
// Look at machine ID.
shims = append(shims, func() (bool, bool, error) { return matchMachineId(m, patterns) })
// Look at machine status.
statusInfo, err := m.Status()
if err != nil {
return nil, err
}
shims = append(shims, func() (bool, bool, error) { return matchAgentStatus(patterns, statusInfo.Status) })
// Look at machine addresses. WARNING: Avoid the temptation to
// bring the append into the loop. The value we would close over
// will continue to change after the closure is created, and we'd
// only examine the last element of the loop for all closures.
var addrs []string
for _, a := range m.Addresses() {
addrs = append(addrs, a.Value)
}
shims = append(shims, func() (bool, bool, error) { return matchSubnet(patterns, addrs...) })
// Units may be able to match the pattern. Ultimately defer to
// that logic, and guard against breaking the predicate-chain.
shims = append(shims, func() (bool, bool, error) { return false, true, nil })
return
}
func buildUnitMatcherShims(u *state.Unit, patterns []string) []closurePredicate {
closeOver := func(f func(*state.Unit, []string) (bool, bool, error)) closurePredicate {
return func() (bool, bool, error) { return f(u, patterns) }
}
return []closurePredicate{
closeOver(unitMatchUnitName),
closeOver(unitMatchAgentStatus),
closeOver(unitMatchWorkloadStatus),
closeOver(unitMatchExposure),
closeOver(unitMatchPort),
}
}
// portsFromString gets "from port" and "to port" value from port string.
func getPortsFromString(portStr string) (int, int, error) {
var portFrom, portTo int
var err error
portFromStr, portToStr, isPortRange := strings.Cut(portStr, "-")
if isPortRange {
portFrom, err = strconv.Atoi(portFromStr)
if err != nil {
return -1, -1, err
}
portTo, err = strconv.Atoi(portToStr)
if err != nil {
return -1, -1, err
}
} else {
portFrom, err = strconv.Atoi(portStr)
if err != nil {
return -1, -1, err
}
portTo = portFrom
}
return portFrom, portTo, nil
}
func matchPortRanges(patterns []string, portRanges ...network.PortRange) (bool, bool, error) {
for _, p := range portRanges {
pNumStr, pProto, _ := strings.Cut(p.String(), "/")
pFrom, pTo, err := getPortsFromString(pNumStr)
if err != nil {
return false, true, nil
}
for _, patt := range patterns {
pattNumStr, pattProto, isPortPattern := strings.Cut(patt, "/")
pattFrom, pattTo, err := getPortsFromString(pattNumStr)
if err != nil {
return false, true, nil
}
isPortInRange := pattFrom <= pTo && pattTo >= pFrom
if isPortPattern && isPortInRange && pProto == pattProto {
return true, true, nil
}
}
}
return false, true, nil
}
func matchSubnet(patterns []string, addresses ...string) (bool, bool, error) {
oneValidPattern := false
for _, p := range patterns {
for _, a := range addresses {
if p == a {
return true, true, nil
}
}
if _, ipNet, err := net.ParseCIDR(p); err == nil {
oneValidPattern = true
for _, a := range addresses {
if ip := net.ParseIP(a); ip != nil {
if ipNet.Contains(ip) {
return true, true, nil
}
}
}
}
}
return false, oneValidPattern, nil
}
func matchExposure(patterns []string, s *state.Application) (bool, bool, error) {
if len(patterns) >= 1 && patterns[0] == "exposed" {
return s.IsExposed(), true, nil
} else if len(patterns) >= 2 && patterns[0] == "not" && patterns[1] == "exposed" {
return !s.IsExposed(), true, nil
}
return false, false, nil
}
func matchWorkloadStatus(patterns []string, workloadStatus status.Status, agentStatus status.Status) (bool, bool, error) {
oneValidStatus := false
for _, p := range patterns {
// If the pattern isn't a known status, ignore it.
ps := status.Status(p)
if !ps.KnownWorkloadStatus() {
continue
}
oneValidStatus = true
// To preserve current expected behaviour, we only report on workload status
// if the agent itself is not in error.
if agentStatus != status.Error && workloadStatus.WorkloadMatches(ps) {
return true, true, nil
}
}
return false, oneValidStatus, nil
}
func matchAgentStatus(patterns []string, agentStatus status.Status) (bool, bool, error) {
oneValidStatus := false
for _, p := range patterns {
// If the pattern isn't a known status, ignore it.
ps := status.Status(p)
if !ps.KnownAgentStatus() {
continue
}
oneValidStatus = true
if agentStatus.Matches(ps) {
return true, true, nil
}
}
return false, oneValidStatus, nil
}
type unitMatcher struct {
patterns []string
}
// matchesAny returns true if the unitMatcher will
// match any unit, regardless of its attributes.
func (m unitMatcher) matchesAny() bool {
return len(m.patterns) == 0
}
// matchUnit attempts to match a state.Unit to one of
// a set of patterns, taking into account subordinate
// relationships.
func (m unitMatcher) matchUnit(u *state.Unit) bool {
if m.matchesAny() {
return true
}
// Keep the unit if:
// (a) its name matches a pattern, or
// (b) it's a principal and one of its subordinates matches, or
// (c) it's a subordinate and its principal matches.
//
// Note: do *not* include a second subordinate if the principal is
// only matched on account of a first subordinate matching.
if m.matchString(u.Name()) {
return true
}
if u.IsPrincipal() {
for _, s := range u.SubordinateNames() {
if m.matchString(s) {
return true
}
}
return false
}
principal, valid := u.PrincipalName()
if !valid {
panic("PrincipalName failed for subordinate unit")
}
return m.matchString(principal)
}
// matchString matches a string to one of the patterns in
// the unit matcher, returning an error if a pattern with
// invalid syntax is encountered.
func (m unitMatcher) matchString(s string) bool {
for _, pattern := range m.patterns {
ok, err := path.Match(pattern, s)
if err != nil {
// We validate patterns, so should never get here.
panic(fmt.Errorf("pattern syntax error in %q", pattern))
} else if ok {
return true
}
}
return false
}
// validPattern must match the parts of a unit or application name
// pattern either side of the '/' for it to be valid.
var validPattern = regexp.MustCompile("^[a-z0-9-*]+$")
// NewUnitMatcher returns a unitMatcher that matches units
// with one of the specified patterns, or all units if no
// patterns are specified.
//
// An error will be returned if any of the specified patterns
// is invalid. Patterns are valid if they contain only
// alpha-numeric characters, hyphens, or asterisks (and one
// optional '/' to separate service/unit).
func NewUnitMatcher(patterns []string) (unitMatcher, error) {
pattCopy := make([]string, len(patterns))
for i, pattern := range patterns {
pattCopy[i] = patterns[i]
fields := strings.Split(pattern, "/")
if len(fields) > 2 {
return unitMatcher{}, fmt.Errorf("pattern %q contains too many '/' characters", pattern)
}
for _, f := range fields {
if !validPattern.MatchString(f) {
return unitMatcher{}, fmt.Errorf("pattern %q contains invalid characters", pattern)
}
}
if len(fields) == 1 {
pattCopy[i] += "/*"
}
}
return unitMatcher{pattCopy}, nil
}