|
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; |
@@ -233,28 +234,30 @@ private void addTpmToVm(AddTpmToVmContext context, Completion completion) { |
233 | 234 | .build()) |
234 | 235 | .then(Flow.of("create-tpm-db-records") |
235 | 236 | .handle(trigger -> { |
236 | | - try { |
237 | | - TpmVO tpm = vmTpmManager.persistTpmVO(context.tpmUuid, context.vmInstanceUuid); |
238 | | - context.createdTpmUuid = tpm.getUuid(); |
239 | | - context.tpmCreated = true; |
240 | | - if (context.keyProviderUuid != null) { |
241 | | - tpmKeyBackend.attachKeyProviderToTpm(context.createdTpmUuid, context.keyProviderUuid); |
242 | | - context.keyProviderAttached = true; |
243 | | - } |
244 | | - trigger.next(); |
245 | | - } catch (Exception e) { |
246 | | - trigger.fail(operr("failed to add TPM to vm[uuid:%s]: %s", context.vmInstanceUuid, e.getMessage())); |
| 237 | + TpmVO tpm = vmTpmManager.persistTpmVO(context.tpmUuid, context.vmInstanceUuid); |
| 238 | + context.createdTpmUuid = tpm.getUuid(); |
| 239 | + context.tpmCreated = true; |
| 240 | + trigger.next(); |
| 241 | + }) |
| 242 | + .rollback(trigger -> { |
| 243 | + if (context.tpmCreated && context.createdTpmUuid != null) { |
| 244 | + vmTpmManager.deleteTpmVO(context.createdTpmUuid); |
| 245 | + } |
| 246 | + trigger.rollback(); |
| 247 | + }) |
| 248 | + .build()) |
| 249 | + .then(Flow.of("attach-key-provider-to-tpm") |
| 250 | + .skipIf(data -> VmGlobalConfig.ALLOWED_TPM_VM_WITHOUT_KMS.value(Boolean.class)) |
| 251 | + .handle(trigger -> { |
| 252 | + if (context.keyProviderUuid != null) { |
| 253 | + tpmKeyBackend.attachKeyProviderToTpm(context.createdTpmUuid, context.keyProviderUuid); |
| 254 | + context.keyProviderAttached = true; |
247 | 255 | } |
| 256 | + trigger.next(); |
248 | 257 | }) |
249 | 258 | .rollback(trigger -> { |
250 | | - try { |
251 | | - if (context.keyProviderAttached && context.createdTpmUuid != null) { |
252 | | - tpmKeyBackend.detachKeyProviderFromTpm(context.createdTpmUuid); |
253 | | - } |
254 | | - } finally { |
255 | | - if (context.tpmCreated && context.createdTpmUuid != null) { |
256 | | - vmTpmManager.deleteTpmVO(context.createdTpmUuid); |
257 | | - } |
| 259 | + if (context.keyProviderAttached && context.createdTpmUuid != null) { |
| 260 | + tpmKeyBackend.detachKeyProviderFromTpm(context.createdTpmUuid); |
258 | 261 | } |
259 | 262 | trigger.rollback(); |
260 | 263 | }) |
|
0 commit comments