|
1 | 1 | package org.zstack.kvm.tpm; |
2 | 2 |
|
3 | 3 | import org.springframework.beans.factory.annotation.Autowired; |
| 4 | +import org.zstack.compute.vm.VmGlobalConfig; |
4 | 5 | import org.zstack.compute.vm.devices.TpmEncryptedResourceKeyBackend; |
5 | 6 | import org.zstack.compute.vm.devices.VmTpmManager; |
6 | 7 | import org.zstack.core.asyncbatch.While; |
@@ -230,28 +231,30 @@ private void addTpmToVm(AddTpmToVmContext context, Completion completion) { |
230 | 231 | .build()) |
231 | 232 | .then(Flow.of("create-tpm-db-records") |
232 | 233 | .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; |
244 | 252 | } |
| 253 | + trigger.next(); |
245 | 254 | }) |
246 | 255 | .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); |
255 | 258 | } |
256 | 259 | trigger.rollback(); |
257 | 260 | }) |
|
0 commit comments