default.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. Copyright 2017 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 nodeidentifier
  14. import (
  15. "strings"
  16. "k8s.io/apiserver/pkg/authentication/user"
  17. )
  18. // NewDefaultNodeIdentifier returns a default NodeIdentifier implementation,
  19. // which returns isNode=true if the user groups contain the system:nodes group
  20. // and the user name matches the format system:node:<nodeName>, and populates
  21. // nodeName if isNode is true
  22. func NewDefaultNodeIdentifier() NodeIdentifier {
  23. return defaultNodeIdentifier{}
  24. }
  25. // defaultNodeIdentifier implements NodeIdentifier
  26. type defaultNodeIdentifier struct{}
  27. // nodeUserNamePrefix is the prefix for usernames in the form `system:node:<nodeName>`
  28. const nodeUserNamePrefix = "system:node:"
  29. // NodeIdentity returns isNode=true if the user groups contain the system:nodes
  30. // group and the user name matches the format system:node:<nodeName>, and
  31. // populates nodeName if isNode is true
  32. func (defaultNodeIdentifier) NodeIdentity(u user.Info) (string, bool) {
  33. // Make sure we're a node, and can parse the node name
  34. if u == nil {
  35. return "", false
  36. }
  37. userName := u.GetName()
  38. if !strings.HasPrefix(userName, nodeUserNamePrefix) {
  39. return "", false
  40. }
  41. isNode := false
  42. for _, g := range u.GetGroups() {
  43. if g == user.NodesGroup {
  44. isNode = true
  45. break
  46. }
  47. }
  48. if !isNode {
  49. return "", false
  50. }
  51. nodeName := strings.TrimPrefix(userName, nodeUserNamePrefix)
  52. return nodeName, true
  53. }