kubescape / regolibrary

The regolibrary package contains the controls Kubescape uses for detecting misconfigurations in Kubernetes manifests.
Apache License 2.0
121 stars 48 forks source link

fix C-0069 - anonymous auth disabled by default #615

Closed YiscahLevySilas1 closed 8 months ago

YiscahLevySilas1 commented 8 months ago

User description

Overview


Type

bug_fix


Description


Changes walkthrough

Relevant files
Bug fix
raw.rego
Adjust Anonymous Authentication Detection Logic                   

rules/anonymous-requests-to-kubelet-updated/raw.rego
  • Removed the rule that triggers an alert when anonymous authentication
    is not explicitly disabled via command-line arguments.
  • Adjusted the rule to trigger an alert only if anonymous authentication
    is explicitly enabled in the kubelet configuration file.
  • +1/-20   
    Tests
    expected.json
    Update Test Expectations for Anonymous Auth Checks             

    rules/anonymous-requests-to-kubelet-updated/test/invalid-config-no-value/expected.json
  • Removed all expected alerts for tests involving configurations where
    anonymous authentication setting is not explicitly defined, reflecting
    the new logic.
  • +1/-28   
    expected.json
    Align Test Expectations with Updated Auth Logic                   

    rules/anonymous-requests-to-kubelet-updated/test/no-cli-params/expected.json
  • Removed all expected alerts for tests involving missing command-line
    parameters for anonymous authentication, aligning with the updated
    detection logic.
  • +1/-22   
    kubelet-info.json
    Update Test Input for Anonymous Auth Configuration             

    rules/anonymous-requests-to-kubelet-updated/test/valid-cli/input/kubelet-info.json
  • Updated test input to reflect the scenario where anonymous
    authentication is not explicitly disabled via command-line but is
    disabled in the configuration file.
  • +1/-1     

    PR-Agent usage: Comment /help on the PR to get a list of all available PR-Agent tools and their descriptions

    codiumai-pr-agent-free[bot] commented 8 months ago

    PR Description updated to latest commit (https://github.com/kubescape/regolibrary/commit/54d0c7d65d8c9f14fe263d889ad9dc37f4ab48fe)

    codiumai-pr-agent-free[bot] commented 8 months ago

    PR Review

    ⏱️ Estimated effort to review [1-5] 2, because the changes are focused and involve specific logic adjustments in the detection of anonymous authentication settings in kubelet configurations. The modifications are straightforward and localized to a single Rego file and associated test files.
    🧪 Relevant tests Yes
    🔍 Possible issues Possible Regression: The removal of the check for the absence of "--anonymous-auth" and "--config" command-line arguments might lead to scenarios where anonymous authentication is inadvertently allowed due to misconfigurations not caught by this logic.
    🔒 Security concerns No

    ✨ Review tool usage guide:
    **Overview:** The `review` tool scans the PR code changes, and generates a PR review which includes several types of feedbacks, such as possible PR issues, security threats and relevant test in the PR. More feedbacks can be [added](https://pr-agent-docs.codium.ai/tools/review/#general-configurations) by configuring the tool. The tool can be triggered [automatically](https://pr-agent-docs.codium.ai/usage-guide/automations_and_usage/#github-app-automatic-tools-when-a-new-pr-is-opened) every time a new PR is opened, or can be invoked manually by commenting on any PR. - When commenting, to edit [configurations](https://github.com/Codium-ai/pr-agent/blob/main/pr_agent/settings/configuration.toml#L23) related to the review tool (`pr_reviewer` section), use the following template: ``` /review --pr_reviewer.some_config1=... --pr_reviewer.some_config2=... ``` - With a [configuration file](https://pr-agent-docs.codium.ai/usage-guide/configuration_options/), use the following template: ``` [pr_reviewer] some_config1=... some_config2=... ``` See the review [usage page](https://pr-agent-docs.codium.ai/tools/review/) for a comprehensive guide on using this tool.
    codiumai-pr-agent-free[bot] commented 8 months ago

    PR Code Suggestions

    CategorySuggestions                                                                                                                                                       
    Enhancement
    Change the condition to check if anonymous authentication is not explicitly disabled. ___ **It seems like the condition yamlConfig.authentication.anonymous.enabled == true is
    directly asserting that anonymous authentication is enabled without considering the
    possibility that the configuration might not explicitly set this field, thus defaulting to
    the Kubernetes default (which might vary between versions). A safer approach would be to
    check if the field is not explicitly set to false, which would cover more cases and ensure
    that the policy is effective even if the configuration defaults change.** [rules/anonymous-requests-to-kubelet-updated/raw.rego [36]](https://github.com/kubescape/regolibrary/pull/615/files#diff-a84785820ea9ae2cdb37df951ad48ad8a568fdbbcbe87478895868dfb46e2588R36-R36) ```diff -yamlConfig.authentication.anonymous.enabled == true +not yamlConfig.authentication.anonymous.enabled == false ```
    Add a test case for implicitly enabled anonymous authentication. ___ **The test case expected.json for invalid configurations without a value has been changed to
    an empty array [], which might not accurately test the behavior of the policy when
    anonymous authentication is implicitly enabled (by not being explicitly disabled). It
    would be beneficial to include a test case that ensures the policy triggers an alert when
    the configuration does not explicitly disable anonymous authentication, reflecting more
    realistic scenarios.** [rules/anonymous-requests-to-kubelet-updated/test/invalid-config-no-value/expected.json [1]](https://github.com/kubescape/regolibrary/pull/615/files#diff-22bded34abd6510e4f915fda41dc36b39ce1942168079fcb56b3b585a019495eR1-R1) ```diff -[] +[{ + "alertMessage": "Anonymous requests is enabled.", + "alertScore": 7, + "failedPaths": ["authentication.anonymous.enabled"], + "fixPaths": ["authentication.anonymous.enabled"], + "packagename": "armo_builtins" +}] ```
    Add a test case for scenarios with no CLI parameters where anonymous authentication might be enabled by default. ___ **Similar to the previous suggestion, the test case for scenarios with no CLI parameters has
    been reduced to an empty array []. This change might miss testing the policy's ability to
    detect and alert on configurations where anonymous authentication is enabled by default or
    not explicitly disabled. Consider adding a detailed test case that verifies the policy's
    effectiveness in such scenarios.** [rules/anonymous-requests-to-kubelet-updated/test/no-cli-params/expected.json [1]](https://github.com/kubescape/regolibrary/pull/615/files#diff-ecc1f959a13f9554e78fc0935b73bbfc6356eef6470286b5445b5a49af1a75cdR1-R1) ```diff -[] +[{ + "alertMessage": "Anonymous requests is enabled.", + "alertScore": 7, + "failedPaths": [], + "fixPaths": ["authentication.anonymous.enabled"], + "packagename": "armo_builtins" +}] ```
    Include command line scenarios both with and without the --anonymous-auth=false flag. ___ **The modification in kubelet-info.json removes the --anonymous-auth=false flag from the
    command line, which is a significant change for testing the policy's behavior. However, to
    ensure comprehensive testing, it would be beneficial to include both scenarios: one with
    the flag explicitly set to false and another without the flag. This approach would help
    verify that the policy correctly interprets the absence of the flag as not explicitly
    disabling anonymous authentication.** [rules/anonymous-requests-to-kubelet-updated/test/valid-cli/input/kubelet-info.json [5]](https://github.com/kubescape/regolibrary/pull/615/files#diff-3243da95e28d4ff97248f77d47eeb0977b01f1afa2b2b6a121f6e33c0027ad87R5-R5) ```diff -"cmdLine": "/var/lib/minikube/binaries/v1.23.1/kubelet --bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf --config=ss", +"cmdLine": "/var/lib/minikube/binaries/v1.23.1/kubelet --bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf --anonymous-auth=false --config=ss", ```

    ✨ Improve tool usage guide:
    **Overview:** The `improve` tool scans the PR code changes, and automatically generates suggestions for improving the PR code. The tool can be triggered [automatically](https://pr-agent-docs.codium.ai/usage-guide/automations_and_usage/#github-app-automatic-tools-when-a-new-pr-is-opened) every time a new PR is opened, or can be invoked manually by commenting on a PR. - When commenting, to edit [configurations](https://github.com/Codium-ai/pr-agent/blob/main/pr_agent/settings/configuration.toml#L78) related to the improve tool (`pr_code_suggestions` section), use the following template: ``` /improve --pr_code_suggestions.some_config1=... --pr_code_suggestions.some_config2=... ``` - With a [configuration file](https://pr-agent-docs.codium.ai/usage-guide/configuration_options/), use the following template: ``` [pr_code_suggestions] some_config1=... some_config2=... ``` See the improve [usage page](https://pr-agent-docs.codium.ai/tools/improve/) for a comprehensive guide on using this tool.
    github-actions[bot] commented 8 months ago

    Summary: