helpers.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 util
  14. import (
  15. "k8s.io/apiserver/pkg/authentication/user"
  16. "k8s.io/apiserver/pkg/authorization/authorizer"
  17. authorizationapi "k8s.io/kubernetes/pkg/apis/authorization"
  18. )
  19. // ResourceAttributesFrom combines the API object information and the user.Info from the context to build a full authorizer.AttributesRecord for resource access
  20. func ResourceAttributesFrom(user user.Info, in authorizationapi.ResourceAttributes) authorizer.AttributesRecord {
  21. return authorizer.AttributesRecord{
  22. User: user,
  23. Verb: in.Verb,
  24. Namespace: in.Namespace,
  25. APIGroup: in.Group,
  26. APIVersion: in.Version,
  27. Resource: in.Resource,
  28. Subresource: in.Subresource,
  29. Name: in.Name,
  30. ResourceRequest: true,
  31. }
  32. }
  33. // NonResourceAttributesFrom combines the API object information and the user.Info from the context to build a full authorizer.AttributesRecord for non resource access
  34. func NonResourceAttributesFrom(user user.Info, in authorizationapi.NonResourceAttributes) authorizer.AttributesRecord {
  35. return authorizer.AttributesRecord{
  36. User: user,
  37. ResourceRequest: false,
  38. Path: in.Path,
  39. Verb: in.Verb,
  40. }
  41. }
  42. func convertToUserInfoExtra(extra map[string]authorizationapi.ExtraValue) map[string][]string {
  43. if extra == nil {
  44. return nil
  45. }
  46. ret := map[string][]string{}
  47. for k, v := range extra {
  48. ret[k] = []string(v)
  49. }
  50. return ret
  51. }
  52. // AuthorizationAttributesFrom takes a spec and returns the proper authz attributes to check it.
  53. func AuthorizationAttributesFrom(spec authorizationapi.SubjectAccessReviewSpec) authorizer.AttributesRecord {
  54. userToCheck := &user.DefaultInfo{
  55. Name: spec.User,
  56. Groups: spec.Groups,
  57. UID: spec.UID,
  58. Extra: convertToUserInfoExtra(spec.Extra),
  59. }
  60. var authorizationAttributes authorizer.AttributesRecord
  61. if spec.ResourceAttributes != nil {
  62. authorizationAttributes = ResourceAttributesFrom(userToCheck, *spec.ResourceAttributes)
  63. } else {
  64. authorizationAttributes = NonResourceAttributesFrom(userToCheck, *spec.NonResourceAttributes)
  65. }
  66. return authorizationAttributes
  67. }