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.util.Map;
022import java.util.concurrent.atomic.AtomicReference;
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.AuthorizeCallback;
030import javax.security.sasl.RealmCallback;
031import javax.security.sasl.Sasl;
032import javax.security.sasl.SaslServer;
033
034import org.apache.hadoop.hbase.security.AccessDeniedException;
035import org.apache.hadoop.hbase.security.HBaseSaslRpcServer;
036import org.apache.hadoop.hbase.security.SaslUtil;
037import org.apache.hadoop.security.UserGroupInformation;
038import org.apache.hadoop.security.token.SecretManager;
039import org.apache.hadoop.security.token.SecretManager.InvalidToken;
040import org.apache.hadoop.security.token.TokenIdentifier;
041import org.apache.yetus.audience.InterfaceAudience;
042import org.slf4j.Logger;
043import org.slf4j.LoggerFactory;
044
045@InterfaceAudience.Private
046public class DigestSaslServerAuthenticationProvider extends DigestSaslAuthenticationProvider
047    implements SaslServerAuthenticationProvider {
048  private static final Logger LOG = LoggerFactory.getLogger(
049      DigestSaslServerAuthenticationProvider.class);
050
051  private AtomicReference<UserGroupInformation> attemptingUser = new AtomicReference<>(null);
052
053  @Override
054  public AttemptingUserProvidingSaslServer createServer(
055      SecretManager<TokenIdentifier> secretManager,
056      Map<String, String> saslProps) throws IOException {
057    if (secretManager == null) {
058      throw new AccessDeniedException("Server is not configured to do DIGEST authentication.");
059    }
060    final SaslServer server = Sasl.createSaslServer(getSaslAuthMethod().getSaslMechanism(), null,
061      SaslUtil.SASL_DEFAULT_REALM, saslProps,
062      new SaslDigestCallbackHandler(secretManager, attemptingUser));
063
064    return new AttemptingUserProvidingSaslServer(server, () -> attemptingUser.get());
065  }
066
067  /** CallbackHandler for SASL DIGEST-MD5 mechanism */
068  private static class SaslDigestCallbackHandler implements CallbackHandler {
069    private final SecretManager<TokenIdentifier> secretManager;
070    private final AtomicReference<UserGroupInformation> attemptingUser;
071
072    public SaslDigestCallbackHandler(SecretManager<TokenIdentifier> secretManager,
073        AtomicReference<UserGroupInformation> attemptingUser) {
074      this.secretManager = secretManager;
075      this.attemptingUser = attemptingUser;
076    }
077
078    private char[] getPassword(TokenIdentifier tokenid) throws InvalidToken {
079      return SaslUtil.encodePassword(secretManager.retrievePassword(tokenid));
080    }
081
082    /** {@inheritDoc} */
083    @Override
084    public void handle(Callback[] callbacks) throws InvalidToken, UnsupportedCallbackException {
085      NameCallback nc = null;
086      PasswordCallback pc = null;
087      AuthorizeCallback ac = null;
088      for (Callback callback : callbacks) {
089        if (callback instanceof AuthorizeCallback) {
090          ac = (AuthorizeCallback) callback;
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          continue; // realm is ignored
097        } else {
098          throw new UnsupportedCallbackException(callback, "Unrecognized SASL DIGEST-MD5 Callback");
099        }
100      }
101      if (pc != null) {
102        TokenIdentifier tokenIdentifier = HBaseSaslRpcServer.getIdentifier(
103            nc.getDefaultName(), secretManager);
104        attemptingUser.set(tokenIdentifier.getUser());
105        char[] password = getPassword(tokenIdentifier);
106        if (LOG.isTraceEnabled()) {
107          LOG.trace("SASL server DIGEST-MD5 callback: setting password for client: {}",
108              tokenIdentifier.getUser());
109        }
110        pc.setPassword(password);
111      }
112      if (ac != null) {
113        // The authentication ID is the identifier (username) of the user who authenticated via
114        // SASL (the one who provided credentials). The authorization ID is who the remote user
115        // "asked" to be once they authenticated. This is akin to the UGI/JAAS "doAs" notion, e.g.
116        // authentication ID is the "real" user and authorization ID is the "proxy" user.
117        //
118        // For DelegationTokens: we do not expect any remote user with a delegation token to execute
119        // any RPCs as a user other than themselves. We disallow all cases where the real user
120        // does not match who the remote user wants to execute a request as someone else.
121        String authenticatedUserId = ac.getAuthenticationID();
122        String userRequestedToExecuteAs = ac.getAuthorizationID();
123        if (authenticatedUserId.equals(userRequestedToExecuteAs)) {
124          ac.setAuthorized(true);
125          if (LOG.isTraceEnabled()) {
126            String username = HBaseSaslRpcServer.getIdentifier(
127                userRequestedToExecuteAs, secretManager).getUser().getUserName();
128            LOG.trace(
129              "SASL server DIGEST-MD5 callback: setting " + "canonicalized client ID: " + username);
130          }
131          ac.setAuthorizedID(userRequestedToExecuteAs);
132        } else {
133          ac.setAuthorized(false);
134        }
135      }
136    }
137  }
138
139  @Override
140  public boolean supportsProtocolAuthentication() {
141    return false;
142  }
143
144  @Override
145  public UserGroupInformation getAuthorizedUgi(String authzId,
146      SecretManager<TokenIdentifier> secretManager) throws IOException {
147    UserGroupInformation authorizedUgi;
148    TokenIdentifier tokenId = HBaseSaslRpcServer.getIdentifier(authzId, secretManager);
149    authorizedUgi = tokenId.getUser();
150    if (authorizedUgi == null) {
151      throw new AccessDeniedException(
152          "Can't retrieve username from tokenIdentifier.");
153    }
154    authorizedUgi.addTokenIdentifier(tokenId);
155    authorizedUgi.setAuthenticationMethod(getSaslAuthMethod().getAuthMethod());
156    return authorizedUgi;
157  }
158}