-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #203 from keel-hq/feature/ecr_token_cache
cache for credentials
- keel-v1.0.5
- keel-v1.0.4
- chart-v1.0.5
- chart-v1.0.4
- 0.20.0
- 0.20.0-beta.3
- 0.20.0-beta.2
- 0.20.0-beta.1
- 0.20.0-beta
- 0.19.2
- 0.19.1
- 0.19.0
- 0.18.1
- 0.18.0
- 0.17.0-rc1
- 0.16.1
- 0.16.0
- 0.15.1
- 0.15.0
- 0.15.0-rc2
- 0.15.0-rc1
- 0.14.3-rc1
- 0.14.2
- 0.14.1
- 0.14.0
- 0.13.2
- 0.13.1
- 0.13.0
- 0.13.0-rc3
- 0.13.0-rc2
- 0.13.0-rc1
- 0.12.0
- 0.11.2
- 0.11.1
- 0.11.0
- 0.10.0
- 0.9.7
- 0.9.6
- 0.9.5
- 0.9.4
- 0.9.3
- 0.9.2
- 0.9.1
- 0.9.0
- 0.9.0-rc5
- 0.9.0-rc4
Showing
4 changed files
with
184 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
"time" | ||
|
||
"github.com/keel-hq/keel/types" | ||
) | ||
|
||
type item struct { | ||
credentials *types.Credentials | ||
created time.Time | ||
} | ||
|
||
// Cache - internal cache for aws | ||
type Cache struct { | ||
creds map[string]*item | ||
tick time.Duration | ||
ttl time.Duration | ||
mu *sync.RWMutex | ||
} | ||
|
||
// NewCache - new credentials cache | ||
func NewCache(ttl time.Duration) *Cache { | ||
c := &Cache{ | ||
creds: make(map[string]*item), | ||
ttl: ttl, | ||
tick: 30 * time.Second, | ||
mu: &sync.RWMutex{}, | ||
} | ||
go c.expiryService() | ||
return c | ||
} | ||
|
||
func (c *Cache) expiryService() { | ||
ticker := time.NewTicker(c.tick) | ||
defer ticker.Stop() | ||
for { | ||
select { | ||
case <-ticker.C: | ||
c.expire() | ||
} | ||
} | ||
} | ||
|
||
func (c *Cache) expire() { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
t := time.Now() | ||
for k, v := range c.creds { | ||
if t.Sub(v.created) > c.ttl { | ||
delete(c.creds, k) | ||
} | ||
} | ||
} | ||
|
||
// Put - saves new creds | ||
func (c *Cache) Put(registry string, creds *types.Credentials) { | ||
c.mu.Lock() | ||
defer c.mu.Unlock() | ||
c.creds[registry] = &item{credentials: creds, created: time.Now()} | ||
} | ||
|
||
// Get - retrieves creds | ||
func (c *Cache) Get(registry string) (*types.Credentials, error) { | ||
c.mu.RLock() | ||
defer c.mu.RUnlock() | ||
|
||
item, ok := c.creds[registry] | ||
if !ok { | ||
return nil, fmt.Errorf("not found") | ||
} | ||
|
||
cr := new(types.Credentials) | ||
*cr = *item.credentials | ||
|
||
return cr, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package aws | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
|
||
"github.com/keel-hq/keel/types" | ||
|
||
"testing" | ||
) | ||
|
||
func TestPutCreds(t *testing.T) { | ||
c := NewCache(time.Second * 5) | ||
|
||
creds := &types.Credentials{ | ||
Username: "user-1", | ||
Password: "pass-1", | ||
} | ||
|
||
c.Put("reg1", creds) | ||
|
||
stored, err := c.Get("reg1") | ||
if err != nil { | ||
t.Fatalf("unexpected error: %s", err) | ||
} | ||
|
||
if stored.Username != "user-1" { | ||
t.Errorf("username mismatch: %s", stored.Username) | ||
} | ||
if stored.Password != "pass-1" { | ||
t.Errorf("password mismatch: %s", stored.Password) | ||
} | ||
} | ||
|
||
func TestExpiry(t *testing.T) { | ||
c := &Cache{ | ||
creds: make(map[string]*item), | ||
mu: &sync.RWMutex{}, | ||
ttl: time.Millisecond * 500, | ||
tick: time.Millisecond * 100, | ||
} | ||
|
||
go c.expiryService() | ||
|
||
creds := &types.Credentials{ | ||
Username: "user-1", | ||
Password: "pass-1", | ||
} | ||
|
||
c.Put("reg1", creds) | ||
|
||
time.Sleep(1100 * time.Millisecond) | ||
|
||
_, err := c.Get("reg1") | ||
if err == nil { | ||
t.Fatalf("expected to get an error about missing record") | ||
} | ||
|
||
} |