-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: RBAC config when OwnerReferencesPermissionEnforcement admission …
…controller is enabled (#517) Resolves: #514 Signed-off-by: Daniel Pacak <[email protected]>
- Loading branch information
1 parent
977996e
commit d1cbe3a
Showing
14 changed files
with
206 additions
and
61 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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,82 @@ | ||
package kubebench | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/aquasecurity/starboard/pkg/apis/aquasecurity/v1alpha1" | ||
"github.com/aquasecurity/starboard/pkg/kube" | ||
"github.com/aquasecurity/starboard/pkg/starboard" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/utils/pointer" | ||
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" | ||
) | ||
|
||
type Builder interface { | ||
Controller(controller metav1.Object) Builder | ||
Data(data v1alpha1.CISKubeBenchOutput) Builder | ||
Get() (v1alpha1.CISKubeBenchReport, error) | ||
} | ||
|
||
func NewBuilder(scheme *runtime.Scheme) Builder { | ||
return &builder{ | ||
scheme: scheme, | ||
} | ||
} | ||
|
||
type builder struct { | ||
scheme *runtime.Scheme | ||
controller metav1.Object | ||
data v1alpha1.CISKubeBenchOutput | ||
} | ||
|
||
func (b *builder) Controller(controller metav1.Object) Builder { | ||
b.controller = controller | ||
return b | ||
} | ||
|
||
func (b *builder) Data(data v1alpha1.CISKubeBenchOutput) Builder { | ||
b.data = data | ||
return b | ||
} | ||
|
||
func (b *builder) reportName() string { | ||
return b.controller.GetName() | ||
} | ||
|
||
func (b *builder) Get() (v1alpha1.CISKubeBenchReport, error) { | ||
kind, err := kube.KindForObject(b.controller, b.scheme) | ||
if err != nil { | ||
return v1alpha1.CISKubeBenchReport{}, fmt.Errorf("getting kind for object: %w", err) | ||
} | ||
|
||
labels := map[string]string{ | ||
starboard.LabelResourceKind: kind, | ||
starboard.LabelResourceName: b.controller.GetName(), | ||
} | ||
|
||
reportName := b.reportName() | ||
|
||
report := v1alpha1.CISKubeBenchReport{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: reportName, | ||
Namespace: b.controller.GetNamespace(), | ||
Labels: labels, | ||
}, | ||
Report: b.data, | ||
} | ||
err = controllerutil.SetControllerReference(b.controller, &report, b.scheme) | ||
if err != nil { | ||
return v1alpha1.CISKubeBenchReport{}, fmt.Errorf("setting controller reference: %w", err) | ||
} | ||
// The OwnerReferencesPermissionsEnforcement admission controller protects the | ||
// access to metadata.ownerReferences[x].blockOwnerDeletion of an object, so | ||
// that only users with "update" permission to the finalizers subresource of the | ||
// referenced owner can change it. | ||
// We set metadata.ownerReferences[x].blockOwnerDeletion to false so that | ||
// additional RBAC permissions are not required when the OwnerReferencesPermissionsEnforcement | ||
// is enabled. | ||
// See https://kubernetes.io/docs/reference/access-authn-authz/admission-controllers/#ownerreferencespermissionenforcement | ||
report.OwnerReferences[0].BlockOwnerDeletion = pointer.BoolPtr(false) | ||
return report, nil | ||
} |
This file contains 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,47 @@ | ||
package kubebench_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/aquasecurity/starboard/pkg/apis/aquasecurity/v1alpha1" | ||
"github.com/aquasecurity/starboard/pkg/kubebench" | ||
"github.com/aquasecurity/starboard/pkg/starboard" | ||
"github.com/onsi/gomega" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes/scheme" | ||
"k8s.io/utils/pointer" | ||
) | ||
|
||
func TestBuilder(t *testing.T) { | ||
g := gomega.NewGomegaWithT(t) | ||
|
||
report, err := kubebench.NewBuilder(scheme.Scheme). | ||
Controller(&corev1.Node{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "control-plane", | ||
}, | ||
}). | ||
Data(v1alpha1.CISKubeBenchOutput{}).Get() | ||
|
||
g.Expect(err).ToNot(gomega.HaveOccurred()) | ||
g.Expect(report).To(gomega.Equal(v1alpha1.CISKubeBenchReport{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "control-plane", | ||
OwnerReferences: []metav1.OwnerReference{ | ||
{ | ||
APIVersion: "v1", | ||
Kind: "Node", | ||
Name: "control-plane", | ||
Controller: pointer.BoolPtr(true), | ||
BlockOwnerDeletion: pointer.BoolPtr(false), | ||
}, | ||
}, | ||
Labels: map[string]string{ | ||
starboard.LabelResourceKind: "Node", | ||
starboard.LabelResourceName: "control-plane", | ||
}, | ||
}, | ||
Report: v1alpha1.CISKubeBenchOutput{}, | ||
})) | ||
} |
This file contains 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 @@ | ||
package kubebench_test |
This file contains 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 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
Oops, something went wrong.