-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccess_decision_manager.go
More file actions
240 lines (197 loc) · 6.02 KB
/
access_decision_manager.go
File metadata and controls
240 lines (197 loc) · 6.02 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
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
// Copyright 2026 Hyperscale. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package security
import (
"context"
"strings"
"go.opentelemetry.io/otel/codes"
)
// AccessDecisionManager combines the verdicts of multiple [Voter]s into a
// single decision. Three strategies are provided, mirroring Spring Security:
//
// - Affirmative — a single [DecisionGrant] grants access; everything else
// denies. Abstentions are ignored. The strictest "fail closed
// by default" policy.
// - Consensus — the majority wins. Ties default to deny; pass
// [WithTieBreak](DecisionGrant) to flip the policy.
// - Unanimous — every voter that does not abstain MUST grant. A single
// deny refuses; if every voter abstains, the result depends on
// [WithAbstainFallback].
//
// Implementations are safe for concurrent use.
type AccessDecisionManager interface {
// Decide returns nil on grant, [ErrAccessDenied] on deny.
// Wrapping callers add a short message indicating the strategy used.
Decide(ctx context.Context, auth Authentication, attrs []Attribute) error
}
// admOption configures NewAffirmative/NewConsensus/NewUnanimous.
type admOption func(*admConfig)
type admConfig struct {
tieBreak Decision // for consensus
abstainFallback Decision // for unanimous
}
// WithTieBreak controls the consensus strategy when grant and deny votes
// are equal in number. Default: DecisionDeny.
func WithTieBreak(d Decision) admOption { //nolint:revive // exported via constructors
return func(c *admConfig) { c.tieBreak = d }
}
// WithAbstainFallback controls the verdict when every unanimous voter
// abstains. Default: DecisionDeny.
func WithAbstainFallback(d Decision) admOption { //nolint:revive // exported via constructors
return func(c *admConfig) { c.abstainFallback = d }
}
// NewAffirmativeDecisionManager returns an [AccessDecisionManager] that
// grants access as soon as one voter does, and denies otherwise.
func NewAffirmativeDecisionManager(voters ...Voter) AccessDecisionManager {
return &accessDecisionManager{
strategy: "affirmative",
voters: cloneVoters(voters),
decide: affirmative,
}
}
// NewConsensusDecisionManager returns an [AccessDecisionManager] that
// follows majority rule. Pass [WithTieBreak] to override the default
// (deny-on-tie) behavior.
func NewConsensusDecisionManager(voters []Voter, opts ...admOption) AccessDecisionManager {
cfg := admConfig{tieBreak: DecisionDeny}
for _, o := range opts {
o(&cfg)
}
return &accessDecisionManager{
strategy: "consensus",
voters: cloneVoters(voters),
decide: consensus(cfg.tieBreak),
}
}
// NewUnanimousDecisionManager returns an [AccessDecisionManager] that
// refuses on a single deny and otherwise grants when at least one voter
// grants. Pass [WithAbstainFallback] to control the all-abstain case.
func NewUnanimousDecisionManager(voters []Voter, opts ...admOption) AccessDecisionManager {
cfg := admConfig{abstainFallback: DecisionDeny}
for _, o := range opts {
o(&cfg)
}
return &accessDecisionManager{
strategy: "unanimous",
voters: cloneVoters(voters),
decide: unanimous(cfg.abstainFallback),
}
}
type accessDecisionManager struct {
strategy string
voters []Voter
decide func(votes []Decision) Decision
}
// Decide implements [AccessDecisionManager].
func (m *accessDecisionManager) Decide(ctx context.Context, auth Authentication, attrs []Attribute) error {
ctx, span := tracer().Start(ctx, "security.AccessDecisionManager.Decide")
defer span.End()
span.SetAttributes(
AttrStrategy.String(m.strategy),
AttrAttributes.String(joinAttributes(attrs)),
)
votes := make([]Decision, 0, len(m.voters))
for _, v := range m.voters {
if !anySupported(v, attrs) {
votes = append(votes, DecisionAbstain)
continue
}
votes = append(votes, v.Vote(ctx, auth, attrs))
}
final := m.decide(votes)
span.SetAttributes(AttrDecision.String(final.String()))
if final == DecisionGrant {
return nil
}
span.SetStatus(codes.Error, ErrAccessDenied.Error())
return ErrAccessDenied
}
// affirmative returns Grant on first grant, Deny otherwise. Abstentions are
// ignored.
func affirmative(votes []Decision) Decision {
denySeen := false
for _, v := range votes {
switch v {
case DecisionGrant:
return DecisionGrant
case DecisionDeny:
denySeen = true
case DecisionAbstain:
// ignore
}
}
if denySeen {
return DecisionDeny
}
return DecisionDeny // all abstain -> deny by default
}
// consensus returns Grant if grants > denies, Deny if denies > grants, and
// the configured tie-break otherwise. All abstentions -> deny by default.
func consensus(tieBreak Decision) func([]Decision) Decision {
return func(votes []Decision) Decision {
grants, denies := 0, 0
for _, v := range votes {
switch v {
case DecisionGrant:
grants++
case DecisionDeny:
denies++
case DecisionAbstain:
// ignore
}
}
switch {
case grants == 0 && denies == 0:
return DecisionDeny
case grants > denies:
return DecisionGrant
case denies > grants:
return DecisionDeny
default:
return tieBreak
}
}
}
// unanimous returns Deny on any deny; otherwise Grant if at least one voter
// granted; otherwise the configured all-abstain fallback.
func unanimous(allAbstainFallback Decision) func([]Decision) Decision {
return func(votes []Decision) Decision {
grantSeen := false
for _, v := range votes {
if v == DecisionDeny {
return DecisionDeny
}
if v == DecisionGrant {
grantSeen = true
}
}
if grantSeen {
return DecisionGrant
}
return allAbstainFallback
}
}
func anySupported(v Voter, attrs []Attribute) bool {
for _, a := range attrs {
if v.Supports(a) {
return true
}
}
return false
}
func joinAttributes(attrs []Attribute) string {
if len(attrs) == 0 {
return ""
}
parts := make([]string, len(attrs))
for i, a := range attrs {
parts[i] = a.String()
}
return strings.Join(parts, ",")
}
func cloneVoters(in []Voter) []Voter {
cp := make([]Voter, len(in))
copy(cp, in)
return cp
}