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 java.io.IOException; 022import java.util.HashMap; 023import java.util.Map; 024 025import com.google.common.annotations.VisibleForTesting; 026import org.apache.hadoop.io.Text; 027import org.apache.hadoop.security.UserGroupInformation; 028import org.apache.hadoop.security.token.Token; 029import org.apache.oozie.service.ConfigurationService; 030import org.apache.oozie.util.StringUtils; 031import org.apache.oozie.util.XLog; 032 033public class CredentialsProviderFactory { 034 public static final String CRED_KEY = "oozie.credentials.credentialclasses"; 035 private static final XLog LOG = XLog.getLog(CredentialsProviderFactory.class); 036 public static final String HDFS = "hdfs"; 037 public static final String YARN = "yarnRM"; 038 public static final String JHS = "jhs"; 039 private static CredentialsProviderFactory instance; 040 private final Map<String, Class<? extends CredentialsProvider>> providerCache; 041 042 @VisibleForTesting 043 static void destroy() { 044 instance = null; 045 } 046 047 public static CredentialsProviderFactory getInstance() throws Exception { 048 if(instance == null) { 049 instance = new CredentialsProviderFactory(); 050 } 051 return instance; 052 } 053 054 private CredentialsProviderFactory() throws Exception { 055 providerCache = new HashMap<>(); 056 for (String function : ConfigurationService.getStrings(CRED_KEY)) { 057 function = StringUtils.trim(function); 058 LOG.debug("Creating Credential class for : " + function); 059 String[] str = function.split("="); 060 if (str.length > 0) { 061 String type = str[0]; 062 String classname = str[1]; 063 if (classname != null) { 064 LOG.debug("Creating Credential type : '{0}', class Name : '{1}'", type, classname); 065 Class<?> klass = null; 066 try { 067 klass = Thread.currentThread().getContextClassLoader().loadClass(classname); 068 } 069 catch (ClassNotFoundException ex) { 070 LOG.warn("Exception while loading the class '{0}'", classname, ex); 071 throw ex; 072 } 073 providerCache.put(type, (Class<CredentialsProvider>) klass); 074 } else { 075 LOG.warn("Credential provider class is null for '{0}', skipping", type); 076 } 077 } 078 } 079 providerCache.put(HDFS, HDFSCredentials.class); 080 providerCache.put(YARN, YarnRMCredentials.class); 081 providerCache.put(JHS, JHSCredentials.class); 082 } 083 084 static Text getUniqueAlias(Token<?> token) { 085 return new Text(String.format("%s_%s_%d", token.getKind().toString(), 086 token.getService().toString(), System.currentTimeMillis())); 087 } 088 089 /** 090 * Create Credential object 091 * 092 * @param type 093 * @return Credential object 094 * @throws Exception 095 */ 096 public CredentialsProvider createCredentialsProvider(String type) throws Exception { 097 Class<? extends CredentialsProvider> providerClass = providerCache.get(type); 098 if(providerClass == null){ 099 return null; 100 } 101 return providerClass.newInstance(); 102 } 103 104 /** 105 * Relogs into Kerberos using the Keytab for the Oozie server user. This should be called before attempting to get delegation 106 * tokens via {@link CredentialsProvider} implementations to ensure that the Kerberos credentials are current and won't expire 107 * too soon. 108 * 109 * @throws IOException 110 */ 111 public static void ensureKerberosLogin() throws IOException { 112 LOG.debug("About to relogin from keytab"); 113 UserGroupInformation.getLoginUser().checkTGTAndReloginFromKeytab(); 114 LOG.debug("Relogin from keytab successful"); 115 } 116}