Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Credentials as options on Google Cloud Storage package #81

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions pkg/storage/gcs/hystrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (

const userAgent = "gcloud-golang-storage/20151204"

func newHeimdallHTTPClient(ctx context.Context, hc heimdall.Client, credentialsJSON []byte) (*http.Client, error) {
t, err := newTransport(ctx, hc, credentialsJSON)
func newHeimdallHTTPClient(ctx context.Context, opts *Options) (*http.Client, error) {
t, err := newTransport(ctx, opts)
if err != nil {
return nil, err
}
Expand All @@ -24,16 +24,23 @@ func newHeimdallHTTPClient(ctx context.Context, hc heimdall.Client, credentialsJ
}, nil
}

func newTransport(ctx context.Context, hc heimdall.Client, credentialsJSON []byte) (http.RoundTripper, error) {
o := option.WithoutAuthentication()
if len(credentialsJSON) > 0 {
o = option.WithCredentialsJSON(credentialsJSON)
func getCredentialsOption(opts *Options) option.ClientOption {
if opts.Credentials != nil {
return option.WithCredentials(opts.Credentials)
}
if len(opts.CredentialsJSON) > 0 {
return option.WithCredentialsJSON(opts.CredentialsJSON)
}
return option.WithoutAuthentication()
}

func newTransport(ctx context.Context, opts *Options) (http.RoundTripper, error) {
return gcloud.NewTransport(ctx,
&hystrixTransport{client: hc},
&hystrixTransport{client: opts.Client},
option.WithUserAgent(userAgent),
option.WithScopes(storage.ScopeReadOnly),
o)
getCredentialsOption(opts),
)
}

type hystrixTransport struct {
Expand Down
25 changes: 23 additions & 2 deletions pkg/storage/gcs/hystrix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gcs

import (
"context"
"golang.org/x/oauth2/google"
"net/http"
"testing"

Expand All @@ -11,14 +12,34 @@ import (

func TestNewHeimdallHTTPClientWithInvalidCredentials(t *testing.T) {
hc := hystrix.NewClient()
hhc, err := newHeimdallHTTPClient(context.TODO(), hc, []byte("random"))
hhc, err := newHeimdallHTTPClient(context.TODO(), &Options{
CredentialsJSON: []byte("random"),
Client: hc,
})
assert.Nil(t, hhc)
assert.Error(t, err)
}

func TestNewHeimdallHTTPClientWithNoCredentials(t *testing.T) {
hc := hystrix.NewClient()
hhc, err := newHeimdallHTTPClient(context.TODO(), hc, []byte(""))
hhc, err := newHeimdallHTTPClient(context.TODO(), &Options{
CredentialsJSON: []byte(""),
Client: hc,
})
assert.NotNil(t, hhc)
assert.NoError(t, err)
req, _ := http.NewRequest(http.MethodGet, "", nil)
_, err = hhc.Do(req)
assert.Error(t, err, "expecting unsupported protocol error")
}

func TestNewHeimdallHTTPClientWithCustomCredentials(t *testing.T) {
hc := hystrix.NewClient()
hhc, err := newHeimdallHTTPClient(context.TODO(), &Options{
CredentialsJSON: nil,
Credentials: &google.Credentials{ProjectID: "sample"},
Client: hc,
})
assert.NotNil(t, hhc)
assert.NoError(t, err)
req, _ := http.NewRequest(http.MethodGet, "", nil)
Expand Down
7 changes: 6 additions & 1 deletion pkg/storage/gcs/options.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package gcs

import "github.com/gojektech/heimdall"
import (
"github.com/gojektech/heimdall"
"golang.org/x/oauth2/google"
)

// Options represents the Google Cloud Storage storage options
type Options struct {
// BucketName represents the name of the bucket
BucketName string
// CredentialsJSON holds the json data for credentials of a service account
CredentialsJSON []byte
// Credentials represents google credentials, including Application Default Credentials
Credentials *google.Credentials
// Client can be used to specify a heimdall.Client with hystrix like circuit breaker
Client heimdall.Client
}
2 changes: 1 addition & 1 deletion pkg/storage/gcs/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type Storage struct {
// NewStorage returns a new gcs.Storage instance
func NewStorage(opts Options) (*Storage, error) {
ctx := context.TODO()
client, err := newHeimdallHTTPClient(ctx, opts.Client, opts.CredentialsJSON)
client, err := newHeimdallHTTPClient(ctx, &opts)
if err != nil {
return nil, err
}
Expand Down
Loading