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 HCatClient client = null; 056 try { 057 client = getHCatClient(launcherJobConf, principal, server); 058 XLog.getLog(getClass()).debug( 059 "HCatCredentialHelper: set: User name for which token will be asked from HCat: " 060 + launcherJobConf.get(USER_NAME)); 061 String tokenStrForm = client.getDelegationToken(launcherJobConf.get(USER_NAME), UserGroupInformation 062 .getLoginUser().getShortUserName()); 063 Token<DelegationTokenIdentifier> hcatToken = new Token<DelegationTokenIdentifier>(); 064 hcatToken.decodeFromUrlString(tokenStrForm); 065 launcherJobConf.getCredentials().addToken(new Text("HCat Token"), hcatToken); 066 XLog.getLog(getClass()).debug("Added the HCat token in job conf"); 067 } 068 catch (Exception ex) { 069 XLog.getLog(getClass()).debug("set Exception" + ex.getMessage()); 070 throw ex; 071 } 072 finally { 073 if (client != null) { 074 client.close(); 075 } 076 } 077 } 078 079 /** 080 * Getting the HCat client. 081 * @param launcherJobConf 082 * @param principal 083 * @param server 084 * @return HCatClient 085 * @throws HCatException 086 */ 087 public HCatClient getHCatClient(JobConf launcherJobConf, 088 String principal, String server) throws HCatException { 089 HiveConf hiveConf = null; 090 HCatClient hiveclient = null; 091 hiveConf = new HiveConf(); 092 XLog.getLog(getClass()).debug("getHCatClient: Principal: " + principal + " Server: " + server); 093 // specified a thrift url 094 095 hiveConf.set(HIVE_METASTORE_SASL_ENABLED, "true"); 096 hiveConf.set(HIVE_METASTORE_KERBEROS_PRINCIPAL, principal); 097 hiveConf.set(HIVE_METASTORE_LOCAL, "false"); 098 hiveConf.set(HiveConf.ConfVars.METASTOREURIS.varname, server); 099 String protection = launcherJobConf.get(HADOOP_RPC_PROTECTION, 100 SaslRpcServer.QualityOfProtection.AUTHENTICATION.name() 101 .toLowerCase()); 102 XLog.getLog(getClass()).debug("getHCatClient, setting rpc protection to " + protection); 103 hiveConf.set(HADOOP_RPC_PROTECTION, protection); 104 105 hiveclient = HCatClient.create(hiveConf); 106 return hiveclient; 107 } 108}