001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache license, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the license for the specific language governing permissions and 015 * limitations under the license. 016 */ 017package org.apache.logging.log4j.core.net.ssl; 018 019import org.apache.logging.log4j.Logger; 020import org.apache.logging.log4j.status.StatusLogger; 021import org.apache.logging.log4j.util.PropertiesUtil; 022 023/** 024 * Creates an SSL configuration from Log4j properties. 025 */ 026public class SslConfigurationFactory { 027 028 private static final Logger LOGGER = StatusLogger.getLogger(); 029 private static SslConfiguration sslConfiguration = null; 030 031 private static final String trustStorelocation = "log4j2.trustStoreLocation"; 032 private static final String trustStorePassword = "log4j2.trustStorePassword"; 033 private static final String trustStorePasswordFile = "log4j2.trustStorePasswordFile"; 034 private static final String trustStorePasswordEnvVar = "log4j2.trustStorePasswordEnvironmentVariable"; 035 private static final String trustStoreKeyStoreType = "log4j2.trustStoreKeyStoreType"; 036 private static final String trustStoreKeyManagerFactoryAlgorithm = "log4j2.trustStoreKeyManagerFactoryAlgorithm"; 037 private static final String keyStoreLocation = "log4j2.keyStoreLocation"; 038 private static final String keyStorePassword = "log4j2.keyStorePassword"; 039 private static final String keyStorePasswordFile = "log4j2.keyStorePasswordFile"; 040 private static final String keyStorePasswordEnvVar = "log4j2.keyStorePasswordEnvironmentVariable"; 041 private static final String keyStoreType = "log4j2.keyStoreType"; 042 private static final String keyStoreKeyManagerFactoryAlgorithm = "log4j2.keyStoreKeyManagerFactoryAlgorithm"; 043 private static final String verifyHostName = "log4j2.sslVerifyHostName"; 044 045 static { 046 PropertiesUtil props = PropertiesUtil.getProperties(); 047 KeyStoreConfiguration keyStoreConfiguration = null; 048 TrustStoreConfiguration trustStoreConfiguration = null; 049 String location = props.getStringProperty(trustStorelocation); 050 if (location != null) { 051 String password = props.getStringProperty(trustStorePassword); 052 char[] passwordChars = null; 053 if (password != null) { 054 passwordChars = password.toCharArray(); 055 } 056 try { 057 trustStoreConfiguration = TrustStoreConfiguration.createKeyStoreConfiguration(location, passwordChars, 058 props.getStringProperty(trustStorePasswordEnvVar), props.getStringProperty(trustStorePasswordFile), 059 props.getStringProperty(trustStoreKeyStoreType), props.getStringProperty(trustStoreKeyManagerFactoryAlgorithm)); 060 } catch (Exception ex) { 061 LOGGER.warn("Unable to create trust store configuration due to: {} {}", ex.getClass().getName(), 062 ex.getMessage()); 063 } 064 } 065 location = props.getStringProperty(keyStoreLocation); 066 if (location != null) { 067 String password = props.getStringProperty(keyStorePassword); 068 char[] passwordChars = null; 069 if (password != null) { 070 passwordChars = password.toCharArray(); 071 } 072 try { 073 keyStoreConfiguration = KeyStoreConfiguration.createKeyStoreConfiguration(location, passwordChars, 074 props.getStringProperty(keyStorePasswordEnvVar), props.getStringProperty(keyStorePasswordFile), 075 props.getStringProperty(keyStoreType), props.getStringProperty(keyStoreKeyManagerFactoryAlgorithm)); 076 } catch (Exception ex) { 077 LOGGER.warn("Unable to create key store configuration due to: {} {}", ex.getClass().getName(), 078 ex.getMessage()); 079 } 080 } 081 if (trustStoreConfiguration != null || keyStoreConfiguration != null) { 082 boolean isVerifyHostName = props.getBooleanProperty(verifyHostName, false); 083 sslConfiguration = SslConfiguration.createSSLConfiguration("https", keyStoreConfiguration, 084 trustStoreConfiguration, isVerifyHostName); 085 } 086 } 087 088 public static SslConfiguration getSslConfiguration() { 089 return sslConfiguration; 090 } 091}