1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
95
96
97
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 }