Skip to content

Commit

Permalink
resolver: add credentials cache
Browse files Browse the repository at this point in the history
Signed-off-by: Tonis Tiigi <[email protected]>
  • Loading branch information
tonistiigi committed Jul 5, 2020
1 parent 05013a6 commit ed60a2f
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions util/resolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,32 @@ func NewRegistryConfig(m map[string]config.RegistryConfig) docker.RegistryHosts
}

type SessionAuthenticator struct {
sm *session.Manager
groups []session.Group
mu sync.RWMutex
sm *session.Manager
groups []session.Group
mu sync.RWMutex
cache map[string]credentials
cacheMu sync.RWMutex
}

type credentials struct {
user string
secret string
created time.Time
}

func NewSessionAuthenticator(sm *session.Manager, g session.Group) *SessionAuthenticator {
return &SessionAuthenticator{sm: sm, groups: []session.Group{g}}
return &SessionAuthenticator{sm: sm, groups: []session.Group{g}, cache: map[string]credentials{}}
}

func (a *SessionAuthenticator) credentials(h string) (string, string, error) {
a.cacheMu.RLock()
c, ok := a.cache[h]
if ok && time.Since(c.created) < time.Minute {
a.cacheMu.RUnlock()
return c.user, c.secret, nil
}
a.cacheMu.RUnlock()

a.mu.RLock()
defer a.mu.RUnlock()

Expand All @@ -170,6 +186,9 @@ func (a *SessionAuthenticator) credentials(h string) (string, string, error) {
if err != nil {
continue
}
a.cacheMu.Lock()
a.cache[h] = credentials{user: user, secret: secret, created: time.Now()}
a.cacheMu.Unlock()
return user, secret, nil
}
return "", "", err
Expand Down

0 comments on commit ed60a2f

Please sign in to comment.