Skip to content

Avoid schema keyId uuid representation errors. #3931

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 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.0-GH-3929-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-benchmarks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.0-GH-3929-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.0-GH-3929-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.0-GH-3929-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,15 @@
import java.util.UUID;
import java.util.function.Supplier;

import org.bson.BsonBinary;
import org.bson.BsonBinarySubType;
import org.bson.types.Binary;
import org.springframework.data.mongodb.util.spel.ExpressionUtils;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.Base64Utils;

/**
* Internal utility class for dealing with encryption related matters.
Expand All @@ -35,8 +39,8 @@ public final class EncryptionUtils {
/**
* Resolve a given plain {@link String} value into the store native {@literal keyId} format, considering potential
* {@link Expression expressions}. <br />
* The potential keyId is probed against an {@link UUID#fromString(String) UUID value} and the {@literal base64}
* encoded {@code $binary} representation.
* The potential keyId is probed against an {@link UUID#fromString(String) UUID value} or decoded from the
* {@literal base64} representation prior to conversion into its {@link Binary} format.
*
* @param value the source value to resolve the keyId for. Must not be {@literal null}.
* @param evaluationContext a {@link Supplier} used to provide the {@link EvaluationContext} in case an
Expand All @@ -57,11 +61,13 @@ public static Object resolveKeyId(String value, Supplier<EvaluationContext> eval
return potentialKeyId;
}
}

try {
return UUID.fromString(potentialKeyId.toString());
return new Binary(BsonBinarySubType.UUID_STANDARD,
new BsonBinary(UUID.fromString(potentialKeyId.toString())).getData());
} catch (IllegalArgumentException e) {
return org.bson.Document.parse("{ val : { $binary : { base64 : '" + potentialKeyId + "', subType : '04'} } }")
.get("val");

return new Binary(BsonBinarySubType.UUID_STANDARD, Base64Utils.decodeFromString(potentialKeyId.toString()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.List;
import java.util.Map;

import org.bson.BsonDocument;
import org.bson.Document;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -107,7 +108,7 @@ public void converterRegistered() {
.createSchemaFor(Patient.class);

Document targetSchema = schema.schemaDocument();
assertThat(targetSchema).isEqualTo(Document.parse(PATIENT));
assertThat(targetSchema.toBsonDocument()).isEqualTo(BsonDocument.parse(PATIENT));
}

@Test // GH-3800
Expand Down Expand Up @@ -136,7 +137,7 @@ public void csfleWithKeyFromProperties() {
.filter(MongoJsonSchemaCreator.encryptedOnly()) //
.createSchemaFor(EncryptionMetadataFromProperty.class);

assertThat(schema.schemaDocument()).isEqualTo(Document.parse(ENC_FROM_PROPERTY_SCHEMA));
assertThat(schema.schemaDocument().toBsonDocument()).isEqualTo(BsonDocument.parse(ENC_FROM_PROPERTY_SCHEMA));
}

@Test // GH-3800
Expand All @@ -154,7 +155,7 @@ public void csfleWithKeyFromMethod() {
.filter(MongoJsonSchemaCreator.encryptedOnly()) //
.createSchemaFor(EncryptionMetadataFromMethod.class);

assertThat(schema.schemaDocument()).isEqualTo(Document.parse(ENC_FROM_METHOD_SCHEMA));
assertThat(schema.schemaDocument().toBsonDocument()).isEqualTo(BsonDocument.parse(ENC_FROM_METHOD_SCHEMA));
}

// --> TYPES AND JSON
Expand Down Expand Up @@ -392,7 +393,7 @@ static class Insurance {
}

static final String ENC_FROM_PROPERTY_ENTITY_KEY = "C5a5aMB7Ttq4wSJTFeRn8g==";
static final String ENC_FROM_PROPERTY_PROPOERTY_KEY = "Mw6mdTVPQfm4quqSCLVB3g=";
static final String ENC_FROM_PROPERTY_PROPOERTY_KEY = "Mw6mdTVPQfm4quqSCLVB3g==";
static final String ENC_FROM_PROPERTY_SCHEMA = "{" + //
" 'encryptMetadata': {" + //
" 'keyId': [" + //
Expand Down