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

Consistently use namespaceMatchLabels across rules #316

Merged
merged 10 commits into from
Oct 15, 2024

Conversation

georgibaltiev
Copy link
Contributor

@georgibaltiev georgibaltiev commented Oct 11, 2024

What this PR does / why we need it:
This PR refactors the accepted pod structs implemented for rules #242414 and #242415 by aggregating the label matching options into a singular shared structure.
The accepted pod struct for #242417 is refactored to utilize the new structure and now can add exemptions from the ruleset checks by matching namespaces by labels.
The accepted pod struct for #242383 is refactored as well to use namespace labels for matching its rule exemptions.

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

Special notes for your reviewer:

Release note:

This change affects only the `managedk8s` provider. Configuration for DISA K8s STIG rules `242383` and `242417` now does not accept namespace names directly but rely on matching namespaces by labels. Please, see [the example configuration options](https://github.com/gardener/diki/blob/main/example/config/managedk8s.yaml) for more details.

@georgibaltiev georgibaltiev requested a review from a team as a code owner October 11, 2024 05:39
@gardener-robot gardener-robot added needs/review Needs review size/xl Size of pull request is huge (see gardener-robot robot/bots/size.py) needs/second-opinion Needs second review by someone else labels Oct 11, 2024
@gardener-robot-ci-1 gardener-robot-ci-1 added reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) needs/ok-to-test Needs approval for testing (check PR in detail before setting this label because PR is run on CI/CD) and removed reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) labels Oct 11, 2024
@AleksandarSavchev AleksandarSavchev self-requested a review October 11, 2024 06:04
@dimityrmirchev dimityrmirchev self-requested a review October 11, 2024 06:15
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.

Please adapt the example managedk8s diki config file to use the new namespaceMatchLabels option:

# # Defaults to ["default", "kube-public", "kube-node-lease"]
# namespaceNames:
# - default
# - kube-public
# - kube-node-lease

# namespaceNames:
# - kube-system
# - kube-public
# - kube-node-lease

Also write a comment describing which namespaces can be selected. Example for 242383:

# Only namespaces in ["default", "kube-public", "kube-node-lease"]
# can be selected with namespaceMatchLabels

example/guides/partial-disa-k8s-stig-shoot.yaml Outdated Show resolved Hide resolved
pkg/shared/ruleset/disak8sstig/option/options.go Outdated Show resolved Hide resolved
pkg/provider/gardener/ruleset/disak8sstig/v1r11_ruleset.go Outdated Show resolved Hide resolved
pkg/provider/gardener/ruleset/disak8sstig/v2r1_ruleset.go Outdated Show resolved Hide resolved
pkg/shared/ruleset/disak8sstig/option/options.go Outdated Show resolved Hide resolved
pkg/shared/ruleset/disak8sstig/rules/242417.go Outdated Show resolved Hide resolved
pkg/shared/ruleset/disak8sstig/rules/242417.go Outdated Show resolved Hide resolved
pkg/shared/ruleset/disak8sstig/rules/242417.go Outdated Show resolved Hide resolved
pkg/shared/ruleset/disak8sstig/rules/242383.go Outdated Show resolved Hide resolved
pkg/shared/ruleset/disak8sstig/rules/242383.go Outdated Show resolved Hide resolved
@gardener-robot gardener-robot added the needs/changes Needs (more) changes label Oct 11, 2024
@gardener-robot-ci-3 gardener-robot-ci-3 added the reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) label Oct 11, 2024
@gardener-robot-ci-1 gardener-robot-ci-1 removed the reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) label Oct 11, 2024
@gardener-robot-ci-1 gardener-robot-ci-1 added the reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) label Oct 11, 2024
@gardener-robot-ci-2 gardener-robot-ci-2 removed the reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) label Oct 11, 2024
@georgibaltiev
Copy link
Contributor Author

georgibaltiev commented Oct 11, 2024

I've added 3 new commits that may resolve the issues. The unnecessary newlines are removed, and the structs are renamed accordingly. New configurations are added in the yaml files as well

@gardener-robot-ci-2 gardener-robot-ci-2 added the reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) label Oct 11, 2024
@gardener-robot-ci-3 gardener-robot-ci-3 removed the reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) label Oct 11, 2024
# - default
# - kube-public
# - kube-node-lease
# # only namespaces in ["default", "kube-public", "kube-node-lease" can be selected with namespaceMatchLabels
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
# # only namespaces in ["default", "kube-public", "kube-node-lease" can be selected with namespaceMatchLabels
# # only namespaces in ["default", "kube-public", "kube-node-lease"] are meaningful to be selected with namespaceMatchLabels
# since the rule does not perform checks on objects in namespaces different from the listed above

# - kube-system
# - kube-public
# - kube-node-lease
# # only pods in namespaces ["kube-system", "kube-public", "kube-node-lease"] can be selected with namespaceMatchLabels
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

justification: "Pods managed by Gardener are not considered as user pods"
- ruleID: "242449"
- ruleID: "242449" 
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
- ruleID: "242449" 
- ruleID: "242449"

