Skip to content

Commit a28bac9

Browse files
author
Martin
authored
Merge pull request #1591 from snyk/fix/account_public_access_block
fix issue when scanning without any s3_account_public_access_block
2 parents a1427f4 + 4969995 commit a28bac9

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

enumeration/remote/aws/repository/s3control_repository.go

+15
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package repository
22

33
import (
44
"github.com/aws/aws-sdk-go/aws"
5+
"github.com/aws/aws-sdk-go/aws/awserr"
56
"github.com/aws/aws-sdk-go/service/s3control"
67
"github.com/snyk/driftctl/enumeration/remote/aws/client"
78
"github.com/snyk/driftctl/enumeration/remote/cache"
@@ -33,6 +34,10 @@ func (s *s3ControlRepository) DescribeAccountPublicAccessBlock(accountID string)
3334
})
3435

3536
if err != nil {
37+
if s.shouldSuppressError(err) {
38+
return nil, nil
39+
}
40+
3641
return nil, err
3742
}
3843

@@ -41,3 +46,13 @@ func (s *s3ControlRepository) DescribeAccountPublicAccessBlock(accountID string)
4146
s.cache.Put(cacheKey, result)
4247
return result, nil
4348
}
49+
50+
func (s *s3ControlRepository) shouldSuppressError(err error) bool {
51+
if requestFailure, ok := err.(awserr.RequestFailure); ok {
52+
if requestFailure.Code() == "NoSuchPublicAccessBlockConfiguration" {
53+
// do not throw the error up if there is no access block config
54+
return true
55+
}
56+
}
57+
return false
58+
}

enumeration/remote/aws/repository/s3control_repository_test.go

+21-8
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"github.com/stretchr/testify/mock"
1111

1212
"github.com/aws/aws-sdk-go/aws"
13-
"github.com/aws/aws-sdk-go/aws/awserr"
1413
"github.com/r3labs/diff/v2"
1514
awstest "github.com/snyk/driftctl/test/aws"
1615
"github.com/stretchr/testify/assert"
@@ -23,10 +22,10 @@ func Test_s3ControlRepository_DescribeAccountPublicAccessBlock(t *testing.T) {
2322
name string
2423
mocks func(client *awstest.MockFakeS3Control)
2524
want *s3control.PublicAccessBlockConfiguration
26-
wantErr error
25+
wantErr bool
2726
}{
2827
{
29-
name: "describe account public accessblock",
28+
name: "describe account public access block",
3029
mocks: func(client *awstest.MockFakeS3Control) {
3130
client.On("GetPublicAccessBlock", mock.Anything).Return(
3231
&s3control.GetPublicAccessBlockOutput{
@@ -48,15 +47,29 @@ func Test_s3ControlRepository_DescribeAccountPublicAccessBlock(t *testing.T) {
4847
},
4948
},
5049
{
51-
name: "Error detting account public accessblock",
50+
name: "Error getting account public access block",
5251
mocks: func(client *awstest.MockFakeS3Control) {
52+
fakeRequestFailure := &awstest.MockFakeRequestFailure{}
53+
fakeRequestFailure.On("Code").Return("FakeErrorCode")
5354
client.On("GetPublicAccessBlock", mock.Anything).Return(
5455
nil,
55-
awserr.NewRequestFailure(nil, 403, ""),
56+
fakeRequestFailure,
5657
).Once()
5758
},
5859
want: nil,
59-
wantErr: awserr.NewRequestFailure(nil, 403, ""),
60+
wantErr: true,
61+
},
62+
{
63+
name: "Error no account public access block",
64+
mocks: func(client *awstest.MockFakeS3Control) {
65+
fakeRequestFailure := &awstest.MockFakeRequestFailure{}
66+
fakeRequestFailure.On("Code").Return("NoSuchPublicAccessBlockConfiguration")
67+
client.On("GetPublicAccessBlock", mock.Anything).Return(
68+
nil,
69+
fakeRequestFailure,
70+
).Once()
71+
},
72+
want: nil,
6073
},
6174
}
6275
for _, tt := range tests {
@@ -69,9 +82,9 @@ func Test_s3ControlRepository_DescribeAccountPublicAccessBlock(t *testing.T) {
6982
r := NewS3ControlRepository(&factory, store)
7083
got, err := r.DescribeAccountPublicAccessBlock(accountID)
7184
factory.AssertExpectations(t)
72-
assert.Equal(t, tt.wantErr, err)
85+
assert.Equal(t, tt.wantErr, err != nil)
7386

74-
if err == nil {
87+
if err == nil && got != nil {
7588
// Check that results were cached
7689
cachedData, err := r.DescribeAccountPublicAccessBlock(accountID)
7790
assert.NoError(t, err)

enumeration/remote/aws/s3_account_public_access_block_enumerator.go

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ func (e *S3AccountPublicAccessBlockEnumerator) Enumerate() ([]*resource.Resource
3737

3838
results := make([]*resource.Resource, 0, 1)
3939

40+
if accountPublicAccessBlock == nil {
41+
return results, nil
42+
}
43+
4044
results = append(
4145
results,
4246
e.factory.CreateAbstractResource(

0 commit comments

Comments
 (0)