-
Notifications
You must be signed in to change notification settings - Fork 910
Add a config option to allow users to enable post quantum TLS support in CRT HTTP client #3727
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
4 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"type": "feature", | ||
"category": "AWS CRT HTTP Client", | ||
"contributor": "", | ||
"description": "Allow users to enable Post Quantum TLS via `AwsCrtAsyncHttpClient.builder().preferePostQuantumTls(true).build()`." | ||
} |
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 |
---|---|---|
|
@@ -17,6 +17,10 @@ | |
|
||
import static software.amazon.awssdk.http.HttpMetric.HTTP_CLIENT_NAME; | ||
import static software.amazon.awssdk.http.SdkHttpConfigurationOption.PROTOCOL; | ||
import static software.amazon.awssdk.http.crt.internal.AwsCrtConfigurationUtils.buildProxyOptions; | ||
import static software.amazon.awssdk.http.crt.internal.AwsCrtConfigurationUtils.buildSocketOptions; | ||
import static software.amazon.awssdk.http.crt.internal.AwsCrtConfigurationUtils.resolveCipherPreference; | ||
import static software.amazon.awssdk.http.crt.internal.AwsCrtConfigurationUtils.revolveHttpMonitoringOptions; | ||
import static software.amazon.awssdk.utils.FunctionalUtils.invokeSafely; | ||
import static software.amazon.awssdk.utils.Validate.paramNotNull; | ||
|
||
|
@@ -36,7 +40,6 @@ | |
import software.amazon.awssdk.crt.http.HttpProxyOptions; | ||
import software.amazon.awssdk.crt.io.ClientBootstrap; | ||
import software.amazon.awssdk.crt.io.SocketOptions; | ||
import software.amazon.awssdk.crt.io.TlsCipherPreference; | ||
import software.amazon.awssdk.crt.io.TlsContext; | ||
import software.amazon.awssdk.crt.io.TlsContextOptions; | ||
import software.amazon.awssdk.http.Protocol; | ||
|
@@ -97,10 +100,11 @@ private AwsCrtAsyncHttpClient(DefaultBuilder builder, AttributeMap config) { | |
} | ||
|
||
try (ClientBootstrap clientBootstrap = new ClientBootstrap(null, null); | ||
SocketOptions clientSocketOptions = buildSocketOptions(builder, config); | ||
SocketOptions clientSocketOptions = buildSocketOptions(builder.tcpKeepAliveConfiguration, | ||
config.get(SdkHttpConfigurationOption.CONNECTION_TIMEOUT)); | ||
TlsContextOptions clientTlsContextOptions = | ||
TlsContextOptions.createDefaultClient() | ||
.withCipherPreference(TlsCipherPreference.TLS_CIPHER_SYSTEM_DEFAULT) | ||
.withCipherPreference(resolveCipherPreference(builder.preferPostQuantumTls)) | ||
.withVerifyPeer(!config.get(SdkHttpConfigurationOption.TRUST_ALL_CERTIFICATES)); | ||
TlsContext clientTlsContext = new TlsContext(clientTlsContextOptions)) { | ||
|
||
|
@@ -111,67 +115,10 @@ private AwsCrtAsyncHttpClient(DefaultBuilder builder, AttributeMap config) { | |
this.maxConnectionsPerEndpoint = config.get(SdkHttpConfigurationOption.MAX_CONNECTIONS); | ||
this.monitoringOptions = revolveHttpMonitoringOptions(builder.connectionHealthConfiguration); | ||
this.maxConnectionIdleInMilliseconds = config.get(SdkHttpConfigurationOption.CONNECTION_MAX_IDLE_TIMEOUT).toMillis(); | ||
this.proxyOptions = buildProxyOptions(builder.proxyConfiguration); | ||
this.proxyOptions = buildProxyOptions(builder.proxyConfiguration, tlsContext); | ||
} | ||
} | ||
|
||
private HttpMonitoringOptions revolveHttpMonitoringOptions(ConnectionHealthConfiguration config) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No changes have been made to these methods. I just moved them to the new class. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. gotcha |
||
if (config == null) { | ||
return null; | ||
} | ||
|
||
HttpMonitoringOptions httpMonitoringOptions = new HttpMonitoringOptions(); | ||
httpMonitoringOptions.setMinThroughputBytesPerSecond(config.minimumThroughputInBps()); | ||
int seconds = (int) config.minimumThroughputTimeout().getSeconds(); | ||
httpMonitoringOptions.setAllowableThroughputFailureIntervalSeconds(seconds); | ||
return httpMonitoringOptions; | ||
} | ||
|
||
private HttpProxyOptions buildProxyOptions(ProxyConfiguration proxyConfiguration) { | ||
if (proxyConfiguration == null) { | ||
return null; | ||
} | ||
|
||
HttpProxyOptions clientProxyOptions = new HttpProxyOptions(); | ||
|
||
clientProxyOptions.setHost(proxyConfiguration.host()); | ||
clientProxyOptions.setPort(proxyConfiguration.port()); | ||
|
||
if ("https".equalsIgnoreCase(proxyConfiguration.scheme())) { | ||
clientProxyOptions.setTlsContext(tlsContext); | ||
} | ||
|
||
if (proxyConfiguration.username() != null && proxyConfiguration.password() != null) { | ||
clientProxyOptions.setAuthorizationUsername(proxyConfiguration.username()); | ||
clientProxyOptions.setAuthorizationPassword(proxyConfiguration.password()); | ||
clientProxyOptions.setAuthorizationType(HttpProxyOptions.HttpProxyAuthorizationType.Basic); | ||
} else { | ||
clientProxyOptions.setAuthorizationType(HttpProxyOptions.HttpProxyAuthorizationType.None); | ||
} | ||
|
||
return clientProxyOptions; | ||
} | ||
|
||
private SocketOptions buildSocketOptions(DefaultBuilder builder, AttributeMap config) { | ||
SocketOptions clientSocketOptions = new SocketOptions(); | ||
|
||
Duration connectionTimeout = config.get(SdkHttpConfigurationOption.CONNECTION_TIMEOUT); | ||
if (connectionTimeout != null) { | ||
clientSocketOptions.connectTimeoutMs = NumericUtils.saturatedCast(connectionTimeout.toMillis()); | ||
} | ||
|
||
TcpKeepAliveConfiguration tcpKeepAliveConfiguration = builder.tcpKeepAliveConfiguration; | ||
if (tcpKeepAliveConfiguration != null) { | ||
clientSocketOptions.keepAliveIntervalSecs = | ||
NumericUtils.saturatedCast(tcpKeepAliveConfiguration.keepAliveInterval().getSeconds()); | ||
clientSocketOptions.keepAliveTimeoutSecs = | ||
NumericUtils.saturatedCast(tcpKeepAliveConfiguration.keepAliveTimeout().getSeconds()); | ||
|
||
} | ||
|
||
return clientSocketOptions; | ||
} | ||
|
||
/** | ||
* Marks a Native CrtResource as owned by the current Java Object. | ||
* | ||
|
@@ -418,6 +365,22 @@ Builder connectionHealthConfiguration(Consumer<ConnectionHealthConfiguration.Bui | |
*/ | ||
Builder tcpKeepAliveConfiguration(Consumer<TcpKeepAliveConfiguration.Builder> | ||
tcpKeepAliveConfigurationBuilder); | ||
|
||
/** | ||
* Configure whether to prefer a hybrid post-quantum key exchange option for the Transport Layer Security (TLS) network | ||
* encryption protocol when communicating with services that support Post Quantum TLS. If Post Quantum cipher suites are | ||
* not supported on the platform, the SDK will use the default TLS cipher suites. | ||
* | ||
* <p> | ||
* See <a href="https://docs.aws.amazon.com/kms/latest/developerguide/pqtls.html">Using hybrid post-quantum TLS with AWS KMS</a> | ||
* | ||
* <p> | ||
* It's disabled by default. | ||
* | ||
* @param preferPostQuantumTls whether to prefer Post Quantum TLS | ||
* @return The builder of the method chaining. | ||
*/ | ||
Builder preferPostQuantumTls(Boolean preferPostQuantumTls); | ||
} | ||
|
||
/** | ||
|
@@ -430,6 +393,7 @@ private static final class DefaultBuilder implements Builder { | |
private ProxyConfiguration proxyConfiguration; | ||
private ConnectionHealthConfiguration connectionHealthConfiguration; | ||
private TcpKeepAliveConfiguration tcpKeepAliveConfiguration; | ||
private Boolean preferPostQuantumTls; | ||
|
||
private DefaultBuilder() { | ||
} | ||
|
@@ -509,6 +473,12 @@ public Builder tcpKeepAliveConfiguration(Consumer<TcpKeepAliveConfiguration.Buil | |
return tcpKeepAliveConfiguration(builder.build()); | ||
} | ||
|
||
@Override | ||
public Builder preferPostQuantumTls(Boolean preferPostQuantumTls) { | ||
this.preferPostQuantumTls = preferPostQuantumTls; | ||
return this; | ||
} | ||
|
||
@Override | ||
public Builder proxyConfiguration(Consumer<ProxyConfiguration.Builder> proxyConfigurationBuilderConsumer) { | ||
ProxyConfiguration.Builder builder = ProxyConfiguration.builder(); | ||
|
113 changes: 113 additions & 0 deletions
113
...ient/src/main/java/software/amazon/awssdk/http/crt/internal/AwsCrtConfigurationUtils.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,113 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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 software.amazon.awssdk.http.crt.internal; | ||
|
||
|
||
import java.time.Duration; | ||
import software.amazon.awssdk.annotations.SdkInternalApi; | ||
import software.amazon.awssdk.crt.http.HttpMonitoringOptions; | ||
import software.amazon.awssdk.crt.http.HttpProxyOptions; | ||
import software.amazon.awssdk.crt.io.SocketOptions; | ||
import software.amazon.awssdk.crt.io.TlsCipherPreference; | ||
import software.amazon.awssdk.crt.io.TlsContext; | ||
import software.amazon.awssdk.http.crt.AwsCrtAsyncHttpClient; | ||
import software.amazon.awssdk.http.crt.ConnectionHealthConfiguration; | ||
import software.amazon.awssdk.http.crt.ProxyConfiguration; | ||
import software.amazon.awssdk.http.crt.TcpKeepAliveConfiguration; | ||
import software.amazon.awssdk.utils.Logger; | ||
import software.amazon.awssdk.utils.NumericUtils; | ||
|
||
@SdkInternalApi | ||
public final class AwsCrtConfigurationUtils { | ||
private static final Logger log = Logger.loggerFor(AwsCrtAsyncHttpClient.class); | ||
|
||
private AwsCrtConfigurationUtils() { | ||
} | ||
|
||
public static SocketOptions buildSocketOptions(TcpKeepAliveConfiguration tcpKeepAliveConfiguration, | ||
Duration connectionTimeout) { | ||
SocketOptions clientSocketOptions = new SocketOptions(); | ||
|
||
if (connectionTimeout != null) { | ||
clientSocketOptions.connectTimeoutMs = NumericUtils.saturatedCast(connectionTimeout.toMillis()); | ||
} | ||
|
||
if (tcpKeepAliveConfiguration != null) { | ||
clientSocketOptions.keepAliveIntervalSecs = | ||
NumericUtils.saturatedCast(tcpKeepAliveConfiguration.keepAliveInterval().getSeconds()); | ||
clientSocketOptions.keepAliveTimeoutSecs = | ||
NumericUtils.saturatedCast(tcpKeepAliveConfiguration.keepAliveTimeout().getSeconds()); | ||
|
||
} | ||
|
||
return clientSocketOptions; | ||
} | ||
|
||
public static HttpProxyOptions buildProxyOptions(ProxyConfiguration proxyConfiguration, TlsContext tlsContext) { | ||
if (proxyConfiguration == null) { | ||
return null; | ||
} | ||
|
||
HttpProxyOptions clientProxyOptions = new HttpProxyOptions(); | ||
|
||
clientProxyOptions.setHost(proxyConfiguration.host()); | ||
clientProxyOptions.setPort(proxyConfiguration.port()); | ||
|
||
if ("https".equalsIgnoreCase(proxyConfiguration.scheme())) { | ||
clientProxyOptions.setTlsContext(tlsContext); | ||
} | ||
|
||
if (proxyConfiguration.username() != null && proxyConfiguration.password() != null) { | ||
clientProxyOptions.setAuthorizationUsername(proxyConfiguration.username()); | ||
clientProxyOptions.setAuthorizationPassword(proxyConfiguration.password()); | ||
clientProxyOptions.setAuthorizationType(HttpProxyOptions.HttpProxyAuthorizationType.Basic); | ||
} else { | ||
clientProxyOptions.setAuthorizationType(HttpProxyOptions.HttpProxyAuthorizationType.None); | ||
} | ||
|
||
return clientProxyOptions; | ||
} | ||
|
||
public static HttpMonitoringOptions revolveHttpMonitoringOptions(ConnectionHealthConfiguration config) { | ||
zoewangg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (config == null) { | ||
return null; | ||
} | ||
|
||
HttpMonitoringOptions httpMonitoringOptions = new HttpMonitoringOptions(); | ||
httpMonitoringOptions.setMinThroughputBytesPerSecond(config.minimumThroughputInBps()); | ||
int seconds = (int) config.minimumThroughputTimeout().getSeconds(); | ||
httpMonitoringOptions.setAllowableThroughputFailureIntervalSeconds(seconds); | ||
return httpMonitoringOptions; | ||
} | ||
|
||
public static TlsCipherPreference resolveCipherPreference(Boolean preferTlsCipherPreference) { | ||
TlsCipherPreference defaultTls = TlsCipherPreference.TLS_CIPHER_SYSTEM_DEFAULT; | ||
if (preferTlsCipherPreference == null || !preferTlsCipherPreference) { | ||
return defaultTls; | ||
} | ||
|
||
// TODO: change this to the new PQ TLS Policy that stays up to date when it's ready | ||
TlsCipherPreference pqTls = TlsCipherPreference.TLS_CIPHER_PREF_PQ_TLSv1_0_2021_05; | ||
if (!pqTls.isSupported()) { | ||
log.warn(() -> "Hybrid post-quantum cipher suites are not supported on this platform. The SDK will use the system " | ||
+ "default cipher suites instead"); | ||
return defaultTls; | ||
} | ||
|
||
return pqTls; | ||
} | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
.../src/test/java/software/amazon/awssdk/http/crt/internal/AwsCrtConfigurationUtilsTest.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,54 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file 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 software.amazon.awssdk.http.crt.internal; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
import static software.amazon.awssdk.crt.io.TlsCipherPreference.TLS_CIPHER_PREF_PQ_TLSv1_0_2021_05; | ||
import static software.amazon.awssdk.crt.io.TlsCipherPreference.TLS_CIPHER_SYSTEM_DEFAULT; | ||
|
||
import java.util.stream.Stream; | ||
import org.junit.jupiter.api.Assumptions; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
import software.amazon.awssdk.crt.io.TlsCipherPreference; | ||
|
||
class AwsCrtConfigurationUtilsTest { | ||
|
||
@ParameterizedTest | ||
@MethodSource("cipherPreferences") | ||
void resolveCipherPreference_pqNotSupported_shouldFallbackToSystemDefault(Boolean preferPqTls, | ||
TlsCipherPreference tlsCipherPreference) { | ||
Assumptions.assumeFalse(TLS_CIPHER_PREF_PQ_TLSv1_0_2021_05.isSupported()); | ||
assertThat(AwsCrtConfigurationUtils.resolveCipherPreference(preferPqTls)).isEqualTo(tlsCipherPreference); | ||
} | ||
|
||
@Test | ||
void resolveCipherPreference_pqSupported_shouldHonor() { | ||
Assumptions.assumeTrue(TLS_CIPHER_PREF_PQ_TLSv1_0_2021_05.isSupported()); | ||
assertThat(AwsCrtConfigurationUtils.resolveCipherPreference(true)).isEqualTo(TLS_CIPHER_PREF_PQ_TLSv1_0_2021_05); | ||
} | ||
|
||
private static Stream<Arguments> cipherPreferences() { | ||
return Stream.of( | ||
Arguments.of(null, TLS_CIPHER_SYSTEM_DEFAULT), | ||
Arguments.of(false, TLS_CIPHER_SYSTEM_DEFAULT), | ||
Arguments.of(true, TLS_CIPHER_SYSTEM_DEFAULT) | ||
); | ||
} | ||
|
||
} |
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.