conversion_test.go 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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_test
  14. import (
  15. "reflect"
  16. "testing"
  17. "k8s.io/apiserver/pkg/authentication/user"
  18. "k8s.io/kubernetes/pkg/apis/abac"
  19. "k8s.io/kubernetes/pkg/apis/abac/v0"
  20. )
  21. func TestV0Conversion(t *testing.T) {
  22. testcases := map[string]struct {
  23. old *v0.Policy
  24. expected *abac.Policy
  25. }{
  26. // a completely empty policy rule allows everything to all users
  27. "empty": {
  28. old: &v0.Policy{},
  29. expected: &abac.Policy{Spec: abac.PolicySpec{Group: user.AllAuthenticated, Readonly: false, NonResourcePath: "*", Namespace: "*", Resource: "*", APIGroup: "*"}},
  30. },
  31. // specifying a user is preserved
  32. "user": {
  33. old: &v0.Policy{User: "bob"},
  34. expected: &abac.Policy{Spec: abac.PolicySpec{User: "bob", Readonly: false, NonResourcePath: "*", Namespace: "*", Resource: "*", APIGroup: "*"}},
  35. },
  36. // specifying a group is preserved (and no longer matches all users)
  37. "group": {
  38. old: &v0.Policy{Group: "mygroup"},
  39. expected: &abac.Policy{Spec: abac.PolicySpec{Group: "mygroup", Readonly: false, NonResourcePath: "*", Namespace: "*", Resource: "*", APIGroup: "*"}},
  40. },
  41. // specifying * for user or group maps to all authenticated subjects
  42. "* user": {
  43. old: &v0.Policy{User: "*"},
  44. expected: &abac.Policy{Spec: abac.PolicySpec{Group: user.AllAuthenticated, Readonly: false, NonResourcePath: "*", Namespace: "*", Resource: "*", APIGroup: "*"}},
  45. },
  46. "* group": {
  47. old: &v0.Policy{Group: "*"},
  48. expected: &abac.Policy{Spec: abac.PolicySpec{Group: user.AllAuthenticated, Readonly: false, NonResourcePath: "*", Namespace: "*", Resource: "*", APIGroup: "*"}},
  49. },
  50. // specifying a namespace removes the * match on non-resource path
  51. "namespace": {
  52. old: &v0.Policy{Namespace: "myns"},
  53. expected: &abac.Policy{Spec: abac.PolicySpec{Group: user.AllAuthenticated, Readonly: false, NonResourcePath: "", Namespace: "myns", Resource: "*", APIGroup: "*"}},
  54. },
  55. // specifying a resource removes the * match on non-resource path
  56. "resource": {
  57. old: &v0.Policy{Resource: "myresource"},
  58. expected: &abac.Policy{Spec: abac.PolicySpec{Group: user.AllAuthenticated, Readonly: false, NonResourcePath: "", Namespace: "*", Resource: "myresource", APIGroup: "*"}},
  59. },
  60. // specifying a namespace+resource removes the * match on non-resource path
  61. "namespace+resource": {
  62. old: &v0.Policy{Namespace: "myns", Resource: "myresource"},
  63. expected: &abac.Policy{Spec: abac.PolicySpec{Group: user.AllAuthenticated, Readonly: false, NonResourcePath: "", Namespace: "myns", Resource: "myresource", APIGroup: "*"}},
  64. },
  65. }
  66. for k, tc := range testcases {
  67. internal := &abac.Policy{}
  68. if err := abac.Scheme.Convert(tc.old, internal, nil); err != nil {
  69. t.Errorf("%s: unexpected error: %v", k, err)
  70. }
  71. if !reflect.DeepEqual(internal, tc.expected) {
  72. t.Errorf("%s: expected\n\t%#v, got \n\t%#v", k, tc.expected, internal)
  73. }
  74. }
  75. }