Skip to content

Add support for explicit field encryption (sync client) #4302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 24 commits into from

Conversation

christophstrobl
Copy link
Member

@christophstrobl christophstrobl commented Feb 22, 2023

Closes: #4284

Copy link
Member

@mp911de mp911de left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a few comments from an initial review. I like the direction these changes took.

@Target(ElementType.FIELD)
@Encrypted
@ValueConverter
public @interface ExplicitlyEncrypted {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe ExplicitEncrypted and having a reference in the documentation above that points/explains the difference to automatic encryption.

* @author Christoph Strobl
* @since 4.1
*/
public interface EncryptingConverter<S, T> extends MongoValueConverter<S, T> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder whether we should split the encryption package and move the Converter-related bits into convert.encryption

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

valid point, we need to untangle bits here to avoid cycle between the convert and encrypt package. Basically we need to make sure EncryptionContext is not a ValueConversionContext any longer

if (BsonBinarySubType.isUuid(value.getType())) {
String representation = value.asUuid().toString();
if (representation.length() > 6) {
return String.format("KeyId('%s***')", representation.substring(0, 6));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have the feeling that we should avoid rendering the key, even subparts in toString. We could rather replace each character with a * and retain the dashes if we wanted to.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had looked at EncryptionKey and KeyImpl from javax.security.auth.kerberos which do render the bits right away. DESKey does not. P11Key tells you the type and length. So I'm not sure. Maybe @rwinch has an opinion.

if (value().length() <= 3) {
return "AltKeyName('***')";
}
return String.format("AltKeyName('%s***')", value.substring(0, 3));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same topic about rendering the key name like KeyId.toString()

/**
* @return {@literal true} if refreshed, {@literal false} otherwise.
*/
public boolean refresh() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering whether we should rather show an Lazy example for singleton usage and always call Supplier.get().

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had thought about using Lazy but decided against it to have more control over the refresh. Also calling the supplier may lead to unnecessary creation of ClientEncryption instances that all hold connections to the database.

@christophstrobl christophstrobl changed the title Add support for explicit field encryption. Add support for explicit field encryption (sync client). Mar 8, 2023
@christophstrobl christophstrobl marked this pull request as ready for review March 8, 2023 08:16
christophstrobl and others added 2 commits March 10, 2023 08:11
Remove caching variant of MongoClientEncryption. Rename types for consistent key alt name scheme. Rename annotation to ExplicitEncrypted.

Add package-info. Improve documentation wording. Reduce visibility of KeyId and KeyAltName to package-private.
@mp911de mp911de added the type: enhancement A general enhancement label Mar 14, 2023
@mp911de mp911de added this to the 4.1 M3 (2023.0.0) milestone Mar 14, 2023
@mp911de mp911de changed the title Add support for explicit field encryption (sync client). Add support for explicit field encryption (sync client) Mar 15, 2023
mp911de pushed a commit that referenced this pull request Mar 15, 2023
We now support explicit field encryption using mapped entities through the `@ExplicitEncrypted` annotation.

class Person {
  ObjectId id;

  @ExplicitEncrypted(algorithm = AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic, altKeyName = "my-secret-key")
  String socialSecurityNumber;
}

Encryption is applied transparently to all mapped entities leveraging the existing converter infrastructure.

Original pull request: #4302
Closes: #4284
mp911de added a commit that referenced this pull request Mar 15, 2023
Remove caching variant of MongoClientEncryption. Rename types for consistent key alt name scheme. Rename annotation to ExplicitEncrypted.

Add package-info. Improve documentation wording. Reduce visibility of KeyId and KeyAltName to package-private.

Original pull request: #4302
See: #4284
@mp911de
Copy link
Member

mp911de commented Mar 15, 2023

That's merged and polished now.

@mp911de mp911de closed this Mar 15, 2023
@mp911de mp911de deleted the issue/4284 branch March 15, 2023 09:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support property-based explicit field encryption
2 participants