Skip to content

Commit

Permalink
handle deleted event
Browse files Browse the repository at this point in the history
  • Loading branch information
paullatzelsperger committed Sep 12, 2024
1 parent 641e73b commit d5cf2c8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -63,25 +65,36 @@ public <E extends Event> void on(EventEnvelope<E> event) {
Result<Void> 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()));
}

result.onFailure(f -> monitor.warning(f.getFailureDetail()));
}

private Result<Void> deleteAccount(String participantId) {

Check notice

Code scanning / CodeQL

Useless parameter Note

The parameter 'participantId' is never used.
return Result.failure("Deleting StsClients is not yet implemented");
}

private @NotNull Result<Void> didDocumentPublished(DidDocumentPublished didDocumentPublished) {
Result<Void> 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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()));
Expand Down

0 comments on commit d5cf2c8

Please sign in to comment.