Skip to content

Commit

Permalink
eventuate-clients#9: Enhancing the Eventuate Java Client According GD…
Browse files Browse the repository at this point in the history
…PR. Moving Encryption to EventuateAggregateStoreImpl classes.
  • Loading branch information
dartartem committed Apr 17, 2018
1 parent 12dd70d commit 5701aa7
Show file tree
Hide file tree
Showing 17 changed files with 161 additions and 184 deletions.
4 changes: 0 additions & 4 deletions eventuate-client-java-common-impl/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,4 @@ apply plugin: PrivateModulePlugin

dependencies {
compile project(":eventuate-client-java")

compile "org.springframework.security:spring-security-core:5.0.4.RELEASE"
compile "com.fasterxml.jackson.core:jackson-databind:2.8.8"
compile "com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.8.8"
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.eventuate.javaclient.commonimpl;

import io.eventuate.EventContext;
import io.eventuate.javaclient.commonimpl.encryption.EncryptionKeyStore;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

Expand All @@ -10,30 +9,20 @@
public class AggregateCrudFindOptions {

private Optional<EventContext> triggeringEvent;
private Optional<EncryptionKeyStore> encryptionKeyStore;

public AggregateCrudFindOptions() {
triggeringEvent = Optional.empty();
encryptionKeyStore = Optional.empty();
}

public AggregateCrudFindOptions(Optional<EventContext> triggeringEvent) {
this(triggeringEvent, Optional.empty());
}

public AggregateCrudFindOptions(Optional<EventContext> triggeringEvent, Optional<EncryptionKeyStore> encryptionKeyStore) {
public AggregateCrudFindOptions(Optional<EventContext> triggeringEvent) {
this.triggeringEvent = triggeringEvent;
this.encryptionKeyStore = encryptionKeyStore;
}

public Optional<EventContext> getTriggeringEvent() {
return triggeringEvent;
}

public Optional<EncryptionKeyStore> getEncryptionKeyStore() {
return encryptionKeyStore;
}

public AggregateCrudFindOptions withTriggeringEvent(EventContext eventContext) {
this.triggeringEvent = Optional.ofNullable(eventContext);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

Expand Down Expand Up @@ -53,22 +54,32 @@ public static Snapshot toSnapshot(SerializedSnapshot serializedSnapshot) {
}

public static EventTypeAndData toEventTypeAndData(Event event, Optional<String> metadata) {
return new EventTypeAndData(event.getClass().getName(), JSonMapper.toJson(event), metadata);
return toEventTypeAndData(event, metadata, Function.identity());
}

public static EventTypeAndData toEventTypeAndData(Event event, Optional<String> metadata, Function<String, String> jsonHandler) {
return new EventTypeAndData(event.getClass().getName(), jsonHandler.apply(JSonMapper.toJson(event)), metadata);
}

public static Event toEvent(EventIdTypeAndData eventIdTypeAndData) {
return toEvent(eventIdTypeAndData, Function.identity());
}

public static Event toEvent(EventIdTypeAndData eventIdTypeAndData, Function<String, String> jsonHandler) {
try {
return JSonMapper.fromJson(eventIdTypeAndData.getEventData(), (Class<Event>) Class.forName(eventIdTypeAndData.getEventType()));
return JSonMapper.fromJson(jsonHandler.apply(eventIdTypeAndData.getEventData()), (Class<Event>) Class.forName(eventIdTypeAndData.getEventType()));
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}

public static EventWithMetadata toEventWithMetadata(EventIdTypeAndData eventIdTypeAndData) {
return toEventWithMetadata(eventIdTypeAndData, Function.identity());
}

public static EventWithMetadata toEventWithMetadata(EventIdTypeAndData eventIdTypeAndData, Function<String, String> jsonHandler) {
Optional<String> metadata = eventIdTypeAndData.getMetadata();
return new EventWithMetadata(toEvent(eventIdTypeAndData), eventIdTypeAndData.getId(),
return new EventWithMetadata(toEvent(eventIdTypeAndData, jsonHandler), eventIdTypeAndData.getId(),
metadata == null ? Optional.empty() : metadata.map(md -> JSonMapper.fromJson(md, Map.class)));
}


}
Original file line number Diff line number Diff line change
@@ -1,30 +1,22 @@
package io.eventuate.javaclient.commonimpl;

import io.eventuate.EventContext;
import io.eventuate.javaclient.commonimpl.encryption.EncryptionKey;

import java.util.Optional;

public class AggregateCrudSaveOptions {

private final Optional<String> entityId;
private final Optional<EventContext> triggeringEvent;
private final Optional<EncryptionKey> encryptionKey;

public AggregateCrudSaveOptions() {
entityId = Optional.empty();
triggeringEvent = Optional.empty();
encryptionKey = Optional.empty();
}

public AggregateCrudSaveOptions(Optional<EventContext> triggeringEvent, Optional<String> entityId) {
this(triggeringEvent, entityId, Optional.empty());
}

public AggregateCrudSaveOptions(Optional<EventContext> triggeringEvent, Optional<String> entityId, Optional<EncryptionKey> encryptionKey) {
this.triggeringEvent = triggeringEvent;
this.entityId = entityId;
this.encryptionKey = encryptionKey;
}

public Optional<String> getEntityId() {
Expand All @@ -35,15 +27,11 @@ public Optional<EventContext> getTriggeringEvent() {
return triggeringEvent;
}

public Optional<EncryptionKey> getEncryptionKey() {
return encryptionKey;
}

public AggregateCrudSaveOptions withEventContext(EventContext eventContext) {
return new AggregateCrudSaveOptions(Optional.of(eventContext), entityId, encryptionKey);
return new AggregateCrudSaveOptions(Optional.of(eventContext), entityId);
}

public AggregateCrudSaveOptions withId(String entityId) {
return new AggregateCrudSaveOptions(triggeringEvent, Optional.of(entityId), encryptionKey);
return new AggregateCrudSaveOptions(triggeringEvent, Optional.of(entityId));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.eventuate.javaclient.commonimpl;

import io.eventuate.EventContext;
import io.eventuate.javaclient.commonimpl.encryption.EncryptionKey;
import org.apache.commons.lang.builder.ToStringBuilder;

import java.util.Optional;
Expand All @@ -10,22 +9,15 @@ public class AggregateCrudUpdateOptions {

private final Optional<EventContext> triggeringEvent;
private final Optional<SerializedSnapshot> snapshot;
private final Optional<EncryptionKey> encryptionKey;

public AggregateCrudUpdateOptions() {
triggeringEvent = Optional.empty();
snapshot = Optional.empty();
encryptionKey = Optional.empty();
}

public AggregateCrudUpdateOptions(Optional<EventContext> triggeringEvent, Optional<SerializedSnapshot> snapshot) {
this(triggeringEvent, snapshot, Optional.empty());
}

public AggregateCrudUpdateOptions(Optional<EventContext> triggeringEvent, Optional<SerializedSnapshot> snapshot, Optional<EncryptionKey> encryptionKey) {
this.triggeringEvent = triggeringEvent;
this.snapshot = snapshot;
this.encryptionKey = encryptionKey;
}

public Optional<EventContext> getTriggeringEvent() {
Expand All @@ -36,10 +28,6 @@ public Optional<SerializedSnapshot> getSnapshot() {
return snapshot;
}

public Optional<EncryptionKey> getEncryptionKey() {
return encryptionKey;
}

public AggregateCrudUpdateOptions withSnapshot(SerializedSnapshot serializedSnapshot) {
return new AggregateCrudUpdateOptions(this.triggeringEvent, Optional.of(serializedSnapshot));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.eventuate.javaclient.commonimpl;

import io.eventuate.*;
import io.eventuate.encryption.EncryptedEventData;
import io.eventuate.encryption.EventDataEncryptor;
import io.eventuate.javaclient.commonimpl.schemametadata.EmptyEventSchemaMetadataManager;
import io.eventuate.javaclient.commonimpl.schemametadata.EventSchemaMetadataManager;

Expand All @@ -26,6 +28,7 @@ public class EventuateAggregateStoreImpl implements EventuateAggregateStore {
private SerializedEventDeserializer serializedEventDeserializer = new DefaultSerializedEventDeserializer();
private MissingApplyEventMethodStrategy missingApplyEventMethodStrategy;
private EventSchemaMetadataManager eventSchemaMetadataManager = new EmptyEventSchemaMetadataManager();
private EventDataEncryptor eventDataEncryptor = new EventDataEncryptor();

public EventuateAggregateStoreImpl(AggregateCrud aggregateCrud, AggregateEvents aggregateEvents, SnapshotManager snapshotManager, MissingApplyEventMethodStrategy missingApplyEventMethodStrategy) {
this.aggregateCrud = aggregateCrud;
Expand All @@ -51,7 +54,16 @@ public <T extends Aggregate<T>> CompletableFuture<EntityIdAndVersion> save(Class
@Override
public <T extends Aggregate<T>> CompletableFuture<EntityIdAndVersion> save(Class<T> clasz, List<Event> events, Optional<SaveOptions> saveOptions) {
Optional<String> serializedMetadata = saveOptions.flatMap(so -> withSchemaMetadata(clasz, so.getEventMetadata())).map(JSonMapper::toJson);
List<EventTypeAndData> serializedEvents = events.stream().map(event -> toEventTypeAndData(event, serializedMetadata)).collect(Collectors.toList());
List<EventTypeAndData> serializedEvents = events
.stream()
.map(event ->
toEventTypeAndData(event,
serializedMetadata,
json -> saveOptions
.flatMap(SaveOptions::getEncryptionKey)
.map(k -> new EncryptedEventData(k.getId(), eventDataEncryptor.encrypt(k, json)).asString())
.orElse(json)))
.collect(Collectors.toList());
CompletableFuture<EntityIdVersionAndEventIds> outcome = aggregateCrud.save(clasz.getName(), serializedEvents, AggregateCrudMapping.toAggregateCrudSaveOptions(saveOptions));
if (activityLogger.isDebugEnabled())
return CompletableFutureUtil.tap(outcome, (result, throwable) -> {
Expand Down Expand Up @@ -106,8 +118,18 @@ public <T extends Aggregate<T>> CompletableFuture<EntityWithMetadata<T>> find(Cl
tappedOutcome = outcome;

return tappedOutcome.thenApply(le -> {
List<EventWithMetadata> eventsWithIds = eventSchemaMetadataManager.upcastEvents(clasz, le.getEvents()).stream().map(AggregateCrudMapping::toEventWithMetadata).collect(Collectors.toList());
List<EventWithMetadata> eventsWithIds = eventSchemaMetadataManager
.upcastEvents(clasz, le.getEvents())
.stream()
.map(event ->
AggregateCrudMapping.toEventWithMetadata(event, json -> findOptions
.flatMap(FindOptions::getEncryptionKey)
.map(k -> eventDataEncryptor.decrypt(k, json))
.orElse(json)))
.collect(Collectors.toList());

List<Event> events = eventsWithIds.stream().map(EventWithMetadata::getEvent).collect(Collectors.toList());

return new EntityWithMetadata<T>(
new EntityIdAndVersion(entityId, le.getEvents().isEmpty() ? le.getSnapshot().get().getEntityVersion() : le.getEvents().get(le.getEvents().size() - 1).getId()),
le.getSnapshot().map(SerializedSnapshotWithVersion::getEntityVersion),
Expand All @@ -134,7 +156,16 @@ public <T extends Aggregate<T>> CompletableFuture<EntityIdAndVersion> update(Cla
@Override
public <T extends Aggregate<T>> CompletableFuture<EntityIdAndVersion> update(Class<T> clasz, EntityIdAndVersion entityIdAndVersion, List<Event> events, Optional<UpdateOptions> updateOptions) {
Optional<String> serializedMetadata = updateOptions.flatMap(UpdateOptions::getEventMetadata).map(JSonMapper::toJson);
List<EventTypeAndData> serializedEvents = events.stream().map(event -> toEventTypeAndData(event, serializedMetadata)).collect(Collectors.toList());
List<EventTypeAndData> serializedEvents = events
.stream()
.map(event ->
toEventTypeAndData(event,
serializedMetadata,
json -> updateOptions
.flatMap(UpdateOptions::getEncryptionKey)
.map(k -> new EncryptedEventData(k.getId(), eventDataEncryptor.encrypt(k, json)).asString())
.orElse(json)))
.collect(Collectors.toList());

CompletableFuture<EntityIdVersionAndEventIds> outcome = aggregateCrud.update(new EntityIdAndType(entityIdAndVersion.getEntityId(), clasz.getName()),
entityIdAndVersion.getEntityVersion(),
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
import io.eventuate.SnapshotManager;
import io.eventuate.SubscriberOptions;
import io.eventuate.UpdateOptions;
import io.eventuate.encryption.EncryptedEventData;
import io.eventuate.javaclient.commonimpl.*;
import io.eventuate.encryption.EventDataEncryptor;
import io.eventuate.sync.EventuateAggregateStore;

import java.util.List;
Expand All @@ -37,6 +39,7 @@ public class EventuateAggregateStoreImpl implements EventuateAggregateStore {
private SnapshotManager snapshotManager;
private SerializedEventDeserializer serializedEventDeserializer = new DefaultSerializedEventDeserializer();
private MissingApplyEventMethodStrategy missingApplyEventMethodStrategy;
private EventDataEncryptor eventDataEncryptor = new EventDataEncryptor();

public EventuateAggregateStoreImpl(AggregateCrud aggregateCrud, AggregateEvents aggregateEvents, SnapshotManager snapshotManager, MissingApplyEventMethodStrategy missingApplyEventMethodStrategy) {
this.aggregateCrud = aggregateCrud;
Expand All @@ -62,7 +65,16 @@ public <T extends Aggregate<T>> EntityIdAndVersion save(Class<T> clasz, List<Eve
@Override
public <T extends Aggregate<T>> EntityIdAndVersion save(Class<T> clasz, List<Event> events, Optional<SaveOptions> saveOptions) {
Optional<String> serializedMetadata = saveOptions.flatMap(SaveOptions::getEventMetadata).map(JSonMapper::toJson);
List<EventTypeAndData> serializedEvents = events.stream().map(event -> toEventTypeAndData(event, serializedMetadata)).collect(Collectors.toList());
List<EventTypeAndData> serializedEvents = events
.stream()
.map(event ->
toEventTypeAndData(event,
serializedMetadata,
json -> saveOptions
.flatMap(SaveOptions::getEncryptionKey)
.map(k -> new EncryptedEventData(k.getId(), eventDataEncryptor.encrypt(k, json)).asString())
.orElse(json)))
.collect(Collectors.toList());
try {
EntityIdVersionAndEventIds result = aggregateCrud.save(clasz.getName(), serializedEvents, toAggregateCrudSaveOptions(saveOptions));
if (activityLogger.isDebugEnabled())
Expand Down Expand Up @@ -91,7 +103,15 @@ public <T extends Aggregate<T>> EntityWithMetadata<T> find(Class<T> clasz, Strin
LoadedEvents le = aggregateCrud.find(clasz.getName(), entityId, toAggregateCrudFindOptions(findOptions));
if (activityLogger.isDebugEnabled())
activityLogger.debug("Loaded entity: {} {} {}", clasz.getName(), entityId, le.getEvents());
List<EventWithMetadata> eventsWithIds = le.getEvents().stream().map(AggregateCrudMapping::toEventWithMetadata).collect(Collectors.toList());
List<EventWithMetadata> eventsWithIds = le
.getEvents()
.stream()
.map(event ->
AggregateCrudMapping.toEventWithMetadata(event, json -> findOptions
.flatMap(FindOptions::getEncryptionKey)
.map(k -> eventDataEncryptor.decrypt(k, json))
.orElse(json)))
.collect(Collectors.toList());
List<Event> events = eventsWithIds.stream().map(EventWithMetadata::getEvent).collect(Collectors.toList());
return new EntityWithMetadata<T>(
new EntityIdAndVersion(entityId,
Expand Down Expand Up @@ -123,7 +143,16 @@ public <T extends Aggregate<T>> EntityIdAndVersion update(Class<T> clasz, Entity
public <T extends Aggregate<T>> EntityIdAndVersion update(Class<T> clasz, EntityIdAndVersion entityIdAndVersion, List<Event> events, Optional<UpdateOptions> updateOptions) {
try {
Optional<String> serializedEventMetadata = updateOptions.flatMap(UpdateOptions::getEventMetadata).map(JSonMapper::toJson);
List<EventTypeAndData> serializedEvents = events.stream().map(event -> toEventTypeAndData(event, serializedEventMetadata)).collect(Collectors.toList());
List<EventTypeAndData> serializedEvents = events
.stream()
.map(event ->
toEventTypeAndData(event,
serializedEventMetadata,
json -> updateOptions
.flatMap(UpdateOptions::getEncryptionKey)
.map(k -> new EncryptedEventData(k.getId(), eventDataEncryptor.encrypt(k, json)).asString())
.orElse(json)))
.collect(Collectors.toList());
EntityIdVersionAndEventIds result = aggregateCrud.update(new EntityIdAndType(entityIdAndVersion.getEntityId(), clasz.getName()),
entityIdAndVersion.getEntityVersion(),
serializedEvents,
Expand Down Expand Up @@ -164,6 +193,4 @@ public Optional<Snapshot> possiblySnapshot(Aggregate aggregate, Optional<Int128>
public Aggregate recreateFromSnapshot(Class<?> clasz, Snapshot snapshot) {
return snapshotManager.recreateFromSnapshot(clasz, snapshot, missingApplyEventMethodStrategy);
}


}
Loading

0 comments on commit 5701aa7

Please sign in to comment.