Skip to content

<fix>[conf]: Modify the name of the properties file#3444

Open
MatheMatrix wants to merge 1 commit intofeature-5.4.6-nexavmfrom
sync/haoyu.ding/fix-80219@@3
Open

<fix>[conf]: Modify the name of the properties file#3444
MatheMatrix wants to merge 1 commit intofeature-5.4.6-nexavmfrom
sync/haoyu.ding/fix-80219@@3

Conversation

@MatheMatrix
Copy link
Owner

Resolves: ZSTAC-80219

Change-Id: I7a62637177726f6e61746e7763667278627a7a76

sync from gitlab !9301

Resolves: ZSTAC-80219

Change-Id: I7a62637177726f6e61746e7763667278627a7a76
@coderabbitai
Copy link

coderabbitai bot commented Mar 10, 2026

功能演示

该拉取请求引入了动态属性文件名解析功能,将硬编码的"zstack.properties"替换为通过AppConfig从app_config.xml读取的动态配置,包含默认值回退机制。涉及shell脚本和Java配置加载的多处更新。

变更总览

变更内容分组 / 文件 变更说明
安装脚本动态属性处理
conf/install/install.sh
添加get_properties_file_name()和set_zstack_property_path()函数以支持动态属性文件名解析;用引用的变量替换硬编码的zstack.properties;改进变量引用的安全性;初始化old_zstack_property为空字符串。
Java核心配置框架
core/src/main/java/org/zstack/core/config/AppConfig.java, core/src/main/java/org/zstack/core/Platform.java
新增AppConfig类实现从app_config.xml读取属性文件名,包含线程安全的双重检查锁定缓存机制和默认值回退;Platform.java调用AppConfig获取动态属性文件名,替换硬编码的文件查找逻辑。
测试基础设施适配
test/src/test/java/org/zstack/test/DBUtil.java, testlib/src/main/java/org/zstack/testlib/Test.groovy
DBUtil和Test.groovy改为使用AppConfig.getPropertiesFileName()替代硬编码的"zstack.properties";更新错误消息以反映动态属性文件名。

预估代码审查工作量

🎯 3 (中等复杂度) | ⏱️ ~20 分钟

庆祝诗

🐰✨ 属性文件不再固定,
app_config掌握新方向,
动态加载显灵活,
兔子欢呼迎变化,
配置自由多无限!

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed 标题遵循了所需的格式 [scope]: ,字符数为51个(在72个字符限制内),清楚地反映了提交中修改属性文件名称的主要内容。
Description check ✅ Passed 描述包含了JIRA键、变更ID和来源信息,与提交内容相关,指明了这是关于修改属性文件名称的修改。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch sync/haoyu.ding/fix-80219@@3

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

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (2)
conf/install/install.sh (1)

461-461: 静态分析提示:echo -e 在 POSIX sh 中未定义。

静态分析工具 (Shellcheck SC3037) 指出 echo -e 标志在 POSIX sh 中未定义。不过这是脚本中已有的模式(其他地方也使用了 echo -e),本次更改只是修改了消息内容。如需修复可考虑使用 printf 替代,但这可以作为单独的改进处理。

