Skip to content

Commit 3e0894d

Browse files
Don't allow assign VM to backup offering for VM with encrypted volumes
1 parent 21b2025 commit 3e0894d

2 files changed

Lines changed: 36 additions & 0 deletions

File tree

server/src/main/java/org/apache/cloudstack/backup/BackupManagerImpl.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,13 @@ public boolean assignVMToBackupOffering(Long vmId, Long offeringId) {
441441
throw new CloudRuntimeException("VM is not in running or stopped state");
442442
}
443443

444+
List<VolumeVO> volumes = volumeDao.findByInstance(vmId);
445+
for (VolumeVO volume : volumes) {
446+
if (volume != null && volume.getPassphraseId() != null) {
447+
throw new CloudRuntimeException("VM has encrypted volumes, backup offering assignment is not allowed");
448+
}
449+
}
450+
444451
validateBackupForZone(vm.getDataCenterId());
445452

446453
accountManager.checkAccess(CallContext.current().getCallingAccount(), null, true, vm);

server/src/test/java/org/apache/cloudstack/backup/BackupManagerTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,6 +1217,14 @@ public void testAssignVMToBackupOffering() {
12171217
when(vm.getDataCenterId()).thenReturn(1L);
12181218
when(vm.getBackupOfferingId()).thenReturn(null);
12191219
when(offering.getProvider()).thenReturn("testbackupprovider");
1220+
VolumeVO volume = mock(VolumeVO.class);
1221+
when(volumeDao.findByInstance(vmId)).thenReturn(List.of(volume));
1222+
when(volume.getPassphraseId()).thenReturn(null);
1223+
Long diskOfferingId = 5L;
1224+
when(volume.getDiskOfferingId()).thenReturn(diskOfferingId);
1225+
DiskOfferingVO diskOffering = Mockito.mock(DiskOfferingVO.class);
1226+
Mockito.when(diskOffering.getUuid()).thenReturn("disk-offering-uuid");
1227+
Mockito.when(diskOfferingDao.findById(diskOfferingId)).thenReturn(diskOffering);
12201228
when(backupProvider.assignVMToBackupOffering(vm, offering)).thenReturn(true);
12211229
when(vmInstanceDao.update(1L, vm)).thenReturn(true);
12221230

@@ -1225,11 +1233,32 @@ public void testAssignVMToBackupOffering() {
12251233

12261234
assertTrue(result);
12271235
verify(vmInstanceDao, times(1)).findById(vmId);
1236+
verify(volumeDao, times(2)).findByInstance(vmId);
12281237
verify(backupOfferingDao, times(1)).findById(offeringId);
12291238
verify(backupManager, times(1)).getBackupProvider("testbackupprovider");
12301239
}
12311240
}
12321241

1242+
@Test (expected = CloudRuntimeException.class)
1243+
public void testAssignVMToBackupOfferingForVMWithEncryptedVolumes() {
1244+
Long vmId = 1L;
1245+
Long offeringId = 2L;
1246+
1247+
VMInstanceVO vm = mock(VMInstanceVO.class);
1248+
overrideBackupFrameworkConfigValue();
1249+
1250+
when(vmInstanceDao.findById(vmId)).thenReturn(vm);
1251+
when(vm.getState()).thenReturn(VirtualMachine.State.Running);
1252+
VolumeVO volume = mock(VolumeVO.class);
1253+
when(volumeDao.findByInstance(vmId)).thenReturn(List.of(volume));
1254+
when(volume.getPassphraseId()).thenReturn(42L);
1255+
1256+
try (MockedStatic<UsageEventUtils> ignored2 = Mockito.mockStatic(UsageEventUtils.class)) {
1257+
backupManager.assignVMToBackupOffering(vmId, offeringId);
1258+
verify(vmInstanceDao, times(1)).findById(vmId);
1259+
}
1260+
}
1261+
12331262
@Test
12341263
public void testRemoveVMFromBackupOffering() {
12351264
Long vmId = 1L;

0 commit comments

Comments
 (0)