auth.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. Copyright 2016 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 app
  14. import (
  15. "errors"
  16. "fmt"
  17. "reflect"
  18. "k8s.io/apimachinery/pkg/types"
  19. "k8s.io/apiserver/pkg/authentication/authenticator"
  20. "k8s.io/apiserver/pkg/authentication/authenticatorfactory"
  21. "k8s.io/apiserver/pkg/authorization/authorizer"
  22. "k8s.io/apiserver/pkg/authorization/authorizerfactory"
  23. "k8s.io/apiserver/pkg/server/dynamiccertificates"
  24. clientset "k8s.io/client-go/kubernetes"
  25. authenticationclient "k8s.io/client-go/kubernetes/typed/authentication/v1"
  26. authorizationclient "k8s.io/client-go/kubernetes/typed/authorization/v1"
  27. kubeletconfig "k8s.io/kubernetes/pkg/kubelet/apis/config"
  28. "k8s.io/kubernetes/pkg/kubelet/server"
  29. )
  30. // BuildAuth creates an authenticator, an authorizer, and a matching authorizer attributes getter compatible with the kubelet's needs
  31. func BuildAuth(nodeName types.NodeName, client clientset.Interface, config kubeletconfig.KubeletConfiguration) (server.AuthInterface, error) {
  32. // Get clients, if provided
  33. var (
  34. tokenClient authenticationclient.TokenReviewInterface
  35. sarClient authorizationclient.SubjectAccessReviewInterface
  36. )
  37. if client != nil && !reflect.ValueOf(client).IsNil() {
  38. tokenClient = client.AuthenticationV1().TokenReviews()
  39. sarClient = client.AuthorizationV1().SubjectAccessReviews()
  40. }
  41. authenticator, err := BuildAuthn(tokenClient, config.Authentication)
  42. if err != nil {
  43. return nil, err
  44. }
  45. attributes := server.NewNodeAuthorizerAttributesGetter(nodeName)
  46. authorizer, err := BuildAuthz(sarClient, config.Authorization)
  47. if err != nil {
  48. return nil, err
  49. }
  50. return server.NewKubeletAuth(authenticator, attributes, authorizer), nil
  51. }
  52. // BuildAuthn creates an authenticator compatible with the kubelet's needs
  53. func BuildAuthn(client authenticationclient.TokenReviewInterface, authn kubeletconfig.KubeletAuthentication) (authenticator.Request, error) {
  54. var clientCertificateCAContentProvider authenticatorfactory.CAContentProvider
  55. var err error
  56. if len(authn.X509.ClientCAFile) > 0 {
  57. clientCertificateCAContentProvider, err = dynamiccertificates.NewDynamicCAContentFromFile("client-ca-bundle", authn.X509.ClientCAFile)
  58. if err != nil {
  59. return nil, err
  60. }
  61. }
  62. authenticatorConfig := authenticatorfactory.DelegatingAuthenticatorConfig{
  63. Anonymous: authn.Anonymous.Enabled,
  64. CacheTTL: authn.Webhook.CacheTTL.Duration,
  65. ClientCertificateCAContentProvider: clientCertificateCAContentProvider,
  66. }
  67. if authn.Webhook.Enabled {
  68. if client == nil {
  69. return nil, errors.New("no client provided, cannot use webhook authentication")
  70. }
  71. authenticatorConfig.TokenAccessReviewClient = client
  72. }
  73. authenticator, _, err := authenticatorConfig.New()
  74. return authenticator, err
  75. }
  76. // BuildAuthz creates an authorizer compatible with the kubelet's needs
  77. func BuildAuthz(client authorizationclient.SubjectAccessReviewInterface, authz kubeletconfig.KubeletAuthorization) (authorizer.Authorizer, error) {
  78. switch authz.Mode {
  79. case kubeletconfig.KubeletAuthorizationModeAlwaysAllow:
  80. return authorizerfactory.NewAlwaysAllowAuthorizer(), nil
  81. case kubeletconfig.KubeletAuthorizationModeWebhook:
  82. if client == nil {
  83. return nil, errors.New("no client provided, cannot use webhook authorization")
  84. }
  85. authorizerConfig := authorizerfactory.DelegatingAuthorizerConfig{
  86. SubjectAccessReviewClient: client,
  87. AllowCacheTTL: authz.Webhook.CacheAuthorizedTTL.Duration,
  88. DenyCacheTTL: authz.Webhook.CacheUnauthorizedTTL.Duration,
  89. }
  90. return authorizerConfig.New()
  91. case "":
  92. return nil, fmt.Errorf("no authorization mode specified")
  93. default:
  94. return nil, fmt.Errorf("unknown authorization mode %s", authz.Mode)
  95. }
  96. }