001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.io.crypto.tls;
019
020import org.apache.yetus.audience.InterfaceAudience;
021
022/**
023 * This enum represents the file type of a KeyStore or TrustStore. Currently, JKS (Java keystore),
024 * PEM, PKCS12, and BCFKS types are supported.
025 * <p/>
026 * This file has been copied from the Apache ZooKeeper project.
027 * @see <a href=
028 *      "https://github.com/apache/zookeeper/blob/c74658d398cdc1d207aa296cb6e20de00faec03e/zookeeper-server/src/main/java/org/apache/zookeeper/common/KeyStoreFileType.java">Base
029 *      revision</a>
030 */
031@InterfaceAudience.Private
032public enum KeyStoreFileType {
033  JKS(".jks"),
034  PEM(".pem"),
035  PKCS12(".p12"),
036  BCFKS(".bcfks");
037
038  private final String defaultFileExtension;
039
040  KeyStoreFileType(String defaultFileExtension) {
041    this.defaultFileExtension = defaultFileExtension;
042  }
043
044  /**
045   * The property string that specifies that a key store or trust store should use this store file
046   * type.
047   */
048  public String getPropertyValue() {
049    return this.name();
050  }
051
052  /**
053   * The file extension that is associated with this file type.
054   */
055  public String getDefaultFileExtension() {
056    return defaultFileExtension;
057  }
058
059  /**
060   * Converts a property value to a StoreFileType enum. If the property value is <code>null</code>
061   * or an empty string, returns <code>null</code>.
062   * @param propertyValue the property value.
063   * @return the KeyStoreFileType, or <code>null</code> if <code>propertyValue</code> is
064   *         <code>null</code> or empty.
065   * @throws IllegalArgumentException if <code>propertyValue</code> is not one of "JKS", "PEM",
066   *                                  "BCFKS", "PKCS12", or empty/null.
067   */
068  public static KeyStoreFileType fromPropertyValue(String propertyValue) {
069    if (propertyValue == null || propertyValue.length() == 0) {
070      return null;
071    }
072    return KeyStoreFileType.valueOf(propertyValue.toUpperCase());
073  }
074
075  /**
076   * Detects the type of KeyStore / TrustStore file from the file extension. If the file name ends
077   * with ".jks", returns <code>StoreFileType.JKS</code>. If the file name ends with ".pem", returns
078   * <code>StoreFileType.PEM</code>. If the file name ends with ".p12", returns
079   * <code>StoreFileType.PKCS12</code>. If the file name ends with ".bckfs", returns
080   * <code>StoreFileType.BCKFS</code>. Otherwise, throws an IllegalArgumentException.
081   * @param filename the filename of the key store or trust store file.
082   * @return a KeyStoreFileType.
083   * @throws IllegalArgumentException if the filename does not end with ".jks", ".pem", "p12" or
084   *                                  "bcfks".
085   */
086  public static KeyStoreFileType fromFilename(String filename) {
087    int i = filename.lastIndexOf('.');
088    if (i >= 0) {
089      String extension = filename.substring(i);
090      for (KeyStoreFileType storeFileType : KeyStoreFileType.values()) {
091        if (storeFileType.getDefaultFileExtension().equals(extension)) {
092          return storeFileType;
093        }
094      }
095    }
096    throw new IllegalArgumentException(
097      "Unable to auto-detect store file type from file name: " + filename);
098  }
099
100  /**
101   * If <code>propertyValue</code> is not null or empty, returns the result of
102   * <code>KeyStoreFileType.fromPropertyValue(propertyValue)</code>. Else, returns the result of
103   * <code>KeyStoreFileType.fromFileName(filename)</code>.
104   * @param propertyValue property value describing the KeyStoreFileType, or null/empty to
105   *                      auto-detect the type from the file name.
106   * @param filename      file name of the key store file. The file extension is used to auto-detect
107   *                      the KeyStoreFileType when <code>propertyValue</code> is null or empty.
108   * @return a KeyStoreFileType.
109   * @throws IllegalArgumentException if <code>propertyValue</code> is not one of "JKS", "PEM",
110   *                                  "PKCS12", "BCFKS", or empty/null.
111   * @throws IllegalArgumentException if <code>propertyValue</code>is empty or null and the type
112   *                                  could not be determined from the file name.
113   */
114  public static KeyStoreFileType fromPropertyValueOrFileName(String propertyValue,
115    String filename) {
116    KeyStoreFileType result = KeyStoreFileType.fromPropertyValue(propertyValue);
117    if (result == null) {
118      result = KeyStoreFileType.fromFilename(filename);
119    }
120    return result;
121  }
122}