From fa6ce7b7bff272729331b73e37093e5c637dcf4d Mon Sep 17 00:00:00 2001 From: Jeff Lowdermilk Date: Fri, 17 Apr 2015 15:36:31 -0700 Subject: [PATCH] Add a --raw flag to kubectl config view Defaults to false, unless --flatten is specified. Default behavior (--raw=false) is that byte data (Client{Certificate,Key}data, CertificateAuthorityData) is redacted for a more human-readable view. Useful for manually inspecting files that have embeded data. --- contrib/completions/bash/kubectl | 1 + docs/kubectl.md | 2 +- docs/kubectl_api-versions.md | 2 +- docs/kubectl_cluster-info.md | 2 +- docs/kubectl_config.md | 2 +- docs/kubectl_config_set-cluster.md | 2 +- docs/kubectl_config_set-context.md | 2 +- docs/kubectl_config_set-credentials.md | 2 +- docs/kubectl_config_set.md | 2 +- docs/kubectl_config_unset.md | 2 +- docs/kubectl_config_use-context.md | 2 +- docs/kubectl_config_view.md | 3 +- docs/kubectl_create.md | 2 +- docs/kubectl_delete.md | 2 +- docs/kubectl_describe.md | 2 +- docs/kubectl_exec.md | 2 +- docs/kubectl_expose.md | 2 +- docs/kubectl_get.md | 2 +- docs/kubectl_label.md | 2 +- docs/kubectl_log.md | 2 +- docs/kubectl_namespace.md | 2 +- docs/kubectl_port-forward.md | 2 +- docs/kubectl_proxy.md | 2 +- docs/kubectl_resize.md | 2 +- docs/kubectl_rolling-update.md | 2 +- docs/kubectl_run-container.md | 2 +- docs/kubectl_stop.md | 2 +- docs/kubectl_update.md | 2 +- docs/kubectl_version.md | 2 +- docs/man/man1/kubectl-config-view.1 | 4 + pkg/client/clientcmd/api/helpers.go | 30 +++++++ pkg/client/clientcmd/api/helpers_test.go | 101 ++++++++++++++++++++++- pkg/kubectl/cmd/config/view.go | 4 + 33 files changed, 167 insertions(+), 30 deletions(-) diff --git a/contrib/completions/bash/kubectl b/contrib/completions/bash/kubectl index cc061afcd4a..ea598112398 100644 --- a/contrib/completions/bash/kubectl +++ b/contrib/completions/bash/kubectl @@ -646,6 +646,7 @@ _kubectl_config_view() flags+=("--output=") two_word_flags+=("-o") flags+=("--output-version=") + flags+=("--raw") flags+=("--template=") two_word_flags+=("-t") diff --git a/docs/kubectl.md b/docs/kubectl.md index b238e727c8b..a18a0262669 100644 --- a/docs/kubectl.md +++ b/docs/kubectl.md @@ -66,4 +66,4 @@ kubectl * [kubectl update](kubectl_update.md) - Update a resource by filename or stdin. * [kubectl version](kubectl_version.md) - Print the client and server version information. -###### Auto generated by spf13/cobra at 2015-04-17 18:59:11.488963312 +0000 UTC +###### Auto generated by spf13/cobra at 2015-04-23 00:47:55.406236586 +0000 UTC diff --git a/docs/kubectl_api-versions.md b/docs/kubectl_api-versions.md index 9cf25963db3..94534a580f1 100644 --- a/docs/kubectl_api-versions.md +++ b/docs/kubectl_api-versions.md @@ -50,4 +50,4 @@ kubectl api-versions ### SEE ALSO * [kubectl](kubectl.md) - kubectl controls the Kubernetes cluster manager -###### Auto generated by spf13/cobra at 2015-04-17 18:59:11.488505223 +0000 UTC +###### Auto generated by spf13/cobra at 2015-04-23 00:47:55.405815046 +0000 UTC diff --git a/docs/kubectl_cluster-info.md b/docs/kubectl_cluster-info.md index 28742b6a767..aecaf26c981 100644 --- a/docs/kubectl_cluster-info.md +++ b/docs/kubectl_cluster-info.md @@ -50,4 +50,4 @@ kubectl cluster-info ### SEE ALSO * [kubectl](kubectl.md) - kubectl controls the Kubernetes cluster manager -###### Auto generated by spf13/cobra at 2015-04-17 18:59:11.48831375 +0000 UTC +###### Auto generated by spf13/cobra at 2015-04-23 00:47:55.405639667 +0000 UTC diff --git a/docs/kubectl_config.md b/docs/kubectl_config.md index 2a7f1bf20f1..f50ba53ed7e 100644 --- a/docs/kubectl_config.md +++ b/docs/kubectl_config.md @@ -63,4 +63,4 @@ kubectl config SUBCOMMAND * [kubectl config use-context](kubectl_config_use-context.md) - Sets the current-context in a kubeconfig file * [kubectl config view](kubectl_config_view.md) - displays Merged kubeconfig settings or a specified kubeconfig file. -###### Auto generated by spf13/cobra at 2015-04-17 18:59:11.488116168 +0000 UTC +###### Auto generated by spf13/cobra at 2015-04-23 00:47:55.405466384 +0000 UTC diff --git a/docs/kubectl_config_set-cluster.md b/docs/kubectl_config_set-cluster.md index 7816ada83d8..9aaeedd7f12 100644 --- a/docs/kubectl_config_set-cluster.md +++ b/docs/kubectl_config_set-cluster.md @@ -65,4 +65,4 @@ $ kubectl config set-cluster e2e --insecure-skip-tls-verify=true ### SEE ALSO * [kubectl config](kubectl_config.md) - config modifies kubeconfig files -###### Auto generated by spf13/cobra at 2015-04-17 18:59:11.486460859 +0000 UTC +###### Auto generated by spf13/cobra at 2015-04-23 00:47:55.404357726 +0000 UTC diff --git a/docs/kubectl_config_set-context.md b/docs/kubectl_config_set-context.md index fbd2f1e3697..227eacc30ad 100644 --- a/docs/kubectl_config_set-context.md +++ b/docs/kubectl_config_set-context.md @@ -58,4 +58,4 @@ $ kubectl config set-context gce --user=cluster-admin ### SEE ALSO * [kubectl config](kubectl_config.md) - config modifies kubeconfig files -###### Auto generated by spf13/cobra at 2015-04-17 18:59:11.486736724 +0000 UTC +###### Auto generated by spf13/cobra at 2015-04-23 00:47:55.40472172 +0000 UTC diff --git a/docs/kubectl_config_set-credentials.md b/docs/kubectl_config_set-credentials.md index e3538fbf34c..9d2dee9fca3 100644 --- a/docs/kubectl_config_set-credentials.md +++ b/docs/kubectl_config_set-credentials.md @@ -78,4 +78,4 @@ $ kubectl set-credentials cluster-admin --client-certificate=~/.kube/admin.crt - ### SEE ALSO * [kubectl config](kubectl_config.md) - config modifies kubeconfig files -###### Auto generated by spf13/cobra at 2015-04-17 18:59:11.486604006 +0000 UTC +###### Auto generated by spf13/cobra at 2015-04-23 00:47:55.40454463 +0000 UTC diff --git a/docs/kubectl_config_set.md b/docs/kubectl_config_set.md index 871ea936657..57dd9a1941d 100644 --- a/docs/kubectl_config_set.md +++ b/docs/kubectl_config_set.md @@ -52,4 +52,4 @@ kubectl config set PROPERTY_NAME PROPERTY_VALUE ### SEE ALSO * [kubectl config](kubectl_config.md) - config modifies kubeconfig files -###### Auto generated by spf13/cobra at 2015-04-17 18:59:11.486861123 +0000 UTC +###### Auto generated by spf13/cobra at 2015-04-23 00:47:55.404916515 +0000 UTC diff --git a/docs/kubectl_config_unset.md b/docs/kubectl_config_unset.md index 163b2e30e4f..5e42124628d 100644 --- a/docs/kubectl_config_unset.md +++ b/docs/kubectl_config_unset.md @@ -51,4 +51,4 @@ kubectl config unset PROPERTY_NAME ### SEE ALSO * [kubectl config](kubectl_config.md) - config modifies kubeconfig files -###### Auto generated by spf13/cobra at 2015-04-17 18:59:11.487685494 +0000 UTC +###### Auto generated by spf13/cobra at 2015-04-23 00:47:55.405094144 +0000 UTC diff --git a/docs/kubectl_config_use-context.md b/docs/kubectl_config_use-context.md index 0e691726476..0f7ada366d1 100644 --- a/docs/kubectl_config_use-context.md +++ b/docs/kubectl_config_use-context.md @@ -50,4 +50,4 @@ kubectl config use-context CONTEXT_NAME ### SEE ALSO * [kubectl config](kubectl_config.md) - config modifies kubeconfig files -###### Auto generated by spf13/cobra at 2015-04-17 18:59:11.487888021 +0000 UTC +###### Auto generated by spf13/cobra at 2015-04-23 00:47:55.405277784 +0000 UTC diff --git a/docs/kubectl_config_view.md b/docs/kubectl_config_view.md index 3013d4b1142..f3907d7366e 100644 --- a/docs/kubectl_config_view.md +++ b/docs/kubectl_config_view.md @@ -36,6 +36,7 @@ $ kubectl config view -o template --template='{{range .users}}{{ if eq .name "e2 --no-headers=false: When using the default output, don't print headers. -o, --output="": Output format. One of: json|yaml|template|templatefile. --output-version="": Output the formatted object with the given version (default api-version). + --raw=false: display raw byte data -t, --template="": Template string or path to template file to use when -o=template or -o=templatefile. The template format is golang templates [http://golang.org/pkg/text/template/#pkg-overview] ``` @@ -72,4 +73,4 @@ $ kubectl config view -o template --template='{{range .users}}{{ if eq .name "e2 ### SEE ALSO * [kubectl config](kubectl_config.md) - config modifies kubeconfig files -###### Auto generated by spf13/cobra at 2015-04-17 18:59:11.486319118 +0000 UTC +###### Auto generated by spf13/cobra at 2015-04-23 00:47:55.404151238 +0000 UTC diff --git a/docs/kubectl_create.md b/docs/kubectl_create.md index 8b01c7ce45e..d560ba5edf0 100644 --- a/docs/kubectl_create.md +++ b/docs/kubectl_create.md @@ -63,4 +63,4 @@ $ cat pod.json | kubectl create -f - ### SEE ALSO * [kubectl](kubectl.md) - kubectl controls the Kubernetes cluster manager -###### Auto generated by spf13/cobra at 2015-04-17 18:59:11.48343431 +0000 UTC +###### Auto generated by spf13/cobra at 2015-04-23 00:47:55.400230448 +0000 UTC diff --git a/docs/kubectl_delete.md b/docs/kubectl_delete.md index f046bafb948..c120be31f1f 100644 --- a/docs/kubectl_delete.md +++ b/docs/kubectl_delete.md @@ -81,4 +81,4 @@ $ kubectl delete pods --all ### SEE ALSO * [kubectl](kubectl.md) - kubectl controls the Kubernetes cluster manager -###### Auto generated by spf13/cobra at 2015-04-17 18:59:11.483731878 +0000 UTC +###### Auto generated by spf13/cobra at 2015-04-23 00:47:55.400618492 +0000 UTC diff --git a/docs/kubectl_describe.md b/docs/kubectl_describe.md index d68bac797c5..165f7b79197 100644 --- a/docs/kubectl_describe.md +++ b/docs/kubectl_describe.md @@ -53,4 +53,4 @@ kubectl describe RESOURCE ID ### SEE ALSO * [kubectl](kubectl.md) - kubectl controls the Kubernetes cluster manager -###### Auto generated by spf13/cobra at 2015-04-17 18:59:11.483293174 +0000 UTC +###### Auto generated by spf13/cobra at 2015-04-23 00:47:55.40003297 +0000 UTC diff --git a/docs/kubectl_exec.md b/docs/kubectl_exec.md index d473e662136..366f62efe54 100644 --- a/docs/kubectl_exec.md +++ b/docs/kubectl_exec.md @@ -64,4 +64,4 @@ $ kubectl exec -p 123456-7890 -c ruby-container -i -t -- bash -il ### SEE ALSO * [kubectl](kubectl.md) - kubectl controls the Kubernetes cluster manager -###### Auto generated by spf13/cobra at 2015-04-17 18:59:11.484697863 +0000 UTC +###### Auto generated by spf13/cobra at 2015-04-23 00:47:55.401622652 +0000 UTC diff --git a/docs/kubectl_expose.md b/docs/kubectl_expose.md index 76b67987813..2c559d94d88 100644 --- a/docs/kubectl_expose.md +++ b/docs/kubectl_expose.md @@ -82,4 +82,4 @@ $ kubectl expose streamer --port=4100 --protocol=udp --service-name=video-stream ### SEE ALSO * [kubectl](kubectl.md) - kubectl controls the Kubernetes cluster manager -###### Auto generated by spf13/cobra at 2015-04-17 18:59:11.485803902 +0000 UTC +###### Auto generated by spf13/cobra at 2015-04-23 00:47:55.403631642 +0000 UTC diff --git a/docs/kubectl_get.md b/docs/kubectl_get.md index bd7d5e16a14..f8da6d7b53b 100644 --- a/docs/kubectl_get.md +++ b/docs/kubectl_get.md @@ -85,4 +85,4 @@ $ kubectl get rc/web service/frontend pods/web-pod-13je7 ### SEE ALSO * [kubectl](kubectl.md) - kubectl controls the Kubernetes cluster manager -###### Auto generated by spf13/cobra at 2015-04-17 18:59:11.482589064 +0000 UTC +###### Auto generated by spf13/cobra at 2015-04-23 00:47:55.399795546 +0000 UTC diff --git a/docs/kubectl_label.md b/docs/kubectl_label.md index 982f40ffefa..a78d96459fb 100644 --- a/docs/kubectl_label.md +++ b/docs/kubectl_label.md @@ -81,4 +81,4 @@ $ kubectl label pods foo bar- ### SEE ALSO * [kubectl](kubectl.md) - kubectl controls the Kubernetes cluster manager -###### Auto generated by spf13/cobra at 2015-04-17 18:59:11.486060232 +0000 UTC +###### Auto generated by spf13/cobra at 2015-04-23 00:47:55.403876136 +0000 UTC diff --git a/docs/kubectl_log.md b/docs/kubectl_log.md index 418abfb3e57..d11b9be7ec6 100644 --- a/docs/kubectl_log.md +++ b/docs/kubectl_log.md @@ -62,4 +62,4 @@ $ kubectl log -f 123456-7890 ruby-container ### SEE ALSO * [kubectl](kubectl.md) - kubectl controls the Kubernetes cluster manager -###### Auto generated by spf13/cobra at 2015-04-17 18:59:11.484139739 +0000 UTC +###### Auto generated by spf13/cobra at 2015-04-23 00:47:55.400998066 +0000 UTC diff --git a/docs/kubectl_namespace.md b/docs/kubectl_namespace.md index c28b40709f3..721eb077059 100644 --- a/docs/kubectl_namespace.md +++ b/docs/kubectl_namespace.md @@ -53,4 +53,4 @@ kubectl namespace [namespace] ### SEE ALSO * [kubectl](kubectl.md) - kubectl controls the Kubernetes cluster manager -###### Auto generated by spf13/cobra at 2015-04-17 18:59:11.483937463 +0000 UTC +###### Auto generated by spf13/cobra at 2015-04-23 00:47:55.400806182 +0000 UTC diff --git a/docs/kubectl_port-forward.md b/docs/kubectl_port-forward.md index b47b5d0cbd3..e0f514679c1 100644 --- a/docs/kubectl_port-forward.md +++ b/docs/kubectl_port-forward.md @@ -68,4 +68,4 @@ $ kubectl port-forward -p mypod 0:5000 ### SEE ALSO * [kubectl](kubectl.md) - kubectl controls the Kubernetes cluster manager -###### Auto generated by spf13/cobra at 2015-04-17 18:59:11.484899751 +0000 UTC +###### Auto generated by spf13/cobra at 2015-04-23 00:47:55.40181203 +0000 UTC diff --git a/docs/kubectl_proxy.md b/docs/kubectl_proxy.md index 67906f6a4cf..b5bfc830c89 100644 --- a/docs/kubectl_proxy.md +++ b/docs/kubectl_proxy.md @@ -65,4 +65,4 @@ $ kubectl proxy --api-prefix=k8s-api ### SEE ALSO * [kubectl](kubectl.md) - kubectl controls the Kubernetes cluster manager -###### Auto generated by spf13/cobra at 2015-04-17 18:59:11.485099157 +0000 UTC +###### Auto generated by spf13/cobra at 2015-04-23 00:47:55.402034031 +0000 UTC diff --git a/docs/kubectl_resize.md b/docs/kubectl_resize.md index d4b9754392d..ebad20e0ea4 100644 --- a/docs/kubectl_resize.md +++ b/docs/kubectl_resize.md @@ -68,4 +68,4 @@ $ kubectl resize --current-replicas=2 --replicas=3 replicationcontrollers foo ### SEE ALSO * [kubectl](kubectl.md) - kubectl controls the Kubernetes cluster manager -###### Auto generated by spf13/cobra at 2015-04-17 18:59:11.484493463 +0000 UTC +###### Auto generated by spf13/cobra at 2015-04-23 00:47:55.401412855 +0000 UTC diff --git a/docs/kubectl_rolling-update.md b/docs/kubectl_rolling-update.md index 47669766ffe..82646afc41b 100644 --- a/docs/kubectl_rolling-update.md +++ b/docs/kubectl_rolling-update.md @@ -68,4 +68,4 @@ $ cat frontend-v2.json | kubectl rolling-update frontend-v1 -f - ### SEE ALSO * [kubectl](kubectl.md) - kubectl controls the Kubernetes cluster manager -###### Auto generated by spf13/cobra at 2015-04-17 18:59:11.484316119 +0000 UTC +###### Auto generated by spf13/cobra at 2015-04-23 00:47:55.401210692 +0000 UTC diff --git a/docs/kubectl_run-container.md b/docs/kubectl_run-container.md index 09735e0142c..d895102ac55 100644 --- a/docs/kubectl_run-container.md +++ b/docs/kubectl_run-container.md @@ -78,4 +78,4 @@ $ kubectl run-container nginx --image=nginx --overrides='{ "apiVersion": "v1beta ### SEE ALSO * [kubectl](kubectl.md) - kubectl controls the Kubernetes cluster manager -###### Auto generated by spf13/cobra at 2015-04-17 18:59:11.485362986 +0000 UTC +###### Auto generated by spf13/cobra at 2015-04-23 00:47:55.402296924 +0000 UTC diff --git a/docs/kubectl_stop.md b/docs/kubectl_stop.md index 83690f783df..5c62076d0ee 100644 --- a/docs/kubectl_stop.md +++ b/docs/kubectl_stop.md @@ -72,4 +72,4 @@ $ kubectl stop -f path/to/resources ### SEE ALSO * [kubectl](kubectl.md) - kubectl controls the Kubernetes cluster manager -###### Auto generated by spf13/cobra at 2015-04-17 18:59:11.48555328 +0000 UTC +###### Auto generated by spf13/cobra at 2015-04-23 00:47:55.402507426 +0000 UTC diff --git a/docs/kubectl_update.md b/docs/kubectl_update.md index b7b48ce7049..1cdbb651d28 100644 --- a/docs/kubectl_update.md +++ b/docs/kubectl_update.md @@ -67,4 +67,4 @@ $ kubectl update pods my-pod --patch='{ "apiVersion": "v1beta1", "desiredState": ### SEE ALSO * [kubectl](kubectl.md) - kubectl controls the Kubernetes cluster manager -###### Auto generated by spf13/cobra at 2015-04-17 18:59:11.483572524 +0000 UTC +###### Auto generated by spf13/cobra at 2015-04-23 00:47:55.400435658 +0000 UTC diff --git a/docs/kubectl_version.md b/docs/kubectl_version.md index 0692efd1a40..1b93f36b8f7 100644 --- a/docs/kubectl_version.md +++ b/docs/kubectl_version.md @@ -51,4 +51,4 @@ kubectl version ### SEE ALSO * [kubectl](kubectl.md) - kubectl controls the Kubernetes cluster manager -###### Auto generated by spf13/cobra at 2015-04-17 18:59:11.488692518 +0000 UTC +###### Auto generated by spf13/cobra at 2015-04-23 00:47:55.405995955 +0000 UTC diff --git a/docs/man/man1/kubectl-config-view.1 b/docs/man/man1/kubectl-config-view.1 index 0d80c5b7862..0438e65b62f 100644 --- a/docs/man/man1/kubectl-config-view.1 +++ b/docs/man/man1/kubectl-config-view.1 @@ -48,6 +48,10 @@ You can use \-\-output=template \-\-template=TEMPLATE to extract specific values \fB\-\-output\-version\fP="" Output the formatted object with the given version (default api\-version). +.PP +\fB\-\-raw\fP=false + display raw byte data + .PP \fB\-t\fP, \fB\-\-template\fP="" Template string or path to template file to use when \-o=template or \-o=templatefile. The template format is golang templates [ diff --git a/pkg/client/clientcmd/api/helpers.go b/pkg/client/clientcmd/api/helpers.go index 93681cd83bd..6c5df0717db 100644 --- a/pkg/client/clientcmd/api/helpers.go +++ b/pkg/client/clientcmd/api/helpers.go @@ -17,6 +17,7 @@ limitations under the License. package api import ( + "encoding/base64" "errors" "fmt" "io/ioutil" @@ -25,6 +26,11 @@ import ( "path/filepath" ) +func init() { + sDec, _ := base64.StdEncoding.DecodeString("REDACTED+") + redactedBytes = []byte(string(sDec)) +} + // MinifyConfig read the current context and uses that to keep only the relevant pieces of config // This is useful for making secrets based on kubeconfig files func MinifyConfig(config *Config) error { @@ -65,6 +71,30 @@ func MinifyConfig(config *Config) error { return nil } +var redactedBytes []byte + +// Flatten redacts raw data entries from the config object for a human-readable view. +func ShortenConfig(config *Config) { + // trick json encoder into printing a human readable string in the raw data + // by base64 decoding what we want to print. Relies on implementation of + // http://golang.org/pkg/encoding/json/#Marshal using base64 to encode []byte + for key, authInfo := range config.AuthInfos { + if len(authInfo.ClientKeyData) > 0 { + authInfo.ClientKeyData = redactedBytes + } + if len(authInfo.ClientCertificateData) > 0 { + authInfo.ClientCertificateData = redactedBytes + } + config.AuthInfos[key] = authInfo + } + for key, cluster := range config.Clusters { + if len(cluster.CertificateAuthorityData) > 0 { + cluster.CertificateAuthorityData = redactedBytes + } + config.Clusters[key] = cluster + } +} + // Flatten changes the config object into a self contained config (useful for making secrets) // AuthPath is not handled. func FlattenConfig(config *Config) error { diff --git a/pkg/client/clientcmd/api/helpers_test.go b/pkg/client/clientcmd/api/helpers_test.go index 7bbd5da0ad1..6fd77e431b4 100644 --- a/pkg/client/clientcmd/api/helpers_test.go +++ b/pkg/client/clientcmd/api/helpers_test.go @@ -17,10 +17,13 @@ limitations under the License. package api import ( + "fmt" "io/ioutil" "os" "reflect" "testing" + + "github.com/ghodss/yaml" ) func newMergedConfig(certFile, certContent, keyFile, keyContent, caFile, caContent string, t *testing.T) Config { @@ -36,10 +39,10 @@ func newMergedConfig(certFile, certContent, keyFile, keyContent, caFile, caConte return Config{ AuthInfos: map[string]AuthInfo{ - "red-user": {Token: "red-token"}, + "red-user": {Token: "red-token", ClientCertificateData: []byte(certContent), ClientKeyData: []byte(keyContent)}, "blue-user": {Token: "blue-token", ClientCertificate: certFile, ClientKey: keyFile}}, Clusters: map[string]Cluster{ - "cow-cluster": {Server: "http://cow.org:8080"}, + "cow-cluster": {Server: "http://cow.org:8080", CertificateAuthorityData: []byte(caContent)}, "chicken-cluster": {Server: "http://chicken.org:8080", CertificateAuthority: caFile}}, Contexts: map[string]Context{ "federal-context": {AuthInfo: "red-user", Cluster: "cow-cluster"}, @@ -202,3 +205,97 @@ func TestFlattenSuccess(t *testing.T) { } } + +func ExampleMinifyAndShorten() { + certFile, _ := ioutil.TempFile("", "") + defer os.Remove(certFile.Name()) + keyFile, _ := ioutil.TempFile("", "") + defer os.Remove(keyFile.Name()) + caFile, _ := ioutil.TempFile("", "") + defer os.Remove(caFile.Name()) + + certData := "cert" + keyData := "key" + caData := "ca" + + config := newMergedConfig(certFile.Name(), certData, keyFile.Name(), keyData, caFile.Name(), caData, nil) + + MinifyConfig(&config) + ShortenConfig(&config) + + output, _ := yaml.Marshal(config) + fmt.Printf("%s", string(output)) + // Output: + // clusters: + // cow-cluster: + // LocationOfOrigin: "" + // certificate-authority-data: REDACTED + // server: http://cow.org:8080 + // contexts: + // federal-context: + // LocationOfOrigin: "" + // cluster: cow-cluster + // user: red-user + // current-context: federal-context + // preferences: {} + // users: + // red-user: + // LocationOfOrigin: "" + // client-certificate-data: REDACTED + // client-key-data: REDACTED + // token: red-token +} + +func TestShortenSuccess(t *testing.T) { + certFile, _ := ioutil.TempFile("", "") + defer os.Remove(certFile.Name()) + keyFile, _ := ioutil.TempFile("", "") + defer os.Remove(keyFile.Name()) + caFile, _ := ioutil.TempFile("", "") + defer os.Remove(caFile.Name()) + + certData := "cert" + keyData := "key" + caData := "ca" + + unchangingCluster := "chicken-cluster" + unchangingAuthInfo := "blue-user" + changingCluster := "cow-cluster" + changingAuthInfo := "red-user" + + startingConfig := newMergedConfig(certFile.Name(), certData, keyFile.Name(), keyData, caFile.Name(), caData, t) + mutatingConfig := startingConfig + + ShortenConfig(&mutatingConfig) + + if len(mutatingConfig.Contexts) != 2 { + t.Errorf("unexpected contexts: %v", mutatingConfig.Contexts) + } + if !reflect.DeepEqual(startingConfig.Contexts, mutatingConfig.Contexts) { + t.Errorf("expected %v, got %v", startingConfig.Contexts, mutatingConfig.Contexts) + } + + redacted := string(redactedBytes) + if len(mutatingConfig.Clusters) != 2 { + t.Errorf("unexpected clusters: %v", mutatingConfig.Clusters) + } + if !reflect.DeepEqual(startingConfig.Clusters[unchangingCluster], mutatingConfig.Clusters[unchangingCluster]) { + t.Errorf("expected %v, got %v", startingConfig.Clusters[unchangingCluster], mutatingConfig.Clusters[unchangingCluster]) + } + if string(mutatingConfig.Clusters[changingCluster].CertificateAuthorityData) != redacted { + t.Errorf("expected %v, got %v", redacted, string(mutatingConfig.Clusters[changingCluster].CertificateAuthorityData)) + } + + if len(mutatingConfig.AuthInfos) != 2 { + t.Errorf("unexpected users: %v", mutatingConfig.AuthInfos) + } + if !reflect.DeepEqual(startingConfig.AuthInfos[unchangingAuthInfo], mutatingConfig.AuthInfos[unchangingAuthInfo]) { + t.Errorf("expected %v, got %v", startingConfig.AuthInfos[unchangingAuthInfo], mutatingConfig.AuthInfos[unchangingAuthInfo]) + } + if string(mutatingConfig.AuthInfos[changingAuthInfo].ClientCertificateData) != redacted { + t.Errorf("expected %v, got %v", redacted, string(mutatingConfig.AuthInfos[changingAuthInfo].ClientCertificateData)) + } + if string(mutatingConfig.AuthInfos[changingAuthInfo].ClientKeyData) != redacted { + t.Errorf("expected %v, got %v", redacted, string(mutatingConfig.AuthInfos[changingAuthInfo].ClientKeyData)) + } +} diff --git a/pkg/kubectl/cmd/config/view.go b/pkg/kubectl/cmd/config/view.go index e8bb6e2eaa3..f205e5d7fdd 100644 --- a/pkg/kubectl/cmd/config/view.go +++ b/pkg/kubectl/cmd/config/view.go @@ -36,6 +36,7 @@ type ViewOptions struct { Merge util.BoolFlag Flatten bool Minify bool + RawByteData bool } const ( @@ -83,6 +84,7 @@ func NewCmdConfigView(out io.Writer, ConfigAccess ConfigAccess) *cobra.Command { options.Merge.Default(true) cmd.Flags().Var(&options.Merge, "merge", "merge together the full hierarchy of kubeconfig files") + cmd.Flags().BoolVar(&options.RawByteData, "raw", false, "display raw byte data") cmd.Flags().BoolVar(&options.Flatten, "flatten", false, "flatten the resulting kubeconfig file into self contained output (useful for creating portable kubeconfig files)") cmd.Flags().BoolVar(&options.Minify, "minify", false, "remove all information not used by current-context from the output") return cmd @@ -104,6 +106,8 @@ func (o ViewOptions) Run(out io.Writer, printer kubectl.ResourcePrinter) error { if err := clientcmdapi.FlattenConfig(config); err != nil { return err } + } else if !o.RawByteData { + clientcmdapi.ShortenConfig(config) } err = printer.PrintObj(config, out)