Skip to content

Conversation

@georgibaltiev
Copy link
Contributor

@georgibaltiev georgibaltiev commented Nov 17, 2025

How to categorize this PR?

/kind enhancement

What this PR does / why we need it:
This PR enhances the already present evaluation of a Namespace' sNetworkPolicies, by checking if the namespace is marked for deletion with a .deletionTimestamp. If the namespace is marked for deletion, and has neither pods on it deployed nor any allow-all network policies, security finding results are marked as Warning instead of Failed

Which issue(s) this PR fixes:
Fixes #638

Special notes for your reviewer:

Release note:

[Rule 2000 of the Security Hardened Kubernetes Cluster ruleset] Namespaces marked for deletion without any pods will be marked as `Warning` findings instead of `Failed`

@georgibaltiev georgibaltiev requested a review from a team as a code owner November 17, 2025 11:09
@gardener-prow
Copy link

gardener-prow bot commented Nov 17, 2025

@georgibaltiev: The label(s) kind/todo cannot be applied, because the repository doesn't have them.

In response to this:

How to categorize this PR?

/kind TODO

What this PR does / why we need it:
This PR enhances the already present evaluation of a Namespace' sNetworkPolicies, by checking if the namespace is marked for deletion with a .deletionTimestamp. If the namespace is marked for deletion, and has no pods on it deployed, security finding results are marked as Warning instead of Failed

Which issue(s) this PR fixes:
Fixes #638

Special notes for your reviewer:

Release note:

[Rule 2000 of the Security Hardened Kubernetes Cluster ruleset] Namespaces marked for deletion without any pods will be marked as `Warning` findings instead of `Failed`

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@gardener-prow
Copy link

gardener-prow bot commented Nov 17, 2025

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign dimityrmirchev for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@gardener-prow gardener-prow bot added do-not-merge/needs-kind Indicates a PR lacks a `kind/foo` label and requires one. cla: yes Indicates the PR's author has signed the cla-assistant.io CLA. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Nov 17, 2025
@georgibaltiev
Copy link
Contributor Author

/hold
as I've noticed a bug with one of the unit tests

@gardener-prow gardener-prow bot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Nov 17, 2025
@georgibaltiev
Copy link
Contributor Author

/unhold
the bug has been resolved

@gardener-prow gardener-prow bot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Nov 17, 2025
@georgibaltiev georgibaltiev marked this pull request as draft November 17, 2025 15:22
@gardener-prow gardener-prow bot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Nov 17, 2025
@georgibaltiev georgibaltiev force-pushed the enh/rule-2000-shkc-warning-checkresults branch from 4dc35e6 to 5256e60 Compare November 21, 2025 13:59
@georgibaltiev georgibaltiev marked this pull request as ready for review November 21, 2025 13:59
@gardener-prow gardener-prow bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Nov 21, 2025
Copy link
Member

@AleksandarSavchev AleksandarSavchev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Have some minor suggestions

Comment on lines 59 to 60
namespaceDeletionWithoutPodsMessage = "namespace is marked for deletion - no pods are deployed on it"
namespaceDeletionWithPodsMessage = "namespace is marked for deletion - there are still pods deployed on it"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
namespaceDeletionWithoutPodsMessage = "namespace is marked for deletion - no pods are deployed on it"
namespaceDeletionWithPodsMessage = "namespace is marked for deletion - there are still pods deployed on it"
namespaceDeletionWithoutPodsDetails = "namespace is marked for deletion with no present pods"
namespaceDeletionWithPodsDetails = "namespace is marked for deletion with present pods"
ingressTrafficNotDeniedMessage = "Ingress traffic is not denied by default."
egressTrafficNotDeniedMessage = "Egress traffic is not denied by default."

The "not denied by default" message is repeated several times, we can store it in a const.

Comment on lines 156 to 160
pods, err := kubeutils.GetPods(ctx, r.Client, namespace.Name, labels.NewSelector(), 300)
if err != nil {
checkResults = append(checkResults, rule.ErroredCheckResult(err.Error(), rule.NewTarget()))
break
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pods, err := kubeutils.GetPods(ctx, r.Client, namespace.Name, labels.NewSelector(), 300)
if err != nil {
checkResults = append(checkResults, rule.ErroredCheckResult(err.Error(), rule.NewTarget()))
break
}
pods, err := kubeutils.GetPods(ctx, r.Client, namespace.Name, labels.NewSelector(), 300)
if err != nil {
return rule.Result(r, rule.ErroredCheckResult(err.Error(), target)), nil
}

We should return on errored CheckResult here. I think it would only error here if there is a problem with the client. WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My assumption was based on the fact that there could be issues with the GetPods function, related to a specific namespace, for example the user not having permissions to access it, but I am not sure if those use cases are relevant, if even possible. This was my reasoning as to why I want to check all namespaces, even if one of them errors. WDYT?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, the user might not have permissions for a specific namespace. On a side note the break here is not needed.

We tell the user to use an admin kubeconfig when running diki. Most likely there will be a lot of errored rules when such permissions are missing. I do not think we should cater to this case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, that makes sense.

checkResults = append(checkResults, rule.ErroredCheckResult(err.Error(), rule.NewTarget()))
break
}
if len(pods) > 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if len(pods) > 0 {
if len(pods) > 0 {

checkResults = append(checkResults, rule.FailedCheckResult("All Ingress traffic is allowed by default.", allowsAllIngressTarget))
default:
case namespace.DeletionTimestamp == nil:
checkResults = append(checkResults, rule.FailedCheckResult("Ingress traffic is not denied by default.", target))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
checkResults = append(checkResults, rule.FailedCheckResult("Ingress traffic is not denied by default.", target))
checkResults = append(checkResults, rule.FailedCheckResult(ingressTrafficNotDeniedMessage, target))
  • other applicable places.

Comment on lines 196 to 198
if err != nil {
checkResults = append(checkResults, rule.ErroredCheckResult(err.Error(), rule.NewTarget()))
break
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

Comment on lines 56 to 59
ObjectMeta: metav1.ObjectMeta{
Name: "deny-all",
Namespace: "plain-namespace",
},
Copy link
Member

@AleksandarSavchev AleksandarSavchev Nov 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ObjectMeta: metav1.ObjectMeta{
Name: "deny-all",
Namespace: "plain-namespace",
},
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "plain-namespace",
},

I see that most of the entries use the same name & namespace. We can have the ObjectMeta be set in the test func and only pass the Spec here.

},
},
),
Entry("should warn fail an allow-all network policy is configured for the ingress traffic and there are no pods present",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Entry("should warn fail an allow-all network policy is configured for the ingress traffic and there are no pods present",
Entry("should fail when an allow-all network policy is configured for the ingress traffic and there are no pods present",

},
},
),
Entry("should warn fail an allow-all network policy is configured for the egress traffic and there are no pods present",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Entry("should warn fail an allow-all network policy is configured for the egress traffic and there are no pods present",
Entry("should fail when an allow-all network policy is configured for the egress traffic and there are no pods present",

Comment on lines 535 to 546
&networkingv1.NetworkPolicy{
ObjectMeta: metav1.ObjectMeta{
Name: "deny-all",
Namespace: "plain-namespace",
},
Spec: networkingv1.NetworkPolicySpec{
PolicyTypes: []networkingv1.PolicyType{
"Ingress",
"Egress",
},
},
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can also have only the spec passed here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla: yes Indicates the PR's author has signed the cla-assistant.io CLA. do-not-merge/needs-kind Indicates a PR lacks a `kind/foo` label and requires one. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.

Projects

None yet

2 participants