Skip to content

Commit 93f0926

Browse files
mlsorensenMarcus Sorensen
andauthored
server: Don't allow service offering change if encryption value would change (#6776)
This PR blocks change of service offering if the offering root volume encryption values don't match. We don't support dynamically removing or adding encryption to a VM. Signed-off-by: Marcus Sorensen <mls@apple.com> Co-authored-by: Marcus Sorensen <mls@apple.com>
1 parent 713a236 commit 93f0926

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

server/src/main/java/com/cloud/vm/UserVmManagerImpl.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2092,14 +2092,21 @@ private boolean upgradeRunningVirtualMachine(Long vmId, Long newServiceOfferingI
20922092
return success;
20932093
}
20942094

2095-
private void validateDiskOfferingChecks(ServiceOfferingVO currentServiceOffering, ServiceOfferingVO newServiceOffering) {
2095+
protected void validateDiskOfferingChecks(ServiceOfferingVO currentServiceOffering, ServiceOfferingVO newServiceOffering) {
20962096
if (currentServiceOffering.getDiskOfferingStrictness() != newServiceOffering.getDiskOfferingStrictness()) {
20972097
throw new InvalidParameterValueException("Unable to Scale VM, since disk offering strictness flag is not same for new service offering and old service offering");
20982098
}
20992099

21002100
if (currentServiceOffering.getDiskOfferingStrictness() && currentServiceOffering.getDiskOfferingId() != newServiceOffering.getDiskOfferingId()) {
21012101
throw new InvalidParameterValueException("Unable to Scale VM, since disk offering id associated with the old service offering is not same for new service offering");
21022102
}
2103+
2104+
DiskOfferingVO currentRootDiskOffering = _diskOfferingDao.findByIdIncludingRemoved(currentServiceOffering.getDiskOfferingId());
2105+
DiskOfferingVO newRootDiskOffering = _diskOfferingDao.findById(newServiceOffering.getDiskOfferingId());
2106+
2107+
if (currentRootDiskOffering.getEncrypt() != newRootDiskOffering.getEncrypt()) {
2108+
throw new InvalidParameterValueException("Cannot change volume encryption type via service offering change");
2109+
}
21032110
}
21042111

21052112
private void changeDiskOfferingForRootVolume(Long vmId, DiskOfferingVO newDiskOffering, Map<String, String> customParameters) throws ResourceAllocationException {

server/src/test/java/com/cloud/vm/UserVmManagerImplTest.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,34 @@ public void prepareResizeVolumeCmdTestNewOfferingSmaller() {
565565
prepareAndRunResizeVolumeTest(2L, 10L, 20L, largerDisdkOffering, smallerDisdkOffering);
566566
}
567567

568+
@Test
569+
public void validateDiskOfferingCheckForEncryption1Test() {
570+
ServiceOfferingVO currentOffering = prepareOfferingsForEncryptionValidation(1L, true);
571+
ServiceOfferingVO newOffering = prepareOfferingsForEncryptionValidation(2L, true);
572+
userVmManagerImpl.validateDiskOfferingChecks(currentOffering, newOffering);
573+
}
574+
575+
@Test
576+
public void validateDiskOfferingCheckForEncryption2Test() {
577+
ServiceOfferingVO currentOffering = prepareOfferingsForEncryptionValidation(1L, false);
578+
ServiceOfferingVO newOffering = prepareOfferingsForEncryptionValidation(2L, false);
579+
userVmManagerImpl.validateDiskOfferingChecks(currentOffering, newOffering);
580+
}
581+
582+
@Test (expected = InvalidParameterValueException.class)
583+
public void validateDiskOfferingCheckForEncryptionFail1Test() {
584+
ServiceOfferingVO currentOffering = prepareOfferingsForEncryptionValidation(1L, false);
585+
ServiceOfferingVO newOffering = prepareOfferingsForEncryptionValidation(2L, true);
586+
userVmManagerImpl.validateDiskOfferingChecks(currentOffering, newOffering);
587+
}
588+
589+
@Test (expected = InvalidParameterValueException.class)
590+
public void validateDiskOfferingCheckForEncryptionFail2Test() {
591+
ServiceOfferingVO currentOffering = prepareOfferingsForEncryptionValidation(1L, true);
592+
ServiceOfferingVO newOffering = prepareOfferingsForEncryptionValidation(2L, false);
593+
userVmManagerImpl.validateDiskOfferingChecks(currentOffering, newOffering);
594+
}
595+
568596
private void prepareAndRunResizeVolumeTest(Long expectedOfferingId, long expectedMinIops, long expectedMaxIops, DiskOfferingVO currentRootDiskOffering, DiskOfferingVO newRootDiskOffering) {
569597
long rootVolumeId = 1l;
570598
VolumeVO rootVolumeOfVm = Mockito.mock(VolumeVO.class);
@@ -588,6 +616,20 @@ private DiskOfferingVO prepareDiskOffering(long rootSize, long diskOfferingId, l
588616
return newRootDiskOffering;
589617
}
590618

619+
private ServiceOfferingVO prepareOfferingsForEncryptionValidation(long diskOfferingId, boolean encryption) {
620+
ServiceOfferingVO svcOffering = Mockito.mock(ServiceOfferingVO.class);
621+
DiskOfferingVO diskOffering = Mockito.mock(DiskOfferingVO.class);
622+
623+
Mockito.when(svcOffering.getDiskOfferingId()).thenReturn(diskOfferingId);
624+
Mockito.when(diskOffering.getEncrypt()).thenReturn(encryption);
625+
626+
// Be aware - Multiple calls with the same disk offering ID could conflict
627+
Mockito.when(diskOfferingDao.findByIdIncludingRemoved(diskOfferingId)).thenReturn(diskOffering);
628+
Mockito.when(diskOfferingDao.findById(diskOfferingId)).thenReturn(diskOffering);
629+
630+
return svcOffering;
631+
}
632+
591633
@Test (expected = CloudRuntimeException.class)
592634
public void testUserDataDenyOverride() {
593635
Long userDataId = 1L;

0 commit comments

Comments
 (0)