Skip to content

Commit

Permalink
added auth option deduplication
Browse files Browse the repository at this point in the history
  • Loading branch information
adranwit committed Apr 22, 2024
1 parent 77360e6 commit b3d0ac5
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 18 deletions.
12 changes: 6 additions & 6 deletions gcp/secretmanager/storager.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type storager struct {
config *jwt.Config
}

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

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

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

//Close closes storage
// Close closes storage
func (s *storager) Close() error {
return s.client.Close()
}

//NewStorager create a new secreate manager storager
// NewStorager create a new secreate manager storager
func NewStorager(ctx context.Context, baseURL string, options ...storage.Option) (*storager, error) {
authority := strings.ToLower(url.Host(baseURL))
var gcpOptions gs.ClientOptions
Expand All @@ -63,7 +63,7 @@ func NewStorager(ctx context.Context, baseURL string, options ...storage.Option)
if len(gcpOptions) == 0 {
gcpOptions = make(gs.ClientOptions, 0)
}
gcpOptions = append(gs.DefaultOptions, gcpOptions...)
gcpOptions = gs.Options(gs.DefaultOptions, gcpOptions)
client, err := secretmanager.NewClient(ctx, gcpOptions...)
if err != nil {
return nil, err
Expand Down
41 changes: 41 additions & 0 deletions gs/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package gs

import (
"fmt"
"google.golang.org/api/option"
)

var authOptions = map[string]bool{
fmt.Sprintf("%T", option.WithTokenSource(nil)): true,
fmt.Sprintf("%T", option.WithCredentialsJSON(nil)): true,
fmt.Sprintf("%T", option.WithCredentialsFile("")): true,
}

func HasAuthOption(options []option.ClientOption) bool {
for _, option := range options {
if option == nil {
continue
}
if _, ok := authOptions[fmt.Sprintf("%T", option)]; ok {
return true
}
}
return false
}

func Options(base, options []option.ClientOption) []option.ClientOption {
var result = append([]option.ClientOption{}, options...)
hasAuth := HasAuthOption(options)
if hasAuth {
for _, option := range base {
if _, ok := authOptions[fmt.Sprintf("%T", option)]; ok {
continue
}
result = append(result, option)
}

} else {
result = append(result, base...)
}
return result
}
6 changes: 3 additions & 3 deletions gs/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"strings"
)

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

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

//Get returns an object for supplied location
// Get returns an object for supplied location
func (s *storager) Get(ctx context.Context, location string, options ...storage.Option) (os.FileInfo, error) {
info, err := s.get(ctx, location, options)
if err == nil {
Expand Down
18 changes: 9 additions & 9 deletions gs/storager.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,30 @@ type storager struct {
config *jwt.Config
}

//Close closes storager
// Close closes storager
func (s *storager) Close() error {
http.CloseIdleConnections(s.client)
return nil
}

//Bucket returns bucket
// Bucket returns bucket
func (s *storager) Bucket(ctx context.Context) (*gstorage.Bucket, error) {
call := s.Buckets.Get(s.bucket)
call.Context(ctx)
return call.Do()
}

//FilterAuthOptions filters auth options
// FilterAuthOptions filters auth options
func (s storager) FilterAuthOptions(options []storage.Option) []storage.Option {
var authOptions = make([]storage.Option, 0)
if awsConfig, _ := s.filterAuthOption(options); awsConfig != nil {
authOptions = append(authOptions, awsConfig)
if config, _ := s.filterAuthOption(options); config != nil {
authOptions = append(authOptions, config)
}
return authOptions

}

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

//IsAuthChanged return true if auth has changes
// IsAuthChanged return true if auth has changes
func (s *storager) IsAuthChanged(options []storage.Option) bool {
authOptions := s.FilterAuthOptions(options)
changed := s.isAuthChanged(authOptions)
return changed
}

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

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

0 comments on commit b3d0ac5

Please sign in to comment.