Skip to content

Commit 42f1e19

Browse files
authored
Mask vncPasswd being logged in agent.log (#12404)
1 parent a4b1a27 commit 42f1e19

File tree

3 files changed

+42
-13
lines changed

3 files changed

+42
-13
lines changed

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtMigrateCommandWrapper.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ Use VIR_DOMAIN_XML_SECURE (value = 1) prior to v1.0.0.
158158
final String target = command.getDestinationIp();
159159
xmlDesc = dm.getXMLDesc(xmlFlag);
160160
if (logger.isDebugEnabled()) {
161-
logger.debug(String.format("VM [%s] with XML configuration [%s] will be migrated to host [%s].", vmName, xmlDesc, target));
161+
logger.debug("VM {} with XML configuration {} will be migrated to host {}.", vmName, maskSensitiveInfoInXML(xmlDesc), target);
162162
}
163163

164164
// Limit the VNC password in case the length is greater than 8 characters
@@ -173,7 +173,7 @@ Use VIR_DOMAIN_XML_SECURE (value = 1) prior to v1.0.0.
173173
logger.debug(String.format("Editing mount path of ISO from %s to %s", oldIsoVolumePath, newIsoVolumePath));
174174
xmlDesc = replaceDiskSourceFile(xmlDesc, newIsoVolumePath, vmName);
175175
if (logger.isDebugEnabled()) {
176-
logger.debug(String.format("Replaced disk mount point [%s] with [%s] in Instance [%s] XML configuration. New XML configuration is [%s].", oldIsoVolumePath, newIsoVolumePath, vmName, xmlDesc));
176+
logger.debug("Replaced disk mount point {} with {} in Instance {} XML configuration. New XML configuration is {}.", oldIsoVolumePath, newIsoVolumePath, vmName, maskSensitiveInfoInXML(xmlDesc));
177177
}
178178
}
179179

@@ -204,23 +204,23 @@ Use VIR_DOMAIN_XML_SECURE (value = 1) prior to v1.0.0.
204204

205205
if (migrateStorage) {
206206
if (logger.isDebugEnabled()) {
207-
logger.debug(String.format("Changing VM [%s] volumes during migration to host: [%s].", vmName, target));
207+
logger.debug("Changing VM {} volumes during migration to host: {}.", vmName, target);
208208
}
209209
xmlDesc = replaceStorage(xmlDesc, mapMigrateStorage, migrateStorageManaged);
210210
if (logger.isDebugEnabled()) {
211-
logger.debug(String.format("Changed VM [%s] XML configuration of used storage. New XML configuration is [%s].", vmName, xmlDesc));
211+
logger.debug("Changed VM {} XML configuration of used storage. New XML configuration is {}.", vmName, maskSensitiveInfoInXML(xmlDesc));
212212
}
213213
migrateDiskLabels = getMigrateStorageDeviceLabels(disks, mapMigrateStorage);
214214
}
215215

216216
Map<String, DpdkTO> dpdkPortsMapping = command.getDpdkInterfaceMapping();
217217
if (MapUtils.isNotEmpty(dpdkPortsMapping)) {
218218
if (logger.isTraceEnabled()) {
219-
logger.trace(String.format("Changing VM [%s] DPDK interfaces during migration to host: [%s].", vmName, target));
219+
logger.trace("Changing VM {} DPDK interfaces during migration to host: {}.", vmName, target);
220220
}
221221
xmlDesc = replaceDpdkInterfaces(xmlDesc, dpdkPortsMapping);
222222
if (logger.isDebugEnabled()) {
223-
logger.debug(String.format("Changed VM [%s] XML configuration of DPDK interfaces. New XML configuration is [%s].", vmName, xmlDesc));
223+
logger.debug("Changed VM {} XML configuration of DPDK interfaces. New XML configuration is {}.", vmName, maskSensitiveInfoInXML(xmlDesc));
224224
}
225225
}
226226

@@ -233,7 +233,7 @@ Use VIR_DOMAIN_XML_SECURE (value = 1) prior to v1.0.0.
233233
}
234234

235235
//run migration in thread so we can monitor it
236-
logger.info(String.format("Starting live migration of instance [%s] to destination host [%s] having the final XML configuration: [%s].", vmName, dconn.getURI(), xmlDesc));
236+
logger.info("Starting live migration of instance {} to destination host {} having the final XML configuration: {}.", vmName, dconn.getURI(), maskSensitiveInfoInXML(xmlDesc));
237237
final ExecutorService executor = Executors.newFixedThreadPool(1);
238238
boolean migrateNonSharedInc = command.isMigrateNonSharedInc() && !migrateStorageManaged;
239239

