Skip to content

Commit 130e54f

Browse files
committed
Added support for the Key Management API
JAVA-4433
1 parent ac5dde8 commit 130e54f

File tree

51 files changed

+2483
-180
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+2483
-180
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ ext {
4848
nettyTcnativeBoringsslVersion = '2.0.48.Final'
4949
snappyVersion = '1.1.8.4'
5050
zstdVersion = '1.5.0-4'
51-
mongoCryptVersion = '1.5.0-rc2'
51+
mongoCryptVersion = '1.5.1.1'
5252
projectReactorVersion = 'Californium-SR23'
5353
junitBomVersion = '5.8.1'
5454
gitVersion = getGitVersion()

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616

1717
package com.mongodb.client.model.vault;
1818

19+
import com.mongodb.lang.Nullable;
1920
import org.bson.BsonDocument;
2021

22+
import java.util.Arrays;
2123
import java.util.List;
2224

2325
/**
@@ -28,6 +30,7 @@
2830
public class DataKeyOptions {
2931
private List<String> keyAltNames;
3032
private BsonDocument masterKey;
33+
private byte[] keyMaterial;
3134

3235
/**
3336
* Set the alternate key names.
@@ -53,6 +56,23 @@ public DataKeyOptions masterKey(final BsonDocument masterKey) {
5356
return this;
5457
}
5558

59+
/**
60+
* Sets the key material
61+
*
62+
* <p>An optional BinData of 96 bytes to use as custom key material for the data key being created.
63+
* If set the custom key material is used for encrypting and decrypting data. Otherwise, the key material for the new data key is
64+
* generated from a cryptographically secure random device.</p>
65+
*
66+
* @param keyMaterial the optional custom key material for the data key
67+
* @return this
68+
* @since 4.7
69+
* @see #getKeyMaterial()
70+
*/
71+
public DataKeyOptions keyMaterial(final byte[] keyMaterial) {
72+
this.keyMaterial = keyMaterial;
73+
return this;
74+
}
75+
5676
/**
5777
* Gets the alternate key names.
5878
*
@@ -63,6 +83,7 @@ public DataKeyOptions masterKey(final BsonDocument masterKey) {
6383
*
6484
* @return the list of alternate key names
6585
*/
86+
@Nullable
6687
public List<String> getKeyAltNames() {
6788
return keyAltNames;
6889
}
@@ -113,15 +134,32 @@ public List<String> getKeyAltNames() {
113134
* </p>
114135
* @return the master key document
115136
*/
137+
@Nullable
116138
public BsonDocument getMasterKey() {
117139
return masterKey;
118140
}
119141

142+
/**
143+
* Gets the custom key material if set.
144+
*
145+
* <p>The optional BinData of 96 bytes to use as custom key material for the data key being created.
146+
* If set the custom key material is used for encrypting and decrypting data. Otherwise, the key material for the new data key is
147+
* generated from a cryptographically secure random device.</p>
148+
149+
* @return the custom key material for the data key or null
150+
* @since 4.7
151+
*/
152+
@Nullable
153+
public byte[] getKeyMaterial() {
154+
return keyMaterial;
155+
}
156+
120157
@Override
121158
public String toString() {
122159
return "DataKeyOptions{"
123160
+ "keyAltNames=" + keyAltNames
124161
+ ", masterKey=" + masterKey
162+
+ ", keyMaterial=" + Arrays.toString(keyMaterial)
125163
+ '}';
126164
}
127165
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.mongodb.client.model.vault;
18+
19+
import org.bson.BsonDocument;
20+
21+
/**
22+
* The rewrap many data key options
23+
*
24+
* <p>
25+
* The {@link #getMasterKey()} document MUST have the fields corresponding to the given provider as specified in masterKey.
26+
* </p>
27+
*
28+
* @since 4.7
29+
*/
30+
public final class RewrapManyDataKeyOptions {
31+
32+
private String provider;
33+
private BsonDocument masterKey;
34+
35+
36+
/**
37+
* Sets the provider name
38+
*
39+
* @param provider the provider name
40+
* @return this
41+
* @see #getProvider()
42+
*/
43+
public RewrapManyDataKeyOptions provider(final String provider) {
44+
this.provider = provider;
45+
return this;
46+
}
47+
48+
/**
49+
* @return the provider name
50+
*/
51+
public String getProvider() {
52+
return provider;
53+
}
54+
55+
/**
56+
* Sets the optional master key document.
57+
*
58+
* @param masterKey the master key document
59+
* @return this
60+
* @see #getMasterKey()
61+
*/
62+
public RewrapManyDataKeyOptions masterKey(final BsonDocument masterKey) {
63+
this.masterKey = masterKey;
64+
return this;
65+
}
66+
67+
/**
68+
* Gets the master key document
69+
*
70+
* <p>
71+
* The masterKey identifies a KMS-specific key used to encrypt the new data key.
72+
* </p>
73+
* <p>
74+
* If the kmsProvider is "aws" the master key is required and must contain the following fields:
75+
* </p>
76+
* <ul>
77+
* <li>region: a String containing the AWS region in which to locate the master key</li>
78+
* <li>key: a String containing the Amazon Resource Name (ARN) to the AWS customer master key</li>
79+
* </ul>
80+
* <p>
81+
* If the kmsProvider is "azure" the master key is required and must contain the following fields:
82+
* </p>
83+
* <ul>
84+
* <li>keyVaultEndpoint: a String with the host name and an optional port. Example: "example.vault.azure.net".</li>
85+
* <li>keyName: a String</li>
86+
* <li>keyVersion: an optional String, the specific version of the named key, defaults to using the key's primary version.</li>
87+
* </ul>
88+
* <p>
89+
* If the kmsProvider is "gcp" the master key is required and must contain the following fields:
90+
* </p>
91+
* <ul>
92+
* <li>projectId: a String</li>
93+
* <li>location: String</li>
94+
* <li>keyRing: String</li>
95+
* <li>keyName: String</li>
96+
* <li>keyVersion: an optional String, the specific version of the named key, defaults to using the key's primary version.</li>
97+
* <li>endpoint: an optional String, with the host with optional port. Defaults to "cloudkms.googleapis.com".</li>
98+
* </ul>
99+
* <p>
100+
* If the kmsProvider is "kmip" the master key is required and must contain the following fields:
101+
* </p>
102+
* <ul>
103+
* <li>keyId: optional String, keyId is the KMIP Unique Identifier to a 96 byte KMIP Secret Data managed object. If keyId is
104+
* omitted, the driver creates a random 96 byte KMIP Secret Data managed object.</li>
105+
* <li>endpoint: a String, the endpoint as a host with required port. e.g. "example.com:443". If endpoint is not provided, it
106+
* defaults to the required endpoint from the KMS providers map.</li>
107+
* </ul>
108+
* <p>
109+
* If the kmsProvider is "local" the masterKey is not applicable.
110+
* </p>
111+
* @return the master key document
112+
*/
113+
public BsonDocument getMasterKey() {
114+
return masterKey;
115+
}
116+
117+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.mongodb.client.model.vault;
18+
19+
import com.mongodb.bulk.BulkWriteResult;
20+
import com.mongodb.lang.Nullable;
21+
22+
/**
23+
* The result of the rewrapping of data keys
24+
*
25+
* @since 4.7
26+
*/
27+
public final class RewrapManyDataKeyResult {
28+
private final BulkWriteResult bulkWriteResult;
29+
30+
/**
31+
* Construct a new instance with no bulk write result
32+
*/
33+
public RewrapManyDataKeyResult() {
34+
this.bulkWriteResult = null;
35+
}
36+
37+
/**
38+
* Construct a new instance
39+
* @param bulkWriteResult the bulk write result of the rewrapping data keys
40+
*/
41+
public RewrapManyDataKeyResult(final BulkWriteResult bulkWriteResult) {
42+
this.bulkWriteResult = bulkWriteResult;
43+
}
44+
45+
/**
46+
* @return the bulk write result of the rewrapping data keys or null if there was no bulk operation
47+
*/
48+
@Nullable
49+
public BulkWriteResult getBulkWriteResult() {
50+
return bulkWriteResult;
51+
}
52+
}

driver-core/src/test/functional/com/mongodb/internal/connection/TestCommandListener.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,16 @@ public CommandSucceededEvent getCommandSucceededEvent(final String commandName)
133133
throw new IllegalArgumentException(commandName + " not found in command succeeded event list");
134134
}
135135

136+
public CommandFailedEvent getCommandFailedEvent(final String commandName) {
137+
return getEvents()
138+
.stream()
139+
.filter(e -> e instanceof CommandFailedEvent)
140+
.filter(e -> e.getCommandName().equals(commandName))
141+
.map(e -> (CommandFailedEvent) e)
142+
.findFirst()
143+
.orElseThrow(() -> new IllegalArgumentException(commandName + " not found in command failed event list"));
144+
}
145+
136146
public List<CommandEvent> getCommandStartedEvents() {
137147
return getCommandStartedEvents(Integer.MAX_VALUE);
138148
}

0 commit comments

Comments
 (0)