Skip to content

Commit 7aabd09

Browse files
fix: fix credential caching
1 parent b97be73 commit 7aabd09

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

pkg/auth/cache.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,14 @@ package auth
22

33
import (
44
"encoding/json"
5-
"fmt"
65
"os"
76
"path/filepath"
87
"strings"
98
"time"
109

11-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12-
"k8s.io/client-go/pkg/apis/clientauthentication/v1"
13-
1410
"github.com/traviswt/gke-auth-plugin/pkg/conf"
11+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12+
v1 "k8s.io/client-go/pkg/apis/clientauthentication/v1"
1513
)
1614

1715
func GetExecCredential() *v1.ExecCredential {
@@ -44,17 +42,25 @@ func SaveExecCredential(ec *v1.ExecCredential) {
4442
}
4543

4644
// cacheLocation returns the file to Cache the exec cred to, if blank, don't Cache
45+
// KUBECONFIG takes precedence, if not set, the .kube directory will be used, if it exists.
46+
// Otherwise credentials will be cached in the user's home directory, which is not ideal,
47+
// but better than nothing.
4748
func cacheLocation() string {
48-
kubeconfig := os.Getenv("KUBECONFIG")
49-
if kubeconfig == "" {
50-
return ""
49+
if kubeconfig := os.Getenv("KUBECONFIG"); kubeconfig != "" {
50+
return filepath.Join(filepath.Dir(kubeconfig), conf.CacheFileName)
5151
}
52-
abs, err := filepath.Abs(kubeconfig)
52+
53+
userHomeDir, err := os.UserHomeDir()
5354
if err != nil {
5455
return ""
5556
}
56-
fmt.Println(filepath.Join(abs, conf.CacheFileName))
57-
return filepath.Join(abs, conf.CacheFileName)
57+
58+
kubeDir := filepath.Join(userHomeDir, ".kube")
59+
if _, err := os.Stat(kubeDir); err == nil {
60+
return filepath.Join(kubeDir, conf.CacheFileName)
61+
}
62+
63+
return filepath.Join(userHomeDir, conf.CacheFileName)
5864
}
5965

6066
func loadFile(file string) (*v1.ExecCredential, error) {

0 commit comments

Comments
 (0)