Skip to content

Commit

Permalink
Use function name as cache key for function access tokens
Browse files Browse the repository at this point in the history
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) <[email protected]>
  • Loading branch information
welteki authored and alexellis committed Jun 14, 2024
1 parent 0630bc9 commit 6fbe7d4
Showing 1 changed file with 1 addition and 17 deletions.
18 changes: 1 addition & 17 deletions functions.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package sdk

import (
"crypto/sha256"
"fmt"
"net/http"
)
Expand Down Expand Up @@ -36,8 +35,7 @@ func (c *Client) InvokeFunction(name, namespace string, async bool, auth bool, r
if c.fnTokenCache != nil {
// Function access tokens are cached as long as the token is valid
// to prevent having to do a token exchange each time the function is invoked.
cacheKey := getFunctionTokenCacheKey(idToken, fmt.Sprintf("%s.%s", name, namespace))

cacheKey := fmt.Sprintf("%s.%s", name, namespace)
token, ok := c.fnTokenCache.Get(cacheKey)
if !ok {
token, err = ExchangeIDToken(tokenURL, idToken, WithScope(scope), WithAudience(audience))
Expand All @@ -63,17 +61,3 @@ func (c *Client) InvokeFunction(name, namespace string, async bool, auth bool, r

return c.do(req)
}

// getFunctionTokenCacheKey computes a cache key for caching a function access token based
// on the original id token that is exchanged for the function access token and the function
// name e.g. figlet.openfaas-fn.
// The original token is included in the hash to avoid cache hits for a function when the
// source token changes.
func getFunctionTokenCacheKey(idToken string, serviceName string) string {
hash := sha256.New()
hash.Write([]byte(idToken))
hash.Write([]byte(serviceName))

sum := hash.Sum(nil)
return fmt.Sprintf("%x", sum)
}

0 comments on commit 6fbe7d4

Please sign in to comment.