Debian Codegraph @

pkg/server/options/authentication_test.go

raw
1 /*
2 Copyright 2018 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 options
18
19 import (
20 "io/ioutil"
21 "net/http"
22 "os"
23 "reflect"
24 "testing"
25
26 "k8s.io/apiserver/pkg/authentication/authenticatorfactory"
27 "k8s.io/apiserver/pkg/authentication/request/headerrequest"
28 "k8s.io/apiserver/pkg/server"
29 openapicommon "k8s.io/kube-openapi/pkg/common"
30 )
31
32 func TestToAuthenticationRequestHeaderConfig(t *testing.T) {
33 testCases := []struct {
34 name string
35 testOptions *RequestHeaderAuthenticationOptions
36 expectConfig *authenticatorfactory.RequestHeaderConfig
37 }{
38 {
39 name: "test when ClientCAFile is nil",
40 testOptions: &RequestHeaderAuthenticationOptions{
41 UsernameHeaders: headerrequest.StaticStringSlice{"x-remote-user"},
42 UIDHeaders: headerrequest.StaticStringSlice{"x-remote-uid"},
43 GroupHeaders: headerrequest.StaticStringSlice{"x-remote-group"},
44 ExtraHeaderPrefixes: headerrequest.StaticStringSlice{"x-remote-extra-"},
45 AllowedNames: headerrequest.StaticStringSlice{"kube-aggregator"},
46 },
47 },
48 {
49 name: "test when ClientCAFile is not nil",
50 testOptions: &RequestHeaderAuthenticationOptions{
51 ClientCAFile: "testdata/root.pem",
52 UsernameHeaders: headerrequest.StaticStringSlice{"x-remote-user"},
53 UIDHeaders: headerrequest.StaticStringSlice{"x-remote-uid"},
54 GroupHeaders: headerrequest.StaticStringSlice{"x-remote-group"},
55 ExtraHeaderPrefixes: headerrequest.StaticStringSlice{"x-remote-extra-"},
56 AllowedNames: headerrequest.StaticStringSlice{"kube-aggregator"},
57 },
58 expectConfig: &authenticatorfactory.RequestHeaderConfig{
59 UsernameHeaders: headerrequest.StaticStringSlice{"x-remote-user"},
60 UIDHeaders: headerrequest.StaticStringSlice{"x-remote-uid"},
61 GroupHeaders: headerrequest.StaticStringSlice{"x-remote-group"},
62 ExtraHeaderPrefixes: headerrequest.StaticStringSlice{"x-remote-extra-"},
63 CAContentProvider: nil, // this is nil because you can't compare functions
64 AllowedClientNames: headerrequest.StaticStringSlice{"kube-aggregator"},
65 },
66 },
67 }
68
69 for _, testcase := range testCases {
70 t.Run(testcase.name, func(t *testing.T) {
71 resultConfig, err := testcase.testOptions.ToAuthenticationRequestHeaderConfig()
72 if err != nil {
73 t.Fatal(err)
74 }
75 if resultConfig != nil {
76 if resultConfig.CAContentProvider == nil {
77 t.Error("missing requestheader verify")
78 }
79 resultConfig.CAContentProvider = nil
80 }
81
82 if !reflect.DeepEqual(resultConfig, testcase.expectConfig) {
83 t.Errorf("got RequestHeaderConfig: %#v, expected RequestHeaderConfig: %#v", resultConfig, testcase.expectConfig)
84 }
85 })
86 }
87 }
88
89 func TestApplyToFallback(t *testing.T) {
90
91 f, err := ioutil.TempFile("", "authkubeconfig")
92 if err != nil {
93 t.Fatal(err)
94 }
95 defer os.Remove(f.Name())
96
97 if err := ioutil.WriteFile(f.Name(), []byte(`
98 apiVersion: v1
99 kind: Config
100 clusters:
101 - cluster:
102 server: http://localhost:56789
103 name: cluster
104 contexts:
105 - context:
106 cluster: cluster
107 name: cluster
108 current-context: cluster
109 `), os.FileMode(0755)); err != nil {
110 t.Fatal(err)
111 }
112 remoteKubeconfig := f.Name()
113
114 testcases := []struct {
115 name string
116 options *DelegatingAuthenticationOptions
117 expectError bool
118 expectAuthenticator bool
119 expectTokenAnonymous bool
120 expectTokenErrors bool
121 }{
122 {
123 name: "empty",
124 options: nil,
125 expectError: false,
126 expectAuthenticator: false,
127 },
128 {
129 name: "default",
130 options: NewDelegatingAuthenticationOptions(),
131 expectError: true, // in-cluster client building fails, no kubeconfig provided
132 expectAuthenticator: false,
133 },
134 {
135 name: "optional kubeconfig",
136 options: func() *DelegatingAuthenticationOptions {
137 opts := NewDelegatingAuthenticationOptions()
138 opts.RemoteKubeConfigFileOptional = true
139 return opts
140 }(),
141 expectError: false, // in-cluster client building fails, no kubeconfig required
142 expectAuthenticator: true,
143 expectTokenAnonymous: true, // no token validator available
144 },
145 {
146 name: "valid client, failed cluster info lookup",
147 options: func() *DelegatingAuthenticationOptions {
148 opts := NewDelegatingAuthenticationOptions()
149 opts.RemoteKubeConfigFile = remoteKubeconfig
150 return opts
151 }(),
152 expectError: true, // client building is valid, remote config lookup fails
153 expectAuthenticator: false,
154 },
155 {
156 name: "valid client, skip cluster info lookup",
157 options: func() *DelegatingAuthenticationOptions {
158 opts := NewDelegatingAuthenticationOptions()
159 opts.RemoteKubeConfigFile = remoteKubeconfig
160 opts.SkipInClusterLookup = true
161 return opts
162 }(),
163 expectError: false, // client building is valid, skipped cluster lookup
164 expectAuthenticator: true,
165 expectTokenErrors: true, // client fails making tokenreview calls
166 },
167 {
168 name: "valid client, tolerate failed cluster info lookup",
169 options: func() *DelegatingAuthenticationOptions {
170 opts := NewDelegatingAuthenticationOptions()
171 opts.RemoteKubeConfigFile = remoteKubeconfig
172 opts.TolerateInClusterLookupFailure = true
173 return opts
174 }(),
175 expectError: false, // client is valid, skipped cluster lookup
176 expectAuthenticator: true, // anonymous auth
177 expectTokenErrors: true, // client fails making tokenreview calls
178 },
179 }
180
181 for _, tc := range testcases {
182 t.Run(tc.name, func(t *testing.T) {
183 c := &server.AuthenticationInfo{}
184 servingInfo := &server.SecureServingInfo{}
185 openAPIConfig := &openapicommon.Config{}
186
187 err := tc.options.ApplyTo(c, servingInfo, openAPIConfig)
188 if (err != nil) != tc.expectError {
189 t.Errorf("expected error=%v, got %v", tc.expectError, err)
190 }
191 if (c.Authenticator != nil) != tc.expectAuthenticator {
192 t.Errorf("expected authenticator=%v, got %#v", tc.expectError, c.Authenticator)
193 }
194 if c.Authenticator != nil {
195 {
196 result, ok, err := c.Authenticator.AuthenticateRequest(&http.Request{})
197 if err != nil || !ok || result == nil || result.User.GetName() != "system:anonymous" {
198 t.Errorf("expected anonymous, got %#v, %#v, %#v", result, ok, err)
199 }
200 }
201 {
202 result, ok, err := c.Authenticator.AuthenticateRequest(&http.Request{Header: http.Header{"Authorization": []string{"Bearer foo"}}})
203 if tc.expectTokenAnonymous {
204 if err != nil || !ok || result == nil || result.User.GetName() != "system:anonymous" {
205 t.Errorf("expected anonymous, got %#v, %#v, %#v", result, ok, err)
206 }
207 }
208 if tc.expectTokenErrors != (err != nil) {
209 t.Errorf("expected error=%v, got %#v, %#v, %#v", tc.expectTokenErrors, result, ok, err)
210 }
211 }
212 }
213 })
214 }
215 }