♻️ 可选的 POSIX 兼容修复
-[ -f "$old_zstack_property" ] && echo -e "$(tput setaf 2)Original `basename $zstack_property` was saved in $old_zstack_property\n$(tput sgr0)"
+[ -f "$old_zstack_property" ] && printf "%sOriginal %s was saved in %s\n%s" "$(tput setaf 2)" "$(basename "$zstack_property")" "$old_zstack_property" "$(tput sgr0)"
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@conf/install/install.sh` at line 461, The line printing the saved property
uses echo -e which is not POSIX-safe; replace the echo -e invocation with a
POSIX-safe printf call to preserve the colored output and newline. Locate the
statement referencing old_zstack_property and zstack_property in install.sh and
change it to use printf with a format string (e.g., printf '%s\n' "$(tput setaf
2)Original $(basename "$zstack_property") was saved in
$old_zstack_property$(tput sgr0)") ensuring proper quoting of variables and use
of $(basename ...) to match the original behavior.
core/src/main/java/org/zstack/core/config/AppConfig.java (1)

44-46: 建议添加 XXE 攻击防护配置。

在解析 XML 文件时,DocumentBuilderFactory 应配置禁用外部实体以防止 XXE(XML External Entity)攻击。虽然 app_config.xml 是内部配置文件,但添加防护是安全最佳实践。

🛡️ 建议的修复
     private static String loadPropertiesFileNameFromConfig() {
         try {
             File appConfigFile = PathUtil.findFileOnClassPath("app_config.xml", true);
             DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+            // Prevent XXE attacks
+            factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
+            factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
+            factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
             DocumentBuilder builder = factory.newDocumentBuilder();
             Document doc = builder.parse(appConfigFile);
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@core/src/main/java/org/zstack/core/config/AppConfig.java` around lines 44 -
46, 在 AppConfig 类中,DocumentBuilderFactory factory / DocumentBuilder builder /
builder.parse(appConfigFile) 解析 XML 时未禁用外部实体,存在 XXE 风险;修复方法是在创建 factory 后但在调用
factory.newDocumentBuilder() 之前配置安全特性:启用
XMLConstants.FEATURE_SECURE_PROCESSING,设置
disallow-doctype-decl、external-general-entities 和 external-parameter-entities 为
false(或 true/禁用外部实体的相应值),将 setXIncludeAware(false) 和
setExpandEntityReferences(false),然后再调用 factory.newDocumentBuilder() 并继续使用
builder.parse(appConfigFile);确保这些设置应用于 DocumentBuilderFactory 实例以阻止外部实体和 DOCTYPE
声明。
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@core/src/main/java/org/zstack/core/Platform.java`:
- Around line 56-59: Platform.java contains unused XML imports (DocumentBuilder,
DocumentBuilderFactory, Document, NodeList); remove those four import lines so
the class only imports what it actually uses (it should interact with AppConfig
via AppConfig.getPropertiesFileName()). Locate the import block in Platform.java
and delete the lines referencing DocumentBuilder, DocumentBuilderFactory,
Document, and NodeList to eliminate the unused dependencies.

---

Nitpick comments:
In `@conf/install/install.sh`:
- Line 461: The line printing the saved property uses echo -e which is not
POSIX-safe; replace the echo -e invocation with a POSIX-safe printf call to
preserve the colored output and newline. Locate the statement referencing
old_zstack_property and zstack_property in install.sh and change it to use
printf with a format string (e.g., printf '%s\n' "$(tput setaf 2)Original
$(basename "$zstack_property") was saved in $old_zstack_property$(tput sgr0)")
ensuring proper quoting of variables and use of $(basename ...) to match the
original behavior.

In `@core/src/main/java/org/zstack/core/config/AppConfig.java`:
- Around line 44-46: 在 AppConfig 类中,DocumentBuilderFactory factory /
DocumentBuilder builder / builder.parse(appConfigFile) 解析 XML 时未禁用外部实体,存在 XXE
风险;修复方法是在创建 factory 后但在调用 factory.newDocumentBuilder() 之前配置安全特性:启用
XMLConstants.FEATURE_SECURE_PROCESSING,设置
disallow-doctype-decl、external-general-entities 和 external-parameter-entities 为
false(或 true/禁用外部实体的相应值),将 setXIncludeAware(false) 和
setExpandEntityReferences(false),然后再调用 factory.newDocumentBuilder() 并继续使用
builder.parse(appConfigFile);确保这些设置应用于 DocumentBuilderFactory 实例以阻止外部实体和 DOCTYPE
声明。

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: http://open.zstack.ai:20001/code-reviews/zstack-cloud.yaml (via .coderabbit.yaml)

Review profile: CHILL

Plan: Pro

Run ID: b3a2b13e-d610-4382-ac19-a372902516ee

📥 Commits

Reviewing files that changed from the base of the PR and between 49c4f76 and 2da9587.

⛔ Files ignored due to path filters (4)
  • conf/app_config.xml is excluded by !**/*.xml
  • conf/zstack.xml is excluded by !**/*.xml
  • conf/zstackSimulator.xml is excluded by !**/*.xml
  • test/src/test/resources/zstack-template.xml is excluded by !**/*.xml
📒 Files selected for processing (5)
  • conf/install/install.sh
  • core/src/main/java/org/zstack/core/Platform.java
  • core/src/main/java/org/zstack/core/config/AppConfig.java
  • test/src/test/java/org/zstack/test/DBUtil.java
  • testlib/src/main/java/org/zstack/testlib/Test.groovy

Comment on lines +56 to +59
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# 检查 Platform.java 中是否使用了 XML 解析类
rg -n "DocumentBuilder|DocumentBuilderFactory|Document|NodeList" core/src/main/java/org/zstack/core/Platform.java | grep -v "^.*import"

Repository: MatheMatrix/zstack

Length of output: 44


🏁 Script executed:

# 查看Platform.java的完整导入部分
head -80 core/src/main/java/org/zstack/core/Platform.java | tail -40

Repository: MatheMatrix/zstack

Length of output: 1449


🏁 Script executed:

# 搜索Platform.java中是否确实有AppConfig相关的代码
rg -n "AppConfig" core/src/main/java/org/zstack/core/Platform.java

Repository: MatheMatrix/zstack

Length of output: 184


🏁 Script executed:

# 检查AppConfig.java是否使用了这些XML类
rg -n "DocumentBuilder|DocumentBuilderFactory|Document|NodeList" core/src/main/java/org/zstack/core/config/AppConfig.java | head -20

Repository: MatheMatrix/zstack

Length of output: 511


移除 Platform.java 中未使用的 XML 导入。

DocumentBuilderDocumentBuilderFactoryDocumentNodeList 这些 XML 解析类在 Platform.java 中并未使用。XML 解析逻辑完全封装在 AppConfig 类内部。Platform.java 仅通过调用 AppConfig.getPropertiesFileName() 与之交互,无需这些导入。请移除第 56-59 行的这四个 XML 相关导入。

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@core/src/main/java/org/zstack/core/Platform.java` around lines 56 - 59,
Platform.java contains unused XML imports (DocumentBuilder,
DocumentBuilderFactory, Document, NodeList); remove those four import lines so
the class only imports what it actually uses (it should interact with AppConfig
via AppConfig.getPropertiesFileName()). Locate the import block in Platform.java
and delete the lines referencing DocumentBuilder, DocumentBuilderFactory,
Document, and NodeList to eliminate the unused dependencies.

