Skip to content

Commit

Permalink
fixed rare bug of encryption bean getting initialized before mongomap…
Browse files Browse the repository at this point in the history
…pingcontext is fully filled with persistentEntities.

by lazily initializing reflection data, we also spare doing this where not necessary.
  • Loading branch information
agoston committed Sep 20, 2019
1 parent 1103347 commit 0ca2c06
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<artifactId>spring-data-mongodb-encrypt</artifactId>
<packaging>jar</packaging>
<name>spring-data-mongodb-encrypt</name>
<version>2.2</version>
<version>2.3</version>
<description>High performance, per-field encryption for spring-data-mongodb</description>
<url>https://github.com/agoston/spring-data-mongodb-encrypt</url>

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/bol/reflection/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ public enum Type {
/** field is a sub-document, descend */
DOCUMENT
}

public static final Node EMPTY = new Node(null, null, null);
}
23 changes: 12 additions & 11 deletions src/main/java/com/bol/secure/CachedEncryptionEventListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
import org.bson.Document;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.mapping.BasicMongoPersistentEntity;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import org.springframework.data.mongodb.core.mapping.event.AfterLoadEvent;
import org.springframework.data.mongodb.core.mapping.event.BeforeSaveEvent;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -22,28 +22,29 @@
public class CachedEncryptionEventListener extends AbstractEncryptionEventListener<CachedEncryptionEventListener> {
@Autowired MongoMappingContext mappingContext;

Map<Class, Node> encrypted;
HashMap<Class, Node> encrypted = new HashMap<>();

public CachedEncryptionEventListener(CryptVault cryptVault) {
super(cryptVault);
}

@PostConstruct
public void initReflection() {
encrypted = new HashMap<>();
Node node(Class clazz) {
return encrypted.computeIfAbsent(clazz, c -> {
BasicMongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(c);
if (entity == null) return Node.EMPTY;

mappingContext.getPersistentEntities().forEach(entity -> {
List<Node> children = processDocument(entity.getType());
if (!children.isEmpty()) encrypted.put(entity.getType(), new Node("", children, Node.Type.ROOT));
if (!children.isEmpty()) return new Node("", children, Node.Type.ROOT);
return Node.EMPTY;
});
}

@Override
public void onAfterLoad(AfterLoadEvent event) {
Document document = event.getDocument();

Node node = encrypted.get(event.getType());
if (node == null) return;
Node node = node(event.getType());
if (node == Node.EMPTY) return;

try {
cryptFields(document, node, new Decoder());
Expand All @@ -57,8 +58,8 @@ public void onAfterLoad(AfterLoadEvent event) {
public void onBeforeSave(BeforeSaveEvent event) {
Document document = event.getDocument();

Node node = encrypted.get(event.getSource().getClass());
if (node == null) return;
Node node = node(event.getSource().getClass());
if (node == Node.EMPTY) return;

try {
cryptFields(document, node, new Encoder());
Expand Down

0 comments on commit 0ca2c06

Please sign in to comment.