Skip to content

Commit 6cee61d

Browse files
christophstroblmp911de
authored andcommitted
Fix decryption when client is using AutoEncryptionSettings#isBypassAutoEncryption().
This commit makes sure to convert already decrypted entries returned by the driver in case the client is configured with encryption settings. Closes #4432 Original pull request: #4439
1 parent 70530d6 commit 6cee61d

File tree

4 files changed

+865
-361
lines changed

4 files changed

+865
-361
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/encryption/MongoEncryptionConverter.java

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.util.Collection;
1919
import java.util.LinkedHashMap;
20+
import java.util.Map;
2021

2122
import org.apache.commons.logging.Log;
2223
import org.apache.commons.logging.LogFactory;
@@ -25,6 +26,7 @@
2526
import org.bson.BsonDocument;
2627
import org.bson.BsonValue;
2728
import org.bson.Document;
29+
import org.bson.conversions.Bson;
2830
import org.bson.types.Binary;
2931
import org.springframework.core.CollectionFactory;
3032
import org.springframework.data.mongodb.core.convert.MongoConversionContext;
@@ -63,7 +65,7 @@ public MongoEncryptionConverter(Encryption<BsonValue, BsonBinary> encryption, En
6365
public Object read(Object value, MongoConversionContext context) {
6466

6567
Object decrypted = EncryptingConverter.super.read(value, context);
66-
return decrypted instanceof BsonValue ? BsonUtils.toJavaType((BsonValue) decrypted) : decrypted;
68+
return decrypted instanceof BsonValue bsonValue ? BsonUtils.toJavaType(bsonValue) : decrypted;
6769
}
6870

6971
@Override
@@ -87,36 +89,56 @@ public Object decrypt(Object encryptedValue, EncryptionContext context) {
8789
}
8890

8991
MongoPersistentProperty persistentProperty = getProperty(context);
90-
9192
if (getProperty(context).isCollectionLike() && decryptedValue instanceof Iterable<?> iterable) {
9293

9394
int size = iterable instanceof Collection<?> c ? c.size() : 10;
9495

9596
if (!persistentProperty.isEntity()) {
9697
Collection<Object> collection = CollectionFactory.createCollection(persistentProperty.getType(), size);
97-
iterable.forEach(it -> collection.add(BsonUtils.toJavaType((BsonValue) it)));
98+
iterable.forEach(it -> {
99+
if(it instanceof BsonValue bsonValue) {
100+
collection.add(BsonUtils.toJavaType(bsonValue));
101+
} else {
102+
collection.add(context.read(it, persistentProperty.getActualType()));
103+
}
104+
});
105+
98106
return collection;
99107
} else {
100108
Collection<Object> collection = CollectionFactory.createCollection(persistentProperty.getType(), size);
101109
iterable.forEach(it -> {
102-
collection.add(context.read(BsonUtils.toJavaType((BsonValue) it), persistentProperty.getActualType()));
110+
if(it instanceof BsonValue bsonValue) {
111+
collection.add(context.read(BsonUtils.toJavaType(bsonValue), persistentProperty.getActualType()));
112+
} else {
113+
collection.add(context.read(it, persistentProperty.getActualType()));
114+
}
103115
});
104116
return collection;
105117
}
106118
}
107119

108-
if (!persistentProperty.isEntity() && decryptedValue instanceof BsonValue bsonValue) {
109-
if (persistentProperty.isMap() && persistentProperty.getType() != Document.class) {
110-
return new LinkedHashMap<>((Document) BsonUtils.toJavaType(bsonValue));
111-
120+
if (!persistentProperty.isEntity() && persistentProperty.isMap()) {
121+
if(persistentProperty.getType() != Document.class) {
122+
if(decryptedValue instanceof BsonValue bsonValue) {
123+
return new LinkedHashMap<>((Document) BsonUtils.toJavaType(bsonValue));
124+
}
125+
if(decryptedValue instanceof Document document) {
126+
return new LinkedHashMap<>(document);
127+
}
128+
if(decryptedValue instanceof Map map) {
129+
return map;
130+
}
112131
}
113-
return BsonUtils.toJavaType(bsonValue);
114132
}
115133

116134
if (persistentProperty.isEntity() && decryptedValue instanceof BsonDocument bsonDocument) {
117135
return context.read(BsonUtils.toJavaType(bsonDocument), persistentProperty.getTypeInformation().getType());
118136
}
119137

138+
if (persistentProperty.isEntity() && decryptedValue instanceof Document document) {
139+
return context.read(document, persistentProperty.getTypeInformation().getType());
140+
}
141+
120142
return decryptedValue;
121143
}
122144

0 commit comments

Comments
 (0)