1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 package org.apache.zookeeper.common; 20 21 /** 22 * This enum represents the file type of a KeyStore or TrustStore. 23 * Currently, JKS (Java keystore), PEM, PKCS12, and BCFKS types are supported. 24 */ 25 public enum KeyStoreFileType { 26 JKS(".jks"), 27 PEM(".pem"), 28 PKCS12(".p12"), 29 BCFKS(".bcfks"); 30 31 private final String defaultFileExtension; 32 KeyStoreFileType(String defaultFileExtension)33 KeyStoreFileType(String defaultFileExtension) { 34 this.defaultFileExtension = defaultFileExtension; 35 } 36 37 /** 38 * The property string that specifies that a key store or trust store 39 * should use this store file type. 40 */ getPropertyValue()41 public String getPropertyValue() { 42 return this.name(); 43 } 44 45 /** 46 * The file extension that is associated with this file type. 47 */ getDefaultFileExtension()48 public String getDefaultFileExtension() { 49 return defaultFileExtension; 50 } 51 52 /** 53 * Converts a property value to a StoreFileType enum. If the property value 54 * is <code>null</code> or an empty string, returns <code>null</code>. 55 * @param propertyValue the property value. 56 * @return the KeyStoreFileType, or <code>null</code> if 57 * <code>propertyValue</code> is <code>null</code> or empty. 58 * @throws IllegalArgumentException if <code>propertyValue</code> is not 59 * one of "JKS", "PEM", "BCFKS", "PKCS12", or empty/null. 60 */ fromPropertyValue(String propertyValue)61 public static KeyStoreFileType fromPropertyValue(String propertyValue) { 62 if (propertyValue == null || propertyValue.length() == 0) { 63 return null; 64 } 65 return KeyStoreFileType.valueOf(propertyValue.toUpperCase()); 66 } 67 68 /** 69 * Detects the type of KeyStore / TrustStore file from the file extension. 70 * If the file name ends with ".jks", returns <code>StoreFileType.JKS</code>. 71 * If the file name ends with ".pem", returns <code>StoreFileType.PEM</code>. 72 * If the file name ends with ".p12", returns <code>StoreFileType.PKCS12</code>. 73 * If the file name ends with ".bckfs", returns <code>StoreFileType.BCKFS</code>. 74 * Otherwise, throws an IllegalArgumentException. 75 * @param filename the filename of the key store or trust store file. 76 * @return a KeyStoreFileType. 77 * @throws IllegalArgumentException if the filename does not end with 78 * ".jks", ".pem", "p12" or "bcfks". 79 */ fromFilename(String filename)80 public static KeyStoreFileType fromFilename(String filename) { 81 int i = filename.lastIndexOf('.'); 82 if (i >= 0) { 83 String extension = filename.substring(i); 84 for (KeyStoreFileType storeFileType : KeyStoreFileType.values()) { 85 if (storeFileType.getDefaultFileExtension().equals(extension)) { 86 return storeFileType; 87 } 88 } 89 } 90 throw new IllegalArgumentException("Unable to auto-detect store file type from file name: " + filename); 91 } 92 93 /** 94 * If <code>propertyValue</code> is not null or empty, returns the result 95 * of <code>KeyStoreFileType.fromPropertyValue(propertyValue)</code>. Else, 96 * returns the result of <code>KeyStoreFileType.fromFileName(filename)</code>. 97 * @param propertyValue property value describing the KeyStoreFileType, or 98 * null/empty to auto-detect the type from the file 99 * name. 100 * @param filename file name of the key store file. The file extension is 101 * used to auto-detect the KeyStoreFileType when 102 * <code>propertyValue</code> is null or empty. 103 * @return a KeyStoreFileType. 104 * @throws IllegalArgumentException if <code>propertyValue</code> is not 105 * one of "JKS", "PEM", "PKCS12", "BCFKS", or empty/null. 106 * @throws IllegalArgumentException if <code>propertyValue</code>is empty 107 * or null and the type could not be determined from the file name. 108 */ fromPropertyValueOrFileName(String propertyValue, String filename)109 public static KeyStoreFileType fromPropertyValueOrFileName(String propertyValue, String filename) { 110 KeyStoreFileType result = KeyStoreFileType.fromPropertyValue(propertyValue); 111 if (result == null) { 112 result = KeyStoreFileType.fromFilename(filename); 113 } 114 return result; 115 } 116 } 117