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

Feature: Added exceptions flag in scan image cmd #1568

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

VaibhavMalik4187
Copy link
Contributor

Overview

This commit introduces the "exceptions" flag in the scan image command. Users can pass a list of vulnerabilities they ignore while scanning an image using this flag.

Fixes: #1564

How to Test

  1. kubescape scan image <imageName>
  2. kubescape scan image <imageName> -E <comma separated list of exceptions, e.g: CVE-2023-6879,CVE-2023-45853>

Screenshots

image
image

@VaibhavMalik4187
Copy link
Contributor Author

@dwertent @matthyx could you please review this PR whenever you have some spare time? Thanks!

@matthyx
Copy link
Contributor

matthyx commented Jan 1, 2024

Looks good @VaibhavMalik4187 do you think we can add a test covering this new feature?

@VaibhavMalik4187
Copy link
Contributor Author

Looks good @VaibhavMalik4187 do you think we can add a test covering this new feature?

Thanks for the approval @matthyx. Yes, I'll add a test soon.

@VaibhavMalik4187 VaibhavMalik4187 force-pushed the feature-exceptions branch 2 times, most recently from 87a704c to 590b535 Compare January 2, 2024 12:25
@VaibhavMalik4187
Copy link
Contributor Author

Looks good @VaibhavMalik4187 do you think we can add a test covering this new feature?

Thanks for the approval @matthyx. Yes, I'll add a test soon.

@matthyx I've added a few tests.

matthyx
matthyx previously approved these changes Jan 3, 2024
@matthyx
Copy link
Contributor

matthyx commented Jan 3, 2024

waiting for @dwertent to confirm this is what we want

@dwertent
Copy link
Contributor

dwertent commented Jan 9, 2024

Hi @VaibhavMalik4187 Thank you for contributing this PR.
I am still debating how we want the exception support to work.
My initial idea was, to have an object similar to the configurationScanning exceptions file.
e.g.

[
    {
        "metadata": {    
                "name": "exclude-allowed-hostPath-control",
        },
        "kind": "VulnerabilitiesIgnorePolicy",
        "targets": [
            {
                "designatorType": "Attributes",
                "attributes": {
                    "imageTag": ".*",
                    "registry": "docker.io"
                }
            }
        ],
        "vulnerabilities": [
                "CVE-0000", 
                "CVE-11111"
        ]
    }
]

This will allow us to properly support CICDs as well as scan images from the CLI.

Thoughts?

/cc: @slashben @yossi77

@VaibhavMalik4187
Copy link
Contributor Author

Hi @VaibhavMalik4187 Thank you for contributing this PR. I am still debating how we want the exception support to work. My initial idea was, to have an object similar to the configurationScanning exceptions file. e.g.

[
    {
        "metadata": {    
                "name": "exclude-allowed-hostPath-control",
        },
        "kind": "VulnerabilitiesIgnorePolicy",
        "targets": [
            {
                "designatorType": "Attributes",
                "attributes": {
                    "imageTag": ".*",
                    "registry": "docker.io"
                }
            }
        ],
        "vulnerabilities": [
                "CVE-0000", 
                "CVE-11111"
        ]
    }
]

This will allow us to properly support CICDs as well as scan images from the CLI.

Thoughts?

/cc: @slashben @yossi77

Hi @dwertent, thanks for the feedback. I agree that having a consistent exception format for both configuration scanning and vulnerability scanning would be beneficial for CI/CD integration and CLI usage. I'm open to aligning my PR with the proposed object structure.

@dwertent
Copy link
Contributor

dwertent commented Jan 10, 2024

@VaibhavMalik4187 Great, let's do it 🥇

The attributes should support:

  1. registry
  2. organization
  3. imageName
  4. imageTag

e.g. quay.io/kubescape/kubescape-cli:v3.0.0

  1. registry - quay.io
  2. organization - kubescape
  3. imageName - kubescape-cli
  4. imageTag - v3.0.0

We should support regex as well.

Also, we should exclude based on severities as well.
The final object should look like this:

[
    {
        "metadata": {    
                "name": "exclude-allowed-hostPath-control",
        },
        "kind": "VulnerabilitiesIgnorePolicy",
        "targets": [
            {
                "designatorType": "Attributes",
                "attributes": {
                    "imageName": "nginx",
                    "registry": "docker.io" 
                }
            }
        ],
        "vulnerabilities": [
                "CVE-0000",    // not case sensitive
                "CVE-11111"
        ],
        "severities": [
           "low"   // not case sensitive
         ]
    }
]

What do you think?

@VaibhavMalik4187
Copy link
Contributor Author

VaibhavMalik4187 commented Jan 10, 2024

@VaibhavMalik4187 Great, let's do it 🥇

The attributes should support:

1. `registry`

2. `organization`

3. `imageName`

4. `imageTag`

e.g. quay.io/kubescape/kubescape-cli:v3.0.0

1. `registry` - `quay.io`

2. `organization` - `kubescape`

3. `imageName` - `kubescape-cli`

4. `imageTag` - `v3.0.0`

We should support regex as well.

Also, we should exclude based on severities as well. The final object should look like this:

[
    {
        "metadata": {    
                "name": "exclude-allowed-hostPath-control",
        },
        "kind": "VulnerabilitiesIgnorePolicy",
        "targets": [
            {
                "designatorType": "Attributes",
                "attributes": {
                    "imageName": "nginx",
                    "registry": "docker.io" 
                }
            }
        ],
        "vulnerabilities": [
                "CVE-0000",    // not case sensitive
                "CVE-11111"
        ],
        "severities": [
           "low"   // not case sensitive
         ]
    }
]

What do you think?

I'm not very sure about the regex part. Where do you intend to use regex? Could you please elaborate a bit more? In the exceptions object fields?

@VaibhavMalik4187
Copy link
Contributor Author

@dwertent @matthyx I've added some commits to try to align my PR with the ideas suggested in the conversation above. Could you please review this PR once again? Thanks!

@VaibhavMalik4187
Copy link
Contributor Author

@matthyx @dwertent ...

@dwertent
Copy link
Contributor

@VaibhavMalik4187 We are currently very busy with some features.
We will review this PR shortly :)
Thank you for contributing!

@VaibhavMalik4187
Copy link
Contributor Author

@VaibhavMalik4187 We are currently very busy with some features. We will review this PR shortly :) Thank you for contributing!

@dwertent,Thanks for the update.

@VaibhavMalik4187
Copy link
Contributor Author

@matthyx @dwertent any updates?

@VaibhavMalik4187
Copy link
Contributor Author

@craigbox @slashben could you please take a look?

Copy link
Contributor

@dwertent dwertent left a comment

Choose a reason for hiding this comment

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

Hi @VaibhavMalik4187,

Appreciate your contribution! Currently, our team is immersed in prioritizing other features, causing a delay in reviewing your Pull Request (PR). Upon my initial review, I've observed that the unit tests comprehensively cover the entire image scan command.

However, there's a potential concern with testing in diverse environments, given that mock data might not precisely replicate real-world scenarios. To address this, I recommend focusing the unit tests specifically on a function designed to handle exceptions. Essentially, consider implementing a function like the following:

func ignoreVulnerabilities(imageVulnerabilities, exceptions) imageVulnerabilities {
    // Implement logic to filter out vulnerabilities based on exceptions
    // Return the list without the ignored vulnerabilities
}

Please accompany this with thorough unit tests incorporating various scenarios, such as those involving regular expressions (regex). Verify that the function effectively handles different types of mock vulnerabilities and exceptions.

Feel free to update the PR with this refined approach, ensuring the unit tests validate the functionality in a diverse set of circumstances. Thanks for your efforts, and let me know if you have any questions or need further clarification.

@@ -69,6 +75,8 @@ func getImageCmd(ks meta.IKubescape, scanInfo *cautils.ScanInfo) *cobra.Command
},
}

// The exceptions flag
cmd.PersistentFlags().StringVarP(&exceptions, "exceptions", "E", "", "Path to the exceptions file")
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you please remove the short flag E

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure thing

@VaibhavMalik4187
Copy link
Contributor Author

Hi @VaibhavMalik4187,

Appreciate your contribution! Currently, our team is immersed in prioritizing other features, causing a delay in reviewing your Pull Request (PR). Upon my initial review, I've observed that the unit tests comprehensively cover the entire image scan command.

However, there's a potential concern with testing in diverse environments, given that mock data might not precisely replicate real-world scenarios. To address this, I recommend focusing the unit tests specifically on a function designed to handle exceptions. Essentially, consider implementing a function like the following:

func ignoreVulnerabilities(imageVulnerabilities, exceptions) imageVulnerabilities {
    // Implement logic to filter out vulnerabilities based on exceptions
    // Return the list without the ignored vulnerabilities
}

Please accompany this with thorough unit tests incorporating various scenarios, such as those involving regular expressions (regex). Verify that the function effectively handles different types of mock vulnerabilities and exceptions.

Feel free to update the PR with this refined approach, ensuring the unit tests validate the functionality in a diverse set of circumstances. Thanks for your efforts, and let me know if you have any questions or need further clarification.

@dwertent, thank you very much for reviewing the code. I'll update it shortly to ensure that the tests can replicate real-world scenarios and verify that the code functions as intended. I appreciate these suggestions.

This commit introduces the "exceptions" flag in the scan image command.
Users can pass a list of vulnerabilities they ignore while scanning an
image using this flag. Also added tests for the same.

Fixes: kubescape#1564

Signed-off-by: VaibhavMalik4187 <vaibhavmalik2018@gmail.com>
Added initial commit to start loading image exceptions from json files.

Currently, it supports vulnerability exceptions using their CVE-IDs.

Signed-off-by: VaibhavMalik4187 <vaibhavmalik2018@gmail.com>
This commit add relevant functions to support severity exceptions during
image scan.

Signed-off-by: VaibhavMalik4187 <vaibhavmalik2018@gmail.com>
This commit introduces the ability to specify targets in image
exceptions. Each target will have the following 4 attributes:

1. Registry
2. Organization
3. ImageName
4. ImageTag

These attributes will be used to match against the canonical image name
of the image to be scanned. The vulnerabilites and the severities
specified in the VulnerabilitiesIgnorePolicy object will be considered
only if the image to be scanned matches the targets specified for that
policy. Regular expressions can also be used to specify the image
attributes.

Signed-off-by: VaibhavMalik4187 <vaibhavmalik2018@gmail.com>
Signed-off-by: VaibhavMalik4187 <vaibhavmalik2018@gmail.com>
@VaibhavMalik4187
Copy link
Contributor Author

@dwertent I've refactored the code and added more comprehensive tests. Could you please have another look at this PR whenever you have some spare time? Thanks!

@dwertent
Copy link
Contributor

dwertent commented Feb 8, 2024

Thank you!
I will look into it

@matthyx matthyx requested a review from dwertent April 30, 2024 13:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow Kubescape image scan to have an allowed exception/CVE list
3 participants