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 ContainerName #242

Merged
merged 9 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
17 changes: 17 additions & 0 deletions admission/rules/v1/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apiserver/pkg/admission"
"k8s.io/client-go/kubernetes"
)
Expand Down Expand Up @@ -68,3 +69,19 @@ func resolveJob(ownerRef metav1.OwnerReference, namespace string, clientset kube
}
return "Job", ownerRef.Name, namespace
}

// GetContainerNameFromExecToPodEvent returns the container name from the admission event for exec operations.
func GetContainerNameFromExecToPodEvent(event admission.Attributes) string {
if event.GetSubresource() == "exec" {
if obj := event.GetObject(); obj != nil {
if unstructuredObj, ok := obj.(*unstructured.Unstructured); ok {
if object, ok := unstructuredObj.Object["object"].(map[string]interface{}); ok {
if containerName, ok := object["container"].(string); ok {
return containerName
}
}
}
}
}
return ""
}
7 changes: 5 additions & 2 deletions admission/rules/v1/r2000_exec_to_pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ func (rule *R2000ExecToPod) ProcessEvent(event admission.Attributes, access inte
return nil
}

containerName := GetContainerNameFromExecToPodEvent(event)

ruleFailure := GenericRuleFailure{
BaseRuntimeAlert: apitypes.BaseRuntimeAlert{
AlertName: rule.Name(),
Expand Down Expand Up @@ -107,12 +109,13 @@ func (rule *R2000ExecToPod) ProcessEvent(event admission.Attributes, access inte
RuleDescription: fmt.Sprintf("Exec to pod detected on pod %s", event.GetName()),
},
RuntimeAlertK8sDetails: apitypes.RuntimeAlertK8sDetails{
PodName: event.GetName(),
Namespace: event.GetNamespace(),
PodName: event.GetName(),
Namespace: event.GetNamespace(),
WorkloadName: workloadName,
WorkloadNamespace: workloadNamespace,
WorkloadKind: workloadKind,
NodeName: nodeName,
ContainerName: containerName,
},
RuleID: R2000ID,
}
Expand Down
1 change: 0 additions & 1 deletion admission/rules/v1/r2001_portforward.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ func (rule *R2001PortForward) ProcessEvent(event admission.Attributes, access in
logger.L().Error("Failed to get parent workload details", helpers.Error(err))
return nil
}

ruleFailure := GenericRuleFailure{
BaseRuntimeAlert: apitypes.BaseRuntimeAlert{
AlertName: rule.Name(),
Expand Down
Loading