@MatheMatrix
Copy link
Owner Author

Comment from yaohua.wu:

Review: MR !9301 — ZSTAC-80219 (zstack core)

关联 MR: premium !13116 | zstack-utility !6702

未找到关联 Jira issue 详情(API 认证失败),基于 MR 内容和代码上下文进行 review。

变更概述

将硬编码的 zstack.properties 改为通过 app_config.xml 动态确定 properties 文件名,支持 OEM 构建自定义。核心架构:app_config.xml(单一配置源)→ AppConfig.java(Java 读取+缓存)→ Platform.java(设置 System Property)→ Spring XML(${app.properties.file} 占位符)。


Critical

  1. [zstackbuild/projects/zstack-testconf.xml] Ant property 不可变性导致 OEM 测试配置失效

    <property name="testconf.properties.file" value="zstack.properties" />
    <condition property="testconf.properties.file" value="${APP_NAME}.properties" else="zstack.properties">

    Ant property 具有不可变性(immutability):一旦被 <property> 设置,后续的 <condition> 无法覆盖。因此 testconf.properties.file永远zstack.properties<condition> 永远不会生效。

    影响:OEM 构建(APP_NAME != "zstack")的测试环境将使用错误的 properties 文件名,不能真实模拟 OEM 部署环境。

    建议修复:删除第一行 <property>,仅保留 <condition>(其 else 子句已提供默认值):

    <condition property="testconf.properties.file" value="${APP_NAME}.properties" else="zstack.properties">
        <and>
            <isset property="APP_NAME" />
            <not><equals arg1="${APP_NAME}" arg2="zstack" /></not>
        </and>
    </condition>

