-
Notifications
You must be signed in to change notification settings - Fork 1.8k
[v0.19.x] generate: consider service accounts when generating a CSV (#3610) #3714
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
estroz
merged 2 commits into
operator-framework:v0.19.x
from
estroz:cherrypick/3610-v0.19.x
Aug 17, 2020
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| entries: | ||
| - description: > | ||
| Fixed incorrect (cluster) role name assignments in generated CSVs | ||
| [#3600](https://github.com/operator-framework/operator-sdk/issues/3600). | ||
| kind: bugfix | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,220 @@ | ||
| // Copyright 2020 The Operator-SDK Authors | ||
| // | ||
| // 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 collector | ||
|
|
||
| import ( | ||
| rbacv1 "k8s.io/api/rbac/v1" | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
| ) | ||
|
|
||
| // TODO(estroz): there's a significant amount of code dupliation here, a byproduct of Go's type system. | ||
| // However at least a few bits can be refactored so each method is smaller. | ||
|
|
||
| const ( | ||
| // This service account exists in every namespace as the default. | ||
| defaultServiceAccountName = "default" | ||
| ) | ||
|
|
||
| // SplitCSVPermissionsObjects splits roles that should be written to a CSV as permissions (in) | ||
| // from roles and role bindings that should be written directly to the bundle (out). | ||
| func (c *Manifests) SplitCSVPermissionsObjects() (in, out []runtime.Object) { //nolint:dupl | ||
| roleMap := make(map[string]*rbacv1.Role) | ||
| for i := range c.Roles { | ||
| roleMap[c.Roles[i].GetName()] = &c.Roles[i] | ||
| } | ||
| roleBindingMap := make(map[string]*rbacv1.RoleBinding) | ||
| for i := range c.RoleBindings { | ||
| roleBindingMap[c.RoleBindings[i].GetName()] = &c.RoleBindings[i] | ||
| } | ||
|
|
||
| // Check for unbound roles. | ||
| for roleName, role := range roleMap { | ||
| hasRef := false | ||
| for _, roleBinding := range roleBindingMap { | ||
| roleRef := roleBinding.RoleRef | ||
| if roleRef.Kind == "Role" && (roleRef.APIGroup == "" || roleRef.APIGroup == rbacv1.SchemeGroupVersion.Group) { | ||
| if roleRef.Name == roleName { | ||
| hasRef = true | ||
| break | ||
| } | ||
| } | ||
| } | ||
| if !hasRef { | ||
| out = append(out, role) | ||
| delete(roleMap, roleName) | ||
| } | ||
| } | ||
|
|
||
| // If a role is bound and: | ||
| // 1. the binding only has one subject and it is a service account that maps to a deployment service account, | ||
| // add the role to in. | ||
| // 2. the binding only has one subject and it does not map to a deployment service account or is not a service account, | ||
| // add both role and binding to out. | ||
| // 3. the binding has more than one subject and: | ||
| // a. one of those subjects is a deployment's service account, add both role and binding to out and role to in. | ||
| // b. none of those subjects is a service account or maps to a deployment's service account, add both role and binding to out. | ||
| deploymentSANames := make(map[string]struct{}) | ||
| for _, dep := range c.Deployments { | ||
| saName := dep.Spec.Template.Spec.ServiceAccountName | ||
| if saName == "" { | ||
| saName = defaultServiceAccountName | ||
| } | ||
| deploymentSANames[saName] = struct{}{} | ||
| } | ||
|
|
||
| inRoleNames := make(map[string]struct{}) | ||
| outRoleNames := make(map[string]struct{}) | ||
| outRoleBindingNames := make(map[string]struct{}) | ||
| for _, binding := range c.RoleBindings { | ||
| roleRef := binding.RoleRef | ||
| if roleRef.Kind == roleKind && (roleRef.APIGroup == "" || roleRef.APIGroup == rbacv1.SchemeGroupVersion.Group) { | ||
| numSubjects := len(binding.Subjects) | ||
| if numSubjects == 1 { | ||
| // cases (1) and (2). | ||
| if _, hasSA := deploymentSANames[binding.Subjects[0].Name]; hasSA && binding.Subjects[0].Kind == serviceAccountKind { | ||
| inRoleNames[roleRef.Name] = struct{}{} | ||
| } else { | ||
| outRoleNames[roleRef.Name] = struct{}{} | ||
| outRoleBindingNames[binding.GetName()] = struct{}{} | ||
| } | ||
| } else { | ||
| // case (3). | ||
| for _, subject := range binding.Subjects { | ||
| if _, hasSA := deploymentSANames[subject.Name]; hasSA && subject.Kind == serviceAccountKind { | ||
| // case (3a). | ||
| inRoleNames[roleRef.Name] = struct{}{} | ||
| } | ||
| } | ||
| // case (3b). | ||
| outRoleNames[roleRef.Name] = struct{}{} | ||
| outRoleBindingNames[binding.GetName()] = struct{}{} | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for roleName := range inRoleNames { | ||
| if role, hasRoleName := roleMap[roleName]; hasRoleName { | ||
| in = append(in, role) | ||
| } | ||
| } | ||
| for roleName := range outRoleNames { | ||
| if role, hasRoleName := roleMap[roleName]; hasRoleName { | ||
| out = append(out, role) | ||
| } | ||
| } | ||
| for roleBindingName := range outRoleBindingNames { | ||
| if roleBinding, hasRoleBindingName := roleBindingMap[roleBindingName]; hasRoleBindingName { | ||
| out = append(out, roleBinding) | ||
| } | ||
| } | ||
|
|
||
| return in, out | ||
| } | ||
|
|
||
| // SplitCSVClusterPermissionsObjects splits cluster roles that should be written to a CSV as clusterPermissions (in) | ||
| // from cluster roles and cluster role bindings that should be written directly to the bundle (out). | ||
| func (c *Manifests) SplitCSVClusterPermissionsObjects() (in, out []runtime.Object) { //nolint:dupl | ||
| roleMap := make(map[string]*rbacv1.ClusterRole) | ||
| for i := range c.ClusterRoles { | ||
| roleMap[c.ClusterRoles[i].GetName()] = &c.ClusterRoles[i] | ||
| } | ||
| roleBindingMap := make(map[string]*rbacv1.ClusterRoleBinding) | ||
| for i := range c.ClusterRoleBindings { | ||
| roleBindingMap[c.ClusterRoleBindings[i].GetName()] = &c.ClusterRoleBindings[i] | ||
| } | ||
|
|
||
| // Check for unbound roles. | ||
| for roleName, role := range roleMap { | ||
| hasRef := false | ||
| for _, roleBinding := range roleBindingMap { | ||
| roleRef := roleBinding.RoleRef | ||
| if roleRef.Kind == clusterRoleKind && (roleRef.APIGroup == "" || roleRef.APIGroup == rbacv1.SchemeGroupVersion.Group) { | ||
| if roleRef.Name == roleName { | ||
| hasRef = true | ||
| break | ||
| } | ||
| } | ||
| } | ||
| if !hasRef { | ||
| out = append(out, role) | ||
| delete(roleMap, roleName) | ||
| } | ||
| } | ||
|
|
||
| // If a role is bound and: | ||
| // 1. the binding only has one subject and it is a service account that maps to a deployment service account, | ||
| // add the role to in. | ||
| // 2. the binding only has one subject and it does not map to a deployment service account or is not a service account, | ||
| // add both role and binding to out. | ||
| // 3. the binding has more than one subject and: | ||
| // a. one of those subjects is a deployment's service account, add both role and binding to out and role to in. | ||
| // b. none of those subjects is a service account or maps to a deployment's service account, add both role and binding to out. | ||
| deploymentSANames := make(map[string]struct{}) | ||
| for _, dep := range c.Deployments { | ||
| saName := dep.Spec.Template.Spec.ServiceAccountName | ||
| if saName == "" { | ||
| saName = defaultServiceAccountName | ||
| } | ||
| deploymentSANames[saName] = struct{}{} | ||
| } | ||
|
|
||
| inRoleNames := make(map[string]struct{}) | ||
| outRoleNames := make(map[string]struct{}) | ||
| outRoleBindingNames := make(map[string]struct{}) | ||
| for _, binding := range c.ClusterRoleBindings { | ||
| roleRef := binding.RoleRef | ||
| if roleRef.Kind == "ClusterRole" && (roleRef.APIGroup == "" || roleRef.APIGroup == rbacv1.SchemeGroupVersion.Group) { | ||
| numSubjects := len(binding.Subjects) | ||
| if numSubjects == 1 { | ||
| // cases (1) and (2). | ||
| if _, hasSA := deploymentSANames[binding.Subjects[0].Name]; hasSA && binding.Subjects[0].Kind == serviceAccountKind { | ||
| inRoleNames[roleRef.Name] = struct{}{} | ||
| } else { | ||
| outRoleNames[roleRef.Name] = struct{}{} | ||
| outRoleBindingNames[binding.GetName()] = struct{}{} | ||
| } | ||
| } else { | ||
| // case (3). | ||
| for _, subject := range binding.Subjects { | ||
| if _, hasSA := deploymentSANames[subject.Name]; hasSA && subject.Kind == serviceAccountKind { | ||
| // case (3a). | ||
| inRoleNames[roleRef.Name] = struct{}{} | ||
| } | ||
| } | ||
| // case (3b). | ||
| outRoleNames[roleRef.Name] = struct{}{} | ||
| outRoleBindingNames[binding.GetName()] = struct{}{} | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for roleName := range inRoleNames { | ||
| if role, hasRoleName := roleMap[roleName]; hasRoleName { | ||
| in = append(in, role) | ||
| } | ||
| } | ||
| for roleName := range outRoleNames { | ||
| if role, hasRoleName := roleMap[roleName]; hasRoleName { | ||
| out = append(out, role) | ||
| } | ||
| } | ||
| for roleBindingName := range outRoleBindingNames { | ||
| if roleBinding, hasRoleBindingName := roleBindingMap[roleBindingName]; hasRoleBindingName { | ||
| out = append(out, roleBinding) | ||
| } | ||
| } | ||
|
|
||
| return in, out | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do not add the number of the issue. We just add the link of the PR. I think it can bring confusing since it is not the standard.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its helpful to link the issue. In fact I was thinking about making a modification to the changelog generator to add an optional
issue_linkfield. Perhaps some wrapping words to make what this link is obvious.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we just add #number users will expect it to be the PR. Also, in this case, it will probably require manual changes in the release since the changelog is gen automatically.
I am ok with we start to add the number the issue if it is identified as the gen changelog be changed to accommodate this new info.