conversion.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. Copyright 2015 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 v0
  14. import (
  15. "k8s.io/apimachinery/pkg/conversion"
  16. "k8s.io/apimachinery/pkg/runtime"
  17. api "k8s.io/kubernetes/pkg/apis/abac"
  18. )
  19. // allAuthenticated matches k8s.io/apiserver/pkg/authentication/user.AllAuthenticated,
  20. // but we don't want a client library (which must include types), depending on a server library
  21. const allAuthenticated = "system:authenticated"
  22. func addConversionFuncs(scheme *runtime.Scheme) error {
  23. return scheme.AddConversionFuncs(
  24. func(in *Policy, out *api.Policy, s conversion.Scope) error {
  25. // Begin by copying all fields
  26. out.Spec.User = in.User
  27. out.Spec.Group = in.Group
  28. out.Spec.Namespace = in.Namespace
  29. out.Spec.Resource = in.Resource
  30. out.Spec.Readonly = in.Readonly
  31. // In v0, unspecified user and group matches all authenticated subjects
  32. if len(in.User) == 0 && len(in.Group) == 0 {
  33. out.Spec.Group = allAuthenticated
  34. }
  35. // In v0, user or group of * matches all authenticated subjects
  36. if in.User == "*" || in.Group == "*" {
  37. out.Spec.Group = allAuthenticated
  38. out.Spec.User = ""
  39. }
  40. // In v0, leaving namespace empty matches all namespaces
  41. if len(in.Namespace) == 0 {
  42. out.Spec.Namespace = "*"
  43. }
  44. // In v0, leaving resource empty matches all resources
  45. if len(in.Resource) == 0 {
  46. out.Spec.Resource = "*"
  47. }
  48. // Any rule in v0 should match all API groups
  49. out.Spec.APIGroup = "*"
  50. // In v0, leaving namespace and resource blank allows non-resource paths
  51. if len(in.Namespace) == 0 && len(in.Resource) == 0 {
  52. out.Spec.NonResourcePath = "*"
  53. }
  54. return nil
  55. },
  56. )
  57. }