|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 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 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.awssdk.http.nio.netty.internal; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 20 | +import static software.amazon.awssdk.http.SdkHttpConfigurationOption.TLS_KEY_MANAGERS_PROVIDER; |
| 21 | +import static software.amazon.awssdk.http.SdkHttpConfigurationOption.TLS_TRUST_MANAGERS_PROVIDER; |
| 22 | +import static software.amazon.awssdk.http.SdkHttpConfigurationOption.TRUST_ALL_CERTIFICATES; |
| 23 | + |
| 24 | +import io.netty.handler.codec.http2.Http2SecurityUtil; |
| 25 | +import io.netty.handler.ssl.SslProvider; |
| 26 | +import javax.net.ssl.TrustManager; |
| 27 | +import org.junit.Test; |
| 28 | +import org.mockito.Mockito; |
| 29 | +import software.amazon.awssdk.http.Protocol; |
| 30 | +import software.amazon.awssdk.http.SdkHttpConfigurationOption; |
| 31 | +import software.amazon.awssdk.http.TlsKeyManagersProvider; |
| 32 | +import software.amazon.awssdk.http.TlsTrustManagersProvider; |
| 33 | +import software.amazon.awssdk.utils.AttributeMap; |
| 34 | + |
| 35 | +public class SslContextProviderTest { |
| 36 | + |
| 37 | + @Test |
| 38 | + public void sslContext_h2WithJdk_h2CiphersShouldBeUsed() { |
| 39 | + SslContextProvider sslContextProvider = new SslContextProvider(new NettyConfiguration(SdkHttpConfigurationOption.GLOBAL_HTTP_DEFAULTS), |
| 40 | + Protocol.HTTP2, |
| 41 | + SslProvider.JDK); |
| 42 | + |
| 43 | + assertThat(sslContextProvider.sslContext().cipherSuites()).isSubsetOf(Http2SecurityUtil.CIPHERS); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + public void sslContext_h2WithOpenSsl_h2CiphersShouldBeUsed() { |
| 48 | + SslContextProvider sslContextProvider = new SslContextProvider(new NettyConfiguration(SdkHttpConfigurationOption.GLOBAL_HTTP_DEFAULTS), |
| 49 | + Protocol.HTTP2, |
| 50 | + SslProvider.OPENSSL); |
| 51 | + |
| 52 | + assertThat(sslContextProvider.sslContext().cipherSuites()).isSubsetOf(Http2SecurityUtil.CIPHERS); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void sslContext_h1_defaultCipherShouldBeUsed() { |
| 57 | + SslContextProvider sslContextProvider = new SslContextProvider(new NettyConfiguration(SdkHttpConfigurationOption.GLOBAL_HTTP_DEFAULTS), |
| 58 | + Protocol.HTTP1_1, |
| 59 | + SslProvider.JDK); |
| 60 | + |
| 61 | + assertThat(sslContextProvider.sslContext().cipherSuites()).isNotIn(Http2SecurityUtil.CIPHERS); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void customizedKeyManagerPresent_shouldUseCustomized() { |
| 66 | + TlsKeyManagersProvider mockProvider = Mockito.mock(TlsKeyManagersProvider.class); |
| 67 | + SslContextProvider sslContextProvider = new SslContextProvider(new NettyConfiguration(AttributeMap.builder() |
| 68 | + .put(TRUST_ALL_CERTIFICATES, false) |
| 69 | + .put(TLS_KEY_MANAGERS_PROVIDER, mockProvider) |
| 70 | + .build()), |
| 71 | + Protocol.HTTP1_1, |
| 72 | + SslProvider.JDK); |
| 73 | + |
| 74 | + sslContextProvider.sslContext(); |
| 75 | + Mockito.verify(mockProvider).keyManagers(); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void customizedTrustManagerPresent_shouldUseCustomized() { |
| 80 | + TlsTrustManagersProvider mockProvider = Mockito.mock(TlsTrustManagersProvider.class); |
| 81 | + TrustManager mockTrustManager = Mockito.mock(TrustManager.class); |
| 82 | + Mockito.when(mockProvider.trustManagers()).thenReturn(new TrustManager[] {mockTrustManager}); |
| 83 | + SslContextProvider sslContextProvider = new SslContextProvider(new NettyConfiguration(AttributeMap.builder() |
| 84 | + .put(TRUST_ALL_CERTIFICATES, false) |
| 85 | + .put(TLS_TRUST_MANAGERS_PROVIDER, mockProvider) |
| 86 | + .build()), |
| 87 | + Protocol.HTTP1_1, |
| 88 | + SslProvider.JDK); |
| 89 | + |
| 90 | + sslContextProvider.sslContext(); |
| 91 | + Mockito.verify(mockProvider).trustManagers(); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void TlsTrustManagerAndTrustAllCertificates_shouldThrowException() { |
| 96 | + TlsTrustManagersProvider mockProvider = Mockito.mock(TlsTrustManagersProvider.class); |
| 97 | + assertThatThrownBy(() -> new SslContextProvider(new NettyConfiguration(AttributeMap.builder() |
| 98 | + .put(TRUST_ALL_CERTIFICATES, true) |
| 99 | + .put(TLS_TRUST_MANAGERS_PROVIDER, |
| 100 | + mockProvider) |
| 101 | + .build()), |
| 102 | + Protocol.HTTP1_1, |
| 103 | + SslProvider.JDK)).isInstanceOf(IllegalArgumentException.class) |
| 104 | + .hasMessageContaining("A TlsTrustManagerProvider can't" |
| 105 | + + " be provided if " |
| 106 | + + "TrustAllCertificates is also " |
| 107 | + + "set"); |
| 108 | + |
| 109 | + } |
| 110 | +} |
0 commit comments