Introduce Queryable annotation and add Schema derivation.
This commit decouples queryable encryption from explicit encryption and introduces the Queryable annotation to represent different query types like range and equality.
Additionally it removes value conversion from range encryption and fixes update mapping of range encrypted fields.
Original Pull Request: #4885
@ -38,7 +38,7 @@ public class MongoConversionContext implements ValueConversionContext<MongoPersi
@@ -38,7 +38,7 @@ public class MongoConversionContext implements ValueConversionContext<MongoPersi
@ -53,19 +53,19 @@ public class MongoConversionContext implements ValueConversionContext<MongoPersi
@@ -53,19 +53,19 @@ public class MongoConversionContext implements ValueConversionContext<MongoPersi
@ -78,6 +78,17 @@ public class MongoConversionContext implements ValueConversionContext<MongoPersi
@@ -78,6 +78,17 @@ public class MongoConversionContext implements ValueConversionContext<MongoPersi
@ -101,7 +112,78 @@ public class MongoConversionContext implements ValueConversionContext<MongoPersi
@@ -101,7 +112,78 @@ public class MongoConversionContext implements ValueConversionContext<MongoPersi
@ -169,45 +170,53 @@ public class MongoEncryptionConverter implements EncryptingConverter<Object, Obj
@@ -169,45 +170,53 @@ public class MongoEncryptionConverter implements EncryptingConverter<Object, Obj
if(annotation==null){
thrownewIllegalStateException(String.format("Property %s.%s is not annotated with @Encrypted",
@ -221,6 +230,7 @@ public class MongoEncryptionConverter implements EncryptingConverter<Object, Obj
@@ -221,6 +230,7 @@ public class MongoEncryptionConverter implements EncryptingConverter<Object, Obj
@ -234,27 +244,22 @@ public class MongoEncryptionConverter implements EncryptingConverter<Object, Obj
@@ -234,27 +244,22 @@ public class MongoEncryptionConverter implements EncryptingConverter<Object, Obj
@ -74,6 +80,7 @@ public class MongoClientEncryption implements Encryption<BsonValue, BsonBinary>
@@ -74,6 +80,7 @@ public class MongoClientEncryption implements Encryption<BsonValue, BsonBinary>
@ -82,10 +89,58 @@ public class MongoClientEncryption implements Encryption<BsonValue, BsonBinary>
@@ -82,10 +89,58 @@ public class MongoClientEncryption implements Encryption<BsonValue, BsonBinary>
@ -1036,7 +1036,7 @@ public class IdentifiableJsonSchemaProperty<T extends JsonSchemaObject> implemen
@@ -1036,7 +1036,7 @@ public class IdentifiableJsonSchemaProperty<T extends JsonSchemaObject> implemen
privatefinalJsonSchemaPropertytargetProperty;
privatefinal@NullableStringalgorithm;
privatefinal@NullableStringkeyId;
privatefinal@NullableObjectkeyId;
privatefinal@NullableList<?>keyIds;
/**
@ -1048,7 +1048,7 @@ public class IdentifiableJsonSchemaProperty<T extends JsonSchemaObject> implemen
@@ -1048,7 +1048,7 @@ public class IdentifiableJsonSchemaProperty<T extends JsonSchemaObject> implemen
@ -1068,13 +1068,25 @@ public class IdentifiableJsonSchemaProperty<T extends JsonSchemaObject> implemen
@@ -1068,13 +1068,25 @@ public class IdentifiableJsonSchemaProperty<T extends JsonSchemaObject> implemen
@ -1083,7 +1095,7 @@ public class IdentifiableJsonSchemaProperty<T extends JsonSchemaObject> implemen
@@ -1083,7 +1095,7 @@ public class IdentifiableJsonSchemaProperty<T extends JsonSchemaObject> implemen
@ -1103,6 +1115,15 @@ public class IdentifiableJsonSchemaProperty<T extends JsonSchemaObject> implemen
@@ -1103,6 +1115,15 @@ public class IdentifiableJsonSchemaProperty<T extends JsonSchemaObject> implemen
@ -1171,5 +1192,71 @@ public class IdentifiableJsonSchemaProperty<T extends JsonSchemaObject> implemen
@@ -1171,5 +1192,71 @@ public class IdentifiableJsonSchemaProperty<T extends JsonSchemaObject> implemen
Client Side Encryption is a feature that encrypts data in your application before it is sent to MongoDB.
We recommend you get familiar with the concepts, ideally from the https://www.mongodb.com/docs/manual/core/csfle/[MongoDB Documentation] to learn more about its capabilities and restrictions before you continue applying Encryption through Spring Data.
We recommend you get familiar with the concepts, ideally from the https://www.mongodb.com/docs/manual/core/security-in-use-encryption/[MongoDB Documentation] to learn more about its capabilities and restrictions before you continue applying Encryption through Spring Data.
[NOTE]
====
@ -11,8 +11,13 @@ MongoDB does not support encryption for all field types.
@@ -11,8 +11,13 @@ MongoDB does not support encryption for all field types.
Specific data types require deterministic encryption to preserve equality comparison functionality.
====
== Client Side Field Level Encryption (CSFLE)
Choosing CSFLE gives you full flexibility and allows you to use different keys for a single field, eg. in a one key per tenant scenario. +
Please make sure to consult the https://www.mongodb.com/docs/manual/core/csfle/[MongoDB CSFLE Documentation] before you continue reading.
[[mongo.encryption.automatic]]
== Automatic Encryption
=== Automatic Encryption (CSFLE)
MongoDB supports https://www.mongodb.com/docs/manual/core/csfle/[Client-Side Field Level Encryption] out of the box using the MongoDB driver with its Automatic Encryption feature.
Automatic Encryption requires a xref:mongodb/mapping/mapping-schema.adoc[JSON Schema] that allows to perform encrypted read and write operations without the need to provide an explicit en-/decryption step.
Explicit encryption uses the MongoDB driver's encryption library (`org.mongodb:mongodb-crypt`) to perform encryption and decryption tasks.
The `@ExplicitEncrypted` annotation is a combination of the `@Encrypted` annotation used for xref:mongodb/mapping/mapping-schema.adoc#mongo.jsonSchema.encrypted-fields[JSON Schema creation] and a xref:mongodb/mapping/property-converters.adoc[Property Converter].
@ -114,8 +119,147 @@ By default, the `@ExplicitEncrypted(value=…)` attribute references a `MongoEnc
@@ -114,8 +119,147 @@ By default, the `@ExplicitEncrypted(value=…)` attribute references a `MongoEnc
It is possible to change the default implementation and exchange it with any `PropertyValueConverter` implementation by providing the according type reference.
To learn more about custom `PropertyValueConverters` and the required configuration, please refer to the xref:mongodb/mapping/property-converters.adoc[Property Converters - Mapping specific fields] section.
[[mongo.encryption.queryable]]
== Queryable Encryption (QE)
Choosing QE enables you to run different types of queries, like _range_ or _equality_, against encrypted fields. +
Please make sure to consult the https://www.mongodb.com/docs/manual/core/queryable-encryption/[MongoDB QE Documentation] before you continue reading to learn more about QE features and limitations.
=== Collection Setup
Queryable Encryption requires upfront declaration of certain aspects allowed within an actual query against an encrypted field.
The information covers the algorithm in use as well as allowed query types along with their attributes and must be provided when creating the collection.
`MongoOperations#createCollection(...)` can be used to do the initial setup for collections utilizing QE.
The configuration for QE via Spring Data uses the same building blocks (a xref:mongodb/mapping/mapping-schema.adoc#mongo.jsonSchema.encrypted-fields[JSON Schema creation]) as CSFLE, converting the schema/properties into the configuration format required by MongoDB.
<1> Using the template to create the collection may prevent capturing generated keyIds. In this case render the `Document` from the options and use the `createEncryptedCollection(...)` method via the encryption library.
<1> Using the template to create the collection may prevent capturing generated keyIds. In this case render the `Document` from the options and use the `createEncryptedCollection(...)` method via the encryption library.
The `Queryable` annotation allows to define allowed query types for encrypted fields.
`@RangeEncrypted` is a combination of `@Encrypted` and `@Queryable` for fields allowing `range` queries.
It is possible to create custom annotations out of the provided ones.
- It is not possible to use both QE and CSFLE within the same collection.
- It is not possible to query a `range` indexed field with an `equality` operator.
- It is not possible to query an `equality` indexed field with a `range` operator.
- It is not possible to set `bypassAutoEncrytion(true)`.
- It is not possible to use self maintained encryption keys via `@Encrypted` in combination with Queryable Encryption.
- Contention is only optional on the server side, the clients requires you to set the value (Default us `8`).
- Additional options for eg. `min` and `max` need to match the actual field type. Make sure to use `$numberLong` etc. to ensure target types when parsing bson String.
- Queryable Encryption will an extra field `__safeContent__` to each of your documents.
Unless explicitly excluded the field will be loaded into memory when retrieving results.
====
[[mongo.encryption.queryable.automatic]]
=== Automatic Encryption (QE)
MongoDB supports Queryable Encryption out of the box using the MongoDB driver with its Automatic Encryption feature.
Automatic Encryption requires a xref:mongodb/mapping/mapping-schema.adoc[JSON Schema] that allows to perform encrypted read and write operations without the need to provide an explicit en-/decryption step.
All you need to do is create the collection according to the MongoDB documentation.
You may utilize techniques to create the required configuration outlined in the section above.
[[mongo.encryption.queryable.manual]]
=== Explicit Encryption (QE)
Explicit encryption uses the MongoDB driver's encryption library (`org.mongodb:mongodb-crypt`) to perform encryption and decryption tasks based on the meta information provided by annotation within the domain model.
[NOTE]
====
There is no official support for using Explicit Queryable Encryption.
The audacious user may combine `@Encyrpted` and `@Queryable` with `@ValueConverter(MongoEncryptionConverter.class)` at their own risk.
====
[[mongo.encryption.explicit-setup]]
=== MongoEncryptionConverter Setup
[[mongo.encryption.converter-setup]]
== MongoEncryptionConverter Setup
The converter setup for `MongoEncryptionConverter` requires a few steps as several components are involved.
The bean setup consists of the following:
@ -124,7 +268,6 @@ The bean setup consists of the following:
@@ -124,7 +268,6 @@ The bean setup consists of the following:
2. A `MongoEncryptionConverter` instance configured with `ClientEncryption` and a `EncryptionKeyResolver`.
3. A `PropertyValueConverterFactory` that uses the registered `MongoEncryptionConverter` bean.
A side effect of using annotated key resolution is that the `@ExplicitEncrypted` annotation does not need to specify an alt key name.
The `EncryptionKeyResolver` uses an `EncryptionContext` providing access to the property allowing for dynamic DEK resolution.