jmsperu commented on code in PR #12898:
URL: https://github.com/apache/cloudstack/pull/12898#discussion_r4196844553


##########
plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtRestoreBackupCommandWrapper.java:
##########
@@ -257,31 +266,66 @@ private String getBackupPath(String mountDirectory, 
String backupPath, String ba
         return bkpPath;
     }
 
-    private boolean checkBackupFileImage(String backupPath) {
-        int exitValue = 
Script.runSimpleBashScriptForExitValue(String.format("qemu-img check %s", 
backupPath));
-        return exitValue == 0;
+    private boolean checkBackupFileImage(String backupPath, File keyFile) {
+        if (!isEncryptedImage(backupPath)) {
+            int exitValue = 
Script.runSimpleBashScriptForExitValue(String.format("qemu-img check %s", 
backupPath));
+            return exitValue == 0;
+        }
+        List<String> cmd = new ArrayList<>(List.of("qemu-img", "check"));
+        cmd.addAll(encryptedSourceArgs(backupPath, keyFile));
+        return Script.executeCommandForExitValue(cmd.toArray(new String[0])) 
== 0;
+    }
+
+    /**
+     * True when qemu reports the backup qcow2 as encrypted (LUKS, produced by 
nasbackup.sh {@code -e}).
+     * Reading the header needs no secret, so this works before any passphrase 
is involved.
+     */
+    private boolean isEncryptedImage(String backupPath) {
+        String info = Script.executeCommand("qemu-img", "info", 
"--output=json", backupPath);
+        return info != null && info.replaceAll("\\s", 
"").contains("\"encrypted\":true");
+    }

Review Comment:
   Agreed, that was fail-open. Fixed in d32f5707ab: `isEncryptedImage` now 
throws when `qemu-img info` returns nothing usable (missing binary, unreadable 
file, timeout), and the restore fails with "... not known whether it is 
encrypted; refusing to restore it" before any copy. The "not encrypted" result 
is only returned when qemu-img actually read the header and it has no 
`"encrypted": true`. The new test 
`testRestoreFailsClosedWhenEncryptionStateCannotBeDetermined` checks that 
neither rsync nor qemu-img convert runs in that case. Two existing tests had 
been passing only because their mocked `qemu-img info` returned null; they now 
return a real plain-qcow2 result.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to