@@ -14,6 +14,7 @@ import (
14
14
15
15
"k8s.io/apiserver/pkg/server/healthz"
16
16
17
+ "k8s.io/apimachinery/pkg/api/errors"
17
18
"k8s.io/apiserver/pkg/authentication/authenticator"
18
19
"k8s.io/apiserver/pkg/authorization/authorizer"
19
20
)
@@ -62,9 +63,16 @@ func (l Listener) authorizeHandler(protected http.Handler) http.Handler {
62
63
}
63
64
64
65
user , ok , err := l .Authenticator .AuthenticateRequest (req )
65
- if err != nil {
66
- glog .V (3 ).Infof ("Unable to authenticate: %v" , err )
67
- http .Error (w , "Unable to authenticate due to an error" , http .StatusInternalServerError )
66
+ if ! ok || err != nil {
67
+ // older routers will not have permission to check token access review, so treat this
68
+ // as an authorization denied if so
69
+ if ! ok || errors .IsUnauthorized (err ) {
70
+ glog .V (5 ).Infof ("Unable to authenticate: %v" , err )
71
+ http .Error (w , "Unable to authenticate due to an error" , http .StatusUnauthorized )
72
+ } else {
73
+ glog .V (3 ).Infof ("Unable to authenticate: %v" , err )
74
+ http .Error (w , "Unable to authenticate due to an error" , http .StatusInternalServerError )
75
+ }
68
76
return
69
77
}
70
78
scopedRecord := l .Record
@@ -90,13 +98,14 @@ func (l Listener) authorizeHandler(protected http.Handler) http.Handler {
90
98
}
91
99
scopedRecord .User = user
92
100
ok , reason , err := l .Authorizer .Authorize (scopedRecord )
93
- if err != nil {
94
- glog .V (3 ).Infof ("Unable to authenticate: %v" , err )
95
- http .Error (w , "Unable to authenticate due to an error" , http .StatusInternalServerError )
96
- return
97
- }
98
- if ! ok {
99
- http .Error (w , fmt .Sprintf ("Unauthorized %s" , reason ), http .StatusUnauthorized )
101
+ if ! ok || err != nil {
102
+ if ! ok || errors .IsUnauthorized (err ) {
103
+ glog .V (5 ).Infof ("Unable to authorize: %v" , err )
104
+ http .Error (w , fmt .Sprintf ("Forbidden: %s" , reason ), http .StatusForbidden )
105
+ } else {
106
+ glog .V (3 ).Infof ("Unable to authorize: %v" , err )
107
+ http .Error (w , "Unable to authorize the user due to an error" , http .StatusInternalServerError )
108
+ }
100
109
return
101
110
}
102
111
protected .ServeHTTP (w , req )
0 commit comments