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.security.provider; 019 020import java.io.IOException; 021import java.net.InetAddress; 022import java.util.Map; 023import javax.security.sasl.Sasl; 024import javax.security.sasl.SaslClient; 025import org.apache.hadoop.conf.Configuration; 026import org.apache.hadoop.hbase.security.SaslUtil; 027import org.apache.hadoop.hbase.security.User; 028import org.apache.hadoop.security.UserGroupInformation; 029import org.apache.hadoop.security.token.Token; 030import org.apache.hadoop.security.token.TokenIdentifier; 031import org.apache.yetus.audience.InterfaceAudience; 032import org.slf4j.Logger; 033import org.slf4j.LoggerFactory; 034 035import org.apache.hadoop.hbase.shaded.protobuf.generated.RPCProtos.UserInformation; 036 037@InterfaceAudience.Private 038public class GssSaslClientAuthenticationProvider extends GssSaslAuthenticationProvider 039 implements SaslClientAuthenticationProvider { 040 private static final Logger LOG = 041 LoggerFactory.getLogger(GssSaslClientAuthenticationProvider.class); 042 043 @Override 044 public SaslClient createClient(Configuration conf, InetAddress serverAddr, String serverPrincipal, 045 Token<? extends TokenIdentifier> token, boolean fallbackAllowed, Map<String, String> saslProps) 046 throws IOException { 047 LOG.debug("Setting up Kerberos RPC to server={}", serverPrincipal); 048 String[] names = SaslUtil.splitKerberosName(serverPrincipal); 049 if (names.length != 3) { 050 throw new IOException( 051 "Kerberos principal '" + serverPrincipal + "' does not have the expected format"); 052 } 053 return Sasl.createSaslClient(new String[] { getSaslAuthMethod().getSaslMechanism() }, null, 054 names[0], names[1], saslProps, null); 055 } 056 057 @Override 058 public UserInformation getUserInfo(User user) { 059 UserInformation.Builder userInfoPB = UserInformation.newBuilder(); 060 // Send effective user for Kerberos auth 061 userInfoPB.setEffectiveUser(user.getUGI().getUserName()); 062 return userInfoPB.build(); 063 } 064 065 @Override 066 public boolean canRetry() { 067 return true; 068 } 069 070 @Override 071 public void relogin() throws IOException { 072 // Check if UGI thinks we need to do another login 073 if (UserGroupInformation.isLoginKeytabBased()) { 074 UserGroupInformation.getLoginUser().reloginFromKeytab(); 075 } else { 076 UserGroupInformation.getLoginUser().reloginFromTicketCache(); 077 } 078 } 079 080 @Override 081 public UserGroupInformation getRealUser(User user) { 082 final UserGroupInformation ugi = user.getUGI(); 083 // Unwrap the UGI with the real user when we're using Kerberos auth 084 if (ugi != null && ugi.getRealUser() != null) { 085 return ugi.getRealUser(); 086 } 087 088 // Otherwise, use the UGI we were given 089 return ugi; 090 } 091}