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; 17 18 import static org.assertj.core.api.Assertions.assertThat; 19 import static software.amazon.awssdk.utils.JavaSystemSetting.SSL_KEY_STORE; 20 import static software.amazon.awssdk.utils.JavaSystemSetting.SSL_KEY_STORE_PASSWORD; 21 import static software.amazon.awssdk.utils.JavaSystemSetting.SSL_KEY_STORE_TYPE; 22 23 import java.io.IOException; 24 import java.nio.file.Paths; 25 import java.security.Security; 26 import org.junit.jupiter.api.AfterAll; 27 import org.junit.jupiter.api.AfterEach; 28 import org.junit.jupiter.api.BeforeAll; 29 import org.junit.jupiter.api.Test; 30 31 public class SystemPropertyTlsKeyManagersProviderTest extends ClientTlsAuthTestBase { 32 private static final SystemPropertyTlsKeyManagersProvider PROVIDER = SystemPropertyTlsKeyManagersProvider.create(); 33 34 @BeforeAll setUp()35 public static void setUp() throws IOException { 36 ClientTlsAuthTestBase.setUp(); 37 } 38 39 @AfterEach methodTeardown()40 public void methodTeardown() { 41 System.clearProperty(SSL_KEY_STORE.property()); 42 System.clearProperty(SSL_KEY_STORE_TYPE.property()); 43 System.clearProperty(SSL_KEY_STORE_PASSWORD.property()); 44 } 45 46 @AfterAll teardown()47 public static void teardown() throws IOException { 48 ClientTlsAuthTestBase.teardown(); 49 } 50 51 @Test propertiesNotSet_returnsNull()52 public void propertiesNotSet_returnsNull() { 53 assertThat(PROVIDER.keyManagers()).isNull(); 54 } 55 56 @Test propertiesSet_createsKeyManager()57 public void propertiesSet_createsKeyManager() { 58 System.setProperty(SSL_KEY_STORE.property(), clientKeyStore.toAbsolutePath().toString()); 59 System.setProperty(SSL_KEY_STORE_TYPE.property(), CLIENT_STORE_TYPE); 60 System.setProperty(SSL_KEY_STORE_PASSWORD.property(), STORE_PASSWORD); 61 62 assertThat(PROVIDER.keyManagers()).hasSize(1); 63 } 64 65 @Test storeDoesNotExist_returnsNull()66 public void storeDoesNotExist_returnsNull() { 67 System.setProperty(SSL_KEY_STORE.property(), Paths.get("does", "not", "exist").toAbsolutePath().toString()); 68 System.setProperty(SSL_KEY_STORE_TYPE.property(), CLIENT_STORE_TYPE); 69 System.setProperty(SSL_KEY_STORE_PASSWORD.property(), STORE_PASSWORD); 70 71 assertThat(PROVIDER.keyManagers()).isNull(); 72 } 73 74 @Test invalidStoreType_returnsNull()75 public void invalidStoreType_returnsNull() { 76 System.setProperty(SSL_KEY_STORE.property(), clientKeyStore.toAbsolutePath().toString()); 77 System.setProperty(SSL_KEY_STORE_TYPE.property(), "invalid"); 78 System.setProperty(SSL_KEY_STORE_PASSWORD.property(), STORE_PASSWORD); 79 80 assertThat(PROVIDER.keyManagers()).isNull(); 81 } 82 83 @Test passwordIncorrect_returnsNull()84 public void passwordIncorrect_returnsNull() { 85 System.setProperty(SSL_KEY_STORE.property(), clientKeyStore.toAbsolutePath().toString()); 86 System.setProperty(SSL_KEY_STORE_TYPE.property(), CLIENT_STORE_TYPE); 87 System.setProperty(SSL_KEY_STORE_PASSWORD.property(), "not correct password"); 88 89 assertThat(PROVIDER.keyManagers()).isNull(); 90 } 91 92 @Test customKmfAlgorithmSetInProperty_usesAlgorithm()93 public void customKmfAlgorithmSetInProperty_usesAlgorithm() { 94 System.setProperty(SSL_KEY_STORE.property(), clientKeyStore.toAbsolutePath().toString()); 95 System.setProperty(SSL_KEY_STORE_TYPE.property(), CLIENT_STORE_TYPE); 96 System.setProperty(SSL_KEY_STORE_PASSWORD.property(), STORE_PASSWORD); 97 98 assertThat(PROVIDER.keyManagers()).isNotNull(); 99 100 String property = "ssl.KeyManagerFactory.algorithm"; 101 String previousValue = Security.getProperty(property); 102 Security.setProperty(property, "some-bogus-value"); 103 104 try { 105 // This would otherwise be non-null if using the right algorithm, 106 // i.e. not setting the algorithm property will cause the assertion 107 // to fail 108 assertThat(PROVIDER.keyManagers()).isNull(); 109 } finally { 110 Security.setProperty(property, previousValue); 111 } 112 } 113 } 114