Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 38 additions & 9 deletions cmd/kcc-cache/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,47 @@ func main() {
}

// cache key
var cacheKey = strings.Join(os.Args[1:], " ")
//
// When kubectl provides cluster info (provideClusterInfo: true, which
// kcc-injector enables), the target API server URL is available via the
// KUBERNETES_EXEC_INFO env var. Combined with the kubeconfig user name
// (KUBE_CREDENTIAL_CACHE_USER, also set by kcc-injector) this yields a
// stable cache key: it survives cosmetic argv changes and never serves one
// identity's credentials for another identity on the same cluster.
//
// If the server URL is unavailable (provideClusterInfo not set, or a manual
// setup), fall back to the legacy argv + env based key.
var cacheKey string
{
env := ""
for _, key := range cacheKeyEnvlist {
v := os.Getenv(key)
if v == "" {
continue
server := ""
if e := os.Getenv("KUBERNETES_EXEC_INFO"); e != "" {
var execInfo struct {
Spec struct {
Cluster struct {
Server string `json:"server"`
} `json:"cluster"`
} `json:"spec"`
}
if err := json.Unmarshal([]byte(e), &execInfo); err == nil {
server = execInfo.Spec.Cluster.Server
}
env = fmt.Sprintf("%s %s='%s'", env, key, v)
}
if env != "" {
cacheKey = fmt.Sprintf("%s # env:%s", cacheKey, env)

if server != "" {
cacheKey = fmt.Sprintf("user=%q server=%q", os.Getenv("KUBE_CREDENTIAL_CACHE_USER"), server)
} else {
cacheKey = strings.Join(os.Args[1:], " ")
env := ""
for _, key := range cacheKeyEnvlist {
v := os.Getenv(key)
if v == "" {
continue
}
env = fmt.Sprintf("%s %s='%s'", env, key, v)
}
if env != "" {
cacheKey = fmt.Sprintf("%s # env:%s", cacheKey, env)
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions cmd/kcc-injector/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func main() {
if user.Exec.Command == replaceCmd {
user.Exec.Command = user.Exec.Args[0]
user.Exec.Args = user.Exec.Args[1:]
user.Exec.ProvideClusterInfo = false
}

search := func() (index int) {
Expand Down Expand Up @@ -103,6 +104,11 @@ func main() {
user.Exec.Command = replaceCmd
}

// Expose the target cluster's API server URL to kcc-cache via the
// KUBERNETES_EXEC_INFO env var, so it can key the cache on the
// cluster (server URL) in addition to the user name.
user.Exec.ProvideClusterInfo = true

found := false
userEnv := api.ExecEnvVar{
Name: "KUBE_CREDENTIAL_CACHE_USER",
Expand Down