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