From a560d84b25f5dcd029aa0ca687f76d08479bf46c Mon Sep 17 00:00:00 2001 From: Elie Date: Tue, 6 Jul 2021 10:47:28 +0200 Subject: [PATCH] Fix bucket policy return err when no policy exist --- pkg/remote/aws/repository/s3_repository.go | 5 +++++ pkg/remote/aws/repository/s3_repository_test.go | 14 ++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/pkg/remote/aws/repository/s3_repository.go b/pkg/remote/aws/repository/s3_repository.go index 7e698f36..a43f8f0e 100644 --- a/pkg/remote/aws/repository/s3_repository.go +++ b/pkg/remote/aws/repository/s3_repository.go @@ -58,6 +58,11 @@ func (s *s3Repository) GetBucketPolicy(bucketName, region string) (*string, erro &s3.GetBucketPolicyInput{Bucket: &bucketName}, ) if err != nil { + if awsErr, ok := err.(awserr.Error); ok { + if awsErr.Code() == "NoSuchBucketPolicy" { + return nil, nil + } + } return nil, errors.Wrapf( err, "Error listing bucket policy %s", diff --git a/pkg/remote/aws/repository/s3_repository_test.go b/pkg/remote/aws/repository/s3_repository_test.go index 254fe0f2..53d90078 100644 --- a/pkg/remote/aws/repository/s3_repository_test.go +++ b/pkg/remote/aws/repository/s3_repository_test.go @@ -297,6 +297,20 @@ func Test_s3Repository_GetBucketPolicy(t *testing.T) { }, want: awssdk.String("foobar"), }, + { + name: "get bucket location on 404", + bucketName: "test-bucket", + region: "us-east-1", + mocks: func(client *awstest.MockFakeS3) { + client.On("GetBucketPolicy", &s3.GetBucketPolicyInput{ + Bucket: aws.String("test-bucket"), + }).Return( + nil, + awserr.New("NoSuchBucketPolicy", "", nil), + ).Once() + }, + want: nil, + }, { name: "get bucket location when error", bucketName: "test-bucket",