/
helpers.go
99 lines (84 loc) · 3.27 KB
/
helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright (c) 2020 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package helper
import (
"fmt"
"strings"
apisconfig "github.com/gardener/gardener/pkg/admissioncontroller/apis/config"
authenticationv1 "k8s.io/api/authentication/v1"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apiserver/pkg/authentication/serviceaccount"
)
// APIGroupMatches returns `true` if the given group has a match in the given limit.
func APIGroupMatches(limit apisconfig.ResourceLimit, group string) bool {
for _, grp := range limit.APIGroups {
if grp == apisconfig.WildcardAll || grp == group {
return true
}
}
return false
}
// ResourceMatches returns `true` if the given resource has a match in the given limit.
func ResourceMatches(limit apisconfig.ResourceLimit, resource string) bool {
for _, res := range limit.Resources {
if res == apisconfig.WildcardAll || res == resource {
return true
}
}
return false
}
// VersionMatches returns `true` if the given version has a match in the given limit.
func VersionMatches(limit apisconfig.ResourceLimit, version string) bool {
for _, ver := range limit.APIVersions {
if ver == apisconfig.WildcardAll || ver == version {
return true
}
}
return false
}
// UserMatches returns `true` if the given user in the subject has a match in the given userConfig.
func UserMatches(subject rbacv1.Subject, userInfo authenticationv1.UserInfo) bool {
if subject.Kind != rbacv1.UserKind {
return false
}
return subject.Name == apisconfig.WildcardAll || subject.Name == userInfo.Username
}
// UserGroupMatches returns `true` if the given group in the subject has a match in the given userConfig.
// Always returns true if `apisconfig.WildcardAll` is used in subject.
func UserGroupMatches(subject rbacv1.Subject, userInfo authenticationv1.UserInfo) bool {
if subject.Kind != rbacv1.GroupKind {
return false
}
if subject.Name == apisconfig.WildcardAll {
return true
}
for _, group := range userInfo.Groups {
if group == subject.Name {
return true
}
}
return false
}
// ServiceAccountMatches returns `true` if the given service account in the subject has a match in the given userConfig.
// Supports `apisconfig.WildcardAll` in subject name.
func ServiceAccountMatches(subject rbacv1.Subject, userInfo authenticationv1.UserInfo) bool {
if subject.Kind != rbacv1.ServiceAccountKind {
return false
}
if subject.Name == apisconfig.WildcardAll {
saPrefix := fmt.Sprintf("%s%s:", serviceaccount.ServiceAccountUsernamePrefix, subject.Namespace)
return strings.HasPrefix(userInfo.Username, saPrefix)
}
return serviceaccount.MatchesUsername(subject.Namespace, subject.Name, userInfo.Username)
}