-
Notifications
You must be signed in to change notification settings - Fork 490
/
token.go
55 lines (47 loc) · 1.88 KB
/
token.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
package kubernetes
import (
"fmt"
"os"
"time"
"github.com/kiali/kiali/config"
)
// Be careful with how you use this token. This is the Kiali Service Account token, not the user token.
// We need the Service Account token to access third-party in-cluster services (e.g. Grafana).
var DefaultServiceAccountPath = "/var/run/secrets/kubernetes.io/serviceaccount/token"
var (
KialiTokenForHomeCluster string
tokenRead time.Time
)
// GetKialiTokenForHomeCluster returns the Kiali SA token to be used to communicate with the local data plane k8s api endpoint.
func GetKialiTokenForHomeCluster() (string, error) {
// TODO: refresh the token when it changes rather than after it expires
if KialiTokenForHomeCluster == "" || shouldRefreshToken() {
if remoteSecret, err := GetRemoteSecret(config.Get().Deployment.RemoteSecretPath); err == nil { // for experimental feature - for when data plane is in a remote cluster
currentContextAuthInfo := remoteSecret.Contexts[remoteSecret.CurrentContext].AuthInfo
if authInfo, ok := remoteSecret.AuthInfos[currentContextAuthInfo]; ok {
KialiTokenForHomeCluster = authInfo.Token
} else {
return "", fmt.Errorf("auth info not found for current context: [%s]. Current context must be set for kiali remote secret", remoteSecret.CurrentContext)
}
} else {
token, err := os.ReadFile(DefaultServiceAccountPath)
if err != nil {
return "", err
}
KialiTokenForHomeCluster = string(token)
}
tokenRead = time.Now()
}
return KialiTokenForHomeCluster, nil
}
// shouldRefreshToken checks to see if the local Kiali token expired.
// TODO should check all tokens for all clusters
func shouldRefreshToken() bool {
// TODO: hardcoded to 60s, do we want this configurable? Or do we need to obtain this from k8s somehow?
timerDuration := time.Second * 60
if time.Since(tokenRead) > timerDuration {
return true
} else {
return false
}
}