authentication_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package options
  14. import (
  15. "reflect"
  16. "strings"
  17. "testing"
  18. "time"
  19. "github.com/google/go-cmp/cmp"
  20. utilerrors "k8s.io/apimachinery/pkg/util/errors"
  21. "k8s.io/apiserver/pkg/authentication/authenticator"
  22. "k8s.io/apiserver/pkg/authentication/authenticatorfactory"
  23. "k8s.io/apiserver/pkg/authentication/request/headerrequest"
  24. apiserveroptions "k8s.io/apiserver/pkg/server/options"
  25. kubeauthenticator "k8s.io/kubernetes/pkg/kubeapiserver/authenticator"
  26. )
  27. func TestAuthenticationValidate(t *testing.T) {
  28. testCases := []struct {
  29. name string
  30. testOIDC *OIDCAuthenticationOptions
  31. testSA *ServiceAccountAuthenticationOptions
  32. expectErr string
  33. }{
  34. {
  35. name: "test when OIDC and ServiceAccounts are nil",
  36. },
  37. {
  38. name: "test when OIDC and ServiceAccounts are valid",
  39. testOIDC: &OIDCAuthenticationOptions{
  40. UsernameClaim: "sub",
  41. SigningAlgs: []string{"RS256"},
  42. IssuerURL: "testIssuerURL",
  43. },
  44. testSA: &ServiceAccountAuthenticationOptions{
  45. Issuer: "http://foo.bar.com",
  46. },
  47. },
  48. {
  49. name: "test when OIDC is invalid",
  50. testOIDC: &OIDCAuthenticationOptions{
  51. UsernameClaim: "sub",
  52. SigningAlgs: []string{"RS256"},
  53. IssuerURL: "testIssuerURL",
  54. },
  55. testSA: &ServiceAccountAuthenticationOptions{
  56. Issuer: "http://foo.bar.com",
  57. },
  58. expectErr: "oidc-issuer-url and oidc-client-id should be specified together",
  59. },
  60. {
  61. name: "test when ServiceAccount is invalid",
  62. testOIDC: &OIDCAuthenticationOptions{
  63. UsernameClaim: "sub",
  64. SigningAlgs: []string{"RS256"},
  65. IssuerURL: "testIssuerURL",
  66. ClientID: "testClientID",
  67. },
  68. testSA: &ServiceAccountAuthenticationOptions{
  69. Issuer: "http://[::1]:namedport",
  70. },
  71. expectErr: "service-account-issuer contained a ':' but was not a valid URL",
  72. },
  73. }
  74. for _, testcase := range testCases {
  75. t.Run(testcase.name, func(t *testing.T) {
  76. options := NewBuiltInAuthenticationOptions()
  77. options.OIDC = testcase.testOIDC
  78. options.ServiceAccounts = testcase.testSA
  79. errs := options.Validate()
  80. if len(errs) > 0 && !strings.Contains(utilerrors.NewAggregate(errs).Error(), testcase.expectErr) {
  81. t.Errorf("Got err: %v, Expected err: %s", errs, testcase.expectErr)
  82. }
  83. if len(errs) == 0 && len(testcase.expectErr) != 0 {
  84. t.Errorf("Got err nil, Expected err: %s", testcase.expectErr)
  85. }
  86. })
  87. }
  88. }
  89. func TestToAuthenticationConfig(t *testing.T) {
  90. testOptions := &BuiltInAuthenticationOptions{
  91. Anonymous: &AnonymousAuthenticationOptions{
  92. Allow: false,
  93. },
  94. ClientCert: &apiserveroptions.ClientCertAuthenticationOptions{
  95. ClientCA: "testdata/root.pem",
  96. },
  97. WebHook: &WebHookAuthenticationOptions{
  98. CacheTTL: 180000000000,
  99. ConfigFile: "/token-webhook-config",
  100. },
  101. BootstrapToken: &BootstrapTokenAuthenticationOptions{
  102. Enable: false,
  103. },
  104. OIDC: &OIDCAuthenticationOptions{
  105. CAFile: "/testCAFile",
  106. UsernameClaim: "sub",
  107. SigningAlgs: []string{"RS256"},
  108. IssuerURL: "testIssuerURL",
  109. ClientID: "testClientID",
  110. },
  111. PasswordFile: &PasswordFileAuthenticationOptions{
  112. BasicAuthFile: "/testBasicAuthFile",
  113. },
  114. RequestHeader: &apiserveroptions.RequestHeaderAuthenticationOptions{
  115. UsernameHeaders: []string{"x-remote-user"},
  116. GroupHeaders: []string{"x-remote-group"},
  117. ExtraHeaderPrefixes: []string{"x-remote-extra-"},
  118. ClientCAFile: "testdata/root.pem",
  119. AllowedNames: []string{"kube-aggregator"},
  120. },
  121. ServiceAccounts: &ServiceAccountAuthenticationOptions{
  122. Lookup: true,
  123. Issuer: "http://foo.bar.com",
  124. },
  125. TokenFile: &TokenFileAuthenticationOptions{
  126. TokenFile: "/testTokenFile",
  127. },
  128. TokenSuccessCacheTTL: 10 * time.Second,
  129. TokenFailureCacheTTL: 0,
  130. }
  131. expectConfig := kubeauthenticator.Config{
  132. APIAudiences: authenticator.Audiences{"http://foo.bar.com"},
  133. Anonymous: false,
  134. BasicAuthFile: "/testBasicAuthFile",
  135. BootstrapToken: false,
  136. ClientCAContentProvider: nil, // this is nil because you can't compare functions
  137. TokenAuthFile: "/testTokenFile",
  138. OIDCIssuerURL: "testIssuerURL",
  139. OIDCClientID: "testClientID",
  140. OIDCCAFile: "/testCAFile",
  141. OIDCUsernameClaim: "sub",
  142. OIDCSigningAlgs: []string{"RS256"},
  143. ServiceAccountLookup: true,
  144. ServiceAccountIssuer: "http://foo.bar.com",
  145. WebhookTokenAuthnConfigFile: "/token-webhook-config",
  146. WebhookTokenAuthnCacheTTL: 180000000000,
  147. TokenSuccessCacheTTL: 10 * time.Second,
  148. TokenFailureCacheTTL: 0,
  149. RequestHeaderConfig: &authenticatorfactory.RequestHeaderConfig{
  150. UsernameHeaders: headerrequest.StaticStringSlice{"x-remote-user"},
  151. GroupHeaders: headerrequest.StaticStringSlice{"x-remote-group"},
  152. ExtraHeaderPrefixes: headerrequest.StaticStringSlice{"x-remote-extra-"},
  153. CAContentProvider: nil, // this is nil because you can't compare functions
  154. AllowedClientNames: headerrequest.StaticStringSlice{"kube-aggregator"},
  155. },
  156. }
  157. resultConfig, err := testOptions.ToAuthenticationConfig()
  158. if err != nil {
  159. t.Fatal(err)
  160. }
  161. // nil these out because you cannot compare pointers. Ensure they are non-nil first
  162. if resultConfig.ClientCAContentProvider == nil {
  163. t.Error("missing client verify")
  164. }
  165. if resultConfig.RequestHeaderConfig.CAContentProvider == nil {
  166. t.Error("missing requestheader verify")
  167. }
  168. resultConfig.ClientCAContentProvider = nil
  169. resultConfig.RequestHeaderConfig.CAContentProvider = nil
  170. if !reflect.DeepEqual(resultConfig, expectConfig) {
  171. t.Error(cmp.Diff(resultConfig, expectConfig))
  172. }
  173. }