| 1 |
/* |
| 2 |
Copyright 2014 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 serviceaccount |
| 18 |
|
| 19 |
import ( |
| 20 |
"fmt" |
| 21 |
"strings" |
| 22 |
|
| 23 |
v1 "k8s.io/api/core/v1" |
| 24 |
apimachineryvalidation "k8s.io/apimachinery/pkg/api/validation" |
| 25 |
"k8s.io/apiserver/pkg/authentication/user" |
| 26 |
) |
| 27 |
|
| 28 |
const ( |
| 29 |
ServiceAccountUsernamePrefix = "system:serviceaccount:" |
| 30 |
ServiceAccountUsernameSeparator = ":" |
| 31 |
ServiceAccountGroupPrefix = "system:serviceaccounts:" |
| 32 |
AllServiceAccountsGroup = "system:serviceaccounts" |
| 33 |
// IssuedCredentialIDAuditAnnotationKey is the annotation key used in the audit event that is persisted to the |
| 34 |
// '/token' endpoint for service accounts. |
| 35 |
// This annotation indicates the generated credential identifier for the service account token being issued. |
| 36 |
// This is useful when tracing back the origin of tokens that have gone on to make request that have persisted |
| 37 |
// their credential-identifier into the audit log via the user's extra info stored on subsequent audit events. |
| 38 |
IssuedCredentialIDAuditAnnotationKey = "authentication.kubernetes.io/issued-credential-id" |
| 39 |
// PodNameKey is the key used in a user's "extra" to specify the pod name of |
| 40 |
// the authenticating request. |
| 41 |
PodNameKey = "authentication.kubernetes.io/pod-name" |
| 42 |
// PodUIDKey is the key used in a user's "extra" to specify the pod UID of |
| 43 |
// the authenticating request. |
| 44 |
PodUIDKey = "authentication.kubernetes.io/pod-uid" |
| 45 |
// NodeNameKey is the key used in a user's "extra" to specify the node name of |
| 46 |
// the authenticating request. |
| 47 |
NodeNameKey = "authentication.kubernetes.io/node-name" |
| 48 |
// NodeUIDKey is the key used in a user's "extra" to specify the node UID of |
| 49 |
// the authenticating request. |
| 50 |
NodeUIDKey = "authentication.kubernetes.io/node-uid" |
| 51 |
) |
| 52 |
|
| 53 |
// MakeUsername generates a username from the given namespace and ServiceAccount name. |
| 54 |
// The resulting username can be passed to SplitUsername to extract the original namespace and ServiceAccount name. |
| 55 |
func MakeUsername(namespace, name string) string { |
| 56 |
return ServiceAccountUsernamePrefix + namespace + ServiceAccountUsernameSeparator + name |
| 57 |
} |
| 58 |
|
| 59 |
// MatchesUsername checks whether the provided username matches the namespace and name without |
| 60 |
// allocating. Use this when checking a service account namespace and name against a known string. |
| 61 |
func MatchesUsername(namespace, name string, username string) bool { |
| 62 |
if !strings.HasPrefix(username, ServiceAccountUsernamePrefix) { |
| 63 |
return false |
| 64 |
} |
| 65 |
username = username[len(ServiceAccountUsernamePrefix):] |
| 66 |
|
| 67 |
if !strings.HasPrefix(username, namespace) { |
| 68 |
return false |
| 69 |
} |
| 70 |
username = username[len(namespace):] |
| 71 |
|
| 72 |
if !strings.HasPrefix(username, ServiceAccountUsernameSeparator) { |
| 73 |
return false |
| 74 |
} |
| 75 |
username = username[len(ServiceAccountUsernameSeparator):] |
| 76 |
|
| 77 |
return username == name |
| 78 |
} |
| 79 |
|
| 80 |
var invalidUsernameErr = fmt.Errorf("Username must be in the form %s", MakeUsername("namespace", "name")) |
| 81 |
|
| 82 |
// SplitUsername returns the namespace and ServiceAccount name embedded in the given username, |
| 83 |
// or an error if the username is not a valid name produced by MakeUsername |
| 84 |
func SplitUsername(username string) (string, string, error) { |
| 85 |
if !strings.HasPrefix(username, ServiceAccountUsernamePrefix) { |
| 86 |
return "", "", invalidUsernameErr |
| 87 |
} |
| 88 |
trimmed := strings.TrimPrefix(username, ServiceAccountUsernamePrefix) |
| 89 |
parts := strings.Split(trimmed, ServiceAccountUsernameSeparator) |
| 90 |
if len(parts) != 2 { |
| 91 |
return "", "", invalidUsernameErr |
| 92 |
} |
| 93 |
namespace, name := parts[0], parts[1] |
| 94 |
if len(apimachineryvalidation.ValidateNamespaceName(namespace, false)) != 0 { |
| 95 |
return "", "", invalidUsernameErr |
| 96 |
} |
| 97 |
if len(apimachineryvalidation.ValidateServiceAccountName(name, false)) != 0 { |
| 98 |
return "", "", invalidUsernameErr |
| 99 |
} |
| 100 |
return namespace, name, nil |
| 101 |
} |
| 102 |
|
| 103 |
// MakeGroupNames generates service account group names for the given namespace |
| 104 |
func MakeGroupNames(namespace string) []string { |
| 105 |
return []string{ |
| 106 |
AllServiceAccountsGroup, |
| 107 |
MakeNamespaceGroupName(namespace), |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
// MakeNamespaceGroupName returns the name of the group all service accounts in the namespace are included in |
| 112 |
func MakeNamespaceGroupName(namespace string) string { |
| 113 |
return ServiceAccountGroupPrefix + namespace |
| 114 |
} |
| 115 |
|
| 116 |
// UserInfo returns a user.Info interface for the given namespace, service account name and UID |
| 117 |
func UserInfo(namespace, name, uid string) user.Info { |
| 118 |
return (&ServiceAccountInfo{ |
| 119 |
Name: name, |
| 120 |
Namespace: namespace, |
| 121 |
UID: uid, |
| 122 |
}).UserInfo() |
| 123 |
} |
| 124 |
|
| 125 |
type ServiceAccountInfo struct { |
| 126 |
Name, Namespace, UID string |
| 127 |
PodName, PodUID string |
| 128 |
CredentialID string |
| 129 |
NodeName, NodeUID string |
| 130 |
} |
| 131 |
|
| 132 |
func (sa *ServiceAccountInfo) UserInfo() user.Info { |
| 133 |
info := &user.DefaultInfo{ |
| 134 |
Name: MakeUsername(sa.Namespace, sa.Name), |
| 135 |
UID: sa.UID, |
| 136 |
Groups: MakeGroupNames(sa.Namespace), |
| 137 |
} |
| 138 |
|
| 139 |
if sa.PodName != "" && sa.PodUID != "" { |
| 140 |
if info.Extra == nil { |
| 141 |
info.Extra = make(map[string][]string) |
| 142 |
} |
| 143 |
info.Extra[PodNameKey] = []string{sa.PodName} |
| 144 |
info.Extra[PodUIDKey] = []string{sa.PodUID} |
| 145 |
} |
| 146 |
if sa.CredentialID != "" { |
| 147 |
if info.Extra == nil { |
| 148 |
info.Extra = make(map[string][]string) |
| 149 |
} |
| 150 |
info.Extra[user.CredentialIDKey] = []string{sa.CredentialID} |
| 151 |
} |
| 152 |
if sa.NodeName != "" { |
| 153 |
if info.Extra == nil { |
| 154 |
info.Extra = make(map[string][]string) |
| 155 |
} |
| 156 |
info.Extra[NodeNameKey] = []string{sa.NodeName} |
| 157 |
// node UID is optional and will only be set if the node name is set |
| 158 |
if sa.NodeUID != "" { |
| 159 |
info.Extra[NodeUIDKey] = []string{sa.NodeUID} |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
return info |
| 164 |
} |
| 165 |
|
| 166 |
// IsServiceAccountToken returns true if the secret is a valid api token for the service account |
| 167 |
func IsServiceAccountToken(secret *v1.Secret, sa *v1.ServiceAccount) bool { |
| 168 |
if secret.Type != v1.SecretTypeServiceAccountToken { |
| 169 |
return false |
| 170 |
} |
| 171 |
|
| 172 |
name := secret.Annotations[v1.ServiceAccountNameKey] |
| 173 |
uid := secret.Annotations[v1.ServiceAccountUIDKey] |
| 174 |
if name != sa.Name { |
| 175 |
// Name must match |
| 176 |
return false |
| 177 |
} |
| 178 |
if len(uid) > 0 && uid != string(sa.UID) { |
| 179 |
// If UID is specified, it must match |
| 180 |
return false |
| 181 |
} |
| 182 |
|
| 183 |
return true |
| 184 |
} |