-
Notifications
You must be signed in to change notification settings - Fork 4
/
functions.go
63 lines (50 loc) · 1.69 KB
/
functions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package sdk
import (
"fmt"
"net/http"
)
const DefaultNamespace = "openfaas-fn"
func (c *Client) InvokeFunction(name, namespace string, async bool, auth bool, req *http.Request) (*http.Response, error) {
fnEndpoint := "/function"
if async {
fnEndpoint = "/async-function"
}
if len(namespace) == 0 {
namespace = DefaultNamespace
}
req.URL.Scheme = c.GatewayURL.Scheme
req.URL.Host = c.GatewayURL.Host
req.URL.Path = fmt.Sprintf("%s/%s.%s", fnEndpoint, name, namespace)
if auth && c.FunctionTokenSource != nil {
idToken, err := c.FunctionTokenSource.Token()
if err != nil {
return nil, fmt.Errorf("failed to get function access token: %w", err)
}
tokenURL := fmt.Sprintf("%s/oauth/token", c.GatewayURL.String())
scope := []string{"function"}
audience := []string{fmt.Sprintf("%s:%s", namespace, name)}
var bearer string
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 := fmt.Sprintf("%s.%s", name, namespace)
token, ok := c.fnTokenCache.Get(cacheKey)
if !ok {
token, err = ExchangeIDToken(tokenURL, idToken, WithScope(scope), WithAudience(audience))
if err != nil {
return nil, fmt.Errorf("failed to get function access token: %w", err)
}
c.fnTokenCache.Set(cacheKey, token)
}
bearer = token.IDToken
} else {
token, err := ExchangeIDToken(tokenURL, idToken, WithScope(scope), WithAudience(audience))
if err != nil {
return nil, fmt.Errorf("failed to get function access token: %w", err)
}
bearer = token.IDToken
}
req.Header.Add("Authorization", "Bearer "+bearer)
}
return c.do(req)
}