Warning

  1. [core/src/main/java/org/zstack/core/config/AppConfig.java] classpath 查找路径与构建脚本不一致(待确认)

    AppConfig.java 使用:

    PathUtil.findFileOnClassPath("app_config.xml", true)

    查找 classpath 根路径下的 app_config.xml

    app-build.xml(zstack-utility)修改的是:

    ${war.classpath.dir}/zstack/conf/app_config.xml
    

    WEB-INF/classes/zstack/conf/app_config.xml

    同样,install.sh 查找的是 $1/WEB-INF/classes/zstack/conf/app_config.xmlzstacklib.py 同时检查两个路径(app_config.xmlzstack/conf/app_config.xml)。

    如果 PathUtil.findFileOnClassPath 不做递归扫描,而只匹配 classpath 根路径,则 Java 代码在 OEM 构建中找不到被修改的 app_config.xml,会 fallback 到默认值 zstack.properties,使整个 OEM 自定义失效。

    建议:确认 PathUtil.findFileOnClassPath 的行为。如果是精确路径匹配,应改为 PathUtil.findFileOnClassPath("zstack/conf/app_config.xml", true),或在 fallback 中尝试多个候选路径(与 zstacklib.py 保持一致)。

  2. [conf/zstack.xml, conf/zstackSimulator.xml, test/src/test/resources/zstack-template.xml] ignoreResourceNotFound=true 可能掩盖配置错误

    新增的 ignoreResourceNotFound=true 意味着如果 ${app.properties.file} 指向的文件不存在,Spring 将静默跳过而非报错。虽然 Platform.java 会提前 fail-fast(PathUtil.findFileOnClassPath with true),但如果未来启动流程变更导致 Platform 未先执行,properties 将静默丢失,导致难以排查的运行时错误。

    建议:添加启动日志明确记录加载了哪个 properties 文件,或在 Platform.java 中加一个断言确认 app.properties.file 系统属性已设置。

  3. [conf/app_config.xml] 新文件缺少末尾换行

    app_config.xml 文件末尾缺少换行符(\ No newline at end of file)。虽然不影响功能,但某些工具(如 sed、diff)可能表现异常。


Suggestion

  1. [core/src/main/java/org/zstack/core/config/AppConfig.java:51-52] 启动日志使用 System.out.println

    作为 bootstrap 阶段的代码,使用 System.out 可以理解(日志框架尚未初始化)。但 System.err.println + e.printStackTrace() 在异常路径中会产生不够结构化的输出。建议统一使用 System.out,并在消息中加上更多上下文(如尝试的 classpath 路径)。

  2. [core/src/main/java/org/zstack/core/Platform.java] 未使用的 import

    新增了 javax.xml.parsers.DocumentBuilderDocumentBuilderFactoryorg.w3c.dom.Documentorg.w3c.dom.NodeList 等 import,但 Platform.java 本身不直接解析 XML(委托给了 AppConfig)。这些 import 应当移除。

  3. [conf/install/install.sh:25] get_properties_file_namesed 解析 XML

    Shell 函数用 sed 提取 XML 标签内容,对于受控的简单 XML 可以接受,但如果标签跨行或包含注释将失败。建议在函数注释中说明对 XML 格式的假设。

Verdict: REVISION_REQUIRED

zstack-testconf.xml 的 Ant property 不可变性 bug 会导致 OEM 构建的测试配置失效,必须修复。AppConfig.java 的 classpath 查找路径需确认与构建脚本一致。


🤖 Robot Reviewer

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