Skip to content

Commit fd166ed

Browse files
committed
Added alt key name support
JAVA-3335
1 parent 809e7b9 commit fd166ed

File tree

4 files changed

+45
-19
lines changed

4 files changed

+45
-19
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ ext {
4242
nettyVersion = '4.1.17.Final'
4343
snappyVersion = '1.1.4'
4444
zstdVersion = '1.3.8-3'
45-
mongoCryptVersion = '1.0.0-beta2'
45+
mongoCryptVersion = '1.0.0-beta3'
4646
gitVersion = getGitVersion()
4747
}
4848

driver-core/src/main/com/mongodb/client/model/vault/EncryptOptions.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package com.mongodb.client.model.vault;
1818

1919
import org.bson.BsonBinary;
20-
import org.bson.BsonValue;
2120

2221
/**
2322
* The options for explicit encryption.
@@ -26,7 +25,7 @@
2625
*/
2726
public class EncryptOptions {
2827
private BsonBinary keyId;
29-
private BsonValue keyAltName;
28+
private String keyAltName;
3029
private final String algorithm;
3130

3231
/**
@@ -71,7 +70,7 @@ public BsonBinary getKeyId() {
7170
*
7271
* @return the alternate name
7372
*/
74-
public BsonValue getKeyAltName() {
73+
public String getKeyAltName() {
7574
return keyAltName;
7675
}
7776

@@ -94,7 +93,7 @@ public EncryptOptions keyId(final BsonBinary keyId) {
9493
* @return this
9594
* @see #getKeyAltName()
9695
*/
97-
public EncryptOptions keyAltName(final BsonValue keyAltName) {
96+
public EncryptOptions keyAltName(final String keyAltName) {
9897
this.keyAltName = keyAltName;
9998
return this;
10099
}

driver-sync/src/main/com/mongodb/client/internal/Crypt.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,19 @@ BsonBinary encryptExplicitly(final BsonValue value, final EncryptOptions options
173173
notNull("options", options);
174174

175175
try {
176-
MongoCryptContext encryptionContext = mongoCrypt.createExplicitEncryptionContext(
177-
new BsonDocument("v", value), MongoExplicitEncryptOptions.builder()
178-
.keyId(options.getKeyId())
179-
.algorithm(options.getAlgorithm())
180-
.build());
176+
MongoExplicitEncryptOptions.Builder encryptOptionsBuilder = MongoExplicitEncryptOptions.builder()
177+
.algorithm(options.getAlgorithm());
178+
179+
if (options.getKeyId() != null) {
180+
encryptOptionsBuilder.keyId(options.getKeyId());
181+
}
181182

183+
if (options.getKeyAltName() != null) {
184+
encryptOptionsBuilder.keyAltName(options.getKeyAltName());
185+
}
186+
187+
MongoCryptContext encryptionContext = mongoCrypt.createExplicitEncryptionContext(
188+
new BsonDocument("v", value), encryptOptionsBuilder.build());
182189
try {
183190
return executeStateMachine(encryptionContext, null).getBinary("v");
184191
} finally {

driver-sync/src/test/functional/com/mongodb/client/ClientSideEncryptionProseTestSpecification.groovy

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ class ClientSideEncryptionProseTestSpecification extends FunctionalSpecification
8585
"creationDate": { "$date": { "$numberLong": "1232739599082000" } },
8686
"updateDate": { "$date": { "$numberLong": "1232739599082000" } },
8787
"status": { "$numberInt": "0" },
88-
"masterKey": { "provider": "local" }
88+
"masterKey": { "provider": "local" },
89+
"keyAltNames": [ "altname1", "altname2" ]
8990
}
9091
''')
9192

@@ -213,18 +214,26 @@ class ClientSideEncryptionProseTestSpecification extends FunctionalSpecification
213214
def value = new BsonString('hello')
214215

215216
when:
216-
def encryptedValue = keyVault.encrypt(value, new EncryptOptions('AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic')
217-
.keyId(localDataKeyDocument.getBinary('_id')))
217+
def options = new EncryptOptions('AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic')
218+
if (useKeyId) {
219+
options.keyId(localDataKeyDocument.getBinary('_id'))
220+
} else {
221+
options.keyAltName('altname1')
222+
}
223+
224+
def encryptedValue = keyVault.encrypt(value, options)
218225

219226
then:
220-
encryptedValue == new BsonBinary((byte) 6,
221-
Base64.decoder.decode('AWFhYWFhYWFhYWFhYWFhYWEC7ubnsHvOUXvbE4406+XawIhcl+fsvNWO7moBSY7ABkPuCTzsitrqWWp1FbaaT05muIESiB8daggJPgwarTQ3cQ=='))
227+
encryptedValue.type == 6 as byte
222228

223229
when:
224230
def decryptedValue = keyVault.decrypt(encryptedValue)
225231

226232
then:
227233
decryptedValue == value
234+
235+
where:
236+
useKeyId << [true, false]
228237
}
229238

230239
def 'should explicitly encrypt and decrypt with aws provider'() {
@@ -238,18 +247,29 @@ class ClientSideEncryptionProseTestSpecification extends FunctionalSpecification
238247
def value = new BsonString('hello')
239248

240249
when:
241-
def encryptedValue = keyVault.encrypt(value, new EncryptOptions('AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic')
242-
.keyId(awsDataKeyDocument.getBinary('_id')))
250+
def options = new EncryptOptions('AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic')
251+
if (useKeyId) {
252+
options.keyId(awsDataKeyDocument.getBinary('_id'))
253+
} else {
254+
options.keyAltName('altname1')
255+
}
256+
257+
def encryptedValue = keyVault.encrypt(value, options)
258+
259+
then:
260+
encryptedValue.type == 6 as byte
243261

244262
then:
245-
encryptedValue == new BsonBinary((byte) 6,
246-
Base64.decoder.decode('AQAAAAAAAAAAAAAAAAAAAAACN0NwWlfe6YPGDEw+ObxEzbEtk45ewF3sIH2Oj7F0xd3GYoxCGCIp9gg0Q1uHTwdVWwG58SFhJyo4305IVoikEQ=='))
263+
encryptedValue.type == 6 as byte
247264

248265
when:
249266
def decryptedValue = keyVault.decrypt(encryptedValue)
250267

251268
then:
252269
decryptedValue == value
270+
271+
where:
272+
useKeyId << [true, false]
253273
}
254274

255275
/*

0 commit comments

Comments
 (0)