Skip to content

Commit

Permalink
chore: align deleteParticipantContext with DR (#446)
Browse files Browse the repository at this point in the history
  • Loading branch information
paullatzelsperger authored Sep 9, 2024
1 parent dab85b6 commit 4ea5163
Show file tree
Hide file tree
Showing 18 changed files with 565 additions and 246 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
import org.eclipse.edc.identithub.spi.did.store.DidResourceStore;
import org.eclipse.edc.identityhub.spi.keypair.events.KeyPairAdded;
import org.eclipse.edc.identityhub.spi.keypair.events.KeyPairRevoked;
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.events.ParticipantContextUpdated;
import org.eclipse.edc.identityhub.spi.participantcontext.model.ParticipantResource;
import org.eclipse.edc.keys.spi.KeyParserRegistry;
Expand Down Expand Up @@ -130,11 +128,15 @@ public ServiceResult<Void> unpublish(String did) {
if (publisher == null) {
return ServiceResult.badRequest(noPublisherFoundMessage(did));
}
var publishResult = publisher.unpublish(did);
return publishResult.succeeded() ?
success() :
ServiceResult.badRequest(publishResult.getFailureDetail());

// only unpublish if published, NOOP otherwise
if (existingDoc.getState() == DidState.PUBLISHED.code()) {
var publishResult = publisher.unpublish(did);
return publishResult.succeeded() ?
success() :
ServiceResult.badRequest(publishResult.getFailureDetail());
}
monitor.info("Unpublishing DID Document '%s': not in state '%s', unpublishing is a NOOP.".formatted(did, existingDoc.getStateAsEnum()));
return success();
});
}

Expand Down Expand Up @@ -216,8 +218,6 @@ public <E extends Event> void on(EventEnvelope<E> eventEnvelope) {
var payload = eventEnvelope.getPayload();
if (payload instanceof ParticipantContextUpdated event) {
updated(event);
} else if (payload instanceof ParticipantContextDeleted event) {
deleted(event);
} else if (payload instanceof KeyPairAdded event) {
keypairAdded(event);
} else if (payload instanceof KeyPairRevoked event) {
Expand Down Expand Up @@ -296,35 +296,8 @@ private void updated(ParticipantContextUpdated event) {
}
}

private void deleted(ParticipantContextDeleted event) {
var participantId = event.getParticipantId();
//unpublish and delete all DIDs associated with that participant
var errors = findByParticipantId(participantId)
.stream()
.map(didResource -> unpublish(didResource.getDid()).compose(u -> deleteById(didResource.getDid())))
.filter(AbstractResult::failed)
.map(AbstractResult::getFailureDetail)
.collect(Collectors.joining(", "));

if (!errors.isEmpty()) {
monitor.warning("Unpublishing/deleting DID documents after deleting a ParticipantContext failed: %s".formatted(errors));
}
}

private Collection<DidResource> findByParticipantId(String participantId) {
return didResourceStore.query(ParticipantResource.queryByParticipantId(participantId).build());
}


private void created(ParticipantContextCreated event) {
var manifest = event.getManifest();
var doc = DidDocument.Builder.newInstance()
.id(manifest.getDid())
.service(manifest.getServiceEndpoints().stream().toList())
// updating and adding a verification method happens as a result of the KeyPairAddedEvent
.build();
store(doc, manifest.getParticipantId())
.compose(u -> manifest.isActive() ? publish(doc.getId()) : success())
.onFailure(f -> monitor.warning("Creating a DID document after creating a ParticipantContext creation failed: %s".formatted(f.getFailureDetail())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.eclipse.edc.identithub.spi.did.store.DidResourceStore;
import org.eclipse.edc.identityhub.spi.keypair.events.KeyPairAdded;
import org.eclipse.edc.identityhub.spi.keypair.events.KeyPairRevoked;
import org.eclipse.edc.identityhub.spi.participantcontext.events.ParticipantContextDeleted;
import org.eclipse.edc.identityhub.spi.participantcontext.events.ParticipantContextUpdated;
import org.eclipse.edc.keys.spi.KeyParserRegistry;
import org.eclipse.edc.runtime.metamodel.annotation.Extension;
Expand Down Expand Up @@ -65,7 +64,6 @@ public DidDocumentPublisherRegistry getDidPublisherRegistry() {
public DidDocumentService createDidDocumentService(ServiceExtensionContext context) {
var service = new DidDocumentServiceImpl(transactionContext, didResourceStore, getDidPublisherRegistry(), context.getMonitor().withPrefix("DidDocumentService"), keyParserRegistry);
eventRouter.registerSync(ParticipantContextUpdated.class, service);
eventRouter.registerSync(ParticipantContextDeleted.class, service);
eventRouter.registerSync(KeyPairAdded.class, service);
eventRouter.registerSync(KeyPairRevoked.class, service);
return service;
Expand Down
Loading

0 comments on commit 4ea5163

Please sign in to comment.