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 software.amazon.awssdk.utils.JavaSystemSetting.SSL_KEY_STORE;
19 import static software.amazon.awssdk.utils.JavaSystemSetting.SSL_KEY_STORE_PASSWORD;
20 import static software.amazon.awssdk.utils.JavaSystemSetting.SSL_KEY_STORE_TYPE;
21
22 import java.nio.file.Path;
23 import java.nio.file.Paths;
24 import java.security.KeyStore;
25 import java.util.Optional;
26 import javax.net.ssl.KeyManager;
27 import software.amazon.awssdk.annotations.SdkPublicApi;
28 import software.amazon.awssdk.internal.http.AbstractFileStoreTlsKeyManagersProvider;
29 import software.amazon.awssdk.utils.Logger;
30 import software.amazon.awssdk.utils.internal.SystemSettingUtils;
31
32 /**
33  * Implementation of {@link TlsKeyManagersProvider} that gets the information
34  * about the KeyStore to load from the system properties.
35  * <p>
36  * This provider checks the standard {@code javax.net.ssl.keyStore},
37  * {@code javax.net.ssl.keyStorePassword}, and
38  * {@code javax.net.ssl.keyStoreType} properties defined by the
39  * <a href="https://docs.oracle.com/javase/8/docs/technotes/guides/security/jsse/JSSERefGuide.html">JSSE</a>.
40  */

41 @SdkPublicApi
42 public final class SystemPropertyTlsKeyManagersProvider extends AbstractFileStoreTlsKeyManagersProvider {
43     private static final Logger log = Logger.loggerFor(SystemPropertyTlsKeyManagersProvider.class);
44
45     private SystemPropertyTlsKeyManagersProvider() {
46     }
47
48     @Override
49     public KeyManager[] keyManagers() {
50         return getKeyStore().map(p -> {
51             Path path = Paths.get(p);
52             String type = getKeyStoreType();
53             char[] password = getKeyStorePassword().map(String::toCharArray).orElse(null);
54             try {
55                 return createKeyManagers(path, type, password);
56             } catch (Exception e) {
57                 log.warn(() -> String.format("Unable to create KeyManagers from %s property value '%s'",
58                                              SSL_KEY_STORE.property(), p), e);
59                 return null;
60             }
61         }).orElse(null);
62     }
63
64     public static SystemPropertyTlsKeyManagersProvider create() {
65         return new SystemPropertyTlsKeyManagersProvider();
66     }
67
68     private static Optional<String> getKeyStore() {
69         return SystemSettingUtils.resolveSetting(SSL_KEY_STORE);
70     }
71
72     private static String getKeyStoreType() {
73         return SystemSettingUtils.resolveSetting(SSL_KEY_STORE_TYPE)
74                 .orElse(KeyStore.getDefaultType());
75     }
76
77     private static Optional<String> getKeyStorePassword() {
78         return SystemSettingUtils.resolveSetting(SSL_KEY_STORE_PASSWORD);
79     }
80 }
81