View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  package org.apache.hadoop.hbase.security;
20  
21  import org.apache.commons.codec.binary.Base64;
22  import org.apache.hadoop.hbase.classification.InterfaceAudience;
23  
24  import java.util.Map;
25  import java.util.TreeMap;
26  
27  import javax.security.sasl.Sasl;
28  
29  import org.apache.commons.codec.binary.Base64;
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  import org.apache.hadoop.hbase.classification.InterfaceAudience;
33  
34  @InterfaceAudience.Private
35  public class SaslUtil {
36    private static final Log log = LogFactory.getLog(SaslUtil.class);
37    public static final String SASL_DEFAULT_REALM = "default";
38    public static final Map<String, String> SASL_PROPS =
39        new TreeMap<String, String>();
40    public static final int SWITCH_TO_SIMPLE_AUTH = -88;
41  
42    public static enum QualityOfProtection {
43      AUTHENTICATION("auth"),
44      INTEGRITY("auth-int"),
45      PRIVACY("auth-conf");
46  
47      public final String saslQop;
48  
49      private QualityOfProtection(String saslQop) {
50        this.saslQop = saslQop;
51      }
52  
53      public String getSaslQop() {
54        return saslQop;
55      }
56    }
57  
58    /** Splitting fully qualified Kerberos name into parts */
59    public static String[] splitKerberosName(String fullName) {
60      return fullName.split("[/@]");
61    }
62  
63    static String encodeIdentifier(byte[] identifier) {
64      return new String(Base64.encodeBase64(identifier));
65    }
66  
67    static byte[] decodeIdentifier(String identifier) {
68      return Base64.decodeBase64(identifier.getBytes());
69    }
70  
71    static char[] encodePassword(byte[] password) {
72      return new String(Base64.encodeBase64(password)).toCharArray();
73    }
74  
75    /**
76     * Returns {@link org.apache.hadoop.hbase.security.SaslUtil.QualityOfProtection}
77     * corresponding to the given {@code stringQop} value. Returns null if value is
78     * invalid.
79     */
80    public static QualityOfProtection getQop(String stringQop) {
81      QualityOfProtection qop = null;
82      if (QualityOfProtection.AUTHENTICATION.name().toLowerCase().equals(stringQop)
83          || QualityOfProtection.AUTHENTICATION.saslQop.equals(stringQop)) {
84        qop = QualityOfProtection.AUTHENTICATION;
85      } else if (QualityOfProtection.INTEGRITY.name().toLowerCase().equals(stringQop)
86          || QualityOfProtection.INTEGRITY.saslQop.equals(stringQop)) {
87        qop = QualityOfProtection.INTEGRITY;
88      } else if (QualityOfProtection.PRIVACY.name().toLowerCase().equals(stringQop)
89          || QualityOfProtection.PRIVACY.saslQop.equals(stringQop)) {
90        qop = QualityOfProtection.PRIVACY;
91      }
92      if (qop == null) {
93        throw new IllegalArgumentException("Invalid qop: " +  stringQop
94            + ". It must be one of 'authentication', 'integrity', 'privacy'.");
95      }
96      if (QualityOfProtection.AUTHENTICATION.saslQop.equals(stringQop)
97          || QualityOfProtection.INTEGRITY.saslQop.equals(stringQop)
98          || QualityOfProtection.PRIVACY.saslQop.equals(stringQop)) {
99        log.warn("Use authentication/integrity/privacy as value for rpc protection "
100           + "configurations instead of auth/auth-int/auth-conf.");
101     }
102     return qop;
103   }
104 
105   static void initSaslProperties(String rpcProtection) {
106     QualityOfProtection saslQOP = getQop(rpcProtection);
107     if (saslQOP == null) {
108       saslQOP = QualityOfProtection.AUTHENTICATION;
109     }
110     SaslUtil.SASL_PROPS.put(Sasl.QOP, saslQOP.getSaslQop());
111     SaslUtil.SASL_PROPS.put(Sasl.SERVER_AUTH, "true");
112   }
113 }