From b3d0ac5529a7d90c28447953dd9123a05aa962bc Mon Sep 17 00:00:00 2001 From: adranwit Date: Mon, 22 Apr 2024 10:38:05 -0700 Subject: [PATCH] added auth option deduplication --- gcp/secretmanager/storager.go | 12 +++++----- gs/auth.go | 41 +++++++++++++++++++++++++++++++++++ gs/get.go | 6 ++--- gs/storager.go | 18 +++++++-------- 4 files changed, 59 insertions(+), 18 deletions(-) create mode 100644 gs/auth.go diff --git a/gcp/secretmanager/storager.go b/gcp/secretmanager/storager.go index 136bdd9..996afda 100644 --- a/gcp/secretmanager/storager.go +++ b/gcp/secretmanager/storager.go @@ -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 { @@ -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 { @@ -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 @@ -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 diff --git a/gs/auth.go b/gs/auth.go new file mode 100644 index 0000000..4be86fe --- /dev/null +++ b/gs/auth.go @@ -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 +} diff --git a/gs/get.go b/gs/get.go index 82ac82c..0bd7096 100644 --- a/gs/get.go +++ b/gs/get.go @@ -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 { @@ -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) @@ -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 { diff --git a/gs/storager.go b/gs/storager.go index 6def310..13edf96 100644 --- a/gs/storager.go +++ b/gs/storager.go @@ -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 { @@ -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 @@ -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...) }