authentication_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. utilerrors "k8s.io/apimachinery/pkg/util/errors"
  20. "k8s.io/apiserver/pkg/authentication/authenticator"
  21. "k8s.io/apiserver/pkg/authentication/authenticatorfactory"
  22. apiserveroptions "k8s.io/apiserver/pkg/server/options"
  23. kubeauthenticator "k8s.io/kubernetes/pkg/kubeapiserver/authenticator"
  24. )
  25. func TestAuthenticationValidate(t *testing.T) {
  26. testCases := []struct {
  27. name string
  28. testOIDC *OIDCAuthenticationOptions
  29. testSA *ServiceAccountAuthenticationOptions
  30. expectErr string
  31. }{
  32. {
  33. name: "test when OIDC and ServiceAccounts are nil",
  34. },
  35. {
  36. name: "test when OIDC and ServiceAccounts are valid",
  37. testOIDC: &OIDCAuthenticationOptions{
  38. UsernameClaim: "sub",
  39. SigningAlgs: []string{"RS256"},
  40. IssuerURL: "testIssuerURL",
  41. },
  42. testSA: &ServiceAccountAuthenticationOptions{
  43. Issuer: "http://foo.bar.com",
  44. },
  45. },
  46. {
  47. name: "test when OIDC is invalid",
  48. testOIDC: &OIDCAuthenticationOptions{
  49. UsernameClaim: "sub",
  50. SigningAlgs: []string{"RS256"},
  51. IssuerURL: "testIssuerURL",
  52. },
  53. testSA: &ServiceAccountAuthenticationOptions{
  54. Issuer: "http://foo.bar.com",
  55. },
  56. expectErr: "oidc-issuer-url and oidc-client-id should be specified together",
  57. },
  58. {
  59. name: "test when ServiceAccount is invalid",
  60. testOIDC: &OIDCAuthenticationOptions{
  61. UsernameClaim: "sub",
  62. SigningAlgs: []string{"RS256"},
  63. IssuerURL: "testIssuerURL",
  64. ClientID: "testClientID",
  65. },
  66. testSA: &ServiceAccountAuthenticationOptions{
  67. Issuer: "http://[::1]:namedport",
  68. },
  69. expectErr: "service-account-issuer contained a ':' but was not a valid URL",
  70. },
  71. }
  72. for _, testcase := range testCases {
  73. t.Run(testcase.name, func(t *testing.T) {
  74. options := NewBuiltInAuthenticationOptions()
  75. options.OIDC = testcase.testOIDC
  76. options.ServiceAccounts = testcase.testSA
  77. errs := options.Validate()
  78. if len(errs) > 0 && !strings.Contains(utilerrors.NewAggregate(errs).Error(), testcase.expectErr) {
  79. t.Errorf("Got err: %v, Expected err: %s", errs, testcase.expectErr)
  80. }
  81. if len(errs) == 0 && len(testcase.expectErr) != 0 {
  82. t.Errorf("Got err nil, Expected err: %s", testcase.expectErr)
  83. }
  84. })
  85. }
  86. }
  87. func TestToAuthenticationConfig(t *testing.T) {
  88. testOptions := &BuiltInAuthenticationOptions{
  89. Anonymous: &AnonymousAuthenticationOptions{
  90. Allow: false,
  91. },
  92. ClientCert: &apiserveroptions.ClientCertAuthenticationOptions{
  93. ClientCA: "/client-ca",
  94. },
  95. WebHook: &WebHookAuthenticationOptions{
  96. CacheTTL: 180000000000,
  97. ConfigFile: "/token-webhook-config",
  98. },
  99. BootstrapToken: &BootstrapTokenAuthenticationOptions{
  100. Enable: false,
  101. },
  102. OIDC: &OIDCAuthenticationOptions{
  103. CAFile: "/testCAFile",
  104. UsernameClaim: "sub",
  105. SigningAlgs: []string{"RS256"},
  106. IssuerURL: "testIssuerURL",
  107. ClientID: "testClientID",
  108. },
  109. PasswordFile: &PasswordFileAuthenticationOptions{
  110. BasicAuthFile: "/testBasicAuthFile",
  111. },
  112. RequestHeader: &apiserveroptions.RequestHeaderAuthenticationOptions{
  113. UsernameHeaders: []string{"x-remote-user"},
  114. GroupHeaders: []string{"x-remote-group"},
  115. ExtraHeaderPrefixes: []string{"x-remote-extra-"},
  116. ClientCAFile: "/testClientCAFile",
  117. AllowedNames: []string{"kube-aggregator"},
  118. },
  119. ServiceAccounts: &ServiceAccountAuthenticationOptions{
  120. Lookup: true,
  121. Issuer: "http://foo.bar.com",
  122. },
  123. TokenFile: &TokenFileAuthenticationOptions{
  124. TokenFile: "/testTokenFile",
  125. },
  126. TokenSuccessCacheTTL: 10 * time.Second,
  127. TokenFailureCacheTTL: 0,
  128. }
  129. expectConfig := kubeauthenticator.Config{
  130. APIAudiences: authenticator.Audiences{"http://foo.bar.com"},
  131. Anonymous: false,
  132. BasicAuthFile: "/testBasicAuthFile",
  133. BootstrapToken: false,
  134. ClientCAFile: "/client-ca",
  135. TokenAuthFile: "/testTokenFile",
  136. OIDCIssuerURL: "testIssuerURL",
  137. OIDCClientID: "testClientID",
  138. OIDCCAFile: "/testCAFile",
  139. OIDCUsernameClaim: "sub",
  140. OIDCSigningAlgs: []string{"RS256"},
  141. ServiceAccountLookup: true,
  142. ServiceAccountIssuer: "http://foo.bar.com",
  143. WebhookTokenAuthnConfigFile: "/token-webhook-config",
  144. WebhookTokenAuthnCacheTTL: 180000000000,
  145. TokenSuccessCacheTTL: 10 * time.Second,
  146. TokenFailureCacheTTL: 0,
  147. RequestHeaderConfig: &authenticatorfactory.RequestHeaderConfig{
  148. UsernameHeaders: []string{"x-remote-user"},
  149. GroupHeaders: []string{"x-remote-group"},
  150. ExtraHeaderPrefixes: []string{"x-remote-extra-"},
  151. ClientCA: "/testClientCAFile",
  152. AllowedClientNames: []string{"kube-aggregator"},
  153. },
  154. }
  155. resultConfig := testOptions.ToAuthenticationConfig()
  156. if !reflect.DeepEqual(resultConfig, expectConfig) {
  157. t.Errorf("Got AuthenticationConfig:\n\t%v\nExpected AuthenticationConfig:\n\t%v", resultConfig, expectConfig)
  158. }
  159. }