Skip to content

Commit a3861b6

Browse files
christophstroblmp911de
authored andcommitted
Avoid schema keyId uuid representation errors.
To avoid driver configuration specific UUID representation format errors (binary subtype 3 vs. subtype 4) we now directly convert the given key into its subtype 4 format. Resolves: #3929 Original pull request: #3931.
1 parent ee41609 commit a3861b6

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/util/encryption/EncryptionUtils.java

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021 the original author or authors.
2+
* Copyright 2021-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,11 +18,15 @@
1818
import java.util.UUID;
1919
import java.util.function.Supplier;
2020

21+
import org.bson.BsonBinary;
22+
import org.bson.BsonBinarySubType;
23+
import org.bson.types.Binary;
2124
import org.springframework.data.mongodb.util.spel.ExpressionUtils;
2225
import org.springframework.expression.EvaluationContext;
2326
import org.springframework.expression.Expression;
2427
import org.springframework.lang.Nullable;
2528
import org.springframework.util.Assert;
29+
import org.springframework.util.Base64Utils;
2630

2731
/**
2832
* Internal utility class for dealing with encryption related matters.
@@ -35,8 +39,8 @@ public final class EncryptionUtils {
3539
/**
3640
* Resolve a given plain {@link String} value into the store native {@literal keyId} format, considering potential
3741
* {@link Expression expressions}. <br />
38-
* The potential keyId is probed against an {@link UUID#fromString(String) UUID value} and the {@literal base64}
39-
* encoded {@code $binary} representation.
42+
* The potential keyId is probed against an {@link UUID#fromString(String) UUID value} or decoded from the
43+
* {@literal base64} representation prior to conversion into its {@link Binary} format.
4044
*
4145
* @param value the source value to resolve the keyId for. Must not be {@literal null}.
4246
* @param evaluationContext a {@link Supplier} used to provide the {@link EvaluationContext} in case an
@@ -57,11 +61,13 @@ public static Object resolveKeyId(String value, Supplier<EvaluationContext> eval
5761
return potentialKeyId;
5862
}
5963
}
64+
6065
try {
61-
return UUID.fromString(potentialKeyId.toString());
66+
return new Binary(BsonBinarySubType.UUID_STANDARD,
67+
new BsonBinary(UUID.fromString(potentialKeyId.toString())).getData());
6268
} catch (IllegalArgumentException e) {
63-
return org.bson.Document.parse("{ val : { $binary : { base64 : '" + potentialKeyId + "', subType : '04'} } }")
64-
.get("val");
69+
70+
return new Binary(BsonBinarySubType.UUID_STANDARD, Base64Utils.decodeFromString(potentialKeyId.toString()));
6571
}
6672
}
6773
}

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

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2021 the original author or authors.
2+
* Copyright 2019-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
2323
import java.util.List;
2424
import java.util.Map;
2525

26+
import org.bson.BsonDocument;
2627
import org.bson.Document;
2728
import org.junit.jupiter.api.BeforeEach;
2829
import org.junit.jupiter.api.Test;
@@ -107,7 +108,7 @@ public void converterRegistered() {
107108
.createSchemaFor(Patient.class);
108109

109110
Document targetSchema = schema.schemaDocument();
110-
assertThat(targetSchema).isEqualTo(Document.parse(PATIENT));
111+
assertThat(targetSchema.toBsonDocument()).isEqualTo(BsonDocument.parse(PATIENT));
111112
}
112113

113114
@Test // GH-3800
@@ -136,7 +137,7 @@ public void csfleWithKeyFromProperties() {
136137
.filter(MongoJsonSchemaCreator.encryptedOnly()) //
137138
.createSchemaFor(EncryptionMetadataFromProperty.class);
138139

139-
assertThat(schema.schemaDocument()).isEqualTo(Document.parse(ENC_FROM_PROPERTY_SCHEMA));
140+
assertThat(schema.schemaDocument().toBsonDocument()).isEqualTo(BsonDocument.parse(ENC_FROM_PROPERTY_SCHEMA));
140141
}
141142

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

157-
assertThat(schema.schemaDocument()).isEqualTo(Document.parse(ENC_FROM_METHOD_SCHEMA));
158+
assertThat(schema.schemaDocument().toBsonDocument()).isEqualTo(BsonDocument.parse(ENC_FROM_METHOD_SCHEMA));
158159
}
159160

160161
// --> TYPES AND JSON
@@ -392,7 +393,7 @@ static class Insurance {
392393
}
393394

394395
static final String ENC_FROM_PROPERTY_ENTITY_KEY = "C5a5aMB7Ttq4wSJTFeRn8g==";
395-
static final String ENC_FROM_PROPERTY_PROPOERTY_KEY = "Mw6mdTVPQfm4quqSCLVB3g=";
396+
static final String ENC_FROM_PROPERTY_PROPOERTY_KEY = "Mw6mdTVPQfm4quqSCLVB3g==";
396397
static final String ENC_FROM_PROPERTY_SCHEMA = "{" + //
397398
" 'encryptMetadata': {" + //
398399
" 'keyId': [" + //

0 commit comments

Comments
 (0)