Skip to content

Commit b3d0ac5

Browse files
committed
added auth option deduplication
1 parent 77360e6 commit b3d0ac5

4 files changed

Lines changed: 59 additions & 18 deletions

File tree

gcp/secretmanager/storager.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ type storager struct {
2222
config *jwt.Config
2323
}
2424

25-
//Exists returns true if location exists
25+
// Exists returns true if location exists
2626
func (s *storager) Exists(ctx context.Context, resourceID string, options ...storage.Option) (bool, error) {
2727
resource, err := newResource(resourceID)
2828
if err != nil {
@@ -32,7 +32,7 @@ func (s *storager) Exists(ctx context.Context, resourceID string, options ...sto
3232
return secret != nil, nil
3333
}
3434

35-
//Get returns a file info for supplied location
35+
// Get returns a file info for supplied location
3636
func (s *storager) Get(ctx context.Context, location string, options ...storage.Option) (os.FileInfo, error) {
3737
list, err := s.List(ctx, location, options...)
3838
if err != nil {
@@ -44,17 +44,17 @@ func (s *storager) Get(ctx context.Context, location string, options ...storage.
4444
return list[0], nil
4545
}
4646

47-
//Delete deletes locations
47+
// Delete deletes locations
4848
func (s *storager) Delete(ctx context.Context, location string, options ...storage.Option) error {
4949
return fmt.Errorf("unsupported operation")
5050
}
5151

52-
//Close closes storage
52+
// Close closes storage
5353
func (s *storager) Close() error {
5454
return s.client.Close()
5555
}
5656

57-
//NewStorager create a new secreate manager storager
57+
// NewStorager create a new secreate manager storager
5858
func NewStorager(ctx context.Context, baseURL string, options ...storage.Option) (*storager, error) {
5959
authority := strings.ToLower(url.Host(baseURL))
6060
var gcpOptions gs.ClientOptions
@@ -63,7 +63,7 @@ func NewStorager(ctx context.Context, baseURL string, options ...storage.Option)
6363
if len(gcpOptions) == 0 {
6464
gcpOptions = make(gs.ClientOptions, 0)
6565
}
66-
gcpOptions = append(gs.DefaultOptions, gcpOptions...)
66+
gcpOptions = gs.Options(gs.DefaultOptions, gcpOptions)
6767
client, err := secretmanager.NewClient(ctx, gcpOptions...)
6868
if err != nil {
6969
return nil, err

gs/auth.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package gs
2+
3+
import (
4+
"fmt"
5+
"google.golang.org/api/option"
6+
)
7+
8+
var authOptions = map[string]bool{
9+
fmt.Sprintf("%T", option.WithTokenSource(nil)): true,
10+
fmt.Sprintf("%T", option.WithCredentialsJSON(nil)): true,
11+
fmt.Sprintf("%T", option.WithCredentialsFile("")): true,
12+
}
13+
14+
func HasAuthOption(options []option.ClientOption) bool {
15+
for _, option := range options {
16+
if option == nil {
17+
continue
18+
}
19+
if _, ok := authOptions[fmt.Sprintf("%T", option)]; ok {
20+
return true
21+
}
22+
}
23+
return false
24+
}
25+
26+
func Options(base, options []option.ClientOption) []option.ClientOption {
27+
var result = append([]option.ClientOption{}, options...)
28+
hasAuth := HasAuthOption(options)
29+
if hasAuth {
30+
for _, option := range base {
31+
if _, ok := authOptions[fmt.Sprintf("%T", option)]; ok {
32+
continue
33+
}
34+
result = append(result, option)
35+
}
36+
37+
} else {
38+
result = append(result, base...)
39+
}
40+
return result
41+
}

gs/get.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"strings"
1212
)
1313

14-
//Get returns an object for supplied location
14+
// Get returns an object for supplied location
1515
func (s *storager) get(ctx context.Context, location string, options []storage.Option) (os.FileInfo, error) {
1616
object, err := s.getObject(ctx, location, options)
1717
if object != nil {
@@ -20,7 +20,7 @@ func (s *storager) get(ctx context.Context, location string, options []storage.O
2020
return nil, err
2121
}
2222

23-
//Get returns an object for supplied location
23+
// Get returns an object for supplied location
2424
func (s *storager) getObject(ctx context.Context, location string, options []storage.Option) (object *gstorage.Object, err error) {
2525
location = strings.Trim(location, "/")
2626
objectCall := s.Objects.Get(s.bucket, location)
@@ -56,7 +56,7 @@ func (s *storager) getObject(ctx context.Context, location string, options []sto
5656
return object, err
5757
}
5858

59-
//Get returns an object for supplied location
59+
// Get returns an object for supplied location
6060
func (s *storager) Get(ctx context.Context, location string, options ...storage.Option) (os.FileInfo, error) {
6161
info, err := s.get(ctx, location, options)
6262
if err == nil {

gs/storager.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,30 +22,30 @@ type storager struct {
2222
config *jwt.Config
2323
}
2424

25-
//Close closes storager
25+
// Close closes storager
2626
func (s *storager) Close() error {
2727
http.CloseIdleConnections(s.client)
2828
return nil
2929
}
3030

31-
//Bucket returns bucket
31+
// Bucket returns bucket
3232
func (s *storager) Bucket(ctx context.Context) (*gstorage.Bucket, error) {
3333
call := s.Buckets.Get(s.bucket)
3434
call.Context(ctx)
3535
return call.Do()
3636
}
3737

38-
//FilterAuthOptions filters auth options
38+
// FilterAuthOptions filters auth options
3939
func (s storager) FilterAuthOptions(options []storage.Option) []storage.Option {
4040
var authOptions = make([]storage.Option, 0)
41-
if awsConfig, _ := s.filterAuthOption(options); awsConfig != nil {
42-
authOptions = append(authOptions, awsConfig)
41+
if config, _ := s.filterAuthOption(options); config != nil {
42+
authOptions = append(authOptions, config)
4343
}
4444
return authOptions
4545

4646
}
4747

48-
//FilterAuthOptions filters auth options
48+
// FilterAuthOptions filters auth options
4949
func (s storager) filterAuthOption(options []storage.Option) (config *jwt.Config, err error) {
5050
config = &jwt.Config{}
5151
if _, ok := option.Assign(options, &config); ok {
@@ -58,14 +58,14 @@ func (s storager) filterAuthOption(options []storage.Option) (config *jwt.Config
5858
return config, err
5959
}
6060

61-
//IsAuthChanged return true if auth has changes
61+
// IsAuthChanged return true if auth has changes
6262
func (s *storager) IsAuthChanged(options []storage.Option) bool {
6363
authOptions := s.FilterAuthOptions(options)
6464
changed := s.isAuthChanged(authOptions)
6565
return changed
6666
}
6767

68-
//IsAuthChanged return true if auth has changes
68+
// IsAuthChanged return true if auth has changes
6969
func (s *storager) isAuthChanged(authOptions []storage.Option) bool {
7070
if len(authOptions) == 0 {
7171
return false
@@ -126,7 +126,7 @@ func (s *storager) disableProxy(ctx context.Context) error {
126126
return nil
127127
}
128128

129-
//NewStorager returns new storager
129+
// NewStorager returns new storager
130130
func NewStorager(ctx context.Context, baseURL string, options ...storage.Option) (storage.Storager, error) {
131131
return newStorager(ctx, baseURL, options...)
132132
}

0 commit comments

Comments
 (0)