Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,13 @@ public String desensitizeTag(SystemTag systemTag, String tag) {
}

Yaml yaml = new Yaml();
Object obj = yaml.load(userdata);
Object obj;
try {
obj = yaml.load(userdata);
} catch (RuntimeException e) {
tokens.put(t, "*****");
continue;
}
if (!(obj instanceof LinkedHashMap)) {
return tag;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.zstack.test.userdata;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.zstack.compute.vm.VmSystemTags;

import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

public class TestUserdataTagOutputHandler {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

测试类命名未满足约定后缀。

TestUserdataTagOutputHandler 未以 TestCase 结尾,建议重命名为 UserdataTagOutputHandlerTest(并同步文件名),以保持测试命名一致性。
As per coding guidelines, “测试类需要以 TestCase 结尾”。

🤖 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
`@test/src/test/java/org/zstack/test/userdata/TestUserdataTagOutputHandler.java`
at line 12, The test class `TestUserdataTagOutputHandler` does not follow the
naming convention which requires test classes to end with `Test` or `Case`
suffix. Rename the class from `TestUserdataTagOutputHandler` to
`UserdataTagOutputHandlerTest` and also rename the file accordingly from
`TestUserdataTagOutputHandler.java` to `UserdataTagOutputHandlerTest.java` to
maintain consistency with the coding guidelines and standard test naming
conventions.

Source: Coding guidelines

private final VmSystemTags.UserdataTagOutputHandler handler = new VmSystemTags.UserdataTagOutputHandler();

@Before
public void setUp() throws NoSuchFieldException {
VmSystemTags.USERDATA.annotation = VmSystemTags.class.getField("USERDATA").getAnnotation(org.zstack.tag.SensitiveTag.class);
}

@Test
public void desensitizeMalformedCloudConfigMasksUserdata() {
String userdata = "#cloud-config\nchpasswd:\n list: |\nroot:word\nexpire: False\n";
String maskedTag = handler.desensitizeTag(VmSystemTags.USERDATA, userdataTag(userdata));

Assert.assertEquals("*****", VmSystemTags.USERDATA.getTokenByTag(maskedTag, VmSystemTags.USERDATA_TOKEN));
}

@Test
public void desensitizeValidCloudConfigKeepsStructuredMask() {
String userdata = "#cloud-config\nchpasswd:\n list: |\n root:word\n expire: False\n";
String maskedTag = handler.desensitizeTag(VmSystemTags.USERDATA, userdataTag(userdata));
String maskedUserdata = new String(Base64.getDecoder().decode(
VmSystemTags.USERDATA.getTokenByTag(maskedTag, VmSystemTags.USERDATA_TOKEN).getBytes()));

Assert.assertTrue(maskedUserdata.contains("*****:*****"));
Assert.assertFalse(maskedUserdata.contains("root:word"));
}

private String userdataTag(String userdata) {
Map<String, String> tokens = new HashMap<>();
tokens.put(VmSystemTags.USERDATA_TOKEN, new String(Base64.getEncoder().encode(userdata.getBytes())));
return VmSystemTags.USERDATA.instantiateTag(tokens);
}
}