-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(misconf): Add support for aws_ami
Signed-off-by: Simar <[email protected]>
- Loading branch information
Showing
9 changed files
with
208 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package ami | ||
|
||
import ( | ||
"github.com/aquasecurity/trivy/pkg/iac/providers/aws/ami" | ||
"github.com/aquasecurity/trivy/pkg/iac/scanners/cloudformation/parser" | ||
iacTypes "github.com/aquasecurity/trivy/pkg/iac/types" | ||
) | ||
|
||
func Adapt(cfFile parser.FileContext) ami.AMI { | ||
return ami.AMI{ | ||
Metadata: cfFile.Metadata(), | ||
Owners: adaptAMIs(cfFile), | ||
} | ||
} | ||
|
||
func adaptAMIs(cfFile parser.FileContext) iacTypes.StringValueList { | ||
var owners iacTypes.StringValueList | ||
|
||
amis := cfFile.GetResourcesByType("AWS::EC2::Image") | ||
for _, resource := range amis { | ||
owners = append(owners, resource.GetStringProperty("Owners")) | ||
} | ||
|
||
return owners | ||
} |
52 changes: 52 additions & 0 deletions
52
pkg/iac/adapters/cloudformation/aws/ami/amianalyzer_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package ami | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/aquasecurity/trivy/pkg/iac/adapters/cloudformation/testutil" | ||
"github.com/aquasecurity/trivy/pkg/iac/providers/aws/ami" | ||
iacTypes "github.com/aquasecurity/trivy/pkg/iac/types" | ||
) | ||
|
||
func TestAMI(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input string | ||
expected ami.AMI | ||
}{ | ||
{ | ||
name: "AMI with owner", | ||
input: ` | ||
Resources: | ||
MyAMI: | ||
Type: 'AWS::EC2::Image' | ||
Properties: | ||
Owners: amazon`, | ||
expected: ami.AMI{ | ||
Metadata: iacTypes.NewTestMetadata(), | ||
Owners: iacTypes.StringValueList{ | ||
iacTypes.StringTest("amazon"), | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "AMI without owner", | ||
input: ` | ||
Resources: | ||
MyAMI: | ||
Type: 'AWS::EC2::Image' | ||
Properties: | ||
Name: test-ami`, | ||
expected: ami.AMI{ | ||
Metadata: iacTypes.NewTestMetadata(), | ||
Owners: iacTypes.StringValueList{iacTypes.StringTest("")}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
testutil.AdaptAndCompare(t, tt.input, tt.expected, Adapt) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package ami | ||
|
||
import ( | ||
"github.com/aquasecurity/trivy/pkg/iac/providers/aws/ami" | ||
"github.com/aquasecurity/trivy/pkg/iac/terraform" | ||
iacTypes "github.com/aquasecurity/trivy/pkg/iac/types" | ||
) | ||
|
||
func Adapt(modules terraform.Modules) ami.AMI { | ||
return ami.AMI{ | ||
Owners: adaptAMIs(modules), | ||
} | ||
} | ||
|
||
func adaptAMIs(modules terraform.Modules) iacTypes.StringValueList { | ||
var owners iacTypes.StringValueList | ||
|
||
for _, module := range modules { | ||
for _, resource := range module.GetResourcesByType("aws_ami") { | ||
owners = append(owners, adaptOwners(resource)...) | ||
} | ||
} | ||
return owners | ||
} | ||
|
||
func adaptOwners(resource *terraform.Block) iacTypes.StringValueList { | ||
ownersAttr := resource.GetAttribute("owners") | ||
return ownersAttr.AsStringValuesOrDefault(resource, "") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package ami | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/aquasecurity/trivy/internal/testutil" | ||
"github.com/aquasecurity/trivy/pkg/iac/adapters/terraform/tftestutil" | ||
"github.com/aquasecurity/trivy/pkg/iac/providers/aws/ami" | ||
iacTypes "github.com/aquasecurity/trivy/pkg/iac/types" | ||
) | ||
|
||
func Test_AMI(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input string | ||
expected ami.AMI | ||
}{ | ||
{ | ||
name: "AMI with single owner", | ||
input: ` | ||
resource "aws_ami" "example" { | ||
owners = ["amazon"] | ||
}`, | ||
expected: ami.AMI{ | ||
Metadata: iacTypes.NewTestMetadata(), | ||
Owners: iacTypes.StringValueList{ | ||
iacTypes.StringTest("amazon"), | ||
}}, | ||
}, | ||
{ | ||
name: "AMI with multiple owners", | ||
input: ` | ||
resource "aws_ami" "example" { | ||
owners = ["amazon", "badguys"] | ||
}`, | ||
expected: ami.AMI{ | ||
Metadata: iacTypes.NewTestMetadata(), | ||
Owners: iacTypes.StringValueList{ | ||
iacTypes.StringTest("amazon"), | ||
iacTypes.StringTest("badguys"), | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "AMI without owner", | ||
input: ` | ||
resource "aws_ami" "example" { | ||
name = "test-ami" | ||
}`, | ||
expected: ami.AMI{ | ||
Metadata: iacTypes.NewTestMetadata(), | ||
Owners: nil, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
modules := tftestutil.CreateModulesFromSource(t, tt.input, ".tf") | ||
adapted := Adapt(modules) | ||
testutil.AssertDefsecEqual(t, tt.expected, adapted) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package ami | ||
|
||
import iacTypes "github.com/aquasecurity/trivy/pkg/iac/types" | ||
|
||
type AMI struct { | ||
Metadata iacTypes.Metadata | ||
Owners iacTypes.StringValueList | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters