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 */ 018 019package org.apache.oozie.action.hadoop; 020 021import org.apache.hadoop.hive.conf.HiveConf; 022import org.apache.hadoop.hive.metastore.api.MetaException; 023import org.apache.hadoop.io.Text; 024import org.apache.hadoop.mapred.JobConf; 025import org.apache.hadoop.mapreduce.security.token.delegation.DelegationTokenIdentifier; 026import org.apache.hadoop.security.UserGroupInformation; 027import org.apache.hadoop.security.token.Token; 028import org.apache.hadoop.security.SaslRpcServer; 029import org.apache.hive.hcatalog.api.HCatClient; 030import org.apache.hive.hcatalog.common.HCatException; 031import org.apache.oozie.util.XLog; 032 033/** 034 * Helper class to handle the HCat credentials 035 * Performs internally the heavy-lifting of fetching delegation tokens from Hive Metastore, abstracted from the user 036 * Token is added to jobConf 037 */ 038public class HCatCredentialHelper { 039 040 private static final String USER_NAME = "user.name"; 041 // Some Hive Metastore properties 042 private static final String HIVE_METASTORE_SASL_ENABLED = "hive.metastore.sasl.enabled"; 043 private static final String HIVE_METASTORE_KERBEROS_PRINCIPAL = "hive.metastore.kerberos.principal"; 044 private static final String HIVE_METASTORE_LOCAL = "hive.metastore.local"; 045 private static final String HADOOP_RPC_PROTECTION = "hadoop.rpc.protection"; 046 047 /** 048 * This Function will set the HCat token to jobconf 049 * @param launcherJobConf - job conf 050 * @param principal - principal for HCat server 051 * @param server - Serevr URI for HCat server 052 * @throws Exception 053 */ 054 public void set(JobConf launcherJobConf, String principal, String server) throws Exception { 055 try { 056 HCatClient client = getHCatClient(launcherJobConf, principal, server); 057 XLog.getLog(getClass()).debug( 058 "HCatCredentialHelper: set: User name for which token will be asked from HCat: " 059 + launcherJobConf.get(USER_NAME)); 060 String tokenStrForm = client.getDelegationToken(launcherJobConf.get(USER_NAME), UserGroupInformation 061 .getLoginUser().getShortUserName()); 062 Token<DelegationTokenIdentifier> hcatToken = new Token<DelegationTokenIdentifier>(); 063 hcatToken.decodeFromUrlString(tokenStrForm); 064 launcherJobConf.getCredentials().addToken(new Text("HCat Token"), hcatToken); 065 XLog.getLog(getClass()).debug("Added the HCat token in job conf"); 066 } 067 catch (Exception ex) { 068 XLog.getLog(getClass()).debug("set Exception" + ex.getMessage()); 069 throw ex; 070 } 071 } 072 073 /** 074 * Getting the HCat client. 075 * @param jobConf 076 * @param principal 077 * @param server 078 * @return HCatClient 079 * @throws HCatException 080 */ 081 public HCatClient getHCatClient(JobConf launcherJobConf, 082 String principal, String server) throws HCatException { 083 HiveConf hiveConf = null; 084 HCatClient hiveclient = null; 085 hiveConf = new HiveConf(); 086 XLog.getLog(getClass()).debug("getHCatClient: Principal: " + principal + " Server: " + server); 087 // specified a thrift url 088 089 hiveConf.set(HIVE_METASTORE_SASL_ENABLED, "true"); 090 hiveConf.set(HIVE_METASTORE_KERBEROS_PRINCIPAL, principal); 091 hiveConf.set(HIVE_METASTORE_LOCAL, "false"); 092 hiveConf.set(HiveConf.ConfVars.METASTOREURIS.varname, server); 093 String protection = launcherJobConf.get(HADOOP_RPC_PROTECTION, 094 SaslRpcServer.QualityOfProtection.AUTHENTICATION.name() 095 .toLowerCase()); 096 XLog.getLog(getClass()).debug("getHCatClient, setting rpc protection to " + protection); 097 hiveConf.set(HADOOP_RPC_PROTECTION, protection); 098 099 hiveclient = HCatClient.create(hiveConf); 100 return hiveclient; 101 } 102}