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.example;
019
020import java.io.IOException;
021import java.net.InetAddress;
022import java.util.Map;
023
024import javax.security.auth.callback.Callback;
025import javax.security.auth.callback.CallbackHandler;
026import javax.security.auth.callback.NameCallback;
027import javax.security.auth.callback.PasswordCallback;
028import javax.security.auth.callback.UnsupportedCallbackException;
029import javax.security.sasl.RealmCallback;
030import javax.security.sasl.RealmChoiceCallback;
031import javax.security.sasl.Sasl;
032import javax.security.sasl.SaslClient;
033
034import org.apache.hadoop.conf.Configuration;
035import org.apache.hadoop.hbase.security.SaslUtil;
036import org.apache.hadoop.hbase.security.SecurityInfo;
037import org.apache.hadoop.hbase.security.User;
038import org.apache.hadoop.hbase.security.provider.SaslClientAuthenticationProvider;
039import org.apache.hadoop.hbase.util.Bytes;
040import org.apache.hadoop.security.token.Token;
041import org.apache.hadoop.security.token.TokenIdentifier;
042import org.apache.yetus.audience.InterfaceAudience;
043
044import org.apache.hadoop.hbase.shaded.protobuf.generated.RPCProtos.UserInformation;
045
046@InterfaceAudience.Private
047public class ShadeSaslClientAuthenticationProvider extends ShadeSaslAuthenticationProvider
048    implements SaslClientAuthenticationProvider {
049
050  @Override
051  public SaslClient createClient(Configuration conf, InetAddress serverAddr,
052      SecurityInfo securityInfo, Token<? extends TokenIdentifier> token, boolean fallbackAllowed,
053      Map<String, String> saslProps) throws IOException {
054    return Sasl.createSaslClient(new String[] { getSaslAuthMethod().getSaslMechanism()}, null, null,
055        SaslUtil.SASL_DEFAULT_REALM, saslProps, new ShadeSaslClientCallbackHandler(token));
056  }
057
058  @Override
059  public UserInformation getUserInfo(User user) {
060    UserInformation.Builder userInfoPB = UserInformation.newBuilder();
061    userInfoPB.setEffectiveUser(user.getUGI().getUserName());
062    return userInfoPB.build();
063  }
064
065  static class ShadeSaslClientCallbackHandler implements CallbackHandler {
066    private final String username;
067    private final char[] password;
068    public ShadeSaslClientCallbackHandler(
069        Token<? extends TokenIdentifier> token) throws IOException {
070      TokenIdentifier id = token.decodeIdentifier();
071      if (id == null) {
072        // Something is wrong with the environment if we can't get our Identifier back out.
073        throw new IllegalStateException("Could not extract Identifier from Token");
074      }
075      this.username = id.getUser().getUserName();
076      this.password = Bytes.toString(token.getPassword()).toCharArray();
077    }
078
079    @Override
080    public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
081      NameCallback nc = null;
082      PasswordCallback pc = null;
083      RealmCallback rc = null;
084      for (Callback callback : callbacks) {
085        if (callback instanceof RealmChoiceCallback) {
086          continue;
087        } else if (callback instanceof NameCallback) {
088          nc = (NameCallback) callback;
089        } else if (callback instanceof PasswordCallback) {
090          pc = (PasswordCallback) callback;
091        } else if (callback instanceof RealmCallback) {
092          rc = (RealmCallback) callback;
093        } else {
094          throw new UnsupportedCallbackException(callback, "Unrecognized SASL client callback");
095        }
096      }
097      if (nc != null) {
098        nc.setName(username);
099      }
100      if (pc != null) {
101        pc.setPassword(password);
102      }
103      if (rc != null) {
104        rc.setText(rc.getDefaultText());
105      }
106    }
107  }
108}