From f1fb1e2537afd1d95a99f32f0b9c7af3b94349e0 Mon Sep 17 00:00:00 2001 From: Sebastian Malton Date: Mon, 23 Aug 2021 16:21:26 -0400 Subject: [PATCH] Fix check for user exec command Signed-off-by: Sebastian Malton --- package.json | 1 - src/common/custom-errors.ts | 8 +++++--- src/common/kube-helpers.ts | 23 ++++++++++++++++------- yarn.lock | 5 ----- 4 files changed, 21 insertions(+), 16 deletions(-) diff --git a/package.json b/package.json index 262db118b3..0cad8dc9c6 100644 --- a/package.json +++ b/package.json @@ -193,7 +193,6 @@ "byline": "^5.0.0", "chalk": "^4.1.0", "chokidar": "^3.4.3", - "command-exists": "1.2.9", "conf": "^7.0.1", "crypto-js": "^4.1.1", "electron-devtools-installer": "^3.2.0", diff --git a/src/common/custom-errors.ts b/src/common/custom-errors.ts index 2cbc75ccf0..98c5334d6c 100644 --- a/src/common/custom-errors.ts +++ b/src/common/custom-errors.ts @@ -19,15 +19,17 @@ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +import path from "path"; + export class ExecValidationNotFoundError extends Error { - constructor(execPath: string, isAbsolute: boolean) { + constructor(execPath: string) { super(`User Exec command "${execPath}" not found on host.`); let message = `User Exec command "${execPath}" not found on host.`; - if (!isAbsolute) { + if (!path.isAbsolute(execPath)) { message += ` Please ensure binary is found in PATH or use absolute path to binary in Kubeconfig`; } - this.message = message; + this.message = message; this.name = this.constructor.name; Error.captureStackTrace(this, this.constructor); } diff --git a/src/common/kube-helpers.ts b/src/common/kube-helpers.ts index aff05e68c6..34a9c924dd 100644 --- a/src/common/kube-helpers.ts +++ b/src/common/kube-helpers.ts @@ -25,11 +25,11 @@ import path from "path"; import os from "os"; import yaml from "js-yaml"; import logger from "../main/logger"; -import commandExists from "command-exists"; import { ExecValidationNotFoundError } from "./custom-errors"; import { Cluster, Context, newClusters, newContexts, newUsers, User } from "@kubernetes/client-node/dist/config_types"; import { resolvePath } from "./utils"; import Joi from "joi"; +import { execFileSync } from "child_process"; export type KubeConfigValidationOpts = { validateCluster?: boolean; @@ -295,13 +295,22 @@ export function validateKubeConfig(config: KubeConfig, contextName: string, vali // Validate exec command if present if (validateExec && user?.exec) { - const execCommand = user.exec["command"]; - // check if the command is absolute or not - const isAbsolute = path.isAbsolute(execCommand); + try { + execFileSync(user.exec.command, { timeout: 1 }); - // validate the exec struct in the user object, start with the command field - if (!commandExists.sync(execCommand)) { - return new ExecValidationNotFoundError(execCommand, isAbsolute); + // If this doesn't throw an error it also means that it has found the executable. + } catch (error) { + switch (error?.code) { + case "ETIMEDOUT": + // ignore this error code. It means that the exec can be found. + break; + case "ENOENT": + case "EACCES": + case "EPERM": + return new ExecValidationNotFoundError(user.exec.command); + default: + return error; + } } } diff --git a/yarn.lock b/yarn.lock index b6667a6d55..abdfd4c5d9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4066,11 +4066,6 @@ combined-stream@^1.0.6, combined-stream@^1.0.8, combined-stream@~1.0.6: dependencies: delayed-stream "~1.0.0" -command-exists@1.2.9: - version "1.2.9" - resolved "https://registry.yarnpkg.com/command-exists/-/command-exists-1.2.9.tgz#c50725af3808c8ab0260fd60b01fbfa25b954f69" - integrity sha512-LTQ/SGc+s0Xc0Fu5WaKnR0YiygZkm9eKFvyS+fRsU7/ZWFF8ykFM6Pc9aCVf1+xasOOZpO3BAVgVrKvsqKHV7w== - commander@2.9.0: version "2.9.0" resolved "https://registry.yarnpkg.com/commander/-/commander-2.9.0.tgz#9c99094176e12240cb22d6c5146098400fe0f7d4"