@@ -575,9 +575,7 @@ String replaceIpForVNCInDescFileAndNormalizePassword(String xmlDesc, final Strin
575575
graphElem = graphElem.replaceAll("passwd='([^\\s]+)'", "passwd='" + vncPassword + "'");
576576
}
577577
xmlDesc = xmlDesc.replaceAll(GRAPHICS_ELEM_START + CONTENTS_WILDCARD + GRAPHICS_ELEM_END, graphElem);
578-
if (logger.isDebugEnabled()) {
579-
logger.debug(String.format("Replaced the VNC IP address [%s] with [%s] in VM [%s].", originalGraphElem, graphElem, vmName));
580-
}
578+
logger.debug("Replaced the VNC IP address {} with {} in VM {}.", maskSensitiveInfoInXML(originalGraphElem), maskSensitiveInfoInXML(graphElem), vmName);
581579
}
582580
}
583581
return xmlDesc;
@@ -910,4 +908,10 @@ private boolean findSourceNode(Document doc, Node diskNode, String vmName, Strin
910908
}
911909
return false;
912910
}
911+
912+
public static String maskSensitiveInfoInXML(String xmlDesc) {
913+
if (xmlDesc == null) return null;
914+
return xmlDesc.replaceAll("(graphics\\s+[^>]*type=['\"]vnc['\"][^>]*passwd=['\"])([^'\"]*)(['\"])",
915+
"$1*****$3");
916+
}
913917
}

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtStartCommandWrapper.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,9 @@ public Answer execute(final StartCommand command, final LibvirtComputingResource
8080
}
8181

8282
libvirtComputingResource.createVifs(vmSpec, vm);
83-
84-
logger.debug("starting " + vmName + ": " + vm.toString());
83+
if (logger.isDebugEnabled()) {
84+
logger.debug("Starting {} : {}", vmName, LibvirtMigrateCommandWrapper.maskSensitiveInfoInXML(vm.toString()));
85+
}
8586
String vmInitialSpecification = vm.toString();
8687
String vmFinalSpecification = performXmlTransformHook(vmInitialSpecification, libvirtComputingResource);
8788
libvirtComputingResource.startVM(conn, vmName, vmFinalSpecification);

plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtMigrateCommandWrapperTest.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,7 @@ private Map<String, MigrateDiskInfo> createMapMigrateStorage(String sourceText,
589589
@Test
590590
public void testReplaceIpForVNCInDescFile() {
591591
final String targetIp = "192.168.22.21";
592-
final String result = libvirtMigrateCmdWrapper.replaceIpForVNCInDescFileAndNormalizePassword(fullfile, targetIp, null, "");
592+
final String result = libvirtMigrateCmdWrapper.replaceIpForVNCInDescFileAndNormalizePassword(fullfile, targetIp, "vncSecretPwd", "");
593593
assertEquals("transformation does not live up to expectation:\n" + result, targetfile, result);
594594
}
595595

@@ -1019,4 +1019,28 @@ public void replaceCdromIsoPathTest() throws ParserConfigurationException, IOExc
10191019

10201020
Assert.assertTrue(finalXml.contains(newIsoVolumePath));
10211021
}
1022+
1023+
@Test
1024+
public void testMaskVncPwdDomain() {
1025+
// Test case 1: Single quotes
1026+
String xml1 = "<graphics type='vnc' port='5900' passwd='secret123'/>";
1027+
String expected1 = "<graphics type='vnc' port='5900' passwd='*****'/>";
1028+
assertEquals(expected1, LibvirtMigrateCommandWrapper.maskSensitiveInfoInXML(xml1));
1029+
1030+
// Test case 2: Double quotes
1031+
String xml2 = "<graphics type=\"vnc\" port=\"5901\" passwd=\"mypassword\"/>";
1032+
String expected2 = "<graphics type=\"vnc\" port=\"5901\" passwd=\"*****\"/>";
1033+
assertEquals(expected2, LibvirtMigrateCommandWrapper.maskSensitiveInfoInXML(xml2));
1034+
1035+
// Test case 3: Non-VNC graphics (should remain unchanged)
1036+
String xml3 = "<graphics type='spice' port='5902' passwd='notvnc'/>";
1037+
assertEquals(xml3, LibvirtMigrateCommandWrapper.maskSensitiveInfoInXML(xml3));
1038+
1039+
// Test case 4: Multiple VNC entries in one string
1040+
String xml4 = "<graphics type='vnc' port='5900' passwd='a'/>\n" +
1041+
"<graphics type='vnc' port='5901' passwd='b'/>";
1042+
String expected4 = "<graphics type='vnc' port='5900' passwd='*****'/>\n" +
1043+
"<graphics type='vnc' port='5901' passwd='*****'/>";
1044+
assertEquals(expected4, LibvirtMigrateCommandWrapper.maskSensitiveInfoInXML(xml4));
1045+
}
10221046
}

0 commit comments

Comments
 (0)