conversion.go 1.9 KB

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