From 844b43f435c0026cfd4fbd0724e78d68a2fb93af Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Mon, 15 Mar 2021 11:28:14 -0400 Subject: [PATCH] Fix Lens not clearing other KUBECONFIG env vars (#2297) --- src/main/__test__/shell-session.test.ts | 19 +++++++++++++++++++ src/main/shell-session.ts | 19 ++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 src/main/__test__/shell-session.test.ts diff --git a/src/main/__test__/shell-session.test.ts b/src/main/__test__/shell-session.test.ts new file mode 100644 index 0000000000..fe1b0c4288 --- /dev/null +++ b/src/main/__test__/shell-session.test.ts @@ -0,0 +1,19 @@ +/** + * @jest-environment jsdom + */ + +import { clearKubeconfigEnvVars } from "../shell-session"; + +describe("clearKubeconfigEnvVars tests", () => { + it("should not touch non kubeconfig keys", () => { + expect(clearKubeconfigEnvVars({ a: 1 })).toStrictEqual({ a: 1 }); + }); + + it("should remove a single kubeconfig key", () => { + expect(clearKubeconfigEnvVars({ a: 1, kubeconfig: "1" })).toStrictEqual({ a: 1 }); + }); + + it("should remove a two kubeconfig key", () => { + expect(clearKubeconfigEnvVars({ a: 1, kubeconfig: "1", kUbeconfig: "1" })).toStrictEqual({ a: 1 }); + }); +}); diff --git a/src/main/shell-session.ts b/src/main/shell-session.ts index 10a2f9ed47..e11804ac16 100644 --- a/src/main/shell-session.ts +++ b/src/main/shell-session.ts @@ -12,6 +12,23 @@ import { isWindows } from "../common/vars"; import { appEventBus } from "../common/event-bus"; import { userStore } from "../common/user-store"; +const anyKubeconfig = /^kubeconfig$/i; + +/** + * This function deletes all keys of the form /^kubeconfig$/i, returning a new + * object. + * + * This is needed because `kubectl` checks for other version of kubeconfig + * before KUBECONFIG and we only set KUBECONFIG. + * @param env The current copy of env + */ +export function clearKubeconfigEnvVars(env: Record): Record { + return Object.fromEntries( + Object.entries(env) + .filter(([key]) => anyKubeconfig.exec(key) === null) + ); +} + export class ShellSession extends EventEmitter { static shellEnvs: Map = new Map(); @@ -103,7 +120,7 @@ export class ShellSession extends EventEmitter { } protected async getShellEnv() { - const env = JSON.parse(JSON.stringify(await shellEnv())); + const env = clearKubeconfigEnvVars(JSON.parse(JSON.stringify(await shellEnv()))); const pathStr = [this.kubectlBinDir, this.helmBinDir, process.env.PATH].join(path.delimiter); if(isWindows) {