-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
Copy pathauthorizer.go
49 lines (41 loc) · 1.49 KB
/
authorizer.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
package authorizer
import (
"errors"
"k8s.io/apiserver/pkg/authorization/authorizer"
)
type openshiftAuthorizer struct {
delegate authorizer.Authorizer
forbiddenMessageMaker ForbiddenMessageMaker
}
func NewAuthorizer(delegate authorizer.Authorizer, forbiddenMessageMaker ForbiddenMessageMaker) authorizer.Authorizer {
return &openshiftAuthorizer{delegate: delegate, forbiddenMessageMaker: forbiddenMessageMaker}
}
func (a *openshiftAuthorizer) Authorize(attributes authorizer.Attributes) (authorizer.Decision, string, error) {
if attributes.GetUser() == nil {
return authorizer.DecisionNoOpinion, "", errors.New("no user available on context")
}
authorizationDecision, delegateReason, err := a.delegate.Authorize(attributes)
if authorizationDecision == authorizer.DecisionAllow {
return authorizer.DecisionAllow, reason(attributes), nil
}
// errors are allowed to occur
if err != nil {
return authorizationDecision, "", err
}
denyReason, err := a.forbiddenMessageMaker.MakeMessage(attributes)
if err != nil {
denyReason = err.Error()
}
if len(delegateReason) > 0 {
denyReason += ": " + delegateReason
}
return authorizationDecision, denyReason, nil
}
func reason(attributes authorizer.Attributes) string {
if len(attributes.GetNamespace()) == 0 {
return "allowed by cluster rule"
}
// not 100% accurate, because the rule may have been provided by a cluster rule. we no longer have
// this distinction upstream in practice.
return "allowed by openshift authorizer"
}