All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.cdap.cdap.security.hive.JobHistoryServerTokenUtils Maven / Gradle / Ivy

The newest version!
/*
 * Copyright © 2015 Cask Data, Inc.
 *  
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *  
 * http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package io.cdap.cdap.security.hive;

import com.google.common.base.Throwables;
import com.google.common.net.HostAndPort;
import io.cdap.cdap.common.security.YarnTokenUtils;
import java.io.IOException;
import java.net.InetSocketAddress;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.ClientCache;
import org.apache.hadoop.mapred.ResourceMgrDelegate;
import org.apache.hadoop.mapreduce.v2.api.MRClientProtocol;
import org.apache.hadoop.mapreduce.v2.api.protocolrecords.GetDelegationTokenRequest;
import org.apache.hadoop.mapreduce.v2.api.protocolrecords.impl.pb.GetDelegationTokenRequestPBImpl;
import org.apache.hadoop.security.Credentials;
import org.apache.hadoop.security.UserGroupInformation;
import org.apache.hadoop.security.token.Token;
import org.apache.hadoop.security.token.TokenIdentifier;
import org.apache.hadoop.yarn.conf.YarnConfiguration;
import org.apache.hadoop.yarn.util.ConverterUtils;
import org.apache.twill.internal.yarn.YarnUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Helper class for getting JobHistoryServer security delegation token.
 */
public final class JobHistoryServerTokenUtils {

  private static final Logger LOG = LoggerFactory.getLogger(YarnTokenUtils.class);

  /**
   * Gets a JHS delegation token and stores it in the given Credentials.
   *
   * @return the same Credentials instance as the one given in parameter.
   */
  public static Credentials obtainToken(Configuration configuration, Credentials credentials) {
    if (!UserGroupInformation.isSecurityEnabled()) {
      return credentials;
    }

    String historyServerAddress = configuration.get("mapreduce.jobhistory.address");
    HostAndPort hostAndPort = HostAndPort.fromString(historyServerAddress);
    try {
      ResourceMgrDelegate resourceMgrDelegate = new ResourceMgrDelegate(
          new YarnConfiguration(configuration));
      MRClientCache clientCache = new MRClientCache(configuration, resourceMgrDelegate);
      MRClientProtocol hsProxy = clientCache.getInitializedHSProxy();
      GetDelegationTokenRequest request = new GetDelegationTokenRequestPBImpl();
      request.setRenewer(YarnUtils.getYarnTokenRenewer(configuration));

      InetSocketAddress address = new InetSocketAddress(hostAndPort.getHostText(),
          hostAndPort.getPort());
      Token token =
          ConverterUtils.convertFromYarn(hsProxy.getDelegationToken(request).getDelegationToken(),
              address);

      credentials.addToken(new Text(token.getService()), token);
      LOG.debug("Adding JobHistoryServer delegation token {}.", token);
      return credentials;
    } catch (Exception e) {
      throw Throwables.propagate(e);
    }
  }

  /**
   * Overridden class to expose {@code getInitializedHSProxy}.
   */
  private static class MRClientCache extends ClientCache {

    MRClientCache(Configuration conf, ResourceMgrDelegate rm) {
      super(conf, rm);
    }

    @Override
    public synchronized MRClientProtocol getInitializedHSProxy() throws IOException {
      return super.getInitializedHSProxy();
    }
  }

  private JobHistoryServerTokenUtils() {
  }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy