Skip to content

Commit 09e37b7

Browse files
authored
feat(aws): apply filter options to result (aquasecurity#6367)
1 parent 87a9aa6 commit 09e37b7

File tree

5 files changed

+97
-6
lines changed

5 files changed

+97
-6
lines changed

pkg/cloud/aws/commands/run_test.go

+81
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,63 @@ const expectedS3ScanResult = `{
267267
}
268268
`
269269

270+
const expectedS3ScanResultWithExceptions = `{
271+
"CreatedAt": "2021-08-25T12:20:30.000000005Z",
272+
"ArtifactName": "12345678",
273+
"ArtifactType": "aws_account",
274+
"Metadata": {
275+
"ImageConfig": {
276+
"architecture": "",
277+
"created": "0001-01-01T00:00:00Z",
278+
"os": "",
279+
"rootfs": {
280+
"type": "",
281+
"diff_ids": null
282+
},
283+
"config": {}
284+
}
285+
},
286+
"Results": [
287+
{
288+
"Target": "arn:aws:s3:::examplebucket",
289+
"Class": "config",
290+
"Type": "cloud",
291+
"MisconfSummary": {
292+
"Successes": 0,
293+
"Failures": 1,
294+
"Exceptions": 8
295+
},
296+
"Misconfigurations": [
297+
{
298+
"Type": "AWS",
299+
"ID": "AVD-AWS-0094",
300+
"AVDID": "AVD-AWS-0094",
301+
"Title": "S3 buckets should each define an aws_s3_bucket_public_access_block",
302+
"Description": "The \"block public access\" settings in S3 override individual policies that apply to a given bucket, meaning that all public access can be controlled in one central types for that bucket. It is therefore good practice to define these settings for each bucket in order to clearly define the public access that can be allowed for it.",
303+
"Message": "Bucket does not have a corresponding public access block.",
304+
"Resolution": "Define a aws_s3_bucket_public_access_block for the given bucket to control public access policies",
305+
"Severity": "LOW",
306+
"PrimaryURL": "https://avd.aquasec.com/misconfig/avd-aws-0094",
307+
"References": [
308+
"https://avd.aquasec.com/misconfig/avd-aws-0094"
309+
],
310+
"Status": "FAIL",
311+
"Layer": {},
312+
"CauseMetadata": {
313+
"Resource": "arn:aws:s3:::examplebucket",
314+
"Provider": "aws",
315+
"Service": "s3",
316+
"Code": {
317+
"Lines": null
318+
}
319+
}
320+
}
321+
]
322+
}
323+
]
324+
}
325+
`
326+
270327
const expectedCustomScanResult = `{
271328
"CreatedAt": "2021-08-25T12:20:30.000000005Z",
272329
"ArtifactName": "12345678",
@@ -915,6 +972,7 @@ func Test_Run(t *testing.T) {
915972
regoPolicy string
916973
allServices []string
917974
inputData string
975+
ignoreFile string
918976
}{
919977
{
920978
name: "succeed with cached infra",
@@ -1140,6 +1198,25 @@ Summary Report for compliance: my-custom-spec
11401198
cacheContent: "testdata/s3andcloudtrailcache.json",
11411199
expectErr: true,
11421200
},
1201+
{
1202+
name: "ignore findings with .trivyignore",
1203+
options: flag.Options{
1204+
RegoOptions: flag.RegoOptions{SkipPolicyUpdate: true},
1205+
AWSOptions: flag.AWSOptions{
1206+
Region: "us-east-1",
1207+
Services: []string{"s3"},
1208+
Account: "12345678",
1209+
},
1210+
CloudOptions: flag.CloudOptions{
1211+
MaxCacheAge: time.Hour * 24 * 365 * 100,
1212+
},
1213+
MisconfOptions: flag.MisconfOptions{IncludeNonFailures: true},
1214+
},
1215+
cacheContent: "testdata/s3onlycache.json",
1216+
allServices: []string{"s3"},
1217+
ignoreFile: "testdata/.trivyignore",
1218+
want: expectedS3ScanResultWithExceptions,
1219+
},
11431220
}
11441221

11451222
ctx := clock.With(context.Background(), time.Date(2021, 8, 25, 12, 20, 30, 5, time.UTC))
@@ -1192,6 +1269,10 @@ Summary Report for compliance: my-custom-spec
11921269
require.NoError(t, os.WriteFile(cacheFile, cacheData, 0600))
11931270
}
11941271

1272+
if test.ignoreFile != "" {
1273+
test.options.ReportOptions.IgnoreFile = test.ignoreFile
1274+
}
1275+
11951276
err := Run(ctx, test.options)
11961277
if test.expectErr {
11971278
assert.Error(t, err)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
AVD-AWS-0086
2+
AVD-AWS-0087
3+
AVD-AWS-0088
4+
AVD-AWS-0090
5+
AVD-AWS-0132
6+
AVD-AWS-0091
7+
AVD-AWS-0092
8+
AVD-AWS-0093

pkg/cloud/report/report.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,18 @@ func Write(ctx context.Context, rep *Report, opt flag.Options, fromCache bool) e
7070
return writeCompliance(ctx, rep, opt, output)
7171
}
7272

73+
ignoreConf, err := result.ParseIgnoreFile(ctx, opt.IgnoreFile)
74+
if err != nil {
75+
return xerrors.Errorf("%s error: %w", opt.IgnoreFile, err)
76+
}
77+
7378
var filtered []types.Result
7479

7580
// filter results
7681
for _, resultsAtTime := range rep.Results {
7782
for _, res := range resultsAtTime.Results {
7883
resCopy := res
79-
if err := result.FilterResult(ctx, &resCopy, result.IgnoreConfig{}, result.FilterOption{
80-
Severities: opt.Severities,
81-
IncludeNonFailures: opt.IncludeNonFailures,
82-
}); err != nil {
84+
if err := result.FilterResult(ctx, &resCopy, ignoreConf, opt.FilterOpts()); err != nil {
8385
return err
8486
}
8587
sort.Slice(resCopy.Misconfigurations, func(i, j int) bool {

pkg/result/filter.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type FilterOption struct {
3737

3838
// Filter filters out the report
3939
func Filter(ctx context.Context, report types.Report, opt FilterOption) error {
40-
ignoreConf, err := parseIgnoreFile(ctx, opt.IgnoreFile)
40+
ignoreConf, err := ParseIgnoreFile(ctx, opt.IgnoreFile)
4141
if err != nil {
4242
return xerrors.Errorf("%s error: %w", opt.IgnoreFile, err)
4343
}

pkg/result/ignore.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ func (c *IgnoreConfig) MatchLicense(licenseID, filePath string) *IgnoreFinding {
181181
return c.Licenses.Match(licenseID, filePath, nil)
182182
}
183183

184-
func parseIgnoreFile(ctx context.Context, ignoreFile string) (IgnoreConfig, error) {
184+
func ParseIgnoreFile(ctx context.Context, ignoreFile string) (IgnoreConfig, error) {
185185
var conf IgnoreConfig
186186
if _, err := os.Stat(ignoreFile); errors.Is(err, fs.ErrNotExist) {
187187
// .trivyignore doesn't necessarily exist

0 commit comments

Comments
 (0)