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

feat(vm): add region option to vm scan to be able to scan any region's ami and ebs snapshots #3284

Merged

Conversation

tockn
Copy link
Contributor

@tockn tockn commented Dec 10, 2022

Description

Hi. Thank you for such a wonderful product!

VM scanning is performed in the current implementation using default region settings read from environment variables, etc. The only way to scan for EBS snapshots (or AMIs) in any region was probably to dynamically change the AWS_REGION environment variable.
Therefore, in this pr, I have implemented the --region option so that EBS snapshots and AMIs can be fetched by specifying any region.

This is just a personal usage that I implemented on my own, so if you have a different idea, please close this pr!

Related issues

N/A

before

❯ trivy vm ebs:snap-xxxxxxxxxxxx
2022-12-10T16:07:11.899+0900    INFO    Vulnerability scanning is enabled
2022-12-10T16:07:11.901+0900    INFO    Secret scanning is enabled
2022-12-10T16:07:11.901+0900    INFO    If your scanning is slow, please try '--security-checks vuln' to disable secret scanning
2022-12-10T16:07:11.901+0900    INFO    Please see also https://aquasecurity.github.io/trivy/dev/docs/secret/scanning/#recommendation for faster secret detection
2022-12-10T16:07:12.098+0900    FATAL   vm scan error: scan error: scan failed: failed analysis: EBS open error: EBS error: operation error EBS: ListSnapshotBlocks, https response error StatusCode: 404, RequestID: ---------------, ResourceNotFoundException: The snapshot 'snap-xxxxxxxxxxxx' does not exist.

after

❯ trivy vm --region us-west-2 ebs:snap-xxxxxxxxxxxx
2022-12-10T16:26:03.407+0900    INFO    Vulnerability scanning is enabled
2022-12-10T16:26:03.409+0900    INFO    Secret scanning is enabled
2022-12-10T16:26:03.409+0900    INFO    If your scanning is slow, please try '--security-checks vuln' to disable secret scanning
2022-12-10T16:26:03.409+0900    INFO    Please see also https://aquasecurity.github.io/trivy/dev/docs/secret/scanning/#recommendation for faster secret detection
....

Checklist

  • I've read the guidelines for contributing to this repository.
  • I've followed the conventions in the PR title.
  • I've added tests that prove my fix is effective or that my feature works.
  • I've updated the documentation with the relevant information (if needed).
  • I've added usage information (if the PR introduces new options)
  • I've included a "before" and "after" example to the description (if the PR is a user interface change).

@tockn tockn requested a review from knqyf263 as a code owner December 10, 2022 07:47
@CLAassistant
Copy link

CLAassistant commented Dec 10, 2022

CLA assistant check
All committers have signed the CLA.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@@ -0,0 +1,38 @@
package flag
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you use AWSFlagGroup instead?

type AWSFlagGroup struct {
Region *Flag
Endpoint *Flag
Services *Flag
Account *Flag
ARN *Flag
}

If you want to scan a AMI of non-default setting region, you can set any region via `--region` option.

```shell
$ trivy vm --region ap-northeast-1 ami:ami-0123456789abcdefg
Copy link
Collaborator

Choose a reason for hiding this comment

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

VM scanning is not dedicated to AWS. We will probably add support for GCP and Azure.

Suggested change
$ trivy vm --region ap-northeast-1 ami:ami-0123456789abcdefg
$ trivy vm --aws-region ap-northeast-1 ami:ami-0123456789abcdefg

@@ -52,6 +58,13 @@ $ trivy vm --security-checks vuln ebs:snap-0123456789abcdefg
!!! tip
The scanning could be faster if you enable only vulnerability scanning (`--security-checks vuln`) because Trivy tries to download only necessary blocks for vulnerability detection.

If you want to scan an EBS Snapshot of non-default setting region, you can set any region via `--region` option.
Copy link
Collaborator

Choose a reason for hiding this comment

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

ditto

@@ -884,6 +884,7 @@ func NewVMCommand(globalFlags *flag.GlobalFlagGroup) *cobra.Command {
ScanFlagGroup: flag.NewScanFlagGroup(),
SecretFlagGroup: flag.NewSecretFlagGroup(),
VulnerabilityFlagGroup: flag.NewVulnerabilityFlagGroup(),
VMFlagGroups: flag.NewVMFlagGroup(),
Copy link
Collaborator

Choose a reason for hiding this comment

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

You can define a custom flag as below.

awsFlagGroup := flag.AWSFlagGroup{
	Region: flag.Flag{
		Name:       "aws-region",
		ConfigName: "aws.region",
		Value:      "",
		Usage:      "AWS region to scan",
	}
}

The following variable should be exported so it could be reused. I'll refactor that later.

awsRegionFlag = Flag{
Name: "region",
ConfigName: "cloud.aws.region",
Value: "",
Usage: "AWS Region to scan",
}

@tockn
Copy link
Contributor Author

tockn commented Dec 12, 2022

@knqyf263 Thank you for reviewing! I've fixed those points.

@knqyf263 knqyf263 merged commit e92266f into aquasecurity:main Dec 15, 2022
@knqyf263
Copy link
Collaborator

Thanks for your contribution!

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.

3 participants