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 java.util.Objects;
021
022/**
023 * Base class for instances of {@link KeyStoreLoader} which load the key/trust stores from files on
024 * a filesystem.
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/FileKeyStoreLoader.java">Base
029 *      revision</a>
030 */
031abstract class FileKeyStoreLoader implements KeyStoreLoader {
032  final String keyStorePath;
033  final String trustStorePath;
034  final char[] keyStorePassword;
035  final char[] trustStorePassword;
036
037  FileKeyStoreLoader(String keyStorePath, String trustStorePath, char[] keyStorePassword,
038    char[] trustStorePassword) {
039    this.keyStorePath = keyStorePath;
040    this.trustStorePath = trustStorePath;
041    this.keyStorePassword = keyStorePassword;
042    this.trustStorePassword = trustStorePassword;
043  }
044
045  /**
046   * Base class for builder pattern used by subclasses.
047   * @param <T> the subtype of FileKeyStoreLoader created by the Builder.
048   */
049  static abstract class Builder<T extends FileKeyStoreLoader> {
050    String keyStorePath;
051    String trustStorePath;
052    char[] keyStorePassword;
053    char[] trustStorePassword;
054
055    Builder() {
056    }
057
058    Builder<T> setKeyStorePath(String keyStorePath) {
059      this.keyStorePath = Objects.requireNonNull(keyStorePath);
060      return this;
061    }
062
063    Builder<T> setTrustStorePath(String trustStorePath) {
064      this.trustStorePath = Objects.requireNonNull(trustStorePath);
065      return this;
066    }
067
068    Builder<T> setKeyStorePassword(char[] keyStorePassword) {
069      this.keyStorePassword = Objects.requireNonNull(keyStorePassword);
070      return this;
071    }
072
073    Builder<T> setTrustStorePassword(char[] trustStorePassword) {
074      this.trustStorePassword = Objects.requireNonNull(trustStorePassword);
075      return this;
076    }
077
078    abstract T build();
079  }
080}