Changed kubectl config view to redact token

This commit is contained in:
Brian Pursley 2020-03-17 15:13:50 -04:00
parent 0bb125e731
commit 6fad4ee5e5
4 changed files with 16 additions and 10 deletions

View File

@ -98,6 +98,9 @@ func ShortenConfig(config *Config) {
if len(authInfo.ClientCertificateData) > 0 { if len(authInfo.ClientCertificateData) > 0 {
authInfo.ClientCertificateData = redactedBytes authInfo.ClientCertificateData = redactedBytes
} }
if len(authInfo.Token) > 0 {
authInfo.Token = "REDACTED"
}
config.AuthInfos[key] = authInfo config.AuthInfos[key] = authInfo
} }
for key, cluster := range config.Clusters { for key, cluster := range config.Clusters {

View File

@ -243,7 +243,7 @@ func Example_minifyAndShorten() {
// LocationOfOrigin: "" // LocationOfOrigin: ""
// client-certificate-data: REDACTED // client-certificate-data: REDACTED
// client-key-data: REDACTED // client-key-data: REDACTED
// token: red-token // token: REDACTED
} }
func TestShortenSuccess(t *testing.T) { func TestShortenSuccess(t *testing.T) {
@ -299,4 +299,7 @@ func TestShortenSuccess(t *testing.T) {
if string(mutatingConfig.AuthInfos[changingAuthInfo].ClientKeyData) != redacted { if string(mutatingConfig.AuthInfos[changingAuthInfo].ClientKeyData) != redacted {
t.Errorf("expected %v, got %v", redacted, string(mutatingConfig.AuthInfos[changingAuthInfo].ClientKeyData)) t.Errorf("expected %v, got %v", redacted, string(mutatingConfig.AuthInfos[changingAuthInfo].ClientKeyData))
} }
if mutatingConfig.AuthInfos[changingAuthInfo].Token != "REDACTED" {
t.Errorf("expected REDACTED, got %v", mutatingConfig.AuthInfos[changingAuthInfo].Token)
}
} }

View File

@ -72,7 +72,7 @@ func Example_view() {
// users: // users:
// - name: red-user // - name: red-user
// user: // user:
// token: red-token // token: REDACTED
} }
func TestCurrentContext(t *testing.T) { func TestCurrentContext(t *testing.T) {

View File

@ -48,8 +48,8 @@ func TestViewCluster(t *testing.T) {
}, },
CurrentContext: "minikube", CurrentContext: "minikube",
AuthInfos: map[string]*clientcmdapi.AuthInfo{ AuthInfos: map[string]*clientcmdapi.AuthInfo{
"minikube": {Token: "minikube-token"}, "minikube": {Token: "REDACTED"},
"mu-cluster": {Token: "minikube-token"}, "mu-cluster": {Token: "REDACTED"},
}, },
} }
@ -79,10 +79,10 @@ preferences: {}
users: users:
- name: minikube - name: minikube
user: user:
token: minikube-token token: REDACTED
- name: mu-cluster - name: mu-cluster
user: user:
token: minikube-token` + "\n", token: REDACTED` + "\n",
} }
test.run(t) test.run(t)
@ -103,8 +103,8 @@ func TestViewClusterMinify(t *testing.T) {
}, },
CurrentContext: "minikube", CurrentContext: "minikube",
AuthInfos: map[string]*clientcmdapi.AuthInfo{ AuthInfos: map[string]*clientcmdapi.AuthInfo{
"minikube": {Token: "minikube-token"}, "minikube": {Token: "REDACTED"},
"mu-cluster": {Token: "minikube-token"}, "mu-cluster": {Token: "REDACTED"},
}, },
} }
@ -134,7 +134,7 @@ preferences: {}
users: users:
- name: minikube - name: minikube
user: user:
token: minikube-token` + "\n", token: REDACTED` + "\n",
}, },
{ {
description: "Testing for kubectl config view --minify=true --context=my-cluster", description: "Testing for kubectl config view --minify=true --context=my-cluster",
@ -156,7 +156,7 @@ preferences: {}
users: users:
- name: mu-cluster - name: mu-cluster
user: user:
token: minikube-token` + "\n", token: REDACTED` + "\n",
}, },
} }