-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the
ClientEncryption.createEncryptedCollection
helper method (#…
- Loading branch information
Showing
16 changed files
with
842 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
driver-core/src/main/com/mongodb/MongoUpdatedEncryptedFieldsException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright 2008-present MongoDB, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.mongodb; | ||
|
||
import com.mongodb.annotations.Beta; | ||
import org.bson.BsonDocument; | ||
|
||
import static com.mongodb.assertions.Assertions.assertNotNull; | ||
|
||
/** | ||
* An exception thrown by methods that may automatically create data encryption keys | ||
* where needed based on the {@code encryptedFields} configuration. | ||
* | ||
* @since 4.9 | ||
*/ | ||
@Beta(Beta.Reason.SERVER) | ||
public final class MongoUpdatedEncryptedFieldsException extends MongoClientException { | ||
private static final long serialVersionUID = 1; | ||
|
||
private final BsonDocument encryptedFields; | ||
|
||
/** | ||
* Not part of the public API. | ||
* | ||
* @param encryptedFields The (partially) updated {@code encryptedFields} document, | ||
* which allows users to infer which data keys are known to be created before the exception happened | ||
* (see {@link #getEncryptedFields()} for more details). | ||
* Reporting this back to a user may be helpful because creation of a data key includes persisting it in the key vault. | ||
* @param msg The message. | ||
* @param cause The cause. | ||
*/ | ||
public MongoUpdatedEncryptedFieldsException(final BsonDocument encryptedFields, final String msg, final Throwable cause) { | ||
super(msg, assertNotNull(cause)); | ||
this.encryptedFields = assertNotNull(encryptedFields); | ||
} | ||
|
||
/** | ||
* The {@code encryptedFields} document that allows inferring which data keys are <strong>known to be created</strong> | ||
* before {@code this} exception happened by comparing this document with the original {@code encryptedFields} configuration. | ||
* Creation of a data key includes persisting it in the key vault. | ||
* <p> | ||
* Note that the returned {@code encryptedFields} document is not guaranteed to contain information about all the data keys that | ||
* may be created, only about those that the driver is certain about. For example, if persisting a data key times out, | ||
* the driver does not know whether it can be considered created or not, and does not include the information about the key in | ||
* the {@code encryptedFields} document. You can analyze whether the {@linkplain #getCause() cause} is a definite or indefinite | ||
* error, and rely on the returned {@code encryptedFields} to be containing information on all created keys | ||
* only if the error is definite.</p> | ||
* | ||
* @return The updated {@code encryptedFields} document. | ||
*/ | ||
public BsonDocument getEncryptedFields() { | ||
return encryptedFields; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
driver-core/src/main/com/mongodb/client/model/CreateEncryptedCollectionParams.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright 2008-present MongoDB, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.mongodb.client.model; | ||
|
||
import com.mongodb.annotations.Beta; | ||
import com.mongodb.client.model.vault.DataKeyOptions; | ||
import com.mongodb.lang.Nullable; | ||
import org.bson.BsonDocument; | ||
|
||
import static com.mongodb.assertions.Assertions.notNull; | ||
|
||
/** | ||
* Auxiliary parameters for creating an encrypted collection. | ||
* | ||
* @since 4.9 | ||
*/ | ||
@Beta(Beta.Reason.SERVER) | ||
public final class CreateEncryptedCollectionParams { | ||
private final String kmsProvider; | ||
@Nullable | ||
private BsonDocument masterKey; | ||
|
||
/** | ||
* @param kmsProvider The name of the KMS provider. | ||
*/ | ||
public CreateEncryptedCollectionParams(final String kmsProvider) { | ||
this.kmsProvider = notNull("kmsProvider", kmsProvider); | ||
masterKey = null; | ||
} | ||
|
||
/** | ||
* The name of the KMS provider. | ||
* | ||
* @return The name of the KMS provider. | ||
*/ | ||
public String getKmsProvider() { | ||
return kmsProvider; | ||
} | ||
|
||
/** | ||
* Sets the {@linkplain DataKeyOptions#getMasterKey() master key} for creating a data key. | ||
* | ||
* @param masterKey The master key for creating a data key. | ||
* @return {@code this}. | ||
*/ | ||
public CreateEncryptedCollectionParams masterKey(@Nullable final BsonDocument masterKey) { | ||
this.masterKey = masterKey; | ||
return this; | ||
} | ||
|
||
/** | ||
* The {@linkplain DataKeyOptions#getMasterKey() master key} for creating a data key. | ||
* The default is {@code null}. | ||
* | ||
* @return The master key for creating a data key. | ||
*/ | ||
@Nullable | ||
public BsonDocument getMasterKey() { | ||
return masterKey; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "CreateEncryptedCollectionParams{" | ||
+ ", kmsProvider=" + kmsProvider | ||
+ ", masterKey=" + masterKey | ||
+ '}'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.