config.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 authorizer
  14. import (
  15. "fmt"
  16. "time"
  17. "k8s.io/apiserver/pkg/authorization/authorizer"
  18. "k8s.io/apiserver/pkg/authorization/authorizerfactory"
  19. "k8s.io/apiserver/pkg/authorization/union"
  20. "k8s.io/apiserver/plugin/pkg/authorizer/webhook"
  21. versionedinformers "k8s.io/client-go/informers"
  22. "k8s.io/kubernetes/pkg/auth/authorizer/abac"
  23. "k8s.io/kubernetes/pkg/auth/nodeidentifier"
  24. "k8s.io/kubernetes/pkg/kubeapiserver/authorizer/modes"
  25. "k8s.io/kubernetes/plugin/pkg/auth/authorizer/node"
  26. "k8s.io/kubernetes/plugin/pkg/auth/authorizer/rbac"
  27. "k8s.io/kubernetes/plugin/pkg/auth/authorizer/rbac/bootstrappolicy"
  28. )
  29. // Config contains the data on how to authorize a request to the Kube API Server
  30. type Config struct {
  31. AuthorizationModes []string
  32. // Options for ModeABAC
  33. // Path to an ABAC policy file.
  34. PolicyFile string
  35. // Options for ModeWebhook
  36. // Kubeconfig file for Webhook authorization plugin.
  37. WebhookConfigFile string
  38. // API version of subject access reviews to send to the webhook (e.g. "v1", "v1beta1")
  39. WebhookVersion string
  40. // TTL for caching of authorized responses from the webhook server.
  41. WebhookCacheAuthorizedTTL time.Duration
  42. // TTL for caching of unauthorized responses from the webhook server.
  43. WebhookCacheUnauthorizedTTL time.Duration
  44. VersionedInformerFactory versionedinformers.SharedInformerFactory
  45. }
  46. // New returns the right sort of union of multiple authorizer.Authorizer objects
  47. // based on the authorizationMode or an error.
  48. func (config Config) New() (authorizer.Authorizer, authorizer.RuleResolver, error) {
  49. if len(config.AuthorizationModes) == 0 {
  50. return nil, nil, fmt.Errorf("at least one authorization mode must be passed")
  51. }
  52. var (
  53. authorizers []authorizer.Authorizer
  54. ruleResolvers []authorizer.RuleResolver
  55. )
  56. for _, authorizationMode := range config.AuthorizationModes {
  57. // Keep cases in sync with constant list in k8s.io/kubernetes/pkg/kubeapiserver/authorizer/modes/modes.go.
  58. switch authorizationMode {
  59. case modes.ModeNode:
  60. graph := node.NewGraph()
  61. node.AddGraphEventHandlers(
  62. graph,
  63. config.VersionedInformerFactory.Core().V1().Nodes(),
  64. config.VersionedInformerFactory.Core().V1().Pods(),
  65. config.VersionedInformerFactory.Core().V1().PersistentVolumes(),
  66. config.VersionedInformerFactory.Storage().V1().VolumeAttachments(),
  67. )
  68. nodeAuthorizer := node.NewAuthorizer(graph, nodeidentifier.NewDefaultNodeIdentifier(), bootstrappolicy.NodeRules())
  69. authorizers = append(authorizers, nodeAuthorizer)
  70. case modes.ModeAlwaysAllow:
  71. alwaysAllowAuthorizer := authorizerfactory.NewAlwaysAllowAuthorizer()
  72. authorizers = append(authorizers, alwaysAllowAuthorizer)
  73. ruleResolvers = append(ruleResolvers, alwaysAllowAuthorizer)
  74. case modes.ModeAlwaysDeny:
  75. alwaysDenyAuthorizer := authorizerfactory.NewAlwaysDenyAuthorizer()
  76. authorizers = append(authorizers, alwaysDenyAuthorizer)
  77. ruleResolvers = append(ruleResolvers, alwaysDenyAuthorizer)
  78. case modes.ModeABAC:
  79. abacAuthorizer, err := abac.NewFromFile(config.PolicyFile)
  80. if err != nil {
  81. return nil, nil, err
  82. }
  83. authorizers = append(authorizers, abacAuthorizer)
  84. ruleResolvers = append(ruleResolvers, abacAuthorizer)
  85. case modes.ModeWebhook:
  86. webhookAuthorizer, err := webhook.New(config.WebhookConfigFile,
  87. config.WebhookVersion,
  88. config.WebhookCacheAuthorizedTTL,
  89. config.WebhookCacheUnauthorizedTTL)
  90. if err != nil {
  91. return nil, nil, err
  92. }
  93. authorizers = append(authorizers, webhookAuthorizer)
  94. ruleResolvers = append(ruleResolvers, webhookAuthorizer)
  95. case modes.ModeRBAC:
  96. rbacAuthorizer := rbac.New(
  97. &rbac.RoleGetter{Lister: config.VersionedInformerFactory.Rbac().V1().Roles().Lister()},
  98. &rbac.RoleBindingLister{Lister: config.VersionedInformerFactory.Rbac().V1().RoleBindings().Lister()},
  99. &rbac.ClusterRoleGetter{Lister: config.VersionedInformerFactory.Rbac().V1().ClusterRoles().Lister()},
  100. &rbac.ClusterRoleBindingLister{Lister: config.VersionedInformerFactory.Rbac().V1().ClusterRoleBindings().Lister()},
  101. )
  102. authorizers = append(authorizers, rbacAuthorizer)
  103. ruleResolvers = append(ruleResolvers, rbacAuthorizer)
  104. default:
  105. return nil, nil, fmt.Errorf("unknown authorization mode %s specified", authorizationMode)
  106. }
  107. }
  108. return union.New(authorizers...), union.NewRuleResolvers(ruleResolvers...), nil
  109. }