Skip to content
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

diagnostics: AggregatedLogging ClusterRoleBindings false negative fix #18641

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"k8s.io/kubernetes/pkg/apis/rbac"
)

const clusterReaderRoleBindingName = "cluster-readers"
const clusterReaderRoleBindingRoleName = "cluster-reader"

var clusterReaderRoleBindingNames = sets.NewString(fluentdServiceAccountName)

Expand All @@ -22,15 +22,20 @@ the following:

func checkClusterRoleBindings(r diagnosticReporter, adapter clusterRoleBindingsAdapter, project string) {
r.Debug("AGL0600", "Checking ClusterRoleBindings...")
crb, err := adapter.getClusterRoleBinding(clusterReaderRoleBindingName)
crbs, err := adapter.listClusterRoleBindings()
if err != nil {
r.Error("AGL0605", err, fmt.Sprintf("There was an error while trying to retrieve the ClusterRoleBindings for the logging stack: %s", err))
return
}
boundServiceAccounts := sets.NewString()
for _, subject := range crb.Subjects {
if subject.Kind == rbac.ServiceAccountKind && subject.Namespace == project {
boundServiceAccounts.Insert(subject.Name)
for _, crb := range crbs.Items {
if crb.RoleRef.Name != clusterReaderRoleBindingRoleName {
continue
}
for _, subject := range crb.Subjects {
if subject.Kind == rbac.ServiceAccountKind && subject.Namespace == project {
boundServiceAccounts.Insert(subject.Name)
}
}
}
for _, name := range clusterReaderRoleBindingNames.List() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,36 @@ import (

type fakeRoleBindingDiagnostic struct {
fakeDiagnostic
fakeClusterRoleBinding authapi.ClusterRoleBinding
fakeClusterRoleBindingList authapi.ClusterRoleBindingList
}

func newFakeRoleBindingDiagnostic(t *testing.T) *fakeRoleBindingDiagnostic {
return &fakeRoleBindingDiagnostic{
fakeDiagnostic: *newFakeDiagnostic(t),
fakeDiagnostic: *newFakeDiagnostic(t),
fakeClusterRoleBindingList: authapi.ClusterRoleBindingList{},
}
}

func (f *fakeRoleBindingDiagnostic) getClusterRoleBinding(name string) (*authapi.ClusterRoleBinding, error) {
func (f *fakeRoleBindingDiagnostic) listClusterRoleBindings() (*authapi.ClusterRoleBindingList, error) {
if f.err != nil {
return nil, f.err
}
return &f.fakeClusterRoleBinding, nil
return &f.fakeClusterRoleBindingList, nil
}
func (f *fakeRoleBindingDiagnostic) addBinding(name string, namespace string) {
ref := kapi.ObjectReference{
Name: name,
Kind: rbac.ServiceAccountKind,
Namespace: namespace,
}
f.fakeClusterRoleBinding.Subjects = append(f.fakeClusterRoleBinding.Subjects, ref)
if len(f.fakeClusterRoleBindingList.Items) == 0 {
f.fakeClusterRoleBindingList.Items = append(f.fakeClusterRoleBindingList.Items, authapi.ClusterRoleBinding{
RoleRef: kapi.ObjectReference{
Name: clusterReaderRoleBindingRoleName,
},
})
}
f.fakeClusterRoleBindingList.Items[0].Subjects = append(f.fakeClusterRoleBindingList.Items[0].Subjects, ref)
}

// Test error when client error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ func (d *AggregatedLogging) getScc(name string) (*securityapi.SecurityContextCon
return d.SCCClient.SecurityContextConstraints().Get(name, metav1.GetOptions{})
}

func (d *AggregatedLogging) getClusterRoleBinding(name string) (*authapi.ClusterRoleBinding, error) {
return d.CRBClient.ClusterRoleBindings().Get(name, metav1.GetOptions{})
func (d *AggregatedLogging) listClusterRoleBindings() (*authapi.ClusterRoleBindingList, error) {
return d.CRBClient.ClusterRoleBindings().List(metav1.ListOptions{})
}

func (d *AggregatedLogging) routes(project string, options metav1.ListOptions) (*routesapi.RouteList, error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type sccAdapter interface {
}

type clusterRoleBindingsAdapter interface {
getClusterRoleBinding(name string) (*authapi.ClusterRoleBinding, error)
listClusterRoleBindings() (*authapi.ClusterRoleBindingList, error)
}

//deploymentConfigAdapter is an abstraction to retrieve resource for validating dcs
Expand Down