View Javadoc

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  package org.apache.hadoop.hbase.security;
19  
20  import org.apache.hadoop.conf.Configuration;
21  import org.apache.hadoop.fs.CommonConfigurationKeys;
22  import org.apache.hadoop.hbase.HBaseConfiguration;
23  
24  import com.google.common.base.Strings;
25  
26  class HBaseKerberosUtils {
27    public static final String KRB_PRINCIPAL = "hbase.regionserver.kerberos.principal";
28    public static final String KRB_KEYTAB_FILE = "hbase.regionserver.keytab.file";
29  
30    static boolean isKerberosPropertySetted() {
31      String krbPrincipal = System.getProperty(KRB_PRINCIPAL);
32      String krbKeytab = System.getProperty(KRB_KEYTAB_FILE);
33      if (Strings.isNullOrEmpty(krbPrincipal) || Strings.isNullOrEmpty(krbKeytab)) {
34        return false;
35      }
36      return true;
37    }
38  
39    static void setPrincipalForTesting(String principal) {
40      setSystemProperty(KRB_PRINCIPAL, principal);
41    }
42  
43    static void setKeytabFileForTesting(String keytabFile) {
44      setSystemProperty(KRB_KEYTAB_FILE, keytabFile);
45    }
46  
47    static void setSystemProperty(String propertyName, String propertyValue) {
48      System.setProperty(propertyName, propertyValue);
49    }
50  
51    static String getKeytabFileForTesting() {
52      return System.getProperty(KRB_KEYTAB_FILE);
53    }
54  
55    static String getPrincipalForTesting() {
56      return System.getProperty(KRB_PRINCIPAL);
57    }
58  
59    static Configuration getConfigurationWoPrincipal() {
60      Configuration conf = HBaseConfiguration.create();
61      conf.set(CommonConfigurationKeys.HADOOP_SECURITY_AUTHENTICATION, "kerberos");
62      conf.set("hbase.security.authentication", "kerberos");
63      conf.setBoolean("hbase.security.authorization", true);
64      return conf;
65    }
66  
67    static Configuration getSecuredConfiguration() {
68      Configuration conf = HBaseConfiguration.create();
69      conf.set(CommonConfigurationKeys.HADOOP_SECURITY_AUTHENTICATION, "kerberos");
70      conf.set("hbase.security.authentication", "kerberos");
71      conf.setBoolean("hbase.security.authorization", true);
72      conf.set(KRB_KEYTAB_FILE, System.getProperty(KRB_KEYTAB_FILE));
73      conf.set(KRB_PRINCIPAL, System.getProperty(KRB_PRINCIPAL));
74      return conf;
75    }
76  }