mirror of
https://github.com/versity/versitygw.git
synced 2026-09-06 16:16:53 +00:00
Acl integration test (#115)
* feat: Added test an integration test case for acl actions(get, put), fixed PutBucketAcl actions bugs, fixed iam bugs on getting and creating user accounts * fix: Fixed acl unit tests * fix: Fixed cli path in exec command in acl integration test * fix: fixed account creation bug
This commit is contained in:
+53
-18
@@ -41,8 +41,8 @@ type S3ApiController struct {
|
||||
iam auth.IAMService
|
||||
}
|
||||
|
||||
func New(be backend.Backend) S3ApiController {
|
||||
return S3ApiController{be: be}
|
||||
func New(be backend.Backend, iam auth.IAMService) S3ApiController {
|
||||
return S3ApiController{be: be, iam: iam}
|
||||
}
|
||||
|
||||
func (c S3ApiController) ListBuckets(ctx *fiber.Ctx) error {
|
||||
@@ -248,13 +248,54 @@ func (c S3ApiController) PutBucketActions(ctx *fiber.Ctx) error {
|
||||
|
||||
grants := grantFullControl + grantRead + grantReadACP + granWrite + grantWriteACP
|
||||
|
||||
if grants != "" || acl != "" {
|
||||
if grants != "" && acl != "" {
|
||||
return errors.New("wrong api call")
|
||||
}
|
||||
if ctx.Request().URI().QueryArgs().Has("acl") {
|
||||
var input *s3.PutBucketAclInput
|
||||
|
||||
if acl != "" && acl != "private" && acl != "public-read" && acl != "public-read-write" {
|
||||
return errors.New("wrong api call")
|
||||
if len(ctx.Body()) > 0 {
|
||||
if grants+acl != "" {
|
||||
return s3err.GetAPIError(s3err.ErrInvalidRequest)
|
||||
}
|
||||
|
||||
var accessControlPolicy auth.AccessControlPolicy
|
||||
err := xml.Unmarshal(ctx.Body(), &accessControlPolicy)
|
||||
if err != nil {
|
||||
return s3err.GetAPIError(s3err.ErrInvalidRequest)
|
||||
}
|
||||
|
||||
input = &s3.PutBucketAclInput{
|
||||
Bucket: &bucket,
|
||||
ACL: "",
|
||||
AccessControlPolicy: &types.AccessControlPolicy{Owner: &types.Owner{ID: &access}, Grants: accessControlPolicy.AccessControlList.Grants},
|
||||
}
|
||||
}
|
||||
if acl != "" {
|
||||
if acl != "private" && acl != "public-read" && acl != "public-read-write" {
|
||||
return s3err.GetAPIError(s3err.ErrInvalidRequest)
|
||||
}
|
||||
if len(ctx.Body()) > 0 || grants != "" {
|
||||
return s3err.GetAPIError(s3err.ErrInvalidRequest)
|
||||
}
|
||||
|
||||
input = &s3.PutBucketAclInput{
|
||||
Bucket: &bucket,
|
||||
ACL: types.BucketCannedACL(acl),
|
||||
AccessControlPolicy: &types.AccessControlPolicy{Owner: &types.Owner{ID: &access}},
|
||||
}
|
||||
}
|
||||
if grants != "" {
|
||||
if acl != "" || len(ctx.Body()) > 0 {
|
||||
return s3err.GetAPIError(s3err.ErrInvalidRequest)
|
||||
}
|
||||
|
||||
input = &s3.PutBucketAclInput{
|
||||
Bucket: &bucket,
|
||||
GrantFullControl: &grantFullControl,
|
||||
GrantRead: &grantRead,
|
||||
GrantReadACP: &grantReadACP,
|
||||
GrantWrite: &granWrite,
|
||||
GrantWriteACP: &grantWriteACP,
|
||||
AccessControlPolicy: &types.AccessControlPolicy{Owner: &types.Owner{ID: &access}},
|
||||
}
|
||||
}
|
||||
|
||||
data, err := c.be.GetBucketAcl(bucket)
|
||||
@@ -271,18 +312,12 @@ func (c S3ApiController) PutBucketActions(ctx *fiber.Ctx) error {
|
||||
return SendResponse(ctx, err)
|
||||
}
|
||||
|
||||
input := &s3.PutBucketAclInput{
|
||||
Bucket: &bucket,
|
||||
ACL: types.BucketCannedACL(acl),
|
||||
GrantFullControl: &grantFullControl,
|
||||
GrantRead: &grantRead,
|
||||
GrantReadACP: &grantReadACP,
|
||||
GrantWrite: &granWrite,
|
||||
GrantWriteACP: &grantWriteACP,
|
||||
AccessControlPolicy: &types.AccessControlPolicy{Owner: &types.Owner{ID: &access}},
|
||||
updAcl, err := auth.UpdateACL(input, parsedAcl, c.iam)
|
||||
if err != nil {
|
||||
return SendResponse(ctx, err)
|
||||
}
|
||||
|
||||
err = auth.UpdateACL(input, parsedAcl, c.iam)
|
||||
err = c.be.PutBucketAcl(bucket, updAcl)
|
||||
return SendResponse(ctx, err)
|
||||
}
|
||||
|
||||
|
||||
@@ -48,7 +48,8 @@ func init() {
|
||||
|
||||
func TestNew(t *testing.T) {
|
||||
type args struct {
|
||||
be backend.Backend
|
||||
be backend.Backend
|
||||
iam auth.IAMService
|
||||
}
|
||||
|
||||
be := backend.BackendUnsupported{}
|
||||
@@ -61,16 +62,18 @@ func TestNew(t *testing.T) {
|
||||
{
|
||||
name: "Initialize S3 api controller",
|
||||
args: args{
|
||||
be: be,
|
||||
be: be,
|
||||
iam: &auth.IAMServiceInternal{},
|
||||
},
|
||||
want: S3ApiController{
|
||||
be: be,
|
||||
be: be,
|
||||
iam: &auth.IAMServiceInternal{},
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := New(tt.args.be); !reflect.DeepEqual(got, tt.want) {
|
||||
if got := New(tt.args.be, tt.args.iam); !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("New() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
@@ -403,6 +406,12 @@ func TestS3ApiController_PutBucketActions(t *testing.T) {
|
||||
}
|
||||
|
||||
app := fiber.New()
|
||||
acl := auth.ACL{Owner: "valid access", ACL: "public-read-write"}
|
||||
acldata, err := json.Marshal(acl)
|
||||
if err != nil {
|
||||
t.Errorf("Failed to parse the params: %v", err.Error())
|
||||
return
|
||||
}
|
||||
s3ApiController := S3ApiController{
|
||||
be: &BackendMock{
|
||||
GetBucketAclFunc: func(bucket string) ([]byte, error) {
|
||||
@@ -426,13 +435,13 @@ func TestS3ApiController_PutBucketActions(t *testing.T) {
|
||||
app.Put("/:bucket", s3ApiController.PutBucketActions)
|
||||
|
||||
// Error case
|
||||
errorReq := httptest.NewRequest(http.MethodPut, "/my-bucket", nil)
|
||||
errorReq.Header.Set("X-Amz-Acl", "restricted")
|
||||
errorReq := httptest.NewRequest(http.MethodPut, "/my-bucket?acl", nil)
|
||||
errorReq.Header.Set("X-Amz-Acl", "private")
|
||||
errorReq.Header.Set("X-Amz-Grant-Read", "read")
|
||||
|
||||
// PutBucketAcl success
|
||||
aclReq := httptest.NewRequest(http.MethodPut, "/my-bucket", nil)
|
||||
errorReq.Header.Set("X-Amz-Acl", "full")
|
||||
aclReq := httptest.NewRequest(http.MethodPut, "/my-bucket?acl", nil)
|
||||
aclReq.Header.Set("X-Amz-Acl", "private")
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -473,11 +482,11 @@ func TestS3ApiController_PutBucketActions(t *testing.T) {
|
||||
resp, err := tt.app.Test(tt.args.req)
|
||||
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("S3ApiController.GetActions() error = %v, wantErr %v", err, tt.wantErr)
|
||||
t.Errorf("S3ApiController.PutBucketActions() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
|
||||
if resp.StatusCode != tt.statusCode {
|
||||
t.Errorf("S3ApiController.GetActions() statusCode = %v, wantStatusCode = %v", resp.StatusCode, tt.statusCode)
|
||||
t.Errorf("S3ApiController.PutBucketActions() statusCode = %v, wantStatusCode = %v", resp.StatusCode, tt.statusCode)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user