From d5cf2c8a3dabdf501ad126c29600265425b872be Mon Sep 17 00:00:00 2001 From: Paul Latzelsperger Date: Thu, 12 Sep 2024 17:22:29 +0200 Subject: [PATCH] handle deleted event --- .../provisioner/StsAccountProvisioner.java | 31 +++++++++++++------ .../StsAccountProvisionerExtension.java | 2 ++ .../StsAccountProvisionerTest.java | 11 +++++++ 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/extensions/common/sts-account-provisioner/src/main/java/org/eclipse/edc/identityhub/common/provisioner/StsAccountProvisioner.java b/extensions/common/sts-account-provisioner/src/main/java/org/eclipse/edc/identityhub/common/provisioner/StsAccountProvisioner.java index 7d36d1ce8..888cf41a1 100644 --- a/extensions/common/sts-account-provisioner/src/main/java/org/eclipse/edc/identityhub/common/provisioner/StsAccountProvisioner.java +++ b/extensions/common/sts-account-provisioner/src/main/java/org/eclipse/edc/identityhub/common/provisioner/StsAccountProvisioner.java @@ -26,6 +26,7 @@ import org.eclipse.edc.identityhub.spi.keypair.model.KeyPairResource; import org.eclipse.edc.identityhub.spi.keypair.model.KeyPairState; import org.eclipse.edc.identityhub.spi.participantcontext.events.ParticipantContextCreated; +import org.eclipse.edc.identityhub.spi.participantcontext.events.ParticipantContextDeleted; import org.eclipse.edc.identityhub.spi.participantcontext.model.ParticipantManifest; import org.eclipse.edc.identityhub.spi.participantcontext.model.ParticipantResource; import org.eclipse.edc.spi.event.Event; @@ -34,6 +35,7 @@ import org.eclipse.edc.spi.monitor.Monitor; import org.eclipse.edc.spi.result.Result; import org.eclipse.edc.spi.security.Vault; +import org.jetbrains.annotations.NotNull; import java.util.Collections; import java.util.Optional; @@ -63,18 +65,12 @@ public void on(EventEnvelope event) { Result result; if (payload instanceof ParticipantContextCreated createdEvent) { result = createAccount(createdEvent.getManifest()); + } else if (payload instanceof ParticipantContextDeleted deletedEvent) { + result = deleteAccount(deletedEvent.getParticipantId()); } else if (payload instanceof KeyPairRevoked || payload instanceof KeyPairRotated) { result = setKeyAliases(((KeyPairEvent) payload).getParticipantId(), null, null); } else if (payload instanceof DidDocumentPublished didDocumentPublished) { - - var participantId = didDocumentPublished.getParticipantId(); - result = getDefaultKeyPair(participantId) - .map(kpr -> { - var alias = kpr.getPrivateKeyAlias(); - var publicKeyReference = getVerificationMethodWithId(didDocumentPublished.getDid(), kpr.getKeyId()); - return setKeyAliases(participantId, alias, publicKeyReference); - }) - .orElse(Result.failure("No default keypair found for participant " + participantId)); + result = didDocumentPublished(didDocumentPublished); } else { result = Result.failure("Received event with unexpected payload type: %s".formatted(payload.getClass())); } @@ -82,6 +78,23 @@ public void on(EventEnvelope event) { result.onFailure(f -> monitor.warning(f.getFailureDetail())); } + private Result deleteAccount(String participantId) { + return Result.failure("Deleting StsClients is not yet implemented"); + } + + private @NotNull Result didDocumentPublished(DidDocumentPublished didDocumentPublished) { + Result result; + var participantId = didDocumentPublished.getParticipantId(); + result = getDefaultKeyPair(participantId) + .map(kpr -> { + var alias = kpr.getPrivateKeyAlias(); + var publicKeyReference = getVerificationMethodWithId(didDocumentPublished.getDid(), kpr.getKeyId()); + return setKeyAliases(participantId, alias, publicKeyReference); + }) + .orElse(Result.failure("No default keypair found for participant " + participantId)); + return result; + } + private String getVerificationMethodWithId(String did, String keyId) { return ofNullable(didDocumentService.findById(did)) .map(DidResource::getDocument).flatMap(dd -> dd.getVerificationMethod() diff --git a/extensions/common/sts-account-provisioner/src/main/java/org/eclipse/edc/identityhub/common/provisioner/StsAccountProvisionerExtension.java b/extensions/common/sts-account-provisioner/src/main/java/org/eclipse/edc/identityhub/common/provisioner/StsAccountProvisionerExtension.java index 7ebfbcf76..fa58544f0 100644 --- a/extensions/common/sts-account-provisioner/src/main/java/org/eclipse/edc/identityhub/common/provisioner/StsAccountProvisionerExtension.java +++ b/extensions/common/sts-account-provisioner/src/main/java/org/eclipse/edc/identityhub/common/provisioner/StsAccountProvisionerExtension.java @@ -22,6 +22,7 @@ import org.eclipse.edc.identityhub.spi.keypair.events.KeyPairRevoked; import org.eclipse.edc.identityhub.spi.keypair.events.KeyPairRotated; import org.eclipse.edc.identityhub.spi.participantcontext.events.ParticipantContextCreated; +import org.eclipse.edc.identityhub.spi.participantcontext.events.ParticipantContextDeleted; import org.eclipse.edc.runtime.metamodel.annotation.Extension; import org.eclipse.edc.runtime.metamodel.annotation.Inject; import org.eclipse.edc.spi.event.EventRouter; @@ -57,6 +58,7 @@ public void initialize(ServiceExtensionContext context) { if (stsClientStore != null) { var provisioner = new StsAccountProvisioner(monitor, keyPairService, didDocumentService, stsClientStore, vault); eventRouter.registerSync(ParticipantContextCreated.class, provisioner); + eventRouter.registerSync(ParticipantContextDeleted.class, provisioner); eventRouter.registerSync(KeyPairAdded.class, provisioner); eventRouter.registerSync(KeyPairRevoked.class, provisioner); eventRouter.registerSync(KeyPairRotated.class, provisioner); diff --git a/extensions/common/sts-account-provisioner/src/test/java/org/eclipse/edc/identityhub/common/provisioner/StsAccountProvisionerTest.java b/extensions/common/sts-account-provisioner/src/test/java/org/eclipse/edc/identityhub/common/provisioner/StsAccountProvisionerTest.java index ea7d37d87..0a0220644 100644 --- a/extensions/common/sts-account-provisioner/src/test/java/org/eclipse/edc/identityhub/common/provisioner/StsAccountProvisionerTest.java +++ b/extensions/common/sts-account-provisioner/src/test/java/org/eclipse/edc/identityhub/common/provisioner/StsAccountProvisionerTest.java @@ -28,6 +28,7 @@ import org.eclipse.edc.identityhub.spi.keypair.model.KeyPairResource; import org.eclipse.edc.identityhub.spi.keypair.model.KeyPairState; import org.eclipse.edc.identityhub.spi.participantcontext.events.ParticipantContextCreated; +import org.eclipse.edc.identityhub.spi.participantcontext.events.ParticipantContextDeleted; import org.eclipse.edc.identityhub.spi.participantcontext.model.KeyDescriptor; import org.eclipse.edc.identityhub.spi.participantcontext.model.ParticipantManifest; import org.eclipse.edc.spi.event.Event; @@ -170,6 +171,16 @@ void onDidPublished_noDefaultKey_shouldUpdate() { } + @Test + void onParticipantDeleted_shouldDelete() { + accountProvisioner.on(event(ParticipantContextDeleted.Builder.newInstance() + .participantId(PARTICIPANT_CONTEXT_ID) + .build())); + + verify(monitor).warning(eq("Deleting StsClients is not yet implemented")); + verifyNoInteractions(keyPairService, didDocumentService, stsClientStore); + } + @Test void onOtherEvent_shouldLogWarning() { accountProvisioner.on(event(new DummyEvent()));