authorization.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 options
  14. import (
  15. "fmt"
  16. "strings"
  17. "time"
  18. "github.com/spf13/pflag"
  19. "k8s.io/apimachinery/pkg/util/sets"
  20. versionedinformers "k8s.io/client-go/informers"
  21. "k8s.io/kubernetes/pkg/kubeapiserver/authorizer"
  22. authzmodes "k8s.io/kubernetes/pkg/kubeapiserver/authorizer/modes"
  23. )
  24. type BuiltInAuthorizationOptions struct {
  25. Modes []string
  26. PolicyFile string
  27. WebhookConfigFile string
  28. WebhookCacheAuthorizedTTL time.Duration
  29. WebhookCacheUnauthorizedTTL time.Duration
  30. }
  31. func NewBuiltInAuthorizationOptions() *BuiltInAuthorizationOptions {
  32. return &BuiltInAuthorizationOptions{
  33. Modes: []string{authzmodes.ModeAlwaysAllow},
  34. WebhookCacheAuthorizedTTL: 5 * time.Minute,
  35. WebhookCacheUnauthorizedTTL: 30 * time.Second,
  36. }
  37. }
  38. func (s *BuiltInAuthorizationOptions) Validate() []error {
  39. if s == nil {
  40. return nil
  41. }
  42. allErrors := []error{}
  43. if len(s.Modes) == 0 {
  44. allErrors = append(allErrors, fmt.Errorf("at least one authorization-mode must be passed"))
  45. }
  46. allowedModes := sets.NewString(authzmodes.AuthorizationModeChoices...)
  47. modes := sets.NewString(s.Modes...)
  48. for _, mode := range s.Modes {
  49. if !allowedModes.Has(mode) {
  50. allErrors = append(allErrors, fmt.Errorf("authorization-mode %q is not a valid mode", mode))
  51. }
  52. if mode == authzmodes.ModeABAC {
  53. if s.PolicyFile == "" {
  54. allErrors = append(allErrors, fmt.Errorf("authorization-mode ABAC's authorization policy file not passed"))
  55. }
  56. }
  57. if mode == authzmodes.ModeWebhook {
  58. if s.WebhookConfigFile == "" {
  59. allErrors = append(allErrors, fmt.Errorf("authorization-mode Webhook's authorization config file not passed"))
  60. }
  61. }
  62. }
  63. if s.PolicyFile != "" && !modes.Has(authzmodes.ModeABAC) {
  64. allErrors = append(allErrors, fmt.Errorf("cannot specify --authorization-policy-file without mode ABAC"))
  65. }
  66. if s.WebhookConfigFile != "" && !modes.Has(authzmodes.ModeWebhook) {
  67. allErrors = append(allErrors, fmt.Errorf("cannot specify --authorization-webhook-config-file without mode Webhook"))
  68. }
  69. if len(s.Modes) != len(modes.List()) {
  70. allErrors = append(allErrors, fmt.Errorf("authorization-mode %q has mode specified more than once", s.Modes))
  71. }
  72. return allErrors
  73. }
  74. func (s *BuiltInAuthorizationOptions) AddFlags(fs *pflag.FlagSet) {
  75. fs.StringSliceVar(&s.Modes, "authorization-mode", s.Modes, ""+
  76. "Ordered list of plug-ins to do authorization on secure port. Comma-delimited list of: "+
  77. strings.Join(authzmodes.AuthorizationModeChoices, ",")+".")
  78. fs.StringVar(&s.PolicyFile, "authorization-policy-file", s.PolicyFile, ""+
  79. "File with authorization policy in json line by line format, used with --authorization-mode=ABAC, on the secure port.")
  80. fs.StringVar(&s.WebhookConfigFile, "authorization-webhook-config-file", s.WebhookConfigFile, ""+
  81. "File with webhook configuration in kubeconfig format, used with --authorization-mode=Webhook. "+
  82. "The API server will query the remote service to determine access on the API server's secure port.")
  83. fs.DurationVar(&s.WebhookCacheAuthorizedTTL, "authorization-webhook-cache-authorized-ttl",
  84. s.WebhookCacheAuthorizedTTL,
  85. "The duration to cache 'authorized' responses from the webhook authorizer.")
  86. fs.DurationVar(&s.WebhookCacheUnauthorizedTTL,
  87. "authorization-webhook-cache-unauthorized-ttl", s.WebhookCacheUnauthorizedTTL,
  88. "The duration to cache 'unauthorized' responses from the webhook authorizer.")
  89. }
  90. func (s *BuiltInAuthorizationOptions) ToAuthorizationConfig(versionedInformerFactory versionedinformers.SharedInformerFactory) authorizer.Config {
  91. return authorizer.Config{
  92. AuthorizationModes: s.Modes,
  93. PolicyFile: s.PolicyFile,
  94. WebhookConfigFile: s.WebhookConfigFile,
  95. WebhookCacheAuthorizedTTL: s.WebhookCacheAuthorizedTTL,
  96. WebhookCacheUnauthorizedTTL: s.WebhookCacheUnauthorizedTTL,
  97. VersionedInformerFactory: versionedInformerFactory,
  98. }
  99. }