Skip to content

<fix>[core]: add SshFileChecker - #4649

Open
zstack-robot-2 wants to merge 1 commit into
4.8.0from
sync/tao.gan/5.0.0-ZSTAC-46801@@2
Open

<fix>[core]: add SshFileChecker#4649
zstack-robot-2 wants to merge 1 commit into
4.8.0from
sync/tao.gan/5.0.0-ZSTAC-46801@@2

Conversation

@zstack-robot-2

Copy link
Copy Markdown
Collaborator

Resolves: ZSTAC-46801

Change-Id: I756c7a75796f6676766678696d786f616f617676

sync from gitlab !5310

Resolves: ZSTAC-46801

Change-Id: I756c7a75796f6676766678696d786f616f617676
@coderabbitai

coderabbitai Bot commented Jul 26, 2026

Copy link
Copy Markdown

Review Change Stack

Walkthrough

新增 SshFileChecker,通过 SSH 检查远端文件及其目录是否存在,并根据检查结果判断是否需要部署;同时提供连接参数、目标路径的访问器和空的删除接口。

Changes

SSH 文件检查

Layer / File(s) Summary
远端文件存在性检查
core/src/main/java/org/zstack/core/ansible/SshFileChecker.java
新增 AnsibleChecker 实现,支持 SSH 连接配置和目标文件路径配置;needDeploy() 检查远端目录及文件存在性并确保连接关闭,deleteDestFile() 为空实现。

Estimated code review effort: 2 (Simple) | ~10 minutes

Poem

小兔挥挥耳朵连上 SSH,
远端文件一查便知。
目录存在,文件也在,
部署脚步暂且放慢。
若是寻不到踪迹,
小兔立刻开始搬家。

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed 标题简洁且明确,准确概括了新增 SshFileChecker 这一主要变更。
Description check ✅ Passed 描述虽简短,但与本次变更和关联工单一致,没有明显跑题。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch sync/tao.gan/5.0.0-ZSTAC-46801@@2

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@core/src/main/java/org/zstack/core/ansible/SshFileChecker.java`:
- Around line 48-50: Implement deleteDestFile() in SshFileChecker to remove the
remote filePath after a failed deployment, reusing the existing SSH
configuration and safely escaping the path without deleting its parent
directory. Record any cleanup failure while preserving the method’s current
contract.
- Line 33: Update the command construction in SshFileChecker to
POSIX-shell-quote both dirPath and filePath before interpolation, using the
standard single-quote escaping that safely handles embedded quotes, spaces,
substitutions, and shell metacharacters. Add or reuse a helper such as
quoteForPosixShell and preserve the existing directory/file checks.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 00d57050-21b7-48d7-9f2d-9968c272e0d0

📥 Commits

Reviewing files that changed from the base of the PR and between 12bb2b7 and fe01e53.

📒 Files selected for processing (1)
  • core/src/main/java/org/zstack/core/ansible/SshFileChecker.java

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));

Copy link
Copy Markdown

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;包含分号、命令替换或空格的路径可执行额外远端命令,普通含空格路径也会误判。请对每个参数做单引号转义后再构造命令。

建议修改
-            ssh.command(String.format("if [ -d %s ] && [ -f %s ]; then exit 0; else exit 1; fi", dirPath, filePath));
+            ssh.command(String.format("if [ -d %s ] && [ -f %s ]; then exit 0; else exit 1; fi",
+                    quoteForPosixShell(dirPath), quoteForPosixShell(filePath)));
private static String quoteForPosixShell(String value) {
    return "'" + value.replace("'", "'\"'\"'") + "'";
}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
ssh.command(String.format("if [ -d %s ] && [ -f %s ]; then exit 0; else exit 1; fi", dirPath, filePath));
ssh.command(String.format("if [ -d %s ] && [ -f %s ]; then exit 0; else exit 1; fi",
quoteForPosixShell(dirPath), quoteForPosixShell(filePath)));
private static String quoteForPosixShell(String value) {
return "'" + value.replace("'", "'\"'\"'") + "'";
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@core/src/main/java/org/zstack/core/ansible/SshFileChecker.java` at line 33,
Update the command construction in SshFileChecker to POSIX-shell-quote both
dirPath and filePath before interpolation, using the standard single-quote
escaping that safely handles embedded quotes, spaces, substitutions, and shell
metacharacters. Add or reuse a helper such as quoteForPosixShell and preserve
the existing directory/file checks.

Comment on lines +48 to +50
public void deleteDestFile() {
// do nothing.
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

失败后删除远端残留文件。

AnsibleRunner.cleanup() 会在 playbook 失败后调用此方法,而 needDeploy() 只要发现该文件存在就返回 false。当前空实现会保留可能不完整的 filePath,使下一次调用错误跳过部署。请使用相同 SSH 配置以安全转义的路径执行远端文件删除(不删除父目录),并记录清理失败。

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@core/src/main/java/org/zstack/core/ansible/SshFileChecker.java` around lines
48 - 50, Implement deleteDestFile() in SshFileChecker to remove the remote
filePath after a failed deployment, reusing the existing SSH configuration and
safely escaping the path without deleting its parent directory. Record any
cleanup failure while preserving the method’s current contract.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants