github-code-scanning[bot] commented on code in PR #13801:
URL: 
https://github.com/apache/dolphinscheduler/pull/13801#discussion_r1150333674


##########
dolphinscheduler-task-plugin/dolphinscheduler-task-remoteshell/src/main/java/org/apache/dolphinscheduler/plugin/task/remoteshell/RemoteExecutor.java:
##########
@@ -0,0 +1,251 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.dolphinscheduler.plugin.task.remoteshell;
+
+import org.apache.dolphinscheduler.plugin.datasource.ssh.SSHUtils;
+import 
org.apache.dolphinscheduler.plugin.datasource.ssh.param.SSHConnectionParam;
+import org.apache.dolphinscheduler.plugin.task.api.TaskConstants;
+import org.apache.dolphinscheduler.plugin.task.api.TaskException;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.sshd.client.SshClient;
+import org.apache.sshd.client.channel.ChannelExec;
+import org.apache.sshd.client.channel.ClientChannelEvent;
+import org.apache.sshd.client.session.ClientSession;
+import org.apache.sshd.sftp.client.SftpClientFactory;
+import org.apache.sshd.sftp.client.fs.SftpFileSystem;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.EnumSet;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class RemoteExecutor {
+
+    protected final Logger logger =
+            
LoggerFactory.getLogger(String.format(TaskConstants.TASK_LOGGER_THREAD_NAME, 
getClass()));
+
+    protected static final Pattern SETVALUE_REGEX = 
Pattern.compile(TaskConstants.SETVALUE_REGEX);
+
+    static final String REMOTE_SHELL_HOME = 
"/tmp/dolphinscheduler-remote-shell-%s/";
+    static final String STATUS_TAG_MESSAGE = 
"DOLPHINSCHEDULER-REMOTE-SHELL-TASK-STATUS-";
+    static final int TRACK_INTERVAL = 5000;
+
+    protected StringBuilder varPool = new StringBuilder();
+
+    SshClient sshClient;
+    ClientSession session;
+    SSHConnectionParam sshConnectionParam;
+
+    public RemoteExecutor(SSHConnectionParam sshConnectionParam) {
+
+        this.sshConnectionParam = sshConnectionParam;
+        initClient();
+    }
+
+    private void initClient() {
+        sshClient = SshClient.setUpDefaultClient();
+        sshClient.start();
+    }
+
+    private ClientSession getSession() {
+        if (session != null && session.isOpen()) {
+            return session;
+        }
+        try {
+            session = SSHUtils.getSession(sshClient, sshConnectionParam);
+            if (session == null || !session.auth().verify().isSuccess()) {
+                throw new TaskException("SSH connection failed");
+            }
+        } catch (Exception e) {
+            throw new TaskException("SSH connection failed", e);
+        }
+        return session;
+    }
+
+    public int run(String taskId, String localFile) throws IOException {
+        try {
+            // only run task if no exist same task
+            String pid = getTaskPid(taskId);
+            if (StringUtils.isEmpty(pid)) {
+                saveCommand(taskId, localFile);
+                String runCommand = String.format(COMMAND.RUN_COMMAND, 
getRemoteShellHome(), taskId,
+                        getRemoteShellHome(), taskId);
+                runRemote(runCommand);
+            }
+            track(taskId);
+            return getTaskExitCode(taskId);
+        } catch (Exception e) {
+            throw new TaskException("Remote shell task error", e);
+        }
+    }
+
+    public void track(String taskId) throws Exception {
+        int logN = 0;
+        String pid;
+        logger.info("Remote shell task log:");
+        do {
+            pid = getTaskPid(taskId);
+            String trackCommand = String.format(COMMAND.TRACK_COMMAND, logN + 
1, getRemoteShellHome(), taskId);
+            String log = runRemote(trackCommand);
+            if (StringUtils.isEmpty(log)) {
+                Thread.sleep(TRACK_INTERVAL);
+            } else {
+                logN += log.split("\n").length;
+                setVarPool(log);
+                logger.info(log);
+            }
+        } while (StringUtils.isNotEmpty(pid));
+    }
+
+    public String getVarPool() {
+        return varPool.toString();
+    }
+
+    private void setVarPool(String log) {
+        String[] lines = log.split("\n");
+        for (String line : lines) {
+            if (line.startsWith("${setValue(") || 
line.startsWith("#{setValue(")) {
+                varPool.append(findVarPool(line));
+                varPool.append("$VarPool$");
+            }
+        }
+    }
+
+    private String findVarPool(String line) {
+        Matcher matcher = SETVALUE_REGEX.matcher(line);
+        if (matcher.find()) {
+            return matcher.group(1);
+        }
+        return null;
+    }
+
+    public Integer getTaskExitCode(String taskId) throws IOException {
+        String trackCommand = String.format(COMMAND.LOG_TAIL_COMMAND, 
getRemoteShellHome(), taskId);
+        String log = runRemote(trackCommand);
+        int exitCode = -1;
+        logger.info("Remote shell task run status: {}", log);
+        if (log.contains(STATUS_TAG_MESSAGE)) {
+            String status = log.replace(STATUS_TAG_MESSAGE, "").trim();
+            if (status.equals("0")) {
+                logger.info("Remote shell task success");
+                exitCode = 0;
+            } else {
+                logger.error("Remote shell task failed");
+                exitCode = Integer.parseInt(status);

Review Comment:
   ## Missing catch of NumberFormatException
   
   Potential uncaught 'java.lang.NumberFormatException'.
   
   [Show more 
details](https://github.com/apache/dolphinscheduler/security/code-scanning/2740)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to