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

feat: Support dynamically removing the BPF enforcer from policies (WIP) #100

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions internal/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,15 @@ func (agent *Agent) handleCreateOrUpdateArmorProfile(ap *varmor.ArmorProfile, ke
logger.Error(err, "SaveAndApplyBpfProfile()")
return agent.sendStatus(ap, varmortypes.Failed, "SaveBpfProfile(): "+err.Error())
}
} else {
// Unload BPF profile.
if agent.bpfLsmSupported && agent.bpfEnforcer.IsBpfProfileExist(ap.Spec.Profile.Name) {
logger.Info(fmt.Sprintf("unloading the BPF profile ('%s')", ap.Spec.Profile.Name))
err := agent.bpfEnforcer.DeleteBpfProfile(ap.Spec.Profile.Name)
if err != nil {
logger.Error(err, "DeleteBpfProfile()")
}
}
}

// Seccomp
Expand Down
4 changes: 2 additions & 2 deletions internal/policy/clusterpolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,12 +394,12 @@ func (c *ClusterPolicyController) ignoreUpdate(newVp *varmor.VarmorClusterPolicy
// Disallow shutting down the enforcer that has been activated.
newEnforcers := varmortypes.GetEnforcerType(newVp.Spec.Policy.Enforcer)
oldEnforcers := varmortypes.GetEnforcerType(oldAp.Spec.Profile.Enforcer)
if newEnforcers&oldEnforcers != oldEnforcers {
if (newEnforcers&oldEnforcers != oldEnforcers) && (newEnforcers|varmortypes.BPF != oldEnforcers) {
err := fmt.Errorf("disallow shutting down the enforcer that has been activated")
logger.Error(err, "update VarmorClusterPolicy/status with forbidden info")
err = c.updateVarmorClusterPolicyStatus(newVp, "", false, varmortypes.VarmorPolicyUnchanged, varmortypes.VarmorPolicyUpdated, apicorev1.ConditionFalse,
"Forbidden",
"Modifying a policy to remove an already-set enforcer is not allowed. To remove enforcers, you must recreate the VarmorClusterPolicy object.")
"Modifying a policy to remove an already-set enforcer, except for the BPF enforcer, is not allowed. To remove enforcers, you must recreate the VarmorClusterPolicy object.")
return true, err
}

Expand Down
4 changes: 2 additions & 2 deletions internal/policy/policy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,12 +399,12 @@ func (c *PolicyController) ignoreUpdate(newVp *varmor.VarmorPolicy, oldAp *varmo
// Disallow shutting down the enforcer that has been activated.
newEnforcers := varmortypes.GetEnforcerType(newVp.Spec.Policy.Enforcer)
oldEnforcers := varmortypes.GetEnforcerType(oldAp.Spec.Profile.Enforcer)
if newEnforcers&oldEnforcers != oldEnforcers {
if (newEnforcers&oldEnforcers != oldEnforcers) && (newEnforcers|varmortypes.BPF != oldEnforcers) {
err := fmt.Errorf("disallow shutting down the enforcer that has been activated")
logger.Error(err, "update VarmorPolicy/status with forbidden info")
err = c.updateVarmorPolicyStatus(newVp, "", false, varmortypes.VarmorPolicyUnchanged, varmortypes.VarmorPolicyUpdated, apicorev1.ConditionFalse,
"Forbidden",
"Modifying a policy to remove an already-set enforcer is not allowed. To remove enforcers, you must recreate the VarmorPolicy object.")
"Modifying a policy to remove an already-set enforcer, except for the BPF enforcer, is not allowed. To remove enforcers, you must recreate the VarmorPolicy object.")
return true, err
}

Expand Down
109 changes: 49 additions & 60 deletions internal/policy/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,26 @@ func modifyDeploymentAnnotationsAndEnv(enforcer string, mode varmor.VarmorPolicy
// Clean up first
for key, value := range deploy.Spec.Template.Annotations {
// BPF
if (e & varmortypes.BPF) != 0 {
if strings.HasPrefix(key, "container.bpf.security.beta.varmor.org/") && value != "unconfined" {
delete(deploy.Spec.Template.Annotations, key)
}
if strings.HasPrefix(key, "container.bpf.security.beta.varmor.org/") && value != "unconfined" {
delete(deploy.Spec.Template.Annotations, key)
}

// AppArmor
if (e & varmortypes.AppArmor) != 0 {
if strings.HasPrefix(key, "container.apparmor.security.beta.kubernetes.io/") && value != "unconfined" {
delete(deploy.Spec.Template.Annotations, key)
}
if strings.HasPrefix(key, "container.apparmor.security.beta.kubernetes.io/") && value != "unconfined" {
delete(deploy.Spec.Template.Annotations, key)
}

// Seccomp
if (e & varmortypes.Seccomp) != 0 {
if strings.HasPrefix(key, "container.seccomp.security.beta.varmor.org/") && value != "unconfined" {
delete(deploy.Spec.Template.Annotations, key)
parts := strings.Split(key, "/")
if len(parts) != 2 {
continue
}
// Clean up the seccomp settings from the SecurityContext
for index, container := range deploy.Spec.Template.Spec.Containers {
if container.Name == parts[1] {
deploy.Spec.Template.Spec.Containers[index].SecurityContext.SeccompProfile = nil
}
if strings.HasPrefix(key, "container.seccomp.security.beta.varmor.org/") && value != "unconfined" {
delete(deploy.Spec.Template.Annotations, key)
parts := strings.Split(key, "/")
if len(parts) != 2 {
continue
}
// Clean up the seccomp settings from the SecurityContext
for index, container := range deploy.Spec.Template.Spec.Containers {
if container.Name == parts[1] {
deploy.Spec.Template.Spec.Containers[index].SecurityContext.SeccompProfile = nil
}
}
}
Expand Down Expand Up @@ -142,30 +138,26 @@ func modifyStatefulSetAnnotationsAndEnv(enforcer string, mode varmor.VarmorPolic
// Clean up first
for key, value := range stateful.Spec.Template.Annotations {
// BPF
if (e & varmortypes.BPF) != 0 {
if strings.HasPrefix(key, "container.bpf.security.beta.varmor.org/") && value != "unconfined" {
delete(stateful.Spec.Template.Annotations, key)
}
if strings.HasPrefix(key, "container.bpf.security.beta.varmor.org/") && value != "unconfined" {
delete(stateful.Spec.Template.Annotations, key)
}

// AppArmor
if (e & varmortypes.AppArmor) != 0 {
if strings.HasPrefix(key, "container.apparmor.security.beta.kubernetes.io/") && value != "unconfined" {
delete(stateful.Spec.Template.Annotations, key)
}
if strings.HasPrefix(key, "container.apparmor.security.beta.kubernetes.io/") && value != "unconfined" {
delete(stateful.Spec.Template.Annotations, key)
}

// Seccomp
if (e & varmortypes.Seccomp) != 0 {
if strings.HasPrefix(key, "container.seccomp.security.beta.varmor.org/") && value != "unconfined" {
delete(stateful.Spec.Template.Annotations, key)
parts := strings.Split(key, "/")
if len(parts) != 2 {
continue
}
// Clean up the seccomp settings from the SecurityContext
for index, container := range stateful.Spec.Template.Spec.Containers {
if container.Name == parts[1] {
stateful.Spec.Template.Spec.Containers[index].SecurityContext.SeccompProfile = nil
}
if strings.HasPrefix(key, "container.seccomp.security.beta.varmor.org/") && value != "unconfined" {
delete(stateful.Spec.Template.Annotations, key)
parts := strings.Split(key, "/")
if len(parts) != 2 {
continue
}
// Clean up the seccomp settings from the SecurityContext
for index, container := range stateful.Spec.Template.Spec.Containers {
if container.Name == parts[1] {
stateful.Spec.Template.Spec.Containers[index].SecurityContext.SeccompProfile = nil
}
}
}
Expand Down Expand Up @@ -240,33 +232,30 @@ func modifyDaemonSetAnnotationsAndEnv(enforcer string, mode varmor.VarmorPolicyM
// Clean up first
for key, value := range daemon.Spec.Template.Annotations {
// BPF
if (e & varmortypes.BPF) != 0 {
if strings.HasPrefix(key, "container.bpf.security.beta.varmor.org/") && value != "unconfined" {
delete(daemon.Spec.Template.Annotations, key)
}
if strings.HasPrefix(key, "container.bpf.security.beta.varmor.org/") && value != "unconfined" {
delete(daemon.Spec.Template.Annotations, key)
}

// AppArmor
if (e & varmortypes.AppArmor) != 0 {
if strings.HasPrefix(key, "container.apparmor.security.beta.kubernetes.io/") && value != "unconfined" {
delete(daemon.Spec.Template.Annotations, key)
}
if strings.HasPrefix(key, "container.apparmor.security.beta.kubernetes.io/") && value != "unconfined" {
delete(daemon.Spec.Template.Annotations, key)
}

// Seccomp
if (e & varmortypes.Seccomp) != 0 {
if strings.HasPrefix(key, "container.seccomp.security.beta.varmor.org/") && value != "unconfined" {
delete(daemon.Spec.Template.Annotations, key)
parts := strings.Split(key, "/")
if len(parts) != 2 {
continue
}
// Clean up the seccomp settings from the SecurityContext
for index, container := range daemon.Spec.Template.Spec.Containers {
if container.Name == parts[1] {
daemon.Spec.Template.Spec.Containers[index].SecurityContext.SeccompProfile = nil
}
if strings.HasPrefix(key, "container.seccomp.security.beta.varmor.org/") && value != "unconfined" {
delete(daemon.Spec.Template.Annotations, key)
parts := strings.Split(key, "/")
if len(parts) != 2 {
continue
}
// Clean up the seccomp settings from the SecurityContext
for index, container := range daemon.Spec.Template.Spec.Containers {
if container.Name == parts[1] {
daemon.Spec.Template.Spec.Containers[index].SecurityContext.SeccompProfile = nil
}
}
}

}

// Add the modification time to annotation
Expand Down
Loading