Skip to content

Commit 335418f

Browse files
hacking
1 parent 101d0a6 commit 335418f

File tree

2 files changed

+66
-23
lines changed

2 files changed

+66
-23
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/CollectionOptions.java

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.springframework.data.mongodb.core.mapping.Field;
3535
import org.springframework.data.mongodb.core.query.Collation;
3636
import org.springframework.data.mongodb.core.schema.IdentifiableJsonSchemaProperty;
37+
import org.springframework.data.mongodb.core.schema.IdentifiableJsonSchemaProperty.EncryptedJsonSchemaProperty;
3738
import org.springframework.data.mongodb.core.schema.IdentifiableJsonSchemaProperty.QueryableJsonSchemaProperty;
3839
import org.springframework.data.mongodb.core.schema.JsonSchemaProperty;
3940
import org.springframework.data.mongodb.core.schema.MongoJsonSchema;
@@ -681,17 +682,17 @@ public static class EncryptedFieldsOptions {
681682
private static final EncryptedFieldsOptions NONE = new EncryptedFieldsOptions();
682683

683684
private final @Nullable MongoJsonSchema schema;
684-
private final List<QueryableJsonSchemaProperty> queryableProperties;
685+
private final List<JsonSchemaProperty> properties;
685686

686687
EncryptedFieldsOptions() {
687688
this(null, List.of());
688689
}
689690

690691
private EncryptedFieldsOptions(@Nullable MongoJsonSchema schema,
691-
List<QueryableJsonSchemaProperty> queryableProperties) {
692+
List<JsonSchemaProperty> queryableProperties) {
692693

693694
this.schema = schema;
694-
this.queryableProperties = queryableProperties;
695+
this.properties = queryableProperties;
695696
}
696697

697698
/**
@@ -731,13 +732,33 @@ public static EncryptedFieldsOptions fromProperties(List<QueryableJsonSchemaProp
731732
@CheckReturnValue
732733
public EncryptedFieldsOptions queryable(JsonSchemaProperty property, QueryCharacteristic... characteristics) {
733734

734-
List<QueryableJsonSchemaProperty> targetPropertyList = new ArrayList<>(queryableProperties.size() + 1);
735-
targetPropertyList.addAll(queryableProperties);
735+
List<JsonSchemaProperty> targetPropertyList = new ArrayList<>(properties.size() + 1);
736+
targetPropertyList.addAll(properties);
736737
targetPropertyList.add(JsonSchemaProperty.queryable(property, List.of(characteristics)));
737738

738739
return new EncryptedFieldsOptions(schema, targetPropertyList);
739740
}
740741

742+
public EncryptedFieldsOptions with(EncryptedJsonSchemaProperty property) {
743+
return encrypted(property, null);
744+
}
745+
746+
public EncryptedFieldsOptions encrypted(JsonSchemaProperty property, @Nullable Object key) {
747+
748+
List<JsonSchemaProperty> targetPropertyList = new ArrayList<>(properties.size() + 1);
749+
targetPropertyList.addAll(properties);
750+
if(property instanceof IdentifiableJsonSchemaProperty.EncryptedJsonSchemaProperty) {
751+
targetPropertyList.add(property);
752+
} else {
753+
EncryptedJsonSchemaProperty encryptedJsonSchemaProperty = new EncryptedJsonSchemaProperty(property);
754+
if(key != null) {
755+
targetPropertyList.add(encryptedJsonSchemaProperty.keyId(key));
756+
}
757+
}
758+
759+
return new EncryptedFieldsOptions(schema, targetPropertyList);
760+
}
761+
741762
public Document toDocument() {
742763
return new Document("fields", selectPaths());
743764
}
@@ -756,20 +777,20 @@ private List<Document> selectPaths() {
756777

757778
private List<Document> fromProperties() {
758779

759-
if (queryableProperties.isEmpty()) {
780+
if (properties.isEmpty()) {
760781
return List.of();
761782
}
762783

763-
List<Document> converted = new ArrayList<>(queryableProperties.size());
764-
for (QueryableJsonSchemaProperty property : queryableProperties) {
784+
List<Document> converted = new ArrayList<>(properties.size());
785+
for (JsonSchemaProperty property : properties) {
765786

766787
Document field = new Document("path", property.getIdentifier());
767788

768789
if (!property.getTypes().isEmpty()) {
769790
field.append("bsonType", property.getTypes().iterator().next().toBsonType().value());
770791
}
771792

772-
if (property
793+
if (property instanceof QueryableJsonSchemaProperty qproperty && qproperty
773794
.getTargetProperty() instanceof IdentifiableJsonSchemaProperty.EncryptedJsonSchemaProperty encrypted) {
774795
if (encrypted.getKeyId() != null) {
775796
if (encrypted.getKeyId() instanceof String stringKey) {
@@ -780,10 +801,21 @@ private List<Document> fromProperties() {
780801
}
781802
}
782803
}
804+
else if (property instanceof IdentifiableJsonSchemaProperty.EncryptedJsonSchemaProperty encrypted) {
805+
if (encrypted.getKeyId() != null) {
806+
if (encrypted.getKeyId() instanceof String stringKey) {
807+
field.append("keyId",
808+
new BsonBinary(BsonBinarySubType.UUID_STANDARD, stringKey.getBytes(StandardCharsets.UTF_8)));
809+
} else {
810+
field.append("keyId", encrypted.getKeyId());
811+
}
812+
}
813+
}
783814

784-
field.append("queries", StreamSupport.stream(property.getCharacteristics().spliterator(), false)
815+
if (property instanceof QueryableJsonSchemaProperty qproperty) {
816+
field.append("queries", StreamSupport.stream(qproperty.getCharacteristics().spliterator(), false)
785817
.map(QueryCharacteristic::toDocument).toList());
786-
818+
}
787819
if (!field.containsKey("keyId")) {
788820
field.append("keyId", BsonNull.VALUE);
789821
}
@@ -812,7 +844,9 @@ private List<Document> fromSchema() {
812844
if (entry.getValue().containsKey("bsonType")) {
813845
field.append("bsonType", entry.getValue().get("bsonType"));
814846
}
815-
field.put("queries", entry.getValue().get("queries"));
847+
if(entry.getValue().containsKey("queries")) {
848+
field.put("queries", entry.getValue().get("queries"));
849+
}
816850
fields.add(field);
817851
}
818852
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/encryption/MongoQueryableEncryptionCollectionCreationTests.java

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@
1515
*/
1616
package org.springframework.data.mongodb.core.encryption;
1717

18-
import static org.springframework.data.mongodb.core.schema.JsonSchemaProperty.*;
19-
import static org.springframework.data.mongodb.core.schema.QueryCharacteristics.*;
20-
import static org.springframework.data.mongodb.test.util.Assertions.*;
18+
import static org.springframework.data.mongodb.core.schema.JsonSchemaProperty.encrypted;
19+
import static org.springframework.data.mongodb.core.schema.JsonSchemaProperty.int32;
20+
import static org.springframework.data.mongodb.core.schema.JsonSchemaProperty.int64;
21+
import static org.springframework.data.mongodb.core.schema.JsonSchemaProperty.queryable;
22+
import static org.springframework.data.mongodb.core.schema.QueryCharacteristics.range;
23+
import static org.springframework.data.mongodb.test.util.Assertions.assertThat;
2124

2225
import java.util.List;
2326
import java.util.UUID;
@@ -31,7 +34,6 @@
3134
import org.junit.jupiter.params.ParameterizedTest;
3235
import org.junit.jupiter.params.provider.Arguments;
3336
import org.junit.jupiter.params.provider.MethodSource;
34-
3537
import org.springframework.beans.factory.annotation.Autowired;
3638
import org.springframework.context.annotation.Configuration;
3739
import org.springframework.data.mongodb.config.AbstractMongoClientConfiguration;
@@ -94,12 +96,16 @@ public void createsCollectionWithEncryptedFieldsCorrectly(CollectionOptions coll
9496
assertThat(encryptedFields).containsKey("fields");
9597

9698
List<Document> fields = encryptedFields.get("fields", List.of());
97-
assertThat(fields.get(0)).containsEntry("path", "encryptedInt") //
99+
assertThat(fields.get(0)).containsEntry("path", "encrypted-but-not-queryable") //
100+
.containsEntry("bsonType", "int") //
101+
.doesNotContainKey("queries");
102+
103+
assertThat(fields.get(1)).containsEntry("path", "encryptedInt") //
98104
.containsEntry("bsonType", "int") //
99105
.containsEntry("queries", List
100106
.of(Document.parse("{'queryType': 'range', 'contention': { '$numberLong' : '1' }, 'min': 5, 'max': 100}")));
101107

102-
assertThat(fields.get(1)).containsEntry("path", "nested.encryptedLong") //
108+
assertThat(fields.get(2)).containsEntry("path", "nested.encryptedLong") //
103109
.containsEntry("bsonType", "long") //
104110
.containsEntry("queries", List.of(Document.parse(
105111
"{'queryType': 'range', 'contention': { '$numberLong' : '0' }, 'min': { '$numberLong' : '-1' }, 'max': { '$numberLong' : '1' }}")));
@@ -109,16 +115,19 @@ private static Stream<Arguments> collectionOptions() {
109115

110116
BsonBinary key1 = new BsonBinary(UUID.randomUUID(), UuidRepresentation.STANDARD);
111117
BsonBinary key2 = new BsonBinary(UUID.randomUUID(), UuidRepresentation.STANDARD);
118+
BsonBinary key3 = new BsonBinary(UUID.randomUUID(), UuidRepresentation.STANDARD);
112119

113120
CollectionOptions manualOptions = CollectionOptions.encryptedCollection(options -> options //
114-
.queryable(encrypted(int32("encryptedInt")).keys(key1), range().min(5).max(100).contention(1)) //
115-
.queryable(encrypted(JsonSchemaProperty.int64("nested.encryptedLong")).keys(key2),
121+
.encrypted(int32("encrypted-but-not-queryable"), key1) //
122+
.queryable(encrypted(int32("encryptedInt")).keyId(key2), range().min(5).max(100).contention(1)) //
123+
.queryable(encrypted(JsonSchemaProperty.int64("nested.encryptedLong")).keyId(key3),
116124
range().min(-1L).max(1L).contention(0)));
117125

118-
CollectionOptions schemaOptions = CollectionOptions.encryptedCollection(MongoJsonSchema.builder()
126+
CollectionOptions schemaOptions = CollectionOptions.encryptedCollection(MongoJsonSchema.builder() //
127+
.property(encrypted(int32("encrypted-but-not-queryable")).keyId(key1)) //
119128
.property(
120-
queryable(encrypted(int32("encryptedInt")).keyId(key1), List.of(range().min(5).max(100).contention(1))))
121-
.property(queryable(encrypted(int64("nested.encryptedLong")).keyId(key2),
129+
queryable(encrypted(int32("encryptedInt")).keyId(key2), List.of(range().min(5).max(100).contention(1))))
130+
.property(queryable(encrypted(int64("nested.encryptedLong")).keyId(key3),
122131
List.of(range().min(-1L).max(1L).contention(0))))
123132
.build());
124133

0 commit comments

Comments
 (0)