Skip to content

Commit

Permalink
added auth tracker
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianwit@gmail.com authored and adrianwit@gmail.com committed Oct 25, 2019
1 parent a1adcda commit d014451
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 25 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## October 15 2019 0.5.0
* Added AuthTracker

## October 15 2019 0.4.1
* Update copy implementation
* Added url.IsSchemeEquals
Expand Down
41 changes: 34 additions & 7 deletions base/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Manager struct {
storage.Manager
options []storage.Option
scheme string
baseURL string
mutex *sync.RWMutex
storagers map[string]storage.Storager
provider func(ctx context.Context, baseURL string, options ...storage.Option) (storage.Storager, error)
Expand All @@ -28,7 +29,7 @@ type Manager struct {
//List lists content for supplied URL
func (m *Manager) List(ctx context.Context, URL string, options ...storage.Option) ([]storage.Object, error) {
baseURL, URLPath := url.Base(URL, m.scheme)
storager, err := m.Storager(ctx, baseURL, options...)
storager, err := m.Storager(ctx, baseURL, options)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -81,7 +82,7 @@ func (m *Manager) Upload(ctx context.Context, URL string, mode os.FileMode, read
if err != nil {
return err
}
storager, err := m.Storager(ctx, baseURL, options...)
storager, err := m.Storager(ctx, baseURL, options)
if err != nil {
return err
}
Expand All @@ -104,7 +105,7 @@ func (m *Manager) Download(ctx context.Context, object storage.Object, options .
//DownloadWithURL downloads content
func (m *Manager) DownloadWithURL(ctx context.Context, URL string, options ...storage.Option) (io.ReadCloser, error) {
baseURL, URLPath := url.Base(URL, m.scheme)
storager, err := m.Storager(ctx, baseURL, options...)
storager, err := m.Storager(ctx, baseURL, options)
if err != nil {
return nil, err
}
Expand All @@ -118,7 +119,7 @@ func (m *Manager) DownloadWithURL(ctx context.Context, URL string, options ...st
//Delete deletes locations
func (m *Manager) Delete(ctx context.Context, URL string, options ...storage.Option) error {
baseURL, URLPath := url.Base(URL, m.scheme)
storager, err := m.Storager(ctx, baseURL, options...)
storager, err := m.Storager(ctx, baseURL, options)
if err != nil {
return err
}
Expand All @@ -130,7 +131,7 @@ func (m *Manager) Create(ctx context.Context, URL string, mode os.FileMode, isDi
var reader io.Reader
options, _ = option.Assign(options, &reader)
baseURL, URLPath := url.Base(URL, m.scheme)
storager, err := m.Storager(ctx, baseURL, options...)
storager, err := m.Storager(ctx, baseURL, options)
if err != nil {
return err
}
Expand All @@ -146,7 +147,7 @@ func (m *Manager) Create(ctx context.Context, URL string, mode os.FileMode, isDi
//Exists checks if resource exsits
func (m *Manager) Exists(ctx context.Context, URL string, options ...storage.Option) (bool, error) {
baseURL, URLPath := url.Base(URL, m.scheme)
storager, err := m.Storager(ctx, baseURL, options...)
storager, err := m.Storager(ctx, baseURL, options)
if err != nil {
return false, err
}
Expand All @@ -162,7 +163,7 @@ func (m *Manager) Options(options []storage.Option) []storage.Option {
}

//Storager returns storager
func (m *Manager) Storager(ctx context.Context, baseURL string, options ...storage.Option) (storage.Storager, error) {
func (m *Manager) Storager(ctx context.Context, baseURL string, options []storage.Option) (storage.Storager, error) {
m.mutex.RLock()
baseURL, _ = url.Base(baseURL, m.scheme)
storager, ok := m.storagers[baseURL]
Expand Down Expand Up @@ -197,11 +198,37 @@ func (m *Manager) Close() error {
return err
}

func (m *Manager) IsAuthChanged(ctx context.Context, baseURL string, options []storage.Option) bool {
changed := m.isAuthChanged(ctx, baseURL, options)
return changed
}

func (m *Manager) isAuthChanged(ctx context.Context, baseURL string, options []storage.Option) bool {
storager, err := m.Storager(ctx, baseURL, options)
if err != nil {
return false
}
authManager, ok := storager.(storage.StoragerAuthTracker)
if !ok {
return false
}
authOptions := authManager.FilterAuthOptions(options)
if len(authOptions) == 0 {
return false
}
return authManager.IsAuthChanged(authOptions)
}

//Scheme returns scheme
func (m *Manager) Scheme() string {
return m.scheme
}

//Scheme returns scheme
func (m *Manager) BaseURL() string {
return m.scheme
}

//New creates base storager base Manager
func New(manager storage.Manager, scheme string, provider func(ctx context.Context, baseURL string, options ...storage.Option) (storage.Storager, error), options []storage.Option) *Manager {
return &Manager{
Expand Down
12 changes: 5 additions & 7 deletions copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,19 @@ func (s *service) copy(ctx context.Context, sourceURL, destURL string, srcOption
err = s.Create(ctx, destURL, source.Mode()|os.ModeDir, source.IsDir(), *destOptions...)
} else {
destURL, mappedName = url.Split(destURL, file.Scheme)

}

if url.IsSchemeEquals(sourceURL, destURL) && modifier == nil {
srcManager, err := s.manager(ctx, sourceURL, *srcOptions)
sourceManager, err := s.manager(ctx, sourceURL, *srcOptions)
if err != nil {
return err
}

if copier, ok := srcManager.(storage.Copier); ok {
if copier, ok := sourceManager.(storage.Copier); ok {
if mappedName != "" {
destURL = url.Join(destURL, mappedName)
}
return copier.Copy(ctx, sourceURL, destURL, *srcOptions...)
if !s.IsAuthChanged(ctx, sourceManager, sourceURL, *destOptions) {
return copier.Copy(ctx, sourceURL, destURL, *srcOptions...)
}
}
}

Expand Down Expand Up @@ -111,7 +110,6 @@ func (s *service) Copy(ctx context.Context, sourceURL, destURL string, options .
if uploader == nil {
uploader = s
}

destURL = s.updateDestURL(sourceURL, destURL)
return s.copy(ctx, sourceURL, destURL, sourceOptions, destOptions, walker, uploader)
}
3 changes: 2 additions & 1 deletion mem/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package mem

import (
"context"
"github.com/viant/afs/storage"
"github.com/viant/afs/url"
)

//Root returns memory system root folder for supplied base URL
func (s *manager) Root(ctx context.Context, baseURL string) *Folder {
baseURL, _ = url.Base(baseURL, Scheme)
srv, err := s.Storager(ctx, baseURL)
srv, err := s.Storager(ctx, baseURL, []storage.Option{})
if err != nil {
return nil
}
Expand Down
8 changes: 5 additions & 3 deletions move.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ func (s *service) Move(ctx context.Context, sourceURL, destURL string, options .
destOptions := option.NewDest()
option.Assign(options, &sourceOptions, &destOptions)
if url.IsSchemeEquals(sourceURL, destURL) {
if manager, err := s.manager(ctx, sourceURL, *sourceOptions); err == nil {
if mover, ok := manager.(storage.Mover); ok {
return mover.Move(ctx, sourceURL, destURL, options...)
if sourceManager, err := s.manager(ctx, sourceURL, *sourceOptions); err == nil {
if mover, ok := sourceManager.(storage.Mover); ok {
if !s.IsAuthChanged(ctx, sourceManager, sourceURL, *destOptions) {
return mover.Move(ctx, sourceURL, destURL, *sourceOptions...)
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions scp/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type manager struct {

func (m *manager) Uploader(ctx context.Context, URL string, options ...storage.Option) (storage.Upload, io.Closer, error) {
_, URLPath := url.Base(URL, Scheme)
srv, err := m.Storager(ctx, URL, options...)
srv, err := m.Storager(ctx, URL, options)
if err != nil {
return nil, nil, err
}
Expand All @@ -33,7 +33,7 @@ func (m *manager) Uploader(ctx context.Context, URL string, options ...storage.O
func (m *manager) Walk(ctx context.Context, URL string, handler storage.OnVisit, options ...storage.Option) error {
baseURL, URLPath := url.Base(URL, Scheme)
match, modifier := option.GetWalkOptions(options)
srv, err := m.Storager(ctx, baseURL, options...)
srv, err := m.Storager(ctx, baseURL, options)
if err != nil {
return err
}
Expand Down
15 changes: 14 additions & 1 deletion service.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,13 +199,22 @@ func (s *service) CloseAll() error {
return err
}

func (s *service) IsAuthChanged(ctx context.Context, manager storage.Manager, URL string, options []storage.Option) bool {
authTracker, ok := manager.(storage.AuthTracker)
if !ok {
return false
}
return authTracker.IsAuthChanged(ctx, URL, options)
}

func (s *service) manager(ctx context.Context, URL string, options []storage.Option) (storage.Manager, error) {
scheme := url.Scheme(URL, file.Scheme)
noCache := &option.NoCache{}
options, _ = option.Assign(options, &noCache)
if noCache.Source == option.NoCacheBaseURL {
return s.newManager(ctx, scheme, options...)
}

key, _ := url.Base(URL, scheme)
extURL := url.SchemeExtensionURL(URL)
key += extURL
Expand All @@ -223,7 +232,11 @@ func (s *service) manager(ctx context.Context, URL string, options []storage.Opt
result, ok := s.managers[key]
s.mutex.RUnlock()
if ok {
return result, nil
if !s.IsAuthChanged(ctx, result, URL, options) {
return result, nil
}
_ = result.Close()

}
manager, err := s.newManager(ctx, scheme, options...)
s.mutex.Lock()
Expand Down
18 changes: 18 additions & 0 deletions storage/auth.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
package storage

import "context"

//Authenticator represents an authennticator
type Authenticator interface {
//Auth authenticate URL scheme with authentication option
Auth(baseURL string, option ...Option)
}

//AuthTracker represents auth change tracker
type AuthTracker interface {
//IsAuthChanged return true if auth has changed
IsAuthChanged(ctx context.Context, baseURL string, options []Option) bool
}

//StoragerAuthTracker represents auth manager
type StoragerAuthTracker interface {

//FilterAuthOptions filters auth options
FilterAuthOptions(option []Option) []Option

//IsAuthChanged return true if auth has changes
IsAuthChanged(authOptions []Option) bool
}
4 changes: 2 additions & 2 deletions tar/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (m *manager) provider(ctx context.Context, baseURL string, options ...stora
func (m *manager) Walk(ctx context.Context, URL string, handler storage.OnVisit, options ...storage.Option) error {
baseURL, URLPath := url.Base(URL, Scheme)
match, modifier := option.GetWalkOptions(options)
srv, err := m.Storager(ctx, baseURL, options...)
srv, err := m.Storager(ctx, baseURL, options)
if err != nil {
return err
}
Expand All @@ -56,7 +56,7 @@ func (m *manager) Walk(ctx context.Context, URL string, handler storage.OnVisit,

func (m *manager) Uploader(ctx context.Context, URL string, options ...storage.Option) (storage.Upload, io.Closer, error) {
_, URLPath := url.Base(URL, Scheme)
srv, err := m.Storager(ctx, URL, options...)
srv, err := m.Storager(ctx, URL, options)
if err != nil {
return nil, nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions zip/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (m *manager) provider(ctx context.Context, baseURL string, options ...stora

func (m *manager) Walk(ctx context.Context, URL string, handler storage.OnVisit, options ...storage.Option) error {
baseURL, URLPath := url.Base(URL, Scheme)
srv, err := m.Storager(ctx, baseURL, options...)
srv, err := m.Storager(ctx, baseURL, options)
if err != nil {
return err
}
Expand All @@ -46,7 +46,7 @@ func (m *manager) Walk(ctx context.Context, URL string, handler storage.OnVisit,

func (m *manager) Uploader(ctx context.Context, URL string, options ...storage.Option) (storage.Upload, io.Closer, error) {
_, URLPath := url.Base(URL, Scheme)
srv, err := m.Storager(ctx, URL, options...)
srv, err := m.Storager(ctx, URL, options)
if err != nil {
return nil, nil, err
}
Expand Down

0 comments on commit d014451

Please sign in to comment.