1 /* 2 Copyright 2017 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package nodeidentifier 18 19 import ( 20 "strings" 21 22 "k8s.io/apiserver/pkg/authentication/user" 23 ) 24 25 // NewDefaultNodeIdentifier returns a default NodeIdentifier implementation, 26 // which returns isNode=true if the user groups contain the system:nodes group 27 // and the user name matches the format system:node:<nodeName>, and populates 28 // nodeName if isNode is true 29 func NewDefaultNodeIdentifier() NodeIdentifier { 30 return defaultNodeIdentifier{} 31 } 32 33 // defaultNodeIdentifier implements NodeIdentifier 34 type defaultNodeIdentifier struct{} 35 36 // nodeUserNamePrefix is the prefix for usernames in the form `system:node:<nodeName>` 37 const nodeUserNamePrefix = "system:node:" 38 39 // NodeIdentity returns isNode=true if the user groups contain the system:nodes 40 // group and the user name matches the format system:node:<nodeName>, and 41 // populates nodeName if isNode is true 42 func (defaultNodeIdentifier) NodeIdentity(u user.Info) (string, bool) { 43 // Make sure we're a node, and can parse the node name 44 if u == nil { 45 return "", false 46 } 47 48 userName := u.GetName() 49 if !strings.HasPrefix(userName, nodeUserNamePrefix) { 50 return "", false 51 } 52 53 isNode := false 54 for _, g := range u.GetGroups() { 55 if g == user.NodesGroup { 56 isNode = true 57 break 58 } 59 } 60 if !isNode { 61 return "", false 62 } 63 64 nodeName := strings.TrimPrefix(userName, nodeUserNamePrefix) 65 return nodeName, true 66 } 67