Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 43c9016

Browse files
committedNov 9, 2015
only expand resources when strictly needed
1 parent 769ca7a commit 43c9016

File tree

7 files changed

+59
-38
lines changed

7 files changed

+59
-38
lines changed
 

‎pkg/authorization/api/group_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
)
88

99
func TestEscalating(t *testing.T) {
10-
escalatingResources := ExpandResources(sets.NewString(GroupsToResources[EscalatingResourcesGroupName]...))
11-
nonEscalatingResources := ExpandResources(sets.NewString(GroupsToResources[NonEscalatingResourcesGroupName]...))
10+
escalatingResources := NormalizeResources(sets.NewString(GroupsToResources[EscalatingResourcesGroupName]...))
11+
nonEscalatingResources := NormalizeResources(sets.NewString(GroupsToResources[NonEscalatingResourcesGroupName]...))
1212
if len(nonEscalatingResources) <= len(escalatingResources) {
1313
t.Errorf("groups look bad: escalating=%v nonescalating=%v", escalatingResources.List(), nonEscalatingResources.List())
1414
}

‎pkg/authorization/api/helpers.go

+23-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,28 @@ import (
1313
// uservalidation "github.com/openshift/origin/pkg/user/api/validation"
1414
)
1515

16-
func ExpandResources(rawResources sets.String) sets.String {
16+
// NormalizeResources expands all resource groups and forces all resources to lower case.
17+
// If the rawResources are already normalized, it returns the original set to avoid the
18+
// allocation and GC cost, since this is hit multiple times for every REST call.
19+
// That means you should NEVER MODIFY THE RESULT of this call.
20+
func NormalizeResources(rawResources sets.String) sets.String {
21+
// we only need to expand groups if the exist and we don't create them with groups
22+
// by default. Only accept the cost of expansion if we're doing work.
23+
needsNormalization := false
24+
for currResource := range rawResources {
25+
if strings.HasPrefix(currResource, ResourceGroupPrefix) {
26+
needsNormalization = true
27+
break
28+
}
29+
if strings.ToLower(currResource) != currResource {
30+
needsNormalization = true
31+
break
32+
}
33+
}
34+
if !needsNormalization {
35+
return rawResources
36+
}
37+
1738
ret := sets.String{}
1839
toVisit := rawResources.List()
1940
visited := sets.String{}
@@ -25,7 +46,7 @@ func ExpandResources(rawResources sets.String) sets.String {
2546
}
2647
visited.Insert(currResource)
2748

28-
if strings.Index(currResource, ResourceGroupPrefix+":") != 0 {
49+
if !strings.HasPrefix(currResource, ResourceGroupPrefix) {
2950
ret.Insert(strings.ToLower(currResource))
3051
continue
3152
}

‎pkg/authorization/api/types.go

+28-28
Original file line numberDiff line numberDiff line change
@@ -33,43 +33,43 @@ const (
3333
APIGroupExtensions = "extensions"
3434

3535
// ResourceGroupPrefix is the prefix for indicating that a resource entry is actually a group of resources. The groups are defined in code and indicate resources that are commonly permissioned together
36-
ResourceGroupPrefix = "resourcegroup"
37-
BuildGroupName = ResourceGroupPrefix + ":builds"
38-
DeploymentGroupName = ResourceGroupPrefix + ":deployments"
39-
ImageGroupName = ResourceGroupPrefix + ":images"
40-
OAuthGroupName = ResourceGroupPrefix + ":oauth"
41-
UserGroupName = ResourceGroupPrefix + ":users"
42-
TemplateGroupName = ResourceGroupPrefix + ":templates"
43-
SDNGroupName = ResourceGroupPrefix + ":sdn"
36+
ResourceGroupPrefix = "resourcegroup:"
37+
BuildGroupName = ResourceGroupPrefix + "builds"
38+
DeploymentGroupName = ResourceGroupPrefix + "deployments"
39+
ImageGroupName = ResourceGroupPrefix + "images"
40+
OAuthGroupName = ResourceGroupPrefix + "oauth"
41+
UserGroupName = ResourceGroupPrefix + "users"
42+
TemplateGroupName = ResourceGroupPrefix + "templates"
43+
SDNGroupName = ResourceGroupPrefix + "sdn"
4444
// PolicyOwnerGroupName includes the physical resources behind the PermissionGrantingGroupName. Unless these physical objects are created first, users with privileges to PermissionGrantingGroupName will
4545
// only be able to bind to global roles
46-
PolicyOwnerGroupName = ResourceGroupPrefix + ":policy"
46+
PolicyOwnerGroupName = ResourceGroupPrefix + "policy"
4747
// PermissionGrantingGroupName includes resources that are necessary to maintain authorization roles and bindings. By itself, this group is insufficient to create anything except for bindings
4848
// to master roles. If a local Policy already exists, then privileges to this group will allow for modification of local roles.
49-
PermissionGrantingGroupName = ResourceGroupPrefix + ":granter"
49+
PermissionGrantingGroupName = ResourceGroupPrefix + "granter"
5050
// OpenshiftExposedGroupName includes resources that are commonly viewed and modified by end users of the system. It does not include any sensitive resources that control authentication or authorization
51-
OpenshiftExposedGroupName = ResourceGroupPrefix + ":exposedopenshift"
52-
OpenshiftAllGroupName = ResourceGroupPrefix + ":allopenshift"
53-
OpenshiftStatusGroupName = ResourceGroupPrefix + ":allopenshift-status"
51+
OpenshiftExposedGroupName = ResourceGroupPrefix + "exposedopenshift"
52+
OpenshiftAllGroupName = ResourceGroupPrefix + "allopenshift"
53+
OpenshiftStatusGroupName = ResourceGroupPrefix + "allopenshift-status"
5454

55-
QuotaGroupName = ResourceGroupPrefix + ":quota"
55+
QuotaGroupName = ResourceGroupPrefix + "quota"
5656
// KubeInternalsGroupName includes those resources that should reasonably be viewable to end users, but that most users should probably not modify. Kubernetes herself will maintain these resources
57-
KubeInternalsGroupName = ResourceGroupPrefix + ":privatekube"
57+
KubeInternalsGroupName = ResourceGroupPrefix + "privatekube"
5858
// KubeExposedGroupName includes resources that are commonly viewed and modified by end users of the system.
59-
KubeExposedGroupName = ResourceGroupPrefix + ":exposedkube"
60-
KubeAllGroupName = ResourceGroupPrefix + ":allkube"
61-
KubeStatusGroupName = ResourceGroupPrefix + ":allkube-status"
59+
KubeExposedGroupName = ResourceGroupPrefix + "exposedkube"
60+
KubeAllGroupName = ResourceGroupPrefix + "allkube"
61+
KubeStatusGroupName = ResourceGroupPrefix + "allkube-status"
6262

6363
// NonEscalatingResourcesGroupName contains all resources that can be viewed without exposing the risk of using view rights to locate a secret to escalate privileges. For example, view
6464
// rights on secrets could be used locate a secret that happened to be serviceaccount token that has more privileges
65-
NonEscalatingResourcesGroupName = ResourceGroupPrefix + ":non-escalating"
66-
KubeNonEscalatingViewableGroupName = ResourceGroupPrefix + ":kube-non-escalating"
67-
OpenshiftNonEscalatingViewableGroupName = ResourceGroupPrefix + ":openshift-non-escalating"
65+
NonEscalatingResourcesGroupName = ResourceGroupPrefix + "non-escalating"
66+
KubeNonEscalatingViewableGroupName = ResourceGroupPrefix + "kube-non-escalating"
67+
OpenshiftNonEscalatingViewableGroupName = ResourceGroupPrefix + "openshift-non-escalating"
6868

6969
// EscalatingResourcesGroupName contains all resources that can be used to escalate privileges when simply viewed
70-
EscalatingResourcesGroupName = ResourceGroupPrefix + ":escalating"
71-
KubeEscalatingViewableGroupName = ResourceGroupPrefix + ":kube-escalating"
72-
OpenshiftEscalatingViewableGroupName = ResourceGroupPrefix + ":openshift-escalating"
70+
EscalatingResourcesGroupName = ResourceGroupPrefix + "escalating"
71+
KubeEscalatingViewableGroupName = ResourceGroupPrefix + "kube-escalating"
72+
OpenshiftEscalatingViewableGroupName = ResourceGroupPrefix + "openshift-escalating"
7373
)
7474

7575
var (
@@ -107,11 +107,11 @@ var (
107107

108108
func init() {
109109
// set the non-escalating groups
110-
GroupsToResources[OpenshiftNonEscalatingViewableGroupName] = ExpandResources(sets.NewString(GroupsToResources[OpenshiftAllGroupName]...)).
111-
Difference(ExpandResources(sets.NewString(GroupsToResources[OpenshiftEscalatingViewableGroupName]...))).List()
110+
GroupsToResources[OpenshiftNonEscalatingViewableGroupName] = NormalizeResources(sets.NewString(GroupsToResources[OpenshiftAllGroupName]...)).
111+
Difference(NormalizeResources(sets.NewString(GroupsToResources[OpenshiftEscalatingViewableGroupName]...))).List()
112112

113-
GroupsToResources[KubeNonEscalatingViewableGroupName] = ExpandResources(sets.NewString(GroupsToResources[KubeAllGroupName]...)).
114-
Difference(ExpandResources(sets.NewString(GroupsToResources[KubeEscalatingViewableGroupName]...))).List()
113+
GroupsToResources[KubeNonEscalatingViewableGroupName] = NormalizeResources(sets.NewString(GroupsToResources[KubeAllGroupName]...)).
114+
Difference(NormalizeResources(sets.NewString(GroupsToResources[KubeEscalatingViewableGroupName]...))).List()
115115
}
116116

117117
// PolicyRule holds information that describes a policy rule, but does not contain information

‎pkg/authorization/authorizer/attributes.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (a DefaultAuthorizationAttributes) RuleMatches(rule authorizationapi.Policy
4545
if a.verbMatches(rule.Verbs) {
4646
if a.apiGroupMatches(rule.APIGroups) {
4747

48-
allowedResourceTypes := authorizationapi.ExpandResources(rule.Resources)
48+
allowedResourceTypes := authorizationapi.NormalizeResources(rule.Resources)
4949
if a.resourceMatches(allowedResourceTypes) {
5050
if a.nameMatches(rule.ResourceNames) {
5151
// this rule matches the request, so we should check the additional restrictions to be sure that it's allowed

‎pkg/authorization/rulevalidation/policy_comparator.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func breakdownRule(rule authorizationapi.PolicyRule) []authorizationapi.PolicyRu
6363
func breadownRuleForGroup(group string, rule authorizationapi.PolicyRule) []authorizationapi.PolicyRule {
6464
subrules := []authorizationapi.PolicyRule{}
6565

66-
for resource := range authorizationapi.ExpandResources(rule.Resources) {
66+
for resource := range authorizationapi.NormalizeResources(rule.Resources) {
6767
for verb := range rule.Verbs {
6868
if len(rule.ResourceNames) > 0 {
6969
for _, resourceName := range rule.ResourceNames.List() {
@@ -83,7 +83,7 @@ func breadownRuleForGroup(group string, rule authorizationapi.PolicyRule) []auth
8383
// ruleCovers determines whether the ownerRule (which may have multiple verbs, resources, and resourceNames) covers
8484
// the subrule (which may only contain at most one verb, resource, and resourceName)
8585
func ruleCovers(ownerRule, subrule authorizationapi.PolicyRule) bool {
86-
allResources := authorizationapi.ExpandResources(ownerRule.Resources)
86+
allResources := authorizationapi.NormalizeResources(ownerRule.Resources)
8787

8888
ownerGroups := sets.NewString(ownerRule.APIGroups...)
8989
groupMatches := ownerGroups.Has(authorizationapi.APIGroupAll) || ownerGroups.HasAll(subrule.APIGroups...) || (len(ownerRule.APIGroups) == 0 && len(subrule.APIGroups) == 0)

‎pkg/cmd/server/bootstrappolicy/policy.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func GetBootstrapOpenshiftRoles(openshiftNamespace string) []authorizationapi.Ro
3333
// our default roles and hard for them to reason about what power they are granting their users
3434
for i := range roles {
3535
for j := range roles[i].Rules {
36-
roles[i].Rules[j].Resources = authorizationapi.ExpandResources(roles[i].Rules[j].Resources)
36+
roles[i].Rules[j].Resources = authorizationapi.NormalizeResources(roles[i].Rules[j].Resources)
3737
}
3838
}
3939

@@ -756,7 +756,7 @@ func GetBootstrapClusterRoles() []authorizationapi.ClusterRole {
756756
// our default roles and hard for them to reason about what power they are granting their users
757757
for i := range roles {
758758
for j := range roles[i].Rules {
759-
roles[i].Rules[j].Resources = authorizationapi.ExpandResources(roles[i].Rules[j].Resources)
759+
roles[i].Rules[j].Resources = authorizationapi.NormalizeResources(roles[i].Rules[j].Resources)
760760
}
761761
}
762762

‎pkg/cmd/server/origin/reststorage_validation_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func TestValidationRegistration(t *testing.T) {
4949

5050
// TestAllOpenShiftResourceCoverage checks to make sure that the openshift all group actually contains all openshift resources
5151
func TestAllOpenShiftResourceCoverage(t *testing.T) {
52-
allOpenshift := authorizationapi.ExpandResources(sets.NewString(authorizationapi.GroupsToResources[authorizationapi.OpenshiftAllGroupName]...))
52+
allOpenshift := authorizationapi.NormalizeResources(sets.NewString(authorizationapi.GroupsToResources[authorizationapi.OpenshiftAllGroupName]...))
5353

5454
config := fakeMasterConfig()
5555

0 commit comments

Comments
 (0)
Please sign in to comment.