From dd7a6143ffa49bb9b94553df073ca50f6c94a9d6 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Fri, 11 Oct 2024 17:41:39 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E8=A7=A3=E5=86=B3=E6=8C=87=E4=BB=A4=E8=BF=87=E9=95=BF=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/engine/KubernetesExecEngine.java | 16 ++++++++++------ .../api/engine/KubernetesProvider.java | 16 ++++++++++++---- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/engine/KubernetesExecEngine.java b/backend/services/api-test/src/main/java/io/metersphere/api/engine/KubernetesExecEngine.java index c844b79f74..f8ef6ad049 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/engine/KubernetesExecEngine.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/engine/KubernetesExecEngine.java @@ -22,6 +22,7 @@ public class KubernetesExecEngine implements ApiEngine { */ private final Object request; private final TestResourceDTO resource; + private final String scriptId; /** * 单调执行构造函数 @@ -29,9 +30,10 @@ public class KubernetesExecEngine implements ApiEngine { * @param request * @param resource */ - public KubernetesExecEngine(TaskRequestDTO request, TestResourceDTO resource) { + public KubernetesExecEngine(TaskRequestDTO request, TestResourceDTO resource, String scriptId) { this.request = request; this.resource = resource; + this.scriptId = scriptId; } /** @@ -40,9 +42,10 @@ public class KubernetesExecEngine implements ApiEngine { * @param batchRequestDTO * @param resource */ - public KubernetesExecEngine(TaskBatchRequestDTO batchRequestDTO, TestResourceDTO resource) { + public KubernetesExecEngine(TaskBatchRequestDTO batchRequestDTO, TestResourceDTO resource, String scriptId) { this.resource = resource; this.request = batchRequestDTO; + this.scriptId = scriptId; } /** @@ -51,21 +54,22 @@ public class KubernetesExecEngine implements ApiEngine { * @param reportIds * @param resource */ - public KubernetesExecEngine(List reportIds, TestResourceDTO resource) { + public KubernetesExecEngine(List reportIds, TestResourceDTO resource, String scriptId) { this.resource = resource; this.request = reportIds; + this.scriptId = scriptId; } @Override public void execute(String command) { // 初始化任务 LogUtils.info("CURL 命令:【 " + command + " 】"); - this.runApi(command); + this.runApi(command, request); } - private void runApi(String command) { + private void runApi(String command, Object request) { try { - KubernetesProvider.exec(resource, command); + KubernetesProvider.exec(resource, request, scriptId, command); } catch (HttpServerErrorException e) { handleHttpServerError(e); } catch (Exception e) { diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/engine/KubernetesProvider.java b/backend/services/api-test/src/main/java/io/metersphere/api/engine/KubernetesProvider.java index 54ca2571f8..7f5953f73f 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/engine/KubernetesProvider.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/engine/KubernetesProvider.java @@ -57,11 +57,20 @@ public class KubernetesProvider { * @param command * @throws Exception */ - public static void exec(TestResourceDTO resource, String command) throws Exception { + protected static void exec(TestResourceDTO resource, Object runRequest, String scriptId, String command) throws Exception { + // 防止执行非法命令 + if (StringUtils.isEmpty(command) || !StringUtils.contains(command, scriptId)) { + throw new MSException("Invalid command: " + command); + } ExecWatch execWatch = null; try (KubernetesClient client = getKubernetesClient(resource)) { Pod pod = getExecPod(client, resource); LogUtils.info("当前执行 Pod:【 " + pod.getMetadata().getName() + " 】"); + // 创建文件 + String createFile = "echo -e \"" + JSON.toJSONString(runRequest) + "\" > " + scriptId; + + // 删除文件 + String deleteFile = "rm -f " + scriptId; // 同步执行命令 execWatch = client.pods().inNamespace(client.getNamespace()) @@ -69,8 +78,7 @@ public class KubernetesProvider { .redirectingInput() .writingOutput(System.out) .writingError(System.err) - .withTTY() - .exec(SHELL_COMMAND, "-c", command); + .withTTY().exec(SHELL_COMMAND, "-c", createFile + " && " + command + " && " + deleteFile); // 等待命令执行完成,获取结果 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); @@ -107,7 +115,7 @@ public class KubernetesProvider { * @param runRequest * @param command */ - public static void exec(TestResourceDTO resource, Object runRequest, String command) { + public static void execCommand(TestResourceDTO resource, Object runRequest, String command) { try (KubernetesClient client = getKubernetesClient(resource)) { Pod pod = getExecPod(client, resource); LogUtils.info("当前执行 Pod:【 " + pod.getMetadata().getName() + " 】");