Skip to content

Commit

Permalink
chore(storage): implement ListBucketACLs and ListDefaultObjectACLs (#…
Browse files Browse the repository at this point in the history
…5965)

Adds HTTP and gRPC implementations for ListBucketACLs and ListDefaultObjectACL.

gRPC methods use bucket.Get under the hood because the ACL methods are not supported.

Co-authored-by: Chris Cotter <cjcotter@google.com>
  • Loading branch information
cojenco and tritone authored Apr 29, 2022
1 parent 5b6ea2c commit dd8973b
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 4 deletions.
44 changes: 44 additions & 0 deletions storage/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,50 @@ func TestListBucketsEmulated(t *testing.T) {
})
}

func TestListBucketACLsEmulated(t *testing.T) {
transportClientTest(t, func(t *testing.T, project, bucket string, client storageClient) {
ctx := context.Background()
attrs := &BucketAttrs{
Name: bucket,
PredefinedACL: "publicRead",
}
// Create the bucket that will be retrieved.
if _, err := client.CreateBucket(ctx, project, attrs); err != nil {
t.Fatalf("client.CreateBucket: %v", err)
}

acls, err := client.ListBucketACLs(ctx, bucket)
if err != nil {
t.Fatalf("client.ListBucketACLs: %v", err)
}
if want, got := len(acls), 2; want != got {
t.Errorf("ListBucketACLs: got %v, want %v items", acls, want)
}
})
}

func TestListDefaultObjectACLsEmulated(t *testing.T) {
transportClientTest(t, func(t *testing.T, project, bucket string, client storageClient) {
ctx := context.Background()
attrs := &BucketAttrs{
Name: bucket,
PredefinedDefaultObjectACL: "publicRead",
}
// Create the bucket that will be retrieved.
if _, err := client.CreateBucket(ctx, project, attrs); err != nil {
t.Fatalf("client.CreateBucket: %v", err)
}

acls, err := client.ListDefaultObjectACLs(ctx, bucket)
if err != nil {
t.Fatalf("client.ListDefaultObjectACLs: %v", err)
}
if want, got := len(acls), 2; want != got {
t.Errorf("ListDefaultObjectACLs: got %v, want %v items", acls, want)
}
})
}

func initEmulatorClients() func() error {
noopCloser := func() error { return nil }
if !isEmulatorEnvironmentSet() {
Expand Down
12 changes: 10 additions & 2 deletions storage/grpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,11 @@ func (c *grpcStorageClient) DeleteDefaultObjectACL(ctx context.Context, bucket s
return errMethodNotSupported
}
func (c *grpcStorageClient) ListDefaultObjectACLs(ctx context.Context, bucket string, opts ...storageOption) ([]ACLRule, error) {
return nil, errMethodNotSupported
attrs, err := c.GetBucket(ctx, bucket, nil, opts...)
if err != nil {
return nil, err
}
return attrs.DefaultObjectACL, nil
}
func (c *grpcStorageClient) UpdateDefaultObjectACL(ctx context.Context, opts ...storageOption) (*ACLRule, error) {
return nil, errMethodNotSupported
Expand All @@ -415,7 +419,11 @@ func (c *grpcStorageClient) DeleteBucketACL(ctx context.Context, bucket string,
return errMethodNotSupported
}
func (c *grpcStorageClient) ListBucketACLs(ctx context.Context, bucket string, opts ...storageOption) ([]ACLRule, error) {
return nil, errMethodNotSupported
attrs, err := c.GetBucket(ctx, bucket, nil, opts...)
if err != nil {
return nil, err
}
return attrs.ACL, nil
}
func (c *grpcStorageClient) UpdateBucketACL(ctx context.Context, bucket string, entity ACLEntity, role ACLRole, opts ...storageOption) (*ACLRule, error) {
return nil, errMethodNotSupported
Expand Down
42 changes: 40 additions & 2 deletions storage/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"net/http"
"net/url"
"os"
"reflect"
"strings"

"golang.org/x/oauth2/google"
Expand Down Expand Up @@ -381,8 +382,21 @@ func (c *httpStorageClient) UpdateObject(ctx context.Context, bucket, object str
func (c *httpStorageClient) DeleteDefaultObjectACL(ctx context.Context, bucket string, entity ACLEntity, opts ...storageOption) error {
return errMethodNotSupported
}

func (c *httpStorageClient) ListDefaultObjectACLs(ctx context.Context, bucket string, opts ...storageOption) ([]ACLRule, error) {
return nil, errMethodNotSupported
s := callSettings(c.settings, opts...)
var acls *raw.ObjectAccessControls
var err error
err = run(ctx, func() error {
req := c.raw.DefaultObjectAccessControls.List(bucket)
configureACLCall(ctx, s.userProject, req)
acls, err = req.Do()
return err
}, s.retry, true)
if err != nil {
return nil, err
}
return toObjectACLRules(acls.Items), nil
}
func (c *httpStorageClient) UpdateDefaultObjectACL(ctx context.Context, opts ...storageOption) (*ACLRule, error) {
return nil, errMethodNotSupported
Expand All @@ -394,8 +408,32 @@ func (c *httpStorageClient) DeleteBucketACL(ctx context.Context, bucket string,
return errMethodNotSupported
}
func (c *httpStorageClient) ListBucketACLs(ctx context.Context, bucket string, opts ...storageOption) ([]ACLRule, error) {
return nil, errMethodNotSupported
s := callSettings(c.settings, opts...)
var acls *raw.BucketAccessControls
var err error
err = run(ctx, func() error {
req := c.raw.BucketAccessControls.List(bucket)
configureACLCall(ctx, s.userProject, req)
acls, err = req.Do()
return err
}, s.retry, true)
if err != nil {
return nil, err
}
return toBucketACLRules(acls.Items), nil
}

// configureACLCall sets the context, user project and headers on the apiary library call.
// This will panic if the call does not have the correct methods.
func configureACLCall(ctx context.Context, userProject string, call interface{ Header() http.Header }) {
vc := reflect.ValueOf(call)
vc.MethodByName("Context").Call([]reflect.Value{reflect.ValueOf(ctx)})
if userProject != "" {
vc.MethodByName("UserProject").Call([]reflect.Value{reflect.ValueOf(userProject)})
}
setClientHeader(call.Header())
}

func (c *httpStorageClient) UpdateBucketACL(ctx context.Context, bucket string, entity ACLEntity, role ACLRole, opts ...storageOption) (*ACLRule, error) {
return nil, errMethodNotSupported
}
Expand Down

0 comments on commit dd8973b

Please sign in to comment.