From 3d7ce4210a548d3d1ed5052d0d802f627227c01a Mon Sep 17 00:00:00 2001 From: jonaustin09 Date: Thu, 15 Jun 2023 20:39:20 +0400 Subject: [PATCH] fix: Fixed GetBucketAcl return type, fixed staticcheck uppercase error, fixed unit tests for PutActions --- backend/auth/acl.go | 9 +++++++++ backend/backend.go | 5 +++-- backend/backend_moq_test.go | 7 ++++--- backend/backend_test.go | 3 ++- backend/posix/posix.go | 10 ++++++---- s3api/controllers/backend_moq_test.go | 7 ++++--- s3api/controllers/base_test.go | 8 +++++++- 7 files changed, 35 insertions(+), 14 deletions(-) diff --git a/backend/auth/acl.go b/backend/auth/acl.go index 21f344b..8140002 100644 --- a/backend/auth/acl.go +++ b/backend/auth/acl.go @@ -26,3 +26,12 @@ type Grantee struct { Permission types.Permission Access string } + +type GetBucketAclOutput struct { + Owner *types.Owner + AccessControlList AccessControlList +} + +type AccessControlList struct { + Grants []types.Grant +} diff --git a/backend/backend.go b/backend/backend.go index a778d5d..ee5bb48 100644 --- a/backend/backend.go +++ b/backend/backend.go @@ -20,6 +20,7 @@ import ( "github.com/aws/aws-sdk-go-v2/service/s3" "github.com/aws/aws-sdk-go-v2/service/s3/types" + "github.com/versity/versitygw/backend/auth" "github.com/versity/versitygw/s3err" "github.com/versity/versitygw/s3response" ) @@ -32,7 +33,7 @@ type Backend interface { ListBuckets() (*s3.ListBucketsOutput, error) HeadBucket(bucket string) (*s3.HeadBucketOutput, error) - GetBucketAcl(bucket string) (*s3.GetBucketAclOutput, error) + GetBucketAcl(bucket string) (*auth.GetBucketAclOutput, error) PutBucket(bucket, owner string) error PutBucketAcl(*s3.PutBucketAclInput) error DeleteBucket(bucket string) error @@ -90,7 +91,7 @@ func (BackendUnsupported) RestoreObject(bucket, object string, restoreRequest *s func (BackendUnsupported) UploadPartCopy(*s3.UploadPartCopyInput) (*s3.UploadPartCopyOutput, error) { return nil, s3err.GetAPIError(s3err.ErrNotImplemented) } -func (BackendUnsupported) GetBucketAcl(bucket string) (*s3.GetBucketAclOutput, error) { +func (BackendUnsupported) GetBucketAcl(bucket string) (*auth.GetBucketAclOutput, error) { return nil, s3err.GetAPIError(s3err.ErrNotImplemented) } func (BackendUnsupported) HeadBucket(bucket string) (*s3.HeadBucketOutput, error) { diff --git a/backend/backend_moq_test.go b/backend/backend_moq_test.go index 398133b..f3a88d3 100644 --- a/backend/backend_moq_test.go +++ b/backend/backend_moq_test.go @@ -6,6 +6,7 @@ package backend import ( "github.com/aws/aws-sdk-go-v2/service/s3" "github.com/aws/aws-sdk-go-v2/service/s3/types" + "github.com/versity/versitygw/backend/auth" "github.com/versity/versitygw/s3response" "io" "sync" @@ -45,7 +46,7 @@ var _ Backend = &BackendMock{} // DeleteObjectsFunc: func(bucket string, objects *s3.DeleteObjectsInput) error { // panic("mock out the DeleteObjects method") // }, -// GetBucketAclFunc: func(bucket string) (*s3.GetBucketAclOutput, error) { +// GetBucketAclFunc: func(bucket string) (*auth.GetBucketAclOutput, error) { // panic("mock out the GetBucketAcl method") // }, // GetObjectFunc: func(bucket string, object string, acceptRange string, writer io.Writer) (*s3.GetObjectOutput, error) { @@ -146,7 +147,7 @@ type BackendMock struct { DeleteObjectsFunc func(bucket string, objects *s3.DeleteObjectsInput) error // GetBucketAclFunc mocks the GetBucketAcl method. - GetBucketAclFunc func(bucket string) (*s3.GetBucketAclOutput, error) + GetBucketAclFunc func(bucket string) (*auth.GetBucketAclOutput, error) // GetObjectFunc mocks the GetObject method. GetObjectFunc func(bucket string, object string, acceptRange string, writer io.Writer) (*s3.GetObjectOutput, error) @@ -796,7 +797,7 @@ func (mock *BackendMock) DeleteObjectsCalls() []struct { } // GetBucketAcl calls GetBucketAclFunc. -func (mock *BackendMock) GetBucketAcl(bucket string) (*s3.GetBucketAclOutput, error) { +func (mock *BackendMock) GetBucketAcl(bucket string) (*auth.GetBucketAclOutput, error) { if mock.GetBucketAclFunc == nil { panic("BackendMock.GetBucketAclFunc: method is nil but Backend.GetBucketAcl was just called") } diff --git a/backend/backend_test.go b/backend/backend_test.go index a4dc1b6..62515f5 100644 --- a/backend/backend_test.go +++ b/backend/backend_test.go @@ -21,6 +21,7 @@ import ( "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/s3" "github.com/aws/aws-sdk-go-v2/service/s3/types" + "github.com/versity/versitygw/backend/auth" "github.com/versity/versitygw/s3err" ) @@ -120,7 +121,7 @@ func TestBackend_GetBucketAcl(t *testing.T) { tests = append(tests, test{ name: "get bucket acl error", c: &BackendMock{ - GetBucketAclFunc: func(bucket string) (*s3.GetBucketAclOutput, error) { + GetBucketAclFunc: func(bucket string) (*auth.GetBucketAclOutput, error) { return nil, s3err.GetAPIError(s3err.ErrNotImplemented) }, }, diff --git a/backend/posix/posix.go b/backend/posix/posix.go index 81c0408..79690f9 100644 --- a/backend/posix/posix.go +++ b/backend/posix/posix.go @@ -1156,7 +1156,7 @@ func (p *Posix) PutBucketAcl(input *s3.PutBucketAclInput) error { return err } if len(accList) > 0 { - return fmt.Errorf("Accounts does not exist: %s", strings.Join(accList, ", ")) + return fmt.Errorf("accounts does not exist: %s", strings.Join(accList, ", ")) } for _, elem := range grantees { @@ -1188,7 +1188,7 @@ func (p *Posix) PutBucketAcl(input *s3.PutBucketAclInput) error { return nil } -func (p *Posix) GetBucketAcl(bucket string) (*s3.GetBucketAclOutput, error) { +func (p *Posix) GetBucketAcl(bucket string) (*auth.GetBucketAclOutput, error) { var ACL auth.ACL acl, err := xattr.Get(bucket, "user.acl") if err != nil { @@ -1205,11 +1205,13 @@ func (p *Posix) GetBucketAcl(bucket string) (*s3.GetBucketAclOutput, error) { grants = append(grants, types.Grant{Grantee: &types.Grantee{ID: &elem.Access}, Permission: elem.Permission}) } - return &s3.GetBucketAclOutput{ + return &auth.GetBucketAclOutput{ Owner: &types.Owner{ ID: &ACL.Owner, }, - Grants: grants, + AccessControlList: auth.AccessControlList{ + Grants: grants, + }, }, nil } diff --git a/s3api/controllers/backend_moq_test.go b/s3api/controllers/backend_moq_test.go index f1fcbcb..0d958da 100644 --- a/s3api/controllers/backend_moq_test.go +++ b/s3api/controllers/backend_moq_test.go @@ -7,6 +7,7 @@ import ( "github.com/aws/aws-sdk-go-v2/service/s3" "github.com/aws/aws-sdk-go-v2/service/s3/types" "github.com/versity/versitygw/backend" + "github.com/versity/versitygw/backend/auth" "github.com/versity/versitygw/s3response" "io" "sync" @@ -46,7 +47,7 @@ var _ backend.Backend = &BackendMock{} // DeleteObjectsFunc: func(bucket string, objects *s3.DeleteObjectsInput) error { // panic("mock out the DeleteObjects method") // }, -// GetBucketAclFunc: func(bucket string) (*s3.GetBucketAclOutput, error) { +// GetBucketAclFunc: func(bucket string) (*auth.GetBucketAclOutput, error) { // panic("mock out the GetBucketAcl method") // }, // GetObjectFunc: func(bucket string, object string, acceptRange string, writer io.Writer) (*s3.GetObjectOutput, error) { @@ -147,7 +148,7 @@ type BackendMock struct { DeleteObjectsFunc func(bucket string, objects *s3.DeleteObjectsInput) error // GetBucketAclFunc mocks the GetBucketAcl method. - GetBucketAclFunc func(bucket string) (*s3.GetBucketAclOutput, error) + GetBucketAclFunc func(bucket string) (*auth.GetBucketAclOutput, error) // GetObjectFunc mocks the GetObject method. GetObjectFunc func(bucket string, object string, acceptRange string, writer io.Writer) (*s3.GetObjectOutput, error) @@ -797,7 +798,7 @@ func (mock *BackendMock) DeleteObjectsCalls() []struct { } // GetBucketAcl calls GetBucketAclFunc. -func (mock *BackendMock) GetBucketAcl(bucket string) (*s3.GetBucketAclOutput, error) { +func (mock *BackendMock) GetBucketAcl(bucket string) (*auth.GetBucketAclOutput, error) { if mock.GetBucketAclFunc == nil { panic("BackendMock.GetBucketAclFunc: method is nil but Backend.GetBucketAcl was just called") } diff --git a/s3api/controllers/base_test.go b/s3api/controllers/base_test.go index c4af7e8..c92bead 100644 --- a/s3api/controllers/base_test.go +++ b/s3api/controllers/base_test.go @@ -28,6 +28,7 @@ import ( "github.com/gofiber/fiber/v2" "github.com/valyala/fasthttp" "github.com/versity/versitygw/backend" + "github.com/versity/versitygw/backend/auth" "github.com/versity/versitygw/s3err" "github.com/versity/versitygw/s3response" ) @@ -231,7 +232,7 @@ func TestS3ApiController_ListActions(t *testing.T) { app := fiber.New() s3ApiController := S3ApiController{be: &BackendMock{ - GetBucketAclFunc: func(bucket string) (*s3.GetBucketAclOutput, error) { + GetBucketAclFunc: func(bucket string) (*auth.GetBucketAclOutput, error) { return nil, nil }, ListMultipartUploadsFunc: func(output *s3.ListMultipartUploadsInput) (s3response.ListMultipartUploadsResponse, error) { @@ -337,6 +338,11 @@ func TestS3ApiController_PutBucketActions(t *testing.T) { return nil }, }} + // Mock ctx.Locals + app.Use(func(ctx *fiber.Ctx) error { + ctx.Locals("access", "valid access") + return ctx.Next() + }) app.Put("/:bucket", s3ApiController.PutBucketActions) // Error case