View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.net.ssl;
18  
19  import org.apache.logging.log4j.Logger;
20  import org.apache.logging.log4j.status.StatusLogger;
21  import org.apache.logging.log4j.util.PropertiesUtil;
22  
23  /**
24   * Creates an SSL configuration from Log4j properties.
25   */
26  public class SslConfigurationFactory {
27  
28      private static final Logger LOGGER = StatusLogger.getLogger();
29      private static SslConfiguration sslConfiguration = null;
30  
31      private static final String trustStorelocation = "log4j2.trustStoreLocation";
32      private static final String trustStorePassword = "log4j2.trustStorePassword";
33      private static final String trustStorePasswordFile = "log4j2.trustStorePasswordFile";
34      private static final String trustStorePasswordEnvVar = "log4j2.trustStorePasswordEnvironmentVariable";
35      private static final String trustStoreKeyStoreType = "log4j2.trustStoreKeyStoreType";
36      private static final String trustStoreKeyManagerFactoryAlgorithm = "log4j2.trustStoreKeyManagerFactoryAlgorithm";
37      private static final String keyStoreLocation = "log4j2.keyStoreLocation";
38      private static final String keyStorePassword = "log4j2.keyStorePassword";
39      private static final String keyStorePasswordFile = "log4j2.keyStorePasswordFile";
40      private static final String keyStorePasswordEnvVar = "log4j2.keyStorePasswordEnvironmentVariable";
41      private static final String keyStoreType = "log4j2.keyStoreType";
42      private static final String keyStoreKeyManagerFactoryAlgorithm = "log4j2.keyStoreKeyManagerFactoryAlgorithm";
43      private static final String verifyHostName = "log4j2.sslVerifyHostName";
44  
45      static {
46          PropertiesUtil props = PropertiesUtil.getProperties();
47          KeyStoreConfiguration keyStoreConfiguration = null;
48          TrustStoreConfiguration trustStoreConfiguration = null;
49          String location = props.getStringProperty(trustStorelocation);
50          if (location != null) {
51              String password = props.getStringProperty(trustStorePassword);
52              char[] passwordChars = null;
53              if (password != null) {
54                  passwordChars = password.toCharArray();
55              }
56              try {
57                  trustStoreConfiguration = TrustStoreConfiguration.createKeyStoreConfiguration(location, passwordChars,
58                      props.getStringProperty(trustStorePasswordEnvVar), props.getStringProperty(trustStorePasswordFile),
59                      props.getStringProperty(trustStoreKeyStoreType), props.getStringProperty(trustStoreKeyManagerFactoryAlgorithm));
60              } catch (Exception ex) {
61                  LOGGER.warn("Unable to create trust store configuration due to: {} {}", ex.getClass().getName(),
62                      ex.getMessage());
63              }
64          }
65          location = props.getStringProperty(keyStoreLocation);
66          if (location != null) {
67              String password = props.getStringProperty(keyStorePassword);
68              char[] passwordChars = null;
69              if (password != null) {
70                  passwordChars = password.toCharArray();
71              }
72              try {
73                  keyStoreConfiguration = KeyStoreConfiguration.createKeyStoreConfiguration(location, passwordChars,
74                      props.getStringProperty(keyStorePasswordEnvVar), props.getStringProperty(keyStorePasswordFile),
75                      props.getStringProperty(keyStoreType), props.getStringProperty(keyStoreKeyManagerFactoryAlgorithm));
76              } catch (Exception ex) {
77                  LOGGER.warn("Unable to create key store configuration due to: {} {}", ex.getClass().getName(),
78                      ex.getMessage());
79              }
80          }
81          if (trustStoreConfiguration != null || keyStoreConfiguration != null) {
82              boolean isVerifyHostName = props.getBooleanProperty(verifyHostName, false);
83              sslConfiguration = SslConfiguration.createSSLConfiguration("https", keyStoreConfiguration,
84                  trustStoreConfiguration, isVerifyHostName);
85          }
86      }
87  
88      public static SslConfiguration getSslConfiguration() {
89          return sslConfiguration;
90      }
91  }