-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Java 6173 CSFLE/QE Support for HTTP Proxies #2043
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 all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
92a2afa
Version: bump 5.11.0-SNAPSHOT
strogiyotec 170f6f0
Merge branch 'main' of https://github.com/mongodb/mongo-java-driver
strogiyotec 6bd7329
JAVA-6173 callback
strogiyotec 1560391
address PR feedback #1
strogiyotec feddaef
rename method back to enablehostname
strogiyotec c8702f0
restore enableHostName method
strogiyotec 1645282
restore enableHostName
strogiyotec 259293e
remove kmsProvider
strogiyotec 035f967
address copilot feedback
strogiyotec 6689fbd
remove CSOT and prose test that not in spec
strogiyotec 15640fa
remove CSOT from Context and keep it when calling the callback
strogiyotec 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
Some comments aren't visible on the classic Files Changed page.
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
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
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
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,92 @@ | ||
| /* | ||
| * 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.ThreadSafe; | ||
|
|
||
| import java.io.IOException; | ||
| import java.net.Socket; | ||
|
|
||
| /** | ||
| * A callback that establishes the connection used for a Key Management Service (KMS) request made by in-use encryption | ||
| * (client-side field level encryption or queryable encryption). | ||
| * | ||
| * <p>When a callback is configured, the driver invokes it instead of connecting to the KMS host itself, and uses the | ||
| * socket it returns for the KMS request. This enables routing KMS requests through an intermediary, most commonly an | ||
| * HTTP proxy via the {@code HTTP CONNECT} method.</p> | ||
| * | ||
| * <p>The driver always negotiates TLS with the KMS host itself over the returned socket, using the | ||
| * {@link javax.net.ssl.SSLContext} configured for the KMS provider. Server Name Indication and certificate hostname | ||
| * verification target the KMS host named by the context, not the address the callback actually connected to. | ||
| * Implementations therefore MUST NOT negotiate TLS with the KMS host themselves; they must return a socket over which | ||
| * a TLS handshake with the KMS host can be performed. An implementation may use TLS for its own connection to an | ||
| * intermediary, in which case it returns an {@link javax.net.ssl.SSLSocket} and the driver layers the KMS host's TLS | ||
| * session on top of it.</p> | ||
| * | ||
| * <p>An {@link IOException} thrown by an implementation is treated as a transient network error.</p> | ||
| * | ||
| * <p>This is applicable only when using the synchronous variant of {@code MongoClient}. The reactive streams driver, | ||
| * and the drivers built on it, reject a configured callback rather than connecting to KMS hosts directly.</p> | ||
| * | ||
| * <p>Authenticating to an intermediary is the responsibility of the implementation. For a proxy requiring HTTP Basic | ||
| * authentication, for example, the implementation adds a {@code Proxy-Authorization} header to the {@code CONNECT} | ||
| * request.</p> | ||
| * | ||
| * <p>Example of an implementation that tunnels through an HTTP proxy:</p> | ||
| * <pre>{@code | ||
| * KmsConnectCallback callback = context -> { | ||
| * Socket socket = new Socket(); | ||
| * try { | ||
| * socket.connect(new InetSocketAddress("proxy.example.com", 8080)); | ||
| * | ||
| * String target = context.getHost() + ":" + context.getPort(); | ||
| * socket.getOutputStream().write( | ||
| * ("CONNECT " + target + " HTTP/1.1\r\nHost: " + target + "\r\n\r\n").getBytes(StandardCharsets.US_ASCII)); | ||
| * | ||
| * // Read the status line and confirm a 2xx status, throwing an IOException otherwise. Match the status code | ||
| * // rather than the whole status line, as proxies differ in the HTTP version they reply with. Read the | ||
| * // response one byte at a time, up to the end of the header block, so that no byte of the TLS handshake that | ||
| * // the driver performs over this socket is consumed. | ||
| * readAndCheckProxyResponse(socket.getInputStream()); | ||
| * } catch (IOException | RuntimeException e) { | ||
| * // The driver cannot close a socket that was never returned to it. | ||
| * socket.close(); | ||
| * throw e; | ||
| * } | ||
| * | ||
| * return socket; | ||
| * }; | ||
| * }</pre> | ||
| * | ||
| * @see ClientEncryptionSettings.Builder#kmsConnectCallback(KmsConnectCallback) | ||
| * @see AutoEncryptionSettings.Builder#kmsConnectCallback(KmsConnectCallback) | ||
| * @since 5.11 | ||
| */ | ||
| @ThreadSafe | ||
| @FunctionalInterface | ||
| public interface KmsConnectCallback { | ||
|
jyemin marked this conversation as resolved.
jyemin marked this conversation as resolved.
|
||
| /** | ||
| * Returns a socket connected such that a TLS handshake with the KMS host can be performed over it. | ||
| * | ||
| * <p>Ownership of the returned socket passes to the driver, which closes it once the KMS request completes.</p> | ||
| * | ||
| * @param context the details of the connection to establish | ||
| * @return a connected socket, which must not have an established TLS session with the KMS host | ||
| * @throws IOException if the connection cannot be established. This is treated as a transient network error. | ||
| */ | ||
| Socket connect(KmsConnectContext context) throws IOException; | ||
| } | ||
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,77 @@ | ||
| /* | ||
| * 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.Immutable; | ||
|
|
||
| import static com.mongodb.assertions.Assertions.isTrueArgument; | ||
| import static com.mongodb.assertions.Assertions.notNull; | ||
|
|
||
| /** | ||
| * The details of a Key Management Service (KMS) connection that a {@link KmsConnectCallback} is asked to establish. | ||
| * | ||
| * @see KmsConnectCallback | ||
| * @since 5.11 | ||
| */ | ||
| @Immutable | ||
| public final class KmsConnectContext { | ||
| private final String host; | ||
| private final int port; | ||
|
|
||
| /** | ||
| * Construct a new instance. | ||
| * | ||
| * @param host the host name of the KMS host, which may not be null | ||
| * @param port the port of the KMS host, which must be positive | ||
| */ | ||
| public KmsConnectContext(final String host, final int port) { | ||
| this.host = notNull("host", host); | ||
| isTrueArgument("port > 0", port > 0); | ||
| this.port = port; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the host name of the KMS host that the connection must ultimately reach. | ||
| * | ||
| * <p>This is not the host name of any intermediary such as a proxy. It is the host that the driver negotiates TLS | ||
| * with once the callback returns.</p> | ||
| * | ||
| * @return the host name of the KMS host, never null | ||
| */ | ||
| public String getHost() { | ||
| return host; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the port of the KMS host that the connection must ultimately reach. | ||
| * | ||
| * <p>This is not the port of any intermediary such as a proxy.</p> | ||
| * | ||
| * @return the port of the KMS host, always positive | ||
| */ | ||
| public int getPort() { | ||
| return port; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "KmsConnectContext{" | ||
| + "host='" + host + '\'' | ||
| + ", port=" + port | ||
| + '}'; | ||
| } | ||
| } |
74 changes: 74 additions & 0 deletions
74
driver-core/src/test/unit/com/mongodb/KmsConnectCallbackSettingsTest.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,74 @@ | ||
| /* | ||
| * 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 org.junit.jupiter.api.Test; | ||
|
|
||
| import java.net.Socket; | ||
| import java.util.HashMap; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
| import static org.junit.jupiter.api.Assertions.assertSame; | ||
|
|
||
| final class KmsConnectCallbackSettingsTest { | ||
|
|
||
| private static final KmsConnectCallback CALLBACK = context -> new Socket(); | ||
|
|
||
| @Test | ||
| void autoEncryptionSettingsShouldDefaultToNoCallback() { | ||
| AutoEncryptionSettings settings = AutoEncryptionSettings.builder() | ||
| .keyVaultNamespace("keyvault.datakeys") | ||
| .kmsProviders(new HashMap<>()) | ||
| .build(); | ||
|
|
||
| assertNull(settings.getKmsConnectCallback()); | ||
| } | ||
|
|
||
| @Test | ||
| void autoEncryptionSettingsShouldRoundTripCallback() { | ||
| AutoEncryptionSettings settings = AutoEncryptionSettings.builder() | ||
| .keyVaultNamespace("keyvault.datakeys") | ||
| .kmsProviders(new HashMap<>()) | ||
| .kmsConnectCallback(CALLBACK) | ||
| .build(); | ||
|
|
||
| assertSame(CALLBACK, settings.getKmsConnectCallback()); | ||
| } | ||
|
|
||
| @Test | ||
| void clientEncryptionSettingsShouldDefaultToNoCallback() { | ||
| ClientEncryptionSettings settings = clientEncryptionSettingsBuilder().build(); | ||
|
|
||
| assertNull(settings.getKmsConnectCallback()); | ||
| } | ||
|
|
||
| @Test | ||
| void clientEncryptionSettingsShouldRoundTripCallback() { | ||
| ClientEncryptionSettings settings = clientEncryptionSettingsBuilder() | ||
| .kmsConnectCallback(CALLBACK) | ||
| .build(); | ||
|
|
||
| assertSame(CALLBACK, settings.getKmsConnectCallback()); | ||
| } | ||
|
|
||
| private static ClientEncryptionSettings.Builder clientEncryptionSettingsBuilder() { | ||
| return ClientEncryptionSettings.builder() | ||
| .keyVaultMongoClientSettings(MongoClientSettings.builder().build()) | ||
| .keyVaultNamespace("keyvault.datakeys") | ||
| .kmsProviders(new HashMap<>()); | ||
| } | ||
| } |
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.