Skip to content

Commit 6fbe7d4

Browse files
weltekialexellis
authored andcommitted
Use function name as cache key for function access tokens
Remove the source token from the cache key. By using the source token as part of the cache key the cache would be invalidated when the original access token had expired which is confusing for users and leads to higher load on the token exchange endpoint if short lived tokens are used. Signed-off-by: Han Verstraete (OpenFaaS Ltd) <han@openfaas.com>
1 parent 0630bc9 commit 6fbe7d4

File tree

1 file changed

+1
-17
lines changed

1 file changed

+1
-17
lines changed

functions.go

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package sdk
22

33
import (
4-
"crypto/sha256"
54
"fmt"
65
"net/http"
76
)
@@ -36,8 +35,7 @@ func (c *Client) InvokeFunction(name, namespace string, async bool, auth bool, r
3635
if c.fnTokenCache != nil {
3736
// Function access tokens are cached as long as the token is valid
3837
// to prevent having to do a token exchange each time the function is invoked.
39-
cacheKey := getFunctionTokenCacheKey(idToken, fmt.Sprintf("%s.%s", name, namespace))
40-
38+
cacheKey := fmt.Sprintf("%s.%s", name, namespace)
4139
token, ok := c.fnTokenCache.Get(cacheKey)
4240
if !ok {
4341
token, err = ExchangeIDToken(tokenURL, idToken, WithScope(scope), WithAudience(audience))
@@ -63,17 +61,3 @@ func (c *Client) InvokeFunction(name, namespace string, async bool, auth bool, r
6361

6462
return c.do(req)
6563
}
66-
67-
// getFunctionTokenCacheKey computes a cache key for caching a function access token based
68-
// on the original id token that is exchanged for the function access token and the function
69-
// name e.g. figlet.openfaas-fn.
70-
// The original token is included in the hash to avoid cache hits for a function when the
71-
// source token changes.
72-
func getFunctionTokenCacheKey(idToken string, serviceName string) string {
73-
hash := sha256.New()
74-
hash.Write([]byte(idToken))
75-
hash.Write([]byte(serviceName))
76-
77-
sum := hash.Sum(nil)
78-
return fmt.Sprintf("%x", sum)
79-
}

0 commit comments

Comments
 (0)