-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace global map with Cache struct
- Loading branch information
1 parent
90255e8
commit 83ff12f
Showing
4 changed files
with
187 additions
and
65 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package traefik_auth_middleware | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
"time" | ||
) | ||
|
||
const SIZE = 1024 | ||
|
||
type Cache struct { | ||
sync.RWMutex | ||
|
||
dirty map[string]Token | ||
} | ||
|
||
// Get token from cache. If token not found return status false. | ||
func (c *Cache) Get(key string) (token Token, ok bool) { | ||
c.RLock() | ||
token, ok = c.dirty[key] | ||
c.RUnlock() | ||
return token, ok | ||
} | ||
|
||
// Store a token inside cache | ||
func (c *Cache) Store(key string, t Token) { | ||
c.Lock() | ||
if c.dirty == nil { | ||
c.dirty = make(map[string]Token, SIZE) | ||
} | ||
c.dirty[key] = t | ||
c.Unlock() | ||
} | ||
|
||
// Clears cache of any expired tokens | ||
func (c *Cache) ClearExpired() { | ||
c.Lock() | ||
for k, v := range c.dirty { | ||
if v.ExpirationTime.Before(time.Now()) { | ||
fmt.Println("deleting") | ||
delete(c.dirty, k) | ||
} | ||
} | ||
c.Unlock() | ||
} |
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,63 @@ | ||
package traefik_auth_middleware | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestEmpty(t *testing.T) { | ||
cache := Cache{} | ||
|
||
_, ok := cache.Get("foo") | ||
if ok { | ||
t.Error("Expected get on empty cache to be empty, but got ok") | ||
} | ||
|
||
// check that call to ClearExpired doesn't blow up if cache empty | ||
cache.ClearExpired() | ||
} | ||
|
||
func TestCache(t *testing.T) { | ||
cache := Cache{} | ||
|
||
items := map[string]Token{ | ||
"foo": {"fooAccessor", "fooSecret", time.Now()}, | ||
"bar": {"barAccessor", "barSecret", time.Now()}, | ||
"baz": {"bazAccessor", "bazSecret", time.Now()}, | ||
} | ||
|
||
for k, v := range items { | ||
cache.Store(k, v) | ||
} | ||
|
||
for k, v := range items { | ||
rv, ok := cache.Get(k) | ||
if !ok { | ||
t.Errorf("exected %v to be found in cache, but didn't", k) | ||
} | ||
if rv != v { | ||
t.Errorf("exected %v but got %v", v, rv) | ||
} | ||
} | ||
} | ||
|
||
func TestCacheExpiry(t *testing.T) { | ||
cache := Cache{} | ||
|
||
items := map[string]Token{ | ||
"foo": {"fooAccessor", "fooSecret", time.Now().Add(time.Hour)}, | ||
"bar": {"barAccessor", "barSecret", time.Now().Add(time.Hour)}, | ||
"baz": {"bazAccessor", "bazSecret", time.Now()}, | ||
} | ||
|
||
for k, v := range items { | ||
cache.Store(k, v) | ||
} | ||
|
||
cache.ClearExpired() | ||
|
||
if _, ok := cache.Get("baz"); ok { | ||
t.Errorf("expired item still returned from cache") | ||
} | ||
|
||
} |
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,54 @@ | ||
package traefik_auth_middleware | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
"time" | ||
) | ||
|
||
type Token struct { | ||
AccessorID string `json:"AccessorID"` | ||
SecretID string `json:"SecretID"` | ||
ExpirationTime time.Time `json:"ExpirationTime"` | ||
} | ||
|
||
type LoginRequestBody struct { | ||
AuthMethodName string | ||
LoginToken string | ||
} | ||
|
||
// Login to Nomad with jwt and return a Token | ||
func (p *Plugin) login(jwt string) (Token, error) { | ||
req_body, err := json.Marshal(LoginRequestBody{p.config.AuthMethodName, jwt}) | ||
if err != nil { | ||
return Token{}, err | ||
} | ||
|
||
url, err := url.JoinPath(p.config.NomadEndpoint, "v1", "acl/login") | ||
if err != nil { | ||
return Token{}, err | ||
} | ||
|
||
resp, err := p.client.Post(url, "application/json", bytes.NewReader(req_body)) | ||
if err != nil { | ||
return Token{}, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return Token{}, fmt.Errorf("unexpected return code (%v) from nomad", resp.StatusCode) | ||
} | ||
|
||
resp_body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return Token{}, err | ||
} | ||
var token Token | ||
json.Unmarshal(resp_body, &token) | ||
|
||
return token, 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