From 6dda52bd4f911c6a82886d7595301c39ef322e98 Mon Sep 17 00:00:00 2001 From: Gordon Bleux Date: Wed, 26 Jan 2022 20:13:54 +0100 Subject: [PATCH] render kube config using the authenticated user. instead of a hardcoded value, the configuration file uses the user information from the authentication context for the user and context settings. closes #45 --- handlers/kubecfg/kubecfg.go | 5 ++--- handlers/kubecfg/kubecfg_test.go | 12 ++++++------ 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/handlers/kubecfg/kubecfg.go b/handlers/kubecfg/kubecfg.go index 4550989..dfa4ea2 100644 --- a/handlers/kubecfg/kubecfg.go +++ b/handlers/kubecfg/kubecfg.go @@ -32,7 +32,6 @@ import ( ) const ( - templateUser = "kubehook" queryParamLifetime = "lifetime" ) @@ -67,7 +66,7 @@ func Handler(g auth.Generator, template *api.Config, h handlers.AuthHeaders) htt return } - y, err := clientcmd.Write(populateUser(template, templateUser, t)) + y, err := clientcmd.Write(populateUser(template, u, t)) if err != nil { http.Error(w, errors.Wrap(err, "cannot marshal template to YAML").Error(), http.StatusInternalServerError) return @@ -89,7 +88,7 @@ func populateUser(cfg *api.Config, username, token string) api.Config { } for name, cluster := range cfg.Clusters { c.Clusters[name] = cluster - c.Contexts[name] = &api.Context{Cluster: name, AuthInfo: templateUser} + c.Contexts[name] = &api.Context{Cluster: name, AuthInfo: username} } c.CurrentContext = cfg.CurrentContext return c diff --git a/handlers/kubecfg/kubecfg_test.go b/handlers/kubecfg/kubecfg_test.go index 452d3d6..669bd29 100644 --- a/handlers/kubecfg/kubecfg_test.go +++ b/handlers/kubecfg/kubecfg_test.go @@ -60,10 +60,10 @@ func TestHandler(t *testing.T) { "b": &api.Cluster{Server: "https://example.net", CertificateAuthorityData: []byte("PAM")}, }, Contexts: map[string]*api.Context{ - "a": &api.Context{AuthInfo: templateUser, Cluster: "a"}, - "b": &api.Context{AuthInfo: templateUser, Cluster: "b"}, + "a": &api.Context{AuthInfo: user, Cluster: "a"}, + "b": &api.Context{AuthInfo: user, Cluster: "b"}, }, - AuthInfos: map[string]*api.AuthInfo{templateUser: &api.AuthInfo{Token: user}}, + AuthInfos: map[string]*api.AuthInfo{user: &api.AuthInfo{Token: user}}, }, }, { @@ -83,10 +83,10 @@ func TestHandler(t *testing.T) { "b": &api.Cluster{Server: "https://example.net", CertificateAuthorityData: []byte("PAM")}, }, Contexts: map[string]*api.Context{ - "a": &api.Context{AuthInfo: templateUser, Cluster: "a"}, - "b": &api.Context{AuthInfo: templateUser, Cluster: "b"}, + "a": &api.Context{AuthInfo: user, Cluster: "a"}, + "b": &api.Context{AuthInfo: user, Cluster: "b"}, }, - AuthInfos: map[string]*api.AuthInfo{templateUser: &api.AuthInfo{Token: user}}, + AuthInfos: map[string]*api.AuthInfo{user: &api.AuthInfo{Token: user}}, }, }, {