Skip to content

Commit 2785e19

Browse files
author
Zhang Wenhao
committed
<fix>[kvm]: skip key provider attach when no KMS
Split the "create-tpm-db-records" flow into two separate flows: one for persisting TpmVO and another for attaching the key provider to TPM. The new "attach-key-provider-to-tpm" flow uses skipIf to skip execution when ALLOWED_TPM_VM_WITHOUT_KMS global config is set to true, so that tpmKeyBackend.attachKeyProviderToTpm is not called. Resolves: ZSV-11729 Related: ZSV-11310 Change-Id: I72757569696e73707a666b746d776a786e716574
1 parent b5be037 commit 2785e19

1 file changed

Lines changed: 22 additions & 19 deletions

File tree

plugin/kvm/src/main/java/org/zstack/kvm/tpm/KvmTpmManager.java

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.zstack.kvm.tpm;
22

33
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.zstack.compute.vm.VmGlobalConfig;
45
import org.zstack.compute.vm.devices.TpmEncryptedResourceKeyBackend;
56
import org.zstack.compute.vm.devices.VmTpmManager;
67
import org.zstack.core.asyncbatch.While;
@@ -230,28 +231,30 @@ private void addTpmToVm(AddTpmToVmContext context, Completion completion) {
230231
.build())
231232
.then(Flow.of("create-tpm-db-records")
232233
.handle(trigger -> {
233-
try {
234-
TpmVO tpm = vmTpmManager.persistTpmVO(context.tpmUuid, context.vmInstanceUuid);
235-
context.createdTpmUuid = tpm.getUuid();
236-
context.tpmCreated = true;
237-
if (context.keyProviderUuid != null) {
238-
tpmKeyBackend.attachKeyProviderToTpm(context.createdTpmUuid, context.keyProviderUuid);
239-
context.keyProviderAttached = true;
240-
}
241-
trigger.next();
242-
} catch (Exception e) {
243-
trigger.fail(operr("failed to add TPM to vm[uuid:%s]: %s", context.vmInstanceUuid, e.getMessage()));
234+
TpmVO tpm = vmTpmManager.persistTpmVO(context.tpmUuid, context.vmInstanceUuid);
235+
context.createdTpmUuid = tpm.getUuid();
236+
context.tpmCreated = true;
237+
trigger.next();
238+
})
239+
.rollback(trigger -> {
240+
if (context.tpmCreated && context.createdTpmUuid != null) {
241+
vmTpmManager.deleteTpmVO(context.createdTpmUuid);
242+
}
243+
trigger.rollback();
244+
})
245+
.build())
246+
.then(Flow.of("attach-key-provider-to-tpm")
247+
.skipIf(data -> VmGlobalConfig.ALLOWED_TPM_VM_WITHOUT_KMS.value(Boolean.class))
248+
.handle(trigger -> {
249+
if (context.keyProviderUuid != null) {
250+
tpmKeyBackend.attachKeyProviderToTpm(context.createdTpmUuid, context.keyProviderUuid);
251+
context.keyProviderAttached = true;
244252
}
253+
trigger.next();
245254
})
246255
.rollback(trigger -> {
247-
try {
248-
if (context.keyProviderAttached && context.createdTpmUuid != null) {
249-
tpmKeyBackend.detachKeyProviderFromTpm(context.createdTpmUuid);
250-
}
251-
} finally {
252-
if (context.tpmCreated && context.createdTpmUuid != null) {
253-
vmTpmManager.deleteTpmVO(context.createdTpmUuid);
254-
}
256+
if (context.keyProviderAttached && context.createdTpmUuid != null) {
257+
tpmKeyBackend.detachKeyProviderFromTpm(context.createdTpmUuid);
255258
}
256259
trigger.rollback();
257260
})

0 commit comments

Comments
 (0)