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