Browse Source

alright - so far so good - now we only need a ton of tests

issue/4185-light
Christoph Strobl 9 months ago
parent
commit
43ca5e813a
No known key found for this signature in database
GPG Key ID: E6054036D0C37A4B
  1. 12
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/CollectionOptions.java
  2. 1
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MappingMongoJsonSchemaCreator.java
  3. 9
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/schema/IdentifiableJsonSchemaProperty.java
  4. 58
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MappingMongoJsonSchemaCreatorUnitTests.java
  5. 7
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/encryption/RangeEncryptionTests.java

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

@ -697,13 +697,7 @@ public class CollectionOptions {
if (entry.getValue().containsKey("bsonType")) { if (entry.getValue().containsKey("bsonType")) {
field.append("bsonType", entry.getValue().get("bsonType")); field.append("bsonType", entry.getValue().get("bsonType"));
} }
Document query = new Document("queryType", entry.getValue().get("algorithm", "range").toLowerCase()); field.put("queries", entry.getValue().get("queries"));
query.append("contention", entry.getValue().get("contention"));
query.append("trimFactor", entry.getValue().get("trimFactor"));
query.append("sparsity", entry.getValue().get("sparsity"));
query.append("min", entry.getValue().get("min"));
query.append("max", entry.getValue().get("max"));
field.append("queries", List.of(query));
fields.add(field); fields.add(field);
} }
} }
@ -727,9 +721,9 @@ public class CollectionOptions {
String path = currentPath == null ? entry.getKey() : (currentPath + "." + entry.getKey()); String path = currentPath == null ? entry.getKey() : (currentPath + "." + entry.getKey());
if (nested.containsKey("encrypt")) { if (nested.containsKey("encrypt")) {
Document target = new Document(nested.get("encrypt", Document.class)); Document target = new Document(nested.get("encrypt", Document.class));
if(nested.containsKey("queries")) { if (nested.containsKey("queries")) {
List<?> queries = nested.get("queries", List.class); List<?> queries = nested.get("queries", List.class);
if(!queries.isEmpty() && queries.iterator().next() instanceof Document qd) { if (!queries.isEmpty() && queries.iterator().next() instanceof Document qd) {
target.putAll(qd); target.putAll(qd);
} }
} }

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

@ -312,6 +312,7 @@ class MappingMongoJsonSchemaCreator implements MongoJsonSchemaCreator {
if (!rangeEncrypted.rangeOptions().isEmpty()) { if (!rangeEncrypted.rangeOptions().isEmpty()) {
options.putAll(Document.parse(rangeEncrypted.rangeOptions())); options.putAll(Document.parse(rangeEncrypted.rangeOptions()));
} }
options.put("contention", rangeEncrypted.contentionFactor());
return options; return options;
} }
}; };

9
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/schema/IdentifiableJsonSchemaProperty.java

@ -96,9 +96,12 @@ public class IdentifiableJsonSchemaProperty<T extends JsonSchemaObject> implemen
Document doc = targetProperty.toDocument(); Document doc = targetProperty.toDocument();
Document propertySpecification = doc.get(targetProperty.getIdentifier(), Document.class); Document propertySpecification = doc.get(targetProperty.getIdentifier(), Document.class);
List<Document> queries = characteristics.getCharacteristics().stream().map(QueryCharacteristic::toDocument) if (propertySpecification.containsKey("encrypt")) {
.toList(); Document encrypt = propertySpecification.get("encrypt", Document.class);
propertySpecification.append("queries", queries); List<Document> queries = characteristics.getCharacteristics().stream().map(QueryCharacteristic::toDocument)
.toList();
encrypt.append("queries", queries);
}
return doc; return doc;
} }

58
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MappingMongoJsonSchemaCreatorUnitTests.java