pkg/provider/gardener/ruleset/disak8sstig/v1r11_ruleset.go Outdated Show resolved Hide resolved
@@ -352,12 +350,15 @@ func (r *Ruleset) registerV2R1Rules(ruleOptions map[string]config.RuleOptionsCon
Options: &sharedrules.Options242417{
AcceptedPods: []sharedrules.AcceptedPods242417{
{
PodMatchLabels: map[string]string{
PodSelector: option.PodSelector{PodMatchLabels: map[string]string{
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
PodSelector: option.PodSelector{PodMatchLabels: map[string]string{
PodSelector: option.PodSelector{
PodMatchLabels: map[string]string{

@@ -88,19 +107,23 @@ func (r *Rule242383) Name() string {
}

func (r *Rule242383) Run(ctx context.Context) (rule.RuleResult, error) {
allNamespaces, err := kubeutils.GetNamespaces(ctx, r.Client)
if err != nil {
return rule.SingleCheckResult(r, rule.ErroredCheckResult(err.Error(), rule.NewTarget("", ""))), nil
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
return rule.SingleCheckResult(r, rule.ErroredCheckResult(err.Error(), rule.NewTarget("", ""))), nil
return rule.SingleCheckResult(r, rule.ErroredCheckResult(err.Error(), rule.NewTarget())), nil

selector := labels.NewSelector().Add(*notDikiPodReq)

allNamespaces, err := kubeutils.GetNamespaces(ctx, r.Client)
if err != nil {
return rule.SingleCheckResult(r, rule.ErroredCheckResult(err.Error(), rule.NewTarget("", ""))), nil
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
return rule.SingleCheckResult(r, rule.ErroredCheckResult(err.Error(), rule.NewTarget("", ""))), nil
return rule.SingleCheckResult(r, rule.ErroredCheckResult(err.Error(), rule.NewTarget()), nil

APIVersion: "v1",
Kind: "Service",
MatchLabels: map[string]string{
"component": "apiserver",
"provider": "kubernetes",
},
NamespaceNames: []string{"default"},
NamespaceMatchLabels: allNamespaces["default"].Labels,
Copy link
Member

Choose a reason for hiding this comment

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

Let's match the default namespace by the metadata name label similarly to how we match other namespaces. It would be better to keep this consistent since it will be easier for developers to search similar usages through the code.

@@ -106,10 +132,9 @@ var _ Option = (*Options242415)(nil)

// AcceptedPods242415 contains option specifications for appected pods
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
// AcceptedPods242415 contains option specifications for appected pods
// AcceptedPods242415 contains option specifications for accepted pods

@@ -70,10 +98,9 @@ var _ Option = (*Options242414)(nil)

// AcceptedPods242414 contains option specifications for appected pods
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
// AcceptedPods242414 contains option specifications for appected pods
// AcceptedPods242414 contains option specifications for accepted pods

@gardener-robot-ci-3 gardener-robot-ci-3 added reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) and removed reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) labels Oct 14, 2024
@gardener-robot-ci-2 gardener-robot-ci-2 added reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) and removed reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) labels Oct 14, 2024
@gardener-robot-ci-3 gardener-robot-ci-3 added the reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) label Oct 14, 2024
@gardener-robot-ci-1 gardener-robot-ci-1 removed the reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) label Oct 14, 2024
@georgibaltiev georgibaltiev force-pushed the unify-namespace-labels branch from 7330903 to 05d3749 Compare October 14, 2024 13:16
@gardener-robot-ci-1 gardener-robot-ci-1 added reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) and removed reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) labels Oct 14, 2024
@gardener-robot-ci-1 gardener-robot-ci-1 added reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) and removed reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) labels Oct 14, 2024
@gardener-robot-ci-1 gardener-robot-ci-1 added reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) and removed reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) labels Oct 14, 2024
@gardener-robot-ci-1 gardener-robot-ci-1 added reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) and removed reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) labels Oct 14, 2024
@gardener-robot-ci-1 gardener-robot-ci-1 added reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) and removed reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) labels Oct 14, 2024
example/config/managedk8s.yaml Outdated Show resolved Hide resolved
@gardener-robot-ci-2 gardener-robot-ci-2 added the reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) label Oct 15, 2024
Copy link
Member

@dimityrmirchev dimityrmirchev left a comment

Choose a reason for hiding this comment

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

/lgtm

@gardener-robot gardener-robot added reviewed/lgtm Has approval for merging and removed needs/changes Needs (more) changes needs/review Needs review needs/second-opinion Needs second review by someone else labels Oct 15, 2024
@gardener-robot-ci-2 gardener-robot-ci-2 removed the reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) label Oct 15, 2024
@gardener-robot-ci-1 gardener-robot-ci-1 added the reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) label Oct 15, 2024
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.

/lgtm

@AleksandarSavchev AleksandarSavchev merged commit faeeb7b into gardener:main Oct 15, 2024
9 checks passed
@gardener-robot gardener-robot added the status/closed Issue is closed (either delivered or triaged) label Oct 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs/ok-to-test Needs approval for testing (check PR in detail before setting this label because PR is run on CI/CD) reviewed/lgtm Has approval for merging reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) size/xl Size of pull request is huge (see gardener-robot robot/bots/size.py) status/closed Issue is closed (either delivered or triaged)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Consistently use namespaceMatchLabels across rules
7 participants