-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add the ClientEncryption.createEncryptedCollection helper method
#1079
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0714f57
Add the `ClientEncryption.createEncryptedCollection` helper method
stIncMale 4963de7
Simplify the reactive implementation a bit
stIncMale ac41300
Remove the KMS provider name duplication in the test
stIncMale 0016d2c
Address review concerns
stIncMale fd6eebe
Update a comment
stIncMale 6dd7746
Do some changes:
stIncMale 3cc7102
Replace `doOnError` with `onErrorMap`
stIncMale File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
85 changes: 85 additions & 0 deletions
85
driver-core/src/main/com/mongodb/client/model/CreateEncryptedCollectionParams.java
This file contains hidden or 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,85 @@ | ||
| /* | ||
| * 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; | ||
|
|
||
| /** | ||
| * A constructor. | ||
katcharov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * | ||
| * @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 | ||
| + '}'; | ||
| } | ||
| } | ||
69 changes: 69 additions & 0 deletions
69
...active-streams/src/main/com/mongodb/reactivestreams/client/internal/OneShotPublisher.java
This file contains hidden or 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,69 @@ | ||
| /* | ||
| * 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.reactivestreams.client.internal; | ||
|
|
||
| import com.mongodb.annotations.NotThreadSafe; | ||
| import org.reactivestreams.Publisher; | ||
| import org.reactivestreams.Subscriber; | ||
| import reactor.core.publisher.Flux; | ||
| import reactor.core.publisher.Mono; | ||
|
|
||
| import java.util.function.Supplier; | ||
|
|
||
| /** | ||
| * Allows creating {@link Publisher}s that do not allow calling {@link Publisher#subscribe(Subscriber)} more than once. | ||
| * | ||
| * <p>This class is not part of the public API and may be removed or changed at any time</p> | ||
| */ | ||
| public final class OneShotPublisher { | ||
| public static <T> Publisher<T> from(final Publisher<T> publisher) { | ||
| return Flux.defer(new OneShotSupplier<>(publisher)); | ||
| } | ||
|
|
||
| // A `Publisher` does not have to be thread-safe, therefore, `Publisher.subscribe` should not be called concurrently. | ||
| // Hence, `OneShotSupplier.get` is not called concurrently and does not have to be thread-safe. | ||
| @NotThreadSafe | ||
| private static final class OneShotSupplier<T> implements Supplier<Publisher<T>> { | ||
| private final Publisher<T> publisher; | ||
| private boolean used; | ||
|
|
||
| OneShotSupplier(final Publisher<T> publisher) { | ||
| this.publisher = publisher; | ||
| } | ||
|
|
||
| @Override | ||
| public Publisher<T> get() { | ||
| if (used) { | ||
| // we may also `throw` here, and `Flux.defer` will handle the exception and signal `onSubscribe` followed by `onError` | ||
| return Mono.error(new IllegalStateException( | ||
| "This is a one-shot publisher, it does not support subscribing to it more than once.")); | ||
| } | ||
| used = true; | ||
| return publisher; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "OneShotSupplier{" | ||
| + "publisher=" + publisher | ||
| + ", used=" + used | ||
| + '}'; | ||
| } | ||
| } | ||
|
|
||
| private OneShotPublisher() { | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.