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.core.config.plugins.Plugin;
20  import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
21  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
22  import org.apache.logging.log4j.status.StatusLogger;
23  
24  import java.io.*;
25  import java.security.KeyStore;
26  import java.security.KeyStoreException;
27  import java.security.NoSuchAlgorithmException;
28  import java.security.cert.CertificateException;
29  
30  /**
31   * The TrustStoreConfiguration.
32   */
33  @Plugin(name = "trustStore", category = "Core", printObject = true)
34  public class TrustStoreConfiguration extends StoreConfiguration {
35      private KeyStore trustStore;
36      private String trustStoreType;
37  
38      public TrustStoreConfiguration(String location, String password) {
39          super(location, password);
40          trustStoreType = SSLConfigurationDefaults.KEYSTORE_TYPE;
41          trustStore = null;
42      }
43  
44      @Override
45      protected void load() throws StoreConfigurationException {
46          KeyStore ts = null;
47          InputStream in = null;
48  
49          LOGGER.debug("Loading truststore from file with params(location={})", getLocation());
50          try {
51              if (getLocation() == null) {
52                  throw new IOException("The location is null");
53              }
54              ts = KeyStore.getInstance(trustStoreType);
55              in = new FileInputStream(getLocation());
56              ts.load(in, getPasswordAsCharArray());
57          }
58          catch (CertificateException e) {
59              LOGGER.error("No Provider supports a KeyStoreSpi implementation for the specified type {}", trustStoreType);
60              throw new StoreConfigurationException(e);
61          } catch (NoSuchAlgorithmException e) {
62              LOGGER.error("The algorithm used to check the integrity of the keystore cannot be found");
63              throw new StoreConfigurationException(e);
64          } catch (KeyStoreException e) {
65              LOGGER.error(e);
66              throw new StoreConfigurationException(e);
67          } catch (FileNotFoundException e) {
68              LOGGER.error("The keystore file({}) is not found", getLocation());
69              throw new StoreConfigurationException(e);
70          } catch (IOException e) {
71              LOGGER.error("Something is wrong with the format of the truststore or the given password: {}", e.getMessage());
72              throw new StoreConfigurationException(e);
73          } finally {
74              try {
75                  if (in != null) {
76                      in.close();
77                  }
78              }
79              catch (Exception e) {
80                  LOGGER.warn("Error closing {}", getLocation(), e);
81              }
82          }
83          trustStore = ts;
84          LOGGER.debug("Truststore successfully loaded with params(location={})", getLocation());
85      }
86  
87      public KeyStore getTrustStore() throws StoreConfigurationException {
88          if (trustStore == null) {
89              load();
90          }
91          return trustStore;
92      }
93  
94      /**
95       * Create a TrustStoreConfiguration.
96       * @param location The location of the TrustStore.
97       * @param password The password required to access the TrustStore.
98       * @return
99       */
100     @PluginFactory
101     public static TrustStoreConfiguration createTrustStoreConfiguration(@PluginAttribute("location") String location,
102                                                                         @PluginAttribute("password") String password){
103         return new TrustStoreConfiguration(location, password);
104     }
105 }