github-code-scanning[bot] commented on code in PR #13801:
URL:
https://github.com/apache/dolphinscheduler/pull/13801#discussion_r1150205443
##########
dolphinscheduler-spi/src/main/java/org/apache/dolphinscheduler/spi/datasource/ConnectionParam.java:
##########
@@ -23,4 +23,12 @@
* The model of Datasource Connection param
*/
public interface ConnectionParam extends Serializable {
+
+ default String getPassword() {
+ return "";
+ }
+
+ default void setPassword(String s) {
Review Comment:
## Useless parameter
The parameter 's' is never used.
[Show more
details](https://github.com/apache/dolphinscheduler/security/code-scanning/2739)
##########
dolphinscheduler-datasource-plugin/dolphinscheduler-datasource-api/src/main/java/org/apache/dolphinscheduler/plugin/datasource/api/datasource/DataSourceProcessor.java:
##########
@@ -95,6 +95,16 @@
*/
Connection getConnection(ConnectionParam connectionParam) throws
ClassNotFoundException, SQLException, IOException;
+ /**
+ * test connection, use for not jdbc datasource
+ *
+ * @param connectionParam connectionParam
+ * @return true if connection is valid
+ */
+ default boolean testConnection(ConnectionParam connectionParam) {
Review Comment:
## Useless parameter
The parameter 'connectionParam' is never used.
[Show more
details](https://github.com/apache/dolphinscheduler/security/code-scanning/2738)
##########
dolphinscheduler-task-plugin/dolphinscheduler-task-remoteshell/src/main/java/org/apache/dolphinscheduler/plugin/task/remoteshell/RemoteExecutor.java:
##########
@@ -0,0 +1,245 @@
+/*
+ * 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);
+
+ final static String REMOTE_SHELL_HOME =
"/tmp/dolphinscheduler-remote-shell-%s/";
+ final static String STATUS_TAG_MESSAGE =
"DOLPHINSCHEDULER-REMOTE-SHELL-TASK-STATUS-";
+
+ final static 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 task_id, String localFile) throws IOException {
+ try {
+ // only run task if no exist same task
+ String pid = getTaskPid(task_id);
+ if (StringUtils.isEmpty(pid)) {
+ saveCommand(task_id, localFile);
+ String runCommand = String.format(COMMAND.RUN_COMMAND,
getRemoteShellHome(), task_id,
+ getRemoteShellHome(), task_id);
+ runRemote(runCommand);
+ }
+ track(task_id);
+ return getTaskExitCode(task_id);
+ } catch (Exception e) {
+ throw new TaskException("Remote shell task error", e);
+ }
+ }
+
+ public void track(String task_id) throws Exception {
+ int logN = 0;
+ String pid;
+ logger.info("Remote shell task log:");
+ do {
+ pid = getTaskPid(task_id);
+ String trackCommand = String.format(COMMAND.TRACK_COMMAND, logN +
1, getRemoteShellHome(), task_id);
+ 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 task_id) throws IOException {
+ String trackCommand = String.format(COMMAND.LOG_TAIL_COMMAND,
getRemoteShellHome(), task_id);
+ 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/2737)
##########
dolphinscheduler-task-plugin/dolphinscheduler-task-remoteshell/src/main/java/org/apache/dolphinscheduler/plugin/task/remoteshell/RemoteExecutor.java:
##########
@@ -0,0 +1,245 @@
+/*
+ * 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()));
Review Comment:
## Unused format argument
This format call refers to 0 argument(s) but supplies 1 argument(s).
[Show more
details](https://github.com/apache/dolphinscheduler/security/code-scanning/2736)
--
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]