auth.go 3.7 KB

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