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: added kubectl exec alert policy #1818

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"context"

"github.com/go-logr/logr"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

Expand All @@ -26,6 +28,44 @@ type KubeArmorHostPolicyReconciler struct {

func (r *KubeArmorHostPolicyReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {

// definig the kubectl-exec-policy
policy := &securityv1.KubeArmorClusterPolicy{
ObjectMeta: metav1.ObjectMeta{
Name: "alert-kubectl-exec",
},
Spec: securityv1.KubeArmorClusterPolicySpec{
Severity: 1,
Tags: []string{"kubectl", "exec", "alert"},
Message: "Detected kubectl exec",
Selector: securityv1.NsSelectorType{},
Process: securityv1.ProcessType{
MatchPatterns: []securityv1.ProcessPatternType{
{Pattern: "ppid=0.*tty!=null",
Action: securityv1.ActionType("Audit"),
},
},
},
},
}
// Check if the policy already exists
existingPolicy := &securityv1.KubeArmorClusterPolicy{}
err := r.Get(ctx, types.NamespacedName{Name: policy.Name}, existingPolicy)
if err != nil {
if client.IgnoreNotFound(err) != nil {
return ctrl.Result{}, err
}
// Policy not found, create it
if err := r.Create(ctx, policy); err != nil {
return ctrl.Result{}, err
}
} else {
// Policy found, update it if necessary
existingPolicy.Spec = policy.Spec
if err := r.Update(ctx, existingPolicy); err != nil {
return ctrl.Result{}, err
}
}

return ctrl.Result{}, nil
}

Expand Down