forked from brancz/kube-rbac-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.go
254 lines (218 loc) · 8.38 KB
/
auth.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
/*
Copyright 2017 Frederic Branczyk Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"errors"
"fmt"
"net/http"
"reflect"
"strings"
"time"
"github.com/golang/glog"
"k8s.io/apiserver/pkg/authentication/authenticator"
"k8s.io/apiserver/pkg/authentication/authenticatorfactory"
"k8s.io/apiserver/pkg/authentication/user"
"k8s.io/apiserver/pkg/authorization/authorizer"
"k8s.io/apiserver/pkg/authorization/authorizerfactory"
clientset "k8s.io/client-go/kubernetes"
authenticationclient "k8s.io/client-go/kubernetes/typed/authentication/v1beta1"
authorizationclient "k8s.io/client-go/kubernetes/typed/authorization/v1beta1"
)
type X509Config struct {
ClientCAFile string
}
type AuthnConfig struct {
X509 *X509Config
Header *AuthnHeaderConfig
}
type AuthzConfig struct {
ResourceAttributes *ResourceAttributes
}
type AuthnHeaderConfig struct {
// When set to true, kube-rbac-proxy adds auth-related fields to the headers of http requests sent to the upstream
Enabled bool
// Corresponds to the name of the field inside a http(2) request header
// to tell the upstream server about the user's name
UserFieldName string
// Corresponds to the name of the field inside a http(2) request header
// to tell the upstream server about the user's groups
GroupsFieldName string
// The separator string used for concatenating multiple group names in a groups header field's value
GroupSeparator string
}
type ResourceAttributes struct {
Namespace string `json:"namespace,omitempty"`
APIGroup string `json:"apiGroup,omitempty"`
APIVersion string `json:"apiVersion,omitempty"`
Resource string `json:"resource,omitempty"`
Subresource string `json:"subresource,omitempty"`
Name string `json:"name,omitempty"`
}
type AuthConfig struct {
Authentication *AuthnConfig
Authorization *AuthzConfig
}
// kubeRBACProxyAuth implements AuthInterface
type kubeRBACProxyAuth struct {
// authenticator identifies the user for requests to kube-rbac-proxy
authenticator.Request
// authorizerAttributeGetter builds authorization.Attributes for a request to kube-rbac-proxy
authorizer.RequestAttributesGetter
// authorizer determines whether a given authorization.Attributes is allowed
authorizer.Authorizer
// config for kube-rbac-proxy
Config AuthConfig
}
func newKubeRBACProxyAuth(authenticator authenticator.Request, authorizer authorizer.Authorizer, authConfig AuthConfig) AuthInterface {
return &kubeRBACProxyAuth{authenticator, newKubeRBACProxyAuthorizerAttributesGetter(authConfig.Authorization), authorizer, authConfig}
}
// BuildAuthHandler creates an authenticator, an authorizer, and a matching authorizer attributes getter compatible with the kube-rbac-proxy
func BuildAuthHandler(client clientset.Interface, config AuthConfig) (AuthInterface, error) {
// Get clients, if provided
var (
tokenClient authenticationclient.TokenReviewInterface
sarClient authorizationclient.SubjectAccessReviewInterface
)
if client != nil && !reflect.ValueOf(client).IsNil() {
tokenClient = client.AuthenticationV1beta1().TokenReviews()
sarClient = client.AuthorizationV1beta1().SubjectAccessReviews()
}
authenticator, err := buildAuthn(tokenClient, config.Authentication)
if err != nil {
return nil, err
}
authorizer, err := buildAuthz(sarClient)
if err != nil {
return nil, err
}
return newKubeRBACProxyAuth(authenticator, authorizer, config), nil
}
// buildAuthn creates an authenticator compatible with the kubelet's needs
func buildAuthn(client authenticationclient.TokenReviewInterface, authn *AuthnConfig) (authenticator.Request, error) {
authenticatorConfig := authenticatorfactory.DelegatingAuthenticatorConfig{
Anonymous: false, // always require authentication
CacheTTL: 2 * time.Minute,
ClientCAFile: authn.X509.ClientCAFile,
}
if client == nil {
return nil, errors.New("no client provided, cannot use webhook authentication")
}
authenticatorConfig.TokenAccessReviewClient = client
authenticator, _, err := authenticatorConfig.New()
return authenticator, err
}
// buildAuthz creates an authorizer compatible with the kubelet's needs
func buildAuthz(client authorizationclient.SubjectAccessReviewInterface) (authorizer.Authorizer, error) {
if client == nil {
return nil, errors.New("no client provided, cannot use webhook authorization")
}
authorizerConfig := authorizerfactory.DelegatingAuthorizerConfig{
SubjectAccessReviewClient: client,
AllowCacheTTL: 5 * time.Minute,
DenyCacheTTL: 30 * time.Second,
}
return authorizerConfig.New()
}
func newKubeRBACProxyAuthorizerAttributesGetter(authzConfig *AuthzConfig) authorizer.RequestAttributesGetter {
return krpAuthorizerAttributesGetter{authzConfig}
}
type krpAuthorizerAttributesGetter struct {
authzConfig *AuthzConfig
}
// GetRequestAttributes populates authorizer attributes for the requests to kube-rbac-proxy.
func (n krpAuthorizerAttributesGetter) GetRequestAttributes(u user.Info, r *http.Request) authorizer.Attributes {
apiVerb := ""
switch r.Method {
case "POST":
apiVerb = "create"
case "GET":
apiVerb = "get"
case "PUT":
apiVerb = "update"
case "PATCH":
apiVerb = "patch"
case "DELETE":
apiVerb = "delete"
}
requestPath := r.URL.Path
// Default attributes mirror the API attributes that would allow this access to kube-rbac-proxy
attrs := authorizer.AttributesRecord{
User: u,
Verb: apiVerb,
Namespace: "",
APIGroup: "",
APIVersion: "",
Resource: "",
Subresource: "",
Name: "",
ResourceRequest: false,
Path: requestPath,
}
if n.authzConfig.ResourceAttributes != nil {
attrs = authorizer.AttributesRecord{
User: u,
Verb: apiVerb,
Namespace: n.authzConfig.ResourceAttributes.Namespace,
APIGroup: n.authzConfig.ResourceAttributes.APIGroup,
APIVersion: n.authzConfig.ResourceAttributes.APIVersion,
Resource: n.authzConfig.ResourceAttributes.Resource,
Subresource: n.authzConfig.ResourceAttributes.Subresource,
Name: n.authzConfig.ResourceAttributes.Name,
ResourceRequest: true,
}
}
glog.V(5).Infof("kube-rbac-proxy request attributes: attrs=%#v", attrs)
return attrs
}
type AuthHandler interface {
Handle(w http.ResponseWriter, req *http.Request) bool
}
// Handle authenticates the client and authorizes the request.
// If the authn fails, a 401 error is returned. If the authz fails, a 403 error is returned
func (h *kubeRBACProxyAuth) Handle(w http.ResponseWriter, req *http.Request) bool {
// Authenticate
u, ok, err := h.AuthenticateRequest(req)
if err != nil {
glog.Errorf("Unable to authenticate the request due to an error: %v", err)
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return false
}
if !ok {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return false
}
// Get authorization attributes
attrs := h.GetRequestAttributes(u, req)
// Authorize
authorized, _, err := h.Authorize(attrs)
if err != nil {
msg := fmt.Sprintf("Authorization error (user=%s, verb=%s, resource=%s, subresource=%s)", u.GetName(), attrs.GetVerb(), attrs.GetResource(), attrs.GetSubresource())
glog.Errorf(msg, err)
http.Error(w, msg, http.StatusInternalServerError)
return false
}
if authorized != authorizer.DecisionAllow {
msg := fmt.Sprintf("Forbidden (user=%s, verb=%s, resource=%s, subresource=%s)", u.GetName(), attrs.GetVerb(), attrs.GetResource(), attrs.GetSubresource())
glog.V(2).Info(msg)
http.Error(w, msg, http.StatusForbidden)
return false
}
if h.Config.Authentication.Header.Enabled {
// Seemingly well-known headers to tell the upstream about user's identity
// so that the upstream can achieve the original goal of delegating RBAC authn/authz to kube-rbac-proxy
headerCfg := h.Config.Authentication.Header
req.Header.Set(headerCfg.UserFieldName, u.GetName())
req.Header.Set(headerCfg.GroupsFieldName, strings.Join(u.GetGroups(), headerCfg.GroupSeparator))
}
return true
}