Skip to content

Commit

Permalink
Support multiple token key formats in proxy configurations.
Browse files Browse the repository at this point in the history
  • Loading branch information
fancycode committed Aug 31, 2020
1 parent 18bfb0d commit 744b514
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 16 deletions.
3 changes: 2 additions & 1 deletion proxy.conf.in
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ blockkey = -encryption-key-
#cacert = /path/to/etcd-ca.crt

# For token type "etcd": Format of key name to retrieve the public key from,
# "%s" will be replaced with the token id.
# "%s" will be replaced with the token id. Multiple possible formats can be
# comma-separated.
#keyformat = /signaling/proxy/tokens/%s/public-key

[mcu]
Expand Down
60 changes: 45 additions & 15 deletions src/proxy/proxy_tokens_etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ type tokenCacheEntry struct {
type tokensEtcd struct {
client atomic.Value

tokenFormat atomic.Value
tokenCache *signaling.LruCache
tokenFormats atomic.Value
tokenCache *signaling.LruCache
}

func NewProxyTokensEtcd(config *goconf.ConfigFile) (ProxyTokens, error) {
Expand All @@ -77,47 +77,68 @@ func (t *tokensEtcd) getClient() *clientv3.Client {
return c.(*clientv3.Client)
}

func (t *tokensEtcd) getKey(id string) string {
format := t.tokenFormat.Load().(string)
return fmt.Sprintf(format, id)
func (t *tokensEtcd) getKeys(id string) []string {
format := t.tokenFormats.Load().([]string)
var result []string
for _, f := range format {
result = append(result, fmt.Sprintf(f, id))
}
return result
}

func (t *tokensEtcd) Get(id string) (*ProxyToken, error) {
func (t *tokensEtcd) getByKey(id string, key string) (*ProxyToken, error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
resp, err := t.getClient().Get(ctx, t.getKey(id))

resp, err := t.getClient().Get(ctx, key)
if err != nil {
return nil, err
}

if len(resp.Kvs) == 0 {
return nil, nil
} else if len(resp.Kvs) > 1 {
log.Printf("Received multiple keys for %s, using last", id)
log.Printf("Received multiple keys for %s, using last", key)
}

keyValue := resp.Kvs[len(resp.Kvs)-1].Value
cached, _ := t.tokenCache.Get(id).(*tokenCacheEntry)
cached, _ := t.tokenCache.Get(key).(*tokenCacheEntry)
if cached == nil || !bytes.Equal(cached.keyValue, keyValue) {
// Parsed public keys are cached to avoid the parse overhead.
key, err := jwt.ParseRSAPublicKeyFromPEM(keyValue)
publicKey, err := jwt.ParseRSAPublicKeyFromPEM(keyValue)
if err != nil {
return nil, fmt.Errorf("Could not parse public key for %s: %s", id, err)
return nil, err
}

cached = &tokenCacheEntry{
keyValue: keyValue,
token: &ProxyToken{
id: id,
key: key,
key: publicKey,
},
}
t.tokenCache.Set(id, cached)
t.tokenCache.Set(key, cached)
}

return cached.token, nil
}

func (t *tokensEtcd) Get(id string) (*ProxyToken, error) {
for _, k := range t.getKeys(id) {
token, err := t.getByKey(id, k)
if err != nil {
log.Printf("Could not get public key from %s for %s: %s", k, id, err)
continue
} else if token == nil {
continue
}

return token, nil
}

return nil, nil
}

func (t *tokensEtcd) load(config *goconf.ConfigFile, ignoreErrors bool) error {
var endpoints []string
if endpointsString, _ := config.GetString("tokens", "endpoints"); endpointsString != "" {
Expand Down Expand Up @@ -196,8 +217,17 @@ func (t *tokensEtcd) load(config *goconf.ConfigFile, ignoreErrors bool) error {
tokenFormat = "/%s"
}

t.tokenFormat.Store(tokenFormat)
log.Printf("Using %s as token format", tokenFormat)
formats := strings.Split(tokenFormat, ",")
var tokenFormats []string
for _, f := range formats {
f = strings.TrimSpace(f)
if f != "" {
tokenFormats = append(tokenFormats, f)
}
}

t.tokenFormats.Store(tokenFormats)
log.Printf("Using %v as token formats", tokenFormats)
return nil
}

Expand Down

0 comments on commit 744b514

Please sign in to comment.