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