| 1 |
/* |
| 2 |
Copyright 2016 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package headerrequest |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
"net/http" |
| 22 |
"net/url" |
| 23 |
"strings" |
| 24 |
|
| 25 |
"k8s.io/apiserver/pkg/authentication/authenticator" |
| 26 |
x509request "k8s.io/apiserver/pkg/authentication/request/x509" |
| 27 |
"k8s.io/apiserver/pkg/authentication/user" |
| 28 |
) |
| 29 |
|
| 30 |
// StringSliceProvider is a way to get a string slice value. It is heavily used for authentication headers among other places. |
| 31 |
type StringSliceProvider interface { |
| 32 |
// Value returns the current string slice. Callers should never mutate the returned value. |
| 33 |
Value() []string |
| 34 |
} |
| 35 |
|
| 36 |
// StringSliceProviderFunc is a function that matches the StringSliceProvider interface |
| 37 |
type StringSliceProviderFunc func() []string |
| 38 |
|
| 39 |
// Value returns the current string slice. Callers should never mutate the returned value. |
| 40 |
func (d StringSliceProviderFunc) Value() []string { |
| 41 |
return d() |
| 42 |
} |
| 43 |
|
| 44 |
// StaticStringSlice a StringSliceProvider that returns a fixed value |
| 45 |
type StaticStringSlice []string |
| 46 |
|
| 47 |
// Value returns the current string slice. Callers should never mutate the returned value. |
| 48 |
func (s StaticStringSlice) Value() []string { |
| 49 |
return s |
| 50 |
} |
| 51 |
|
| 52 |
type requestHeaderAuthRequestHandler struct { |
| 53 |
// nameHeaders are the headers to check (in order, case-insensitively) for an identity. The first header with a value wins. |
| 54 |
nameHeaders StringSliceProvider |
| 55 |
|
| 56 |
// nameHeaders are the headers to check (in order, case-insensitively) for an identity UID. The first header with a value wins. |
| 57 |
uidHeaders StringSliceProvider |
| 58 |
|
| 59 |
// groupHeaders are the headers to check (case-insensitively) for group membership. All values of all headers will be added. |
| 60 |
groupHeaders StringSliceProvider |
| 61 |
|
| 62 |
// extraHeaderPrefixes are the head prefixes to check (case-insensitively) for filling in |
| 63 |
// the user.Info.Extra. All values of all matching headers will be added. |
| 64 |
extraHeaderPrefixes StringSliceProvider |
| 65 |
} |
| 66 |
|
| 67 |
func New(nameHeaders, uidHeaders, groupHeaders, extraHeaderPrefixes []string) (authenticator.Request, error) { |
| 68 |
trimmedNameHeaders, err := trimHeaders(nameHeaders...) |
| 69 |
if err != nil { |
| 70 |
return nil, err |
| 71 |
} |
| 72 |
trimmedUIDHeaders, err := trimHeaders(uidHeaders...) |
| 73 |
if err != nil { |
| 74 |
return nil, err |
| 75 |
} |
| 76 |
trimmedGroupHeaders, err := trimHeaders(groupHeaders...) |
| 77 |
if err != nil { |
| 78 |
return nil, err |
| 79 |
} |
| 80 |
trimmedExtraHeaderPrefixes, err := trimHeaders(extraHeaderPrefixes...) |
| 81 |
if err != nil { |
| 82 |
return nil, err |
| 83 |
} |
| 84 |
|
| 85 |
return NewDynamic( |
| 86 |
StaticStringSlice(trimmedNameHeaders), |
| 87 |
StaticStringSlice(trimmedUIDHeaders), |
| 88 |
StaticStringSlice(trimmedGroupHeaders), |
| 89 |
StaticStringSlice(trimmedExtraHeaderPrefixes), |
| 90 |
), nil |
| 91 |
} |
| 92 |
|
| 93 |
func NewDynamic(nameHeaders, uidHeaders, groupHeaders, extraHeaderPrefixes StringSliceProvider) authenticator.Request { |
| 94 |
return &requestHeaderAuthRequestHandler{ |
| 95 |
nameHeaders: nameHeaders, |
| 96 |
uidHeaders: uidHeaders, |
| 97 |
groupHeaders: groupHeaders, |
| 98 |
extraHeaderPrefixes: extraHeaderPrefixes, |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
func trimHeaders(headerNames ...string) ([]string, error) { |
| 103 |
ret := []string{} |
| 104 |
for _, headerName := range headerNames { |
| 105 |
trimmedHeader := strings.TrimSpace(headerName) |
| 106 |
if len(trimmedHeader) == 0 { |
| 107 |
return nil, fmt.Errorf("empty header %q", headerName) |
| 108 |
} |
| 109 |
ret = append(ret, trimmedHeader) |
| 110 |
} |
| 111 |
|
| 112 |
return ret, nil |
| 113 |
} |
| 114 |
|
| 115 |
func NewDynamicVerifyOptionsSecure(verifyOptionFn x509request.VerifyOptionFunc, proxyClientNames, nameHeaders, uidHeaders, groupHeaders, extraHeaderPrefixes StringSliceProvider) authenticator.Request { |
| 116 |
headerAuthenticator := NewDynamic(nameHeaders, uidHeaders, groupHeaders, extraHeaderPrefixes) |
| 117 |
|
| 118 |
return x509request.NewDynamicCAVerifier(verifyOptionFn, headerAuthenticator, proxyClientNames) |
| 119 |
} |
| 120 |
|
| 121 |
func (a *requestHeaderAuthRequestHandler) AuthenticateRequest(req *http.Request) (*authenticator.Response, bool, error) { |
| 122 |
name := headerValue(req.Header, a.nameHeaders.Value()) |
| 123 |
if len(name) == 0 { |
| 124 |
return nil, false, nil |
| 125 |
} |
| 126 |
uid := headerValue(req.Header, a.uidHeaders.Value()) |
| 127 |
groups := allHeaderValues(req.Header, a.groupHeaders.Value()) |
| 128 |
extra := newExtra(req.Header, a.extraHeaderPrefixes.Value()) |
| 129 |
|
| 130 |
// clear headers used for authentication |
| 131 |
ClearAuthenticationHeaders(req.Header, a.nameHeaders, a.uidHeaders, a.groupHeaders, a.extraHeaderPrefixes) |
| 132 |
|
| 133 |
return &authenticator.Response{ |
| 134 |
User: &user.DefaultInfo{ |
| 135 |
Name: name, |
| 136 |
UID: uid, |
| 137 |
Groups: groups, |
| 138 |
Extra: extra, |
| 139 |
}, |
| 140 |
}, true, nil |
| 141 |
} |
| 142 |
|
| 143 |
func ClearAuthenticationHeaders(h http.Header, nameHeaders, uidHeaders, groupHeaders, extraHeaderPrefixes StringSliceProvider) { |
| 144 |
for _, headerName := range nameHeaders.Value() { |
| 145 |
h.Del(headerName) |
| 146 |
} |
| 147 |
for _, headerName := range uidHeaders.Value() { |
| 148 |
h.Del(headerName) |
| 149 |
} |
| 150 |
for _, headerName := range groupHeaders.Value() { |
| 151 |
h.Del(headerName) |
| 152 |
} |
| 153 |
for _, prefix := range extraHeaderPrefixes.Value() { |
| 154 |
for k := range h { |
| 155 |
if hasPrefixIgnoreCase(k, prefix) { |
| 156 |
delete(h, k) // we have the raw key so avoid relying on canonicalization |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
func hasPrefixIgnoreCase(s, prefix string) bool { |
| 163 |
return len(s) >= len(prefix) && strings.EqualFold(s[:len(prefix)], prefix) |
| 164 |
} |
| 165 |
|
| 166 |
func headerValue(h http.Header, headerNames []string) string { |
| 167 |
for _, headerName := range headerNames { |
| 168 |
headerValue := h.Get(headerName) |
| 169 |
if len(headerValue) > 0 { |
| 170 |
return headerValue |
| 171 |
} |
| 172 |
} |
| 173 |
return "" |
| 174 |
} |
| 175 |
|
| 176 |
func allHeaderValues(h http.Header, headerNames []string) []string { |
| 177 |
ret := []string{} |
| 178 |
for _, headerName := range headerNames { |
| 179 |
headerKey := http.CanonicalHeaderKey(headerName) |
| 180 |
values, ok := h[headerKey] |
| 181 |
if !ok { |
| 182 |
continue |
| 183 |
} |
| 184 |
|
| 185 |
for _, headerValue := range values { |
| 186 |
if len(headerValue) > 0 { |
| 187 |
ret = append(ret, headerValue) |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
return ret |
| 192 |
} |
| 193 |
|
| 194 |
func unescapeExtraKey(encodedKey string) string { |
| 195 |
key, err := url.PathUnescape(encodedKey) // Decode %-encoded bytes. |
| 196 |
if err != nil { |
| 197 |
return encodedKey // Always record extra strings, even if malformed/unencoded. |
| 198 |
} |
| 199 |
return key |
| 200 |
} |
| 201 |
|
| 202 |
func newExtra(h http.Header, headerPrefixes []string) map[string][]string { |
| 203 |
ret := map[string][]string{} |
| 204 |
|
| 205 |
// we have to iterate over prefixes first in order to have proper ordering inside the value slices |
| 206 |
for _, prefix := range headerPrefixes { |
| 207 |
for headerName, vv := range h { |
| 208 |
if !hasPrefixIgnoreCase(headerName, prefix) { |
| 209 |
continue |
| 210 |
} |
| 211 |
|
| 212 |
extraKey := unescapeExtraKey(strings.ToLower(headerName[len(prefix):])) |
| 213 |
ret[extraKey] = append(ret[extraKey], vv...) |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
return ret |
| 218 |
} |