Skip to content

Commit e568cde

Browse files
authored
Unpack job security updates (#2805)
* Remove unneeded 'get' verb from bundle unpacker role Signed-off-by: perdasilva <[email protected]> * Move bundle unpacker policy rule creation to its own method Signed-off-by: perdasilva <[email protected]>
1 parent fd90173 commit e568cde

File tree

2 files changed

+42
-23
lines changed

2 files changed

+42
-23
lines changed

pkg/controller/bundle/bundle_unpacker.go

+40-21
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ func (c *ConfigMapUnpacker) UnpackBundle(lookup *operatorsv1alpha1.BundleLookup,
435435
return
436436
}
437437

438-
_, err = c.ensureRole(cmRef)
438+
_, err = c.ensureRole(cmRef, c.getRolePolicyRules(cmRef))
439439
if err != nil {
440440
return
441441
}
@@ -610,27 +610,13 @@ func (c *ConfigMapUnpacker) ensureJob(cmRef *corev1.ObjectReference, bundlePath
610610
return
611611
}
612612

613-
func (c *ConfigMapUnpacker) ensureRole(cmRef *corev1.ObjectReference) (role *rbacv1.Role, err error) {
613+
func (c *ConfigMapUnpacker) ensureRole(cmRef *corev1.ObjectReference, policyRules []rbacv1.PolicyRule) (role *rbacv1.Role, err error) {
614614
if cmRef == nil {
615615
return nil, fmt.Errorf("configmap reference is nil")
616616
}
617617

618-
rule := rbacv1.PolicyRule{
619-
APIGroups: []string{
620-
"",
621-
},
622-
Verbs: []string{
623-
"create", "get", "update",
624-
},
625-
Resources: []string{
626-
"configmaps",
627-
},
628-
ResourceNames: []string{
629-
cmRef.Name,
630-
},
631-
}
632618
fresh := &rbacv1.Role{
633-
Rules: []rbacv1.PolicyRule{rule},
619+
Rules: policyRules,
634620
}
635621
fresh.SetNamespace(cmRef.Namespace)
636622
fresh.SetName(cmRef.Name)
@@ -646,19 +632,43 @@ func (c *ConfigMapUnpacker) ensureRole(cmRef *corev1.ObjectReference) (role *rba
646632
}
647633

648634
// Add the policy rule if necessary
649-
for _, existing := range role.Rules {
650-
if equality.Semantic.DeepDerivative(rule, existing) {
651-
return
635+
var ruleDiff []rbacv1.PolicyRule
636+
for _, proposed := range policyRules {
637+
if !containsRule(role.Rules, proposed) {
638+
ruleDiff = append(ruleDiff, proposed)
652639
}
653640
}
641+
654642
role = role.DeepCopy()
655-
role.Rules = append(role.Rules, rule)
643+
role.Rules = append(role.Rules, ruleDiff...)
656644

657645
role, err = c.client.RbacV1().Roles(role.GetNamespace()).Update(context.TODO(), role, metav1.UpdateOptions{})
658646

659647
return
660648
}
661649

650+
// getRolePolicyRules returns the set of policy rules used by the role attached to the
651+
// bundle unpacker service account. This method lends itself to easier downstream patching when additional
652+
// policy rules are required, e.g. for Openshift SCC
653+
func (c *ConfigMapUnpacker) getRolePolicyRules(cmRef *corev1.ObjectReference) []rbacv1.PolicyRule {
654+
return []rbacv1.PolicyRule{
655+
{
656+
APIGroups: []string{
657+
"",
658+
},
659+
Verbs: []string{
660+
"get", "update",
661+
},
662+
Resources: []string{
663+
"configmaps",
664+
},
665+
ResourceNames: []string{
666+
cmRef.Name,
667+
},
668+
},
669+
}
670+
}
671+
662672
func (c *ConfigMapUnpacker) ensureRoleBinding(cmRef *corev1.ObjectReference) (roleBinding *rbacv1.RoleBinding, err error) {
663673
fresh := &rbacv1.RoleBinding{
664674
Subjects: []rbacv1.Subject{
@@ -738,3 +748,12 @@ func getCondition(job *batchv1.Job, conditionType batchv1.JobConditionType) (con
738748
}
739749
return
740750
}
751+
752+
func containsRule(rules []rbacv1.PolicyRule, rule rbacv1.PolicyRule) bool {
753+
for _, r := range rules {
754+
if equality.Semantic.DeepDerivative(r, rule) {
755+
return true
756+
}
757+
}
758+
return false
759+
}

pkg/controller/bundle/bundle_unpacker_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ func TestConfigMapUnpacker(t *testing.T) {
353353
"",
354354
},
355355
Verbs: []string{
356-
"create", "get", "update",
356+
"get", "update",
357357
},
358358
Resources: []string{
359359
"configmaps",
@@ -769,7 +769,7 @@ func TestConfigMapUnpacker(t *testing.T) {
769769
"",
770770
},
771771
Verbs: []string{
772-
"create", "get", "update",
772+
"get", "update",
773773
},
774774
Resources: []string{
775775
"configmaps",

0 commit comments

Comments
 (0)