@ -15,7 +15,8 @@
*/ */
package org.springframework.data.mongodb.core; package org.springframework.data.mongodb.core;
import static org.springframework.data.mongodb.test.util.Assertions.*; import static org.springframework.data.mongodb.test.util.Assertions.assertThat;
import static org.springframework.data.mongodb.test.util.Assertions.assertThatExceptionOfType;
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
@ -38,6 +39,7 @@ import org.springframework.data.mongodb.core.mapping.Field;
import org.springframework.data.mongodb.core.mapping.FieldType; import org.springframework.data.mongodb.core.mapping.FieldType;
import org.springframework.data.mongodb.core.mapping.MongoId; import org.springframework.data.mongodb.core.mapping.MongoId;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext; import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import org.springframework.data.mongodb.core.mapping.RangeEncrypted;
import org.springframework.data.mongodb.core.schema.JsonSchemaObject.Type; import org.springframework.data.mongodb.core.schema.JsonSchemaObject.Type;
import org.springframework.data.mongodb.core.schema.JsonSchemaProperty; import org.springframework.data.mongodb.core.schema.JsonSchemaProperty;
import org.springframework.data.mongodb.core.schema.MongoJsonSchema; import org.springframework.data.mongodb.core.schema.MongoJsonSchema;
@ -282,6 +284,38 @@ class MappingMongoJsonSchemaCreatorUnitTests {
.containsEntry("properties.domainTypeValue", Document.parse("{'encrypt': {'bsonType': 'object' } }")); .containsEntry("properties.domainTypeValue", Document.parse("{'encrypt': {'bsonType': 'object' } }"));
} }
@Test // GH-4454
void qeRangeEncryptedProperties() {
MongoJsonSchema schema = MongoJsonSchemaCreator.create() //
.filter(MongoJsonSchemaCreator.encryptedOnly()) // filter non encrypted fields
.createSchemaFor(RangeEncryptedRoot.class);
String expectedForInt = """
{ 'encrypt' : {
'algorithm' : 'Range',
'bsonType' : 'int',
'queries' : [
{ 'contention' : { '$numberLong' : '0' }, 'max' : 200, 'min' : 0, 'queryType' : 'range', 'sparsity' : 1, 'trimFactor' : 1 }
]
}}""";
String expectedForLong = """
{ 'encrypt' : {
'algorithm' : 'Range',
'bsonType' : 'long',
'queries' : [
{ contention : { '$numberLong' : '1' }, 'max' : 1, 'min' : -1, 'queryType' : 'range', 'sparsity' : 1, 'trimFactor' : 1 }
]
}}""";
assertThat(schema.schemaDocument()) //
.doesNotContainKey("properties.unencrypted") //
.containsEntry("properties.encryptedInt", Document.parse(expectedForInt))
.containsEntry("properties.nested.properties.encrypted_long", Document.parse(expectedForLong));
}
// --> TYPES AND JSON // --> TYPES AND JSON
// --> ENUM // --> ENUM
@ -311,7 +345,8 @@ class MappingMongoJsonSchemaCreatorUnitTests {
" 'binaryDataProperty' : { 'bsonType' : 'binData' }," + // " 'binaryDataProperty' : { 'bsonType' : 'binData' }," + //
" 'collectionProperty' : { 'type' : 'array' }," + // " 'collectionProperty' : { 'type' : 'array' }," + //
" 'simpleTypeCollectionProperty' : { 'type' : 'array', 'items' : { 'type' : 'string' } }," + // " 'simpleTypeCollectionProperty' : { 'type' : 'array', 'items' : { 'type' : 'string' } }," + //
" 'complexTypeCollectionProperty' : { 'type' : 'array', 'items' : { 'type' : 'object', 'properties' : { 'field' : { 'type' : 'string'} } } }" + // " 'complexTypeCollectionProperty' : { 'type' : 'array', 'items' : { 'type' : 'object', 'properties' : { 'field' : { 'type' : 'string'} } } }"
+ //
" 'enumTypeCollectionProperty' : { 'type' : 'array', 'items' : " + JUST_SOME_ENUM + " }" + // " 'enumTypeCollectionProperty' : { 'type' : 'array', 'items' : " + JUST_SOME_ENUM + " }" + //
" 'mapProperty' : { 'type' : 'object' }," + // " 'mapProperty' : { 'type' : 'object' }," + //
" 'objectProperty' : { 'type' : 'object' }," + // " 'objectProperty' : { 'type' : 'object' }," + //
@ -692,4 +727,23 @@ class MappingMongoJsonSchemaCreatorUnitTests {
static class WithEncryptedEntityLikeProperty { static class WithEncryptedEntityLikeProperty {
@Encrypted SomeDomainType domainTypeValue; @Encrypted SomeDomainType domainTypeValue;
} }
static class RangeEncryptedRoot {
String unencrypted;
@RangeEncrypted(contentionFactor = 0L, rangeOptions = "{ 'min': 0, 'max': 200, 'trimFactor': 1, 'sparsity': 1}") //
Integer encryptedInt;
NestedRangeEncrypted nested;
}
static class NestedRangeEncrypted {
@Field("encrypted_long")
@RangeEncrypted(contentionFactor = 1L, rangeOptions = "{ 'min': -1, 'max': 1, 'trimFactor': 1, 'sparsity': 1}") //
Long encryptedLong;
}
} }

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

@ -119,10 +119,12 @@ class RangeEncryptionTests {
Document result = template.execute(Person.class, col -> { Document result = template.execute(Person.class, col -> {
BsonDocument filterSource = new BsonDocument("encryptedInt", new BsonDocument("$gte", new BsonInt32(100)));
BsonDocument filter = clientEncryption.getClientEncryption().encryptExpression( BsonDocument filter = clientEncryption.getClientEncryption().encryptExpression(
new Document("$and", List.of(new BsonDocument("encryptedInt", new BsonDocument("$gte", new BsonInt32(100))))), new Document("$and", List.of(filterSource)),
encryptExpressionOptions); encryptExpressionOptions);
Document first = col.find(filter).first(); Document first = col.find(filter).first();
// Document first = col.find(filterSource).first();
System.out.println("first.toJson(): " + first.toJson()); System.out.println("first.toJson(): " + first.toJson());
return first; return first;
}); });
@ -290,7 +292,8 @@ class RangeEncryptionTests {
builder.autoEncryptionSettings(AutoEncryptionSettings.builder() // builder.autoEncryptionSettings(AutoEncryptionSettings.builder() //
.kmsProviders(clientEncryptionSettings.getKmsProviders()) // .kmsProviders(clientEncryptionSettings.getKmsProviders()) //
.keyVaultNamespace(clientEncryptionSettings.getKeyVaultNamespace()) // .keyVaultNamespace(clientEncryptionSettings.getKeyVaultNamespace()) //
.bypassQueryAnalysis(true).build()); .bypassQueryAnalysis(true)
.build());
} }
} }

Loading…
Cancel
Save