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

Add Support for Selinux Profile Binding #854

Merged
merged 1 commit into from
Apr 6, 2022
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
3 changes: 2 additions & 1 deletion api/profilebinding/v1alpha1/profilebinding_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type ProfileBindingKind string

const (
ProfileBindingKindSeccompProfile ProfileBindingKind = "SeccompProfile"
ProfileBindingKindSelinuxProfile ProfileBindingKind = "SelinuxProfile"
)

// ProfileBindingSpec defines the desired state of ProfileBinding.
Expand All @@ -37,7 +38,7 @@ type ProfileBindingSpec struct {
// ProfileRef contains information that points to the profile being used.
type ProfileRef struct {
// Kind of object to be bound.
// +kubebuilder:validation:Enum=SeccompProfile
// +kubebuilder:validation:Enum=SeccompProfile;SelinuxProfile
Kind ProfileBindingKind `json:"kind"` // TODO(cmurphy) add SelinuxProfile
// Name of the profile within the current namespace to which to bind the selected pods.
Name string `json:"name"`
Expand Down
3 changes: 2 additions & 1 deletion api/selinuxprofile/v1alpha2/selinuxprofile_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ type SelinuxProfileStatus struct {
profilebasev1alpha1.StatusBase `json:",inline"`
// Represents the string that the SelinuxProfile object can be
// referenced as in a pod seLinuxOptions section.
Usage string `json:"usage,omitempty"`
Usage string `json:"usage,omitempty"`
ActiveWorkloads []string `json:"activeWorkloads,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
Expand Down
5 changes: 5 additions & 0 deletions api/selinuxprofile/v1alpha2/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ spec:
description: Kind of object to be bound.
enum:
- SeccompProfile
- SelinuxProfile
type: string
name:
description: Name of the profile within the current namespace
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ spec:
status:
description: SelinuxProfileStatus defines the observed state of SelinuxProfile.
properties:
activeWorkloads:
items:
type: string
type: array
conditions:
description: Conditions of the resource.
items:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ spec:
status:
description: SelinuxProfileStatus defines the observed state of SelinuxProfile.
properties:
activeWorkloads:
items:
type: string
type: array
conditions:
description: Conditions of the resource.
items:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,11 @@ rules:
- get
- list
- watch
- apiGroups:
- security-profiles-operator.x-k8s.io
resources:
- selinuxprofiles
verbs:
- get
- list
- watch
3 changes: 3 additions & 0 deletions cmd/security-profiles-operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,9 @@ func runWebhook(ctx *cli.Context, info *version.Info) error {
if err := seccompprofileapi.AddToScheme(mgr.GetScheme()); err != nil {
return fmt.Errorf("add seccompprofile API to scheme: %w", err)
}
if err := selxv1alpha2.AddToScheme(mgr.GetScheme()); err != nil {
return fmt.Errorf("add selinuxprofile API to scheme: %w", err)
}
if err := profilerecording1alpha1.AddToScheme(mgr.GetScheme()); err != nil {
return fmt.Errorf("add profilerecording API to scheme: %w", err)
}
Expand Down
4 changes: 4 additions & 0 deletions deploy/base/crd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,10 @@ spec:
status:
description: SelinuxProfileStatus defines the observed state of SelinuxProfile.
properties:
activeWorkloads:
items:
type: string
type: array
conditions:
description: Conditions of the resource.
items:
Expand Down
1 change: 1 addition & 0 deletions deploy/base/crds/profilebinding.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ spec:
description: Kind of object to be bound.
enum:
- SeccompProfile
- SelinuxProfile
type: string
name:
description: Name of the profile within the current namespace
Expand Down
8 changes: 8 additions & 0 deletions deploy/base/crds/selinuxpolicy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ spec:
status:
description: SelinuxProfileStatus defines the observed state of SelinuxProfile.
properties:
activeWorkloads:
items:
type: string
type: array
conditions:
description: Conditions of the resource.
items:
Expand Down Expand Up @@ -184,6 +188,10 @@ spec:
status:
description: SelinuxProfileStatus defines the observed state of SelinuxProfile.
properties:
activeWorkloads:
items:
type: string
type: array
conditions:
description: Conditions of the resource.
items:
Expand Down
8 changes: 8 additions & 0 deletions deploy/base/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,14 @@ rules:
- get
- list
- watch
- apiGroups:
- security-profiles-operator.x-k8s.io
resources:
- selinuxprofiles
verbs:
- get
- list
- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
Expand Down
17 changes: 17 additions & 0 deletions deploy/namespace-operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ spec:
description: Kind of object to be bound.
enum:
- SeccompProfile
- SelinuxProfile
type: string
name:
description: Name of the profile within the current namespace
Expand Down Expand Up @@ -776,6 +777,10 @@ spec:
status:
description: SelinuxProfileStatus defines the observed state of SelinuxProfile.
properties:
activeWorkloads:
items:
type: string
type: array
conditions:
description: Conditions of the resource.
items:
Expand Down Expand Up @@ -915,6 +920,10 @@ spec:
status:
description: SelinuxProfileStatus defines the observed state of SelinuxProfile.
properties:
activeWorkloads:
items:
type: string
type: array
conditions:
description: Conditions of the resource.
items:
Expand Down Expand Up @@ -1617,6 +1626,14 @@ rules:
- get
- list
- watch
- apiGroups:
- security-profiles-operator.x-k8s.io
resources:
- selinuxprofiles
verbs:
- get
- list
- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
Expand Down
17 changes: 17 additions & 0 deletions deploy/openshift-dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ spec:
description: Kind of object to be bound.
enum:
- SeccompProfile
- SelinuxProfile
type: string
name:
description: Name of the profile within the current namespace
Expand Down Expand Up @@ -776,6 +777,10 @@ spec:
status:
description: SelinuxProfileStatus defines the observed state of SelinuxProfile.
properties:
activeWorkloads:
items:
type: string
type: array
conditions:
description: Conditions of the resource.
items:
Expand Down Expand Up @@ -915,6 +920,10 @@ spec:
status:
description: SelinuxProfileStatus defines the observed state of SelinuxProfile.
properties:
activeWorkloads:
items:
type: string
type: array
conditions:
description: Conditions of the resource.
items:
Expand Down Expand Up @@ -1617,6 +1626,14 @@ rules:
- get
- list
- watch
- apiGroups:
- security-profiles-operator.x-k8s.io
resources:
- selinuxprofiles
verbs:
- get
- list
- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
Expand Down
17 changes: 17 additions & 0 deletions deploy/operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ spec:
description: Kind of object to be bound.
enum:
- SeccompProfile
- SelinuxProfile
type: string
name:
description: Name of the profile within the current namespace
Expand Down Expand Up @@ -776,6 +777,10 @@ spec:
status:
description: SelinuxProfileStatus defines the observed state of SelinuxProfile.
properties:
activeWorkloads:
items:
type: string
type: array
conditions:
description: Conditions of the resource.
items:
Expand Down Expand Up @@ -915,6 +920,10 @@ spec:
status:
description: SelinuxProfileStatus defines the observed state of SelinuxProfile.
properties:
activeWorkloads:
items:
type: string
type: array
conditions:
description: Conditions of the resource.
items:
Expand Down Expand Up @@ -1617,6 +1626,14 @@ rules:
- get
- list
- watch
- apiGroups:
- security-profiles-operator.x-k8s.io
resources:
- selinuxprofiles
verbs:
- get
- list
- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
Expand Down
40 changes: 39 additions & 1 deletion internal/pkg/manager/workloadannotator/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

seccompprofileapi "sigs.k8s.io/security-profiles-operator/api/seccompprofile/v1beta1"
selinuxprofileapi "sigs.k8s.io/security-profiles-operator/api/selinuxprofile/v1alpha2"
"sigs.k8s.io/security-profiles-operator/internal/pkg/daemon/metrics"
)

Expand All @@ -53,23 +54,47 @@ func (r *PodReconciler) Setup(
return fmt.Errorf("creating pod index: %w", err)
}

// Index Pods using selinux profiles
if err := mgr.GetFieldIndexer().IndexField(ctx, &corev1.Pod{}, seOwnerKey, func(rawObj client.Object) []string {
pod, ok := rawObj.(*corev1.Pod)
if !ok {
return []string{}
}
return getSelinuxProfilesFromPod(r, pod)
}); err != nil {
return fmt.Errorf("creating pod index: %w", err)
}

// Index SeccompProfiles with active pods
if err := mgr.GetFieldIndexer().IndexField(
ctx, &seccompprofileapi.SeccompProfile{}, linkedPodsKey, func(rawObj client.Object) []string {
sp, ok := rawObj.(*seccompprofileapi.SeccompProfile)
if !ok {
return []string{}
}

return sp.Status.ActiveWorkloads
}); err != nil {
return fmt.Errorf("creating seccomp profile index: %w", err)
}

// Index SelinuxProfile with active pods
if err := mgr.GetFieldIndexer().IndexField(
ctx, &selinuxprofileapi.SelinuxProfile{}, linkedPodsKey, func(rawObj client.Object) []string {
sp, ok := rawObj.(*selinuxprofileapi.SelinuxProfile)
if !ok {
return []string{}
}
return sp.Status.ActiveWorkloads
}); err != nil {
return fmt.Errorf("creating selinux profile index: %w", err)
}

// Register a special reconciler for pod events
return ctrl.NewControllerManagedBy(mgr).
Named(name).
For(&corev1.Pod{}).
WithEventFilter(resource.NewPredicates(hasSeccompProfile)).
WithEventFilter(resource.NewPredicates(r.hasValidProfile)).
Complete(r)
}

Expand All @@ -81,3 +106,16 @@ func hasSeccompProfile(obj runtime.Object) bool {

return len(getSeccompProfilesFromPod(pod)) > 0
}

func hasSelinuxProfile(r *PodReconciler, obj runtime.Object) bool {
pod, ok := obj.(*corev1.Pod)
if !ok {
return false
}

return len(getSelinuxProfilesFromPod(r, pod)) > 0
}

func (r *PodReconciler) hasValidProfile(obj runtime.Object) bool {
return hasSeccompProfile(obj) || hasSelinuxProfile(r, obj)
}
Loading