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

bugfix: rbac resource with capital letter name #1322

Merged
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
2 changes: 1 addition & 1 deletion pkg/configauditreport/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (b *ReportBuilder) reportName() string {
kind := b.controller.GetObjectKind().GroupVersionKind().Kind
name := b.controller.GetName()
reportName := fmt.Sprintf("%s-%s", strings.ToLower(kind), name)
if len(validation.IsValidLabelValue(reportName)) == 0 {
if len(validation.IsDNS1123Label(reportName)) == 0 {
return reportName
}
return fmt.Sprintf("%s-%s", strings.ToLower(kind), kube.ComputeHash(name))
Expand Down
48 changes: 48 additions & 0 deletions pkg/configauditreport/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,52 @@ func TestReportBuilder(t *testing.T) {
Report: v1alpha1.ConfigAuditReportData{},
}))
})

t.Run("Should build report with lowercase name", func(t *testing.T) {
g := NewGomegaWithT(t)

report, err := configauditreport.NewReportBuilder(scheme.Scheme).
Controller(&rbacv1.Role{
TypeMeta: metav1.TypeMeta{
Kind: "Role",
APIVersion: "rbac.authorization.k8s.io/v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "pod-Reader",
Labels: labels.Set{"tier": "tier-1", "owner": "team-a"},
Namespace: "test",
},
}).
ResourceSpecHash("xyz").
PluginConfigHash("nop").
Data(v1alpha1.ConfigAuditReportData{}).
ResourceLabelsToInclude([]string{"tier"}).
GetReport()

g.Expect(err).ToNot(HaveOccurred())
g.Expect(report).To(Equal(v1alpha1.ConfigAuditReport{
ObjectMeta: metav1.ObjectMeta{
Name: "role-65c67c5c64",
Namespace: "test",
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: "rbac.authorization.k8s.io/v1",
Kind: "Role",
Name: "pod-Reader",
Controller: pointer.Bool(true),
BlockOwnerDeletion: pointer.Bool(false),
},
},
Labels: map[string]string{
trivyoperator.LabelPluginConfigHash: "nop",
trivyoperator.LabelResourceKind: "Role",
trivyoperator.LabelResourceNamespace: "test",
trivyoperator.LabelResourceName: "pod-Reader",
"tier": "tier-1",
trivyoperator.LabelResourceSpecHash: "xyz",
},
},
Report: v1alpha1.ConfigAuditReportData{},
}))
})
}
2 changes: 1 addition & 1 deletion pkg/rbacassessment/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (b *ReportBuilder) reportName() string {
kind := b.controller.GetObjectKind().GroupVersionKind().Kind
name := b.controller.GetName()
reportName := fmt.Sprintf("%s-%s", strings.ToLower(kind), name)
if len(validation.IsValidLabelValue(reportName)) == 0 {
if len(validation.IsDNS1123Label(reportName)) == 0 {
return reportName
}
return fmt.Sprintf("%s-%s", strings.ToLower(kind), kube.ComputeHash(name))
Expand Down
49 changes: 49 additions & 0 deletions pkg/rbacassessment/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,55 @@ func TestReportBuilder(t *testing.T) {
g.Expect(report).To(Equal(assessmentReport))
})

t.Run("Should build report for namespaced resource with capital letter", func(t *testing.T) {
g := NewGomegaWithT(t)

report, err := rbacassessment.NewReportBuilder(scheme.Scheme).
Controller(&rbacv1.Role{
TypeMeta: metav1.TypeMeta{
Kind: "Role",
APIVersion: "rbac.authorization.k8s.io/v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: "some-Owner",
Namespace: "qa",
Labels: labels.Set{"tier": "tier-1", "owner": "team-a"},
},
Rules: []rbacv1.PolicyRule{},
}).
ResourceSpecHash("xyz").
PluginConfigHash("nop").
Data(v1alpha1.RbacAssessmentReportData{}).
ResourceLabelsToInclude([]string{"tier"}).
GetReport()
g.Expect(err).ToNot(HaveOccurred())
assessmentReport := v1alpha1.RbacAssessmentReport{
ObjectMeta: metav1.ObjectMeta{
Name: "role-5ccc5d4cff",
Namespace: "qa",
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: "rbac.authorization.k8s.io/v1",
Kind: "Role",
Name: "some-Owner",
Controller: pointer.Bool(true),
BlockOwnerDeletion: pointer.Bool(false),
},
},
Labels: map[string]string{
trivyoperator.LabelResourceKind: "Role",
trivyoperator.LabelResourceName: "some-Owner",
trivyoperator.LabelResourceNamespace: "qa",
trivyoperator.LabelResourceSpecHash: "xyz",
trivyoperator.LabelPluginConfigHash: "nop",
"tier": "tier-1",
},
},
Report: v1alpha1.RbacAssessmentReportData{},
}
g.Expect(report).To(Equal(assessmentReport))
})

t.Run("Should build report for cluster scoped resource", func(t *testing.T) {
g := NewGomegaWithT(t)

Expand Down