-
Notifications
You must be signed in to change notification settings - Fork 182
Expand file tree
/
Copy pathconflict.go
More file actions
92 lines (79 loc) · 2.06 KB
/
conflict.go
File metadata and controls
92 lines (79 loc) · 2.06 KB
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
package agentmesh
import "strings"
// ConflictResolutionStrategy defines how conflicting policy decisions are resolved.
type ConflictResolutionStrategy string
const (
DenyOverrides ConflictResolutionStrategy = "deny_overrides"
AllowOverrides ConflictResolutionStrategy = "allow_overrides"
PriorityFirstMatch ConflictResolutionStrategy = "priority_first_match"
MostSpecificWins ConflictResolutionStrategy = "most_specific_wins"
)
// CandidateDecision pairs a matched rule with its resulting decision.
type CandidateDecision struct {
Rule PolicyRule
Decision PolicyDecision
}
// PolicyConflictResolver resolves conflicts between multiple matching rules.
type PolicyConflictResolver struct {
Strategy ConflictResolutionStrategy
}
// Resolve returns a single PolicyDecision from a set of candidates using the configured strategy.
func (r *PolicyConflictResolver) Resolve(candidates []CandidateDecision) PolicyDecision {
if len(candidates) == 0 {
return Deny
}
switch r.Strategy {
case DenyOverrides:
for _, c := range candidates {
if c.Decision == Deny {
return Deny
}
}
return candidates[0].Decision
case AllowOverrides:
for _, c := range candidates {
if c.Decision == Allow {
return Allow
}
}
return candidates[0].Decision
case PriorityFirstMatch:
best := candidates[0]
for _, c := range candidates[1:] {
if c.Rule.Priority < best.Rule.Priority {
best = c
}
}
return best.Decision
case MostSpecificWins:
best := candidates[0]
bestSpec := ruleSpecificity(best)
for _, c := range candidates[1:] {
s := ruleSpecificity(c)
if s > bestSpec {
best = c
bestSpec = s
}
}
return best.Decision
default:
return candidates[0].Decision
}
}
func ruleSpecificity(c CandidateDecision) int {
score := len(c.Rule.Conditions)
switch c.Rule.Scope {
case Agent:
score += 3
case Tenant:
score += 2
case Global:
score += 1
}
if c.Rule.Action != "*" && !strings.HasSuffix(c.Rule.Action, ".*") {
score += 2
}
return score
}