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

split cpu and memory controls #594

Merged
merged 4 commits into from
Mar 3, 2024
Merged

Conversation

YiscahLevySilas1
Copy link
Collaborator

@YiscahLevySilas1 YiscahLevySilas1 commented Mar 3, 2024

User description

Overview


Type

enhancement


Description

  • Introduced new controls for ensuring CPU and memory requests and limits are set for Pods.
  • Updated exceptions and various frameworks to include new control IDs for CPU and memory limits.
  • Added Rego policies for checking CPU and memory requests and limits in Kubernetes resources.

Changes walkthrough

Relevant files
Enhancement
8 files
C-0268-ensurecpurequestsareset.json
Add Control for Ensuring CPU Requests Are Set                       

controls/C-0268-ensurecpurequestsareset.json

  • Introduced a new control for ensuring CPU requests are set for Pods.
  • Specifies control attributes, description, remediation, and rules
    names.
  • Defines control ID, base score, category, and scanning scope.
  • +28/-0   
    C-0269-ensurememoryrequestsareset.json
    Add Control for Ensuring Memory Requests Are Set                 

    controls/C-0269-ensurememoryrequestsareset.json

  • Added a new control for ensuring memory requests are set for Pods.
  • Details control attributes, description, remediation, and rules names.
  • Sets control ID, base score, category, and scanning scope.
  • +28/-0   
    C-0270-ensurecpulimitsareset.json
    Add Control for Ensuring CPU Limits Are Set                           

    controls/C-0270-ensurecpulimitsareset.json

  • New control added for ensuring CPU limits are set for Pods.
  • Describes control attributes, including attack tracks, description,
    remediation, and rules names.
  • Establishes control ID, base score, category, and scanning scope.
  • +37/-0   
    C-0271-ensurememorylimitsareset.json
    Add Control for Ensuring Memory Limits Are Set                     

    controls/C-0271-ensurememorylimitsareset.json

  • Introduced a control for ensuring memory limits are set for Pods.
  • Outlines control attributes, including attack tracks, description,
    remediation, and rules names.
  • Defines control ID, base score, category, and scanning scope.
  • +37/-0   
    raw.rego
    Add Rego Policy for CPU Limits Check                                         

    rules/resources-cpu-limits/raw.rego

  • Introduced a Rego policy to check for CPU limits in Kubernetes
    resources.
  • Provides remediation paths for missing CPU limits.
  • +72/-0   
    raw.rego
    Add Rego Policy for CPU Requests Check                                     

    rules/resources-cpu-requests/raw.rego

  • New Rego policy to ensure CPU requests are set in Kubernetes
    resources.
  • Specifies remediation paths for missing CPU requests.
  • +69/-0   
    raw.rego
    Add Rego Policy for Memory Limits Check                                   

    rules/resources-memory-limits/raw.rego

  • Rego policy added for checking memory limits in Kubernetes resources.
  • Outlines remediation paths for missing memory limits.
  • +57/-0   
    raw.rego
    Add Rego Policy for Memory Requests Check                               

    rules/resources-memory-requests/raw.rego

  • Introduced a Rego policy for ensuring memory requests are set in
    Kubernetes resources.
  • Provides remediation paths for missing memory requests.
  • +58/-0   
    Configuration changes
    7 files
    kube-apiserver.json
    Update Exceptions with New Control IDs for CPU and Memory Limits

    exceptions/kube-apiserver.json

  • Updated exceptions to include new control IDs for CPU and memory
    limits.
  • +2/-5     
    allcontrols.json
    Update Framework with New Control IDs for CPU and Memory Management

    frameworks/allcontrols.json

  • Removed outdated control IDs and added new ones for CPU and memory
    requests and limits.
  • +12/-18 
    armobest.json
    Update ArmoBest Framework with New Control IDs for Resource Limits

    frameworks/armobest.json

  • Updated active controls to include new control IDs for ensuring CPU
    and memory limits are set.
  • +16/-8   
    devopsbest.json
    Update DevOps Best Practices Framework with New Control IDs

    frameworks/devopsbest.json

  • Added new control IDs for CPU and memory requests and limits to the
    active controls list.
  • +24/-12 
    nsaframework.json
    Update NSA Framework with New Control IDs for Resource Management

    frameworks/nsaframework.json

  • Included new control IDs for CPU and memory limits in the active
    controls section.
  • +12/-6   
    security.json
    Update Security Framework with New Control IDs for Resource Limits

    frameworks/security.json

  • Updated the security framework to include new control IDs for CPU and
    memory limits.
  • +12/-6   
    workloadscan.json
    Update Workload Scan Framework with New Control IDs           

    frameworks/workloadscan.json

  • Added new control IDs for CPU and memory requests and limits to the
    workload scan framework.
  • +12/-12 

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

    Signed-off-by: YiscahLevySilas1 <yiscahls@armosec.io>
    @codiumai-pr-agent codiumai-pr-agent bot added the enhancement New feature or request label Mar 3, 2024
    Copy link
    Contributor

    PR Description updated to latest commit (018ee0d)

    Copy link
    Contributor

    PR Review

    ⏱️ Estimated effort to review [1-5]

    2, because the PR introduces new JSON configurations and Rego policies for CPU and memory controls, which are straightforward to review given their structured format and clear purpose. However, understanding the full impact requires some domain knowledge in Kubernetes resource management and security policies.

    🧪 Relevant tests

    Yes

    🔍 Possible issues

    There might be a need to adjust the "baseScore" values in the JSON configurations to better reflect the severity of not setting CPU and memory requests and limits in a Kubernetes environment.

    The Rego policies should be reviewed to ensure they accurately identify Pods and Workloads without CPU and memory requests and limits, without false positives.

    🔒 Security concerns

    No, the PR does not introduce possible vulnerabilities. It enhances compliance and resource management within Kubernetes environments, which indirectly contributes to the overall security posture by preventing resource exhaustion attacks.


    ✨ Review tool usage guide:

    Overview:
    The review tool scans the PR code changes, and generates a PR review. The tool can be triggered automatically every time a new PR is opened, or can be invoked manually by commenting on any PR.
    When commenting, to edit configurations 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, use the following template:

    [pr_reviewer]
    some_config1=...
    some_config2=...
    
    Utilizing extra instructions

    The review tool can be configured with extra instructions, which can be used to guide the model to a feedback tailored to the needs of your project.

    Be specific, clear, and concise in the instructions. With extra instructions, you are the prompter. Specify the relevant sub-tool, and the relevant aspects of the PR that you want to emphasize.

    Examples for extra instructions:

    [pr_reviewer] # /review #
    extra_instructions="""
    In the 'possible issues' section, emphasize the following:
    - Does the code logic cover relevant edge cases?
    - Is the code logic clear and easy to understand?
    - Is the code logic efficient?
    ...
    """
    

    Use triple quotes to write multi-line instructions. Use bullet points to make the instructions more readable.

    How to enable\disable automation
    • When you first install PR-Agent app, the default mode for the review tool is:
    pr_commands = ["/review", ...]
    

    meaning the review tool will run automatically on every PR, with the default configuration.
    Edit this field to enable/disable the tool, or to change the used configurations

    Auto-labels

    The review tool can auto-generate two specific types of labels for a PR:

    • a possible security issue label, that detects possible security issues (enable_review_labels_security flag)
    • a Review effort [1-5]: x label, where x is the estimated effort to review the PR (enable_review_labels_effort flag)
    Extra sub-tools

    The review tool provides a collection of possible feedbacks about a PR.
    It is recommended to review the possible options, and choose the ones relevant for your use case.
    Some of the feature that are disabled by default are quite useful, and should be considered for enabling. For example:
    require_score_review, require_soc2_ticket, and more.

    Auto-approve PRs

    By invoking:

    /review auto_approve
    

    The tool will automatically approve the PR, and add a comment with the approval.

    To ensure safety, the auto-approval feature is disabled by default. To enable auto-approval, you need to actively set in a pre-defined configuration file the following:

    [pr_reviewer]
    enable_auto_approval = true
    

    (this specific flag cannot be set with a command line argument, only in the configuration file, committed to the repository)

    You can also enable auto-approval only if the PR meets certain requirements, such as that the estimated_review_effort is equal or below a certain threshold, by adjusting the flag:

    [pr_reviewer]
    maximal_review_effort = 5
    
    More PR-Agent commands

    To invoke the PR-Agent, add a comment using one of the following commands:

    • /review: Request a review of your Pull Request.
    • /describe: Update the PR title and description based on the contents of the PR.
    • /improve [--extended]: Suggest code improvements. Extended mode provides a higher quality feedback.
    • /ask <QUESTION>: Ask a question about the PR.
    • /update_changelog: Update the changelog based on the PR's contents.
    • /add_docs 💎: Generate docstring for new components introduced in the PR.
    • /generate_labels 💎: Generate labels for the PR based on the PR's contents.
    • /analyze 💎: Automatically analyzes the PR, and presents changes walkthrough for each component.

    See the tools guide for more details.
    To list the possible configuration parameters, add a /config comment.

    See the review usage page for a comprehensive guide on using this tool.

    Copy link
    Contributor

    codiumai-pr-agent bot commented Mar 3, 2024

    PR Code Suggestions

    CategorySuggestions                                                                                                                                                       
    Maintainability
    Improve variable naming for clarity.

    Consider using a more specific variable name instead of msga for the deny messages to
    improve code readability.

    rules/resources-cpu-limits/raw.rego [6]

    -deny[msga] {
    +deny[cpuLimitViolationMessage] {
     
    Refactor repeated logic into a helper function.

    To avoid code duplication, consider refactoring the repeated logic for checking container
    CPU requests into a helper function.

    rules/resources-cpu-requests/raw.rego [5]

    -deny[msga] {
    +deny[cpuRequestViolationMessage] {
     
    Enhancement
    Parameterize the remediation value in fixPaths.

    It's recommended to parameterize the "YOUR_VALUE" in fixPaths to allow for dynamic
    remediation values based on context or policy.

    rules/resources-cpu-limits/raw.rego [12]

    -fixPaths := [{"path": sprintf("spec.containers[%v].resources.limits.cpu", [format_int(i, 10)]), "value": "YOUR_VALUE"}]
    +fixPaths := [{"path": sprintf("spec.containers[%v].resources.limits.cpu", [format_int(i, 10)]), "value": input.desiredCpuLimit}]
     
    Clarify alert messages to accurately reflect the checks.

    To ensure the alert message accurately reflects the check being performed, update the
    message to specifically mention memory requests instead of a generic "limit or request".

    rules/resources-memory-requests/raw.rego [13]

    -"alertMessage": sprintf("Container: %v does not have memory-limit or request", [container.name]),
    +"alertMessage": sprintf("Container: %v does not have a memory request set", [container.name]),
     
    Improve clarity of the alert message for missing CPU requests.

    It's recommended to provide a more specific alertMessage that distinguishes between
    missing CPU limits and missing CPU requests. This will improve clarity for the end user.

    rules/resources-cpu-requests/test/workload/expected.json [3]

    -"alertMessage": "Container: app in Deployment: test   does not have CPU-limit or request",
    +"alertMessage": "Container: app in Deployment: test does not have CPU request set.",
     
    Trim extra spaces in the alert message for memory limits.

    Ensure to trim the extra spaces in the alertMessage for consistency and professionalism in
    the message displayed to the user.

    rules/resources-memory-limits/test/workload/expected.json [3]

    -"alertMessage": "Container: app in Deployment: test   does not have memory-limit or request",
    +"alertMessage": "Container: app in Deployment: test does not have memory limit set.",
     
    Clarify the alert message for missing CPU limits in CronJobs.

    The alertMessage should differentiate between missing CPU limits and requests specifically
    for CronJobs to provide clear guidance.

    rules/resources-cpu-limits/test/cronjob/expected.json [3]

    -"alertMessage": "Container: hello in CronJob: hello   does not have CPU-limit or request",
    +"alertMessage": "Container: hello in CronJob: hello does not have CPU limit set.",
     
    Specify missing memory requests in the alert message for CronJobs.

    For consistency and clarity, ensure the alertMessage specifically mentions the missing
    memory request for CronJobs.

    rules/resources-memory-requests/test/cronjob/expected.json [3]

    -"alertMessage": "Container: hello in CronJob: hello   does not have memory-limit or request",
    +"alertMessage": "Container: hello in CronJob: hello does not have memory request set.",
     
    Best practice
    Use a common function for generating fixPaths.

    For consistency and error prevention, use a common function to generate fixPaths across
    CPU and memory limit checks.

    rules/resources-memory-limits/raw.rego [10]

    -fixPaths := [{"path": sprintf("spec.containers[%v].resources.limits.memory", [format_int(i, 10)]), "value": "YOUR_VALUE"}]
    +fixPaths := generateFixPaths("memory", i, "YOUR_VALUE")
     

    ✨ 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 every time a new PR is opened, or can be invoked manually by commenting on a PR.
    When commenting, to edit configurations 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, use the following template:

    [pr_code_suggestions]
    some_config1=...
    some_config2=...
    
    Enabling\disabling automation

    When you first install the app, the default mode for the improve tool is:

    pr_commands = ["/improve --pr_code_suggestions.summarize=true", ...]
    

    meaning the improve tool will run automatically on every PR, with summarization enabled. Delete this line to disable the tool from running automatically.

    Utilizing extra instructions

    Extra instructions are very important for the improve tool, since they enable to guide the model to suggestions that are more relevant to the specific needs of the project.

    Be specific, clear, and concise in the instructions. With extra instructions, you are the prompter. Specify relevant aspects that you want the model to focus on.

    Examples for extra instructions:

    [pr_code_suggestions] # /improve #
    extra_instructions="""
    Emphasize the following aspects:
    - Does the code logic cover relevant edge cases?
    - Is the code logic clear and easy to understand?
    - Is the code logic efficient?
    ...
    """
    

    Use triple quotes to write multi-line instructions. Use bullet points to make the instructions more readable.

    A note on code suggestions quality
    • While the current AI for code is getting better and better (GPT-4), it's not flawless. Not all the suggestions will be perfect, and a user should not accept all of them automatically.
    • Suggestions are not meant to be simplistic. Instead, they aim to give deep feedback and raise questions, ideas and thoughts to the user, who can then use his judgment, experience, and understanding of the code base.
    • Recommended to use the 'extra_instructions' field to guide the model to suggestions that are more relevant to the specific needs of the project, or use the custom suggestions 💎 tool
    • With large PRs, best quality will be obtained by using 'improve --extended' mode.
    More PR-Agent commands

    To invoke the PR-Agent, add a comment using one of the following commands:

    • /review: Request a review of your Pull Request.
    • /describe: Update the PR title and description based on the contents of the PR.
    • /improve [--extended]: Suggest code improvements. Extended mode provides a higher quality feedback.
    • /ask <QUESTION>: Ask a question about the PR.
    • /update_changelog: Update the changelog based on the PR's contents.
    • /add_docs 💎: Generate docstring for new components introduced in the PR.
    • /generate_labels 💎: Generate labels for the PR based on the PR's contents.
    • /analyze 💎: Automatically analyzes the PR, and presents changes walkthrough for each component.

    See the tools guide for more details.
    To list the possible configuration parameters, add a /config comment.

    See the improve usage page for a more comprehensive guide on using this tool.

    Copy link
    Contributor

    github-actions bot commented Mar 3, 2024

    Summary:

    • License scan: failure
    • Credentials scan: failure
    • Vulnerabilities scan: failure
    • Unit test: success
    • Go linting: success

    YiscahLevySilas1 and others added 3 commits March 3, 2024 10:14
    Signed-off-by: YiscahLevySilas1 <yiscahls@armosec.io>
    …ibrary into split-resources-controls
    
    Signed-off-by: YiscahLevySilas1 <yiscahls@armosec.io>
    Copy link
    Contributor

    github-actions bot commented Mar 3, 2024

    Summary:

    • License scan: failure
    • Credentials scan: failure
    • Vulnerabilities scan: failure
    • Unit test: success
    • Go linting: success

    @YiscahLevySilas1 YiscahLevySilas1 merged commit d4a1f9f into master Mar 3, 2024
    22 of 25 checks passed
    @YiscahLevySilas1 YiscahLevySilas1 deleted the split-resources-controls branch April 14, 2024 10:46
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    Projects
    None yet
    Development

    Successfully merging this pull request may close these issues.

    None yet

    3 participants