forked from zstackio/zstack
-
Notifications
You must be signed in to change notification settings - Fork 0
<fix>[core]: add SshFileChecker #4649
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zstack-robot-2
wants to merge
1
commit into
4.8.0
Choose a base branch
from
sync/tao.gan/5.0.0-ZSTAC-46801@@2
base: 4.8.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
99 changes: 99 additions & 0 deletions
99
core/src/main/java/org/zstack/core/ansible/SshFileChecker.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| package org.zstack.core.ansible; | ||
|
|
||
| import org.apache.logging.log4j.util.Strings; | ||
| import org.zstack.utils.Utils; | ||
| import org.zstack.utils.logging.CLogger; | ||
| import org.zstack.utils.ssh.Ssh; | ||
| import org.zstack.utils.ssh.SshResult; | ||
|
|
||
| import java.io.File; | ||
|
|
||
| public class SshFileChecker implements AnsibleChecker { | ||
| private static final CLogger logger = Utils.getLogger(SshFileChecker.class); | ||
|
|
||
| private String filePath; | ||
| private String username; | ||
| private String password; | ||
| private String privateKey; | ||
| private String targetIp; | ||
| private int sshPort = 22; | ||
|
|
||
| @Override | ||
| public boolean needDeploy() { | ||
| if (Strings.isEmpty(filePath)) { | ||
| return false; | ||
| } | ||
|
|
||
| Ssh ssh = new Ssh(); | ||
| ssh.setUsername(username).setPrivateKey(privateKey).setPassword(password).setPort(sshPort).setHostname(targetIp); | ||
|
|
||
| try { | ||
| String dirPath = new File(filePath).getParent(); | ||
|
|
||
| ssh.command(String.format("if [ -d %s ] && [ -f %s ]; then exit 0; else exit 1; fi", dirPath, filePath)); | ||
| SshResult ret = ssh.run(); | ||
| if (ret.getReturnCode() != 0) { | ||
| return true; | ||
| } | ||
|
|
||
| ssh.reset(); | ||
| } finally { | ||
| ssh.close(); | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public void deleteDestFile() { | ||
| // do nothing. | ||
| } | ||
|
Comment on lines
+48
to
+50
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win 失败后删除远端残留文件。
🤖 Prompt for AI Agents |
||
|
|
||
| public String getUsername() { | ||
| return username; | ||
| } | ||
|
|
||
| public void setUsername(String username) { | ||
| this.username = username; | ||
| } | ||
|
|
||
| public String getPassword() { | ||
| return password; | ||
| } | ||
|
|
||
| public void setPassword(String password) { | ||
| this.password = password; | ||
| } | ||
|
|
||
| public String getPrivateKey() { | ||
| return privateKey; | ||
| } | ||
|
|
||
| public void setPrivateKey(String privateKey) { | ||
| this.privateKey = privateKey; | ||
| } | ||
|
|
||
| public int getSshPort() { | ||
| return sshPort; | ||
| } | ||
|
|
||
| public void setSshPort(int sshPort) { | ||
| this.sshPort = sshPort; | ||
| } | ||
|
|
||
| public String getTargetIp() { | ||
| return targetIp; | ||
| } | ||
|
|
||
| public void setTargetIp(String targetIp) { | ||
| this.targetIp = targetIp; | ||
| } | ||
|
|
||
| public String getFilePath() { | ||
| return filePath; | ||
| } | ||
|
|
||
| public void setFilePath(String filePath) { | ||
| this.filePath = filePath; | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔒 Security & Privacy | 🔴 Critical | ⚡ Quick win
对远端路径进行 POSIX shell 转义,避免远程命令注入。
Line 33 直接将可通过
setFilePath()设置的值拼入 shell;包含分号、命令替换或空格的路径可执行额外远端命令,普通含空格路径也会误判。请对每个参数做单引号转义后再构造命令。建议修改
📝 Committable suggestion
🤖 Prompt for AI Agents