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.mapred.JobConf;
022import org.apache.oozie.ErrorCode;
023import org.apache.oozie.action.ActionExecutor.Context;
024import org.apache.oozie.util.XLog;
025
026/**
027 * Credentials implementation to store in jobConf, HCat-specific properties such as Principal and Uri
028 * User specifies these credential properties along with the action configuration
029 * The jobConf is used further to pass credentials to the tasks while running
030 * Oozie server should be configured to use this Credentials class by including it via property 'oozie.credentials.credentialclasses'
031 * User can extend the parent class to implement own class as well
032 * for handling custom token-based credentials and add to the above server property
033 */
034public class HCatCredentials extends Credentials {
035
036    private static final String HCAT_METASTORE_PRINCIPAL = "hcat.metastore.principal";
037    private static final String HCAT_METASTORE_URI = "hcat.metastore.uri";
038    private static final String HIVE_METASTORE_PRINCIPAL = "hive.metastore.kerberos.principal";
039    private static final String HIVE_METASTORE_URI = "hive.metastore.uris";
040
041    /* (non-Javadoc)
042     * @see org.apache.oozie.action.hadoop.Credentials#addtoJobConf(org.apache.hadoop.mapred.JobConf, org.apache.oozie.action.hadoop.CredentialsProperties, org.apache.oozie.action.ActionExecutor.Context)
043     */
044    @Override
045    public void addtoJobConf(JobConf jobconf, CredentialsProperties props, Context context) throws Exception {
046        try {
047            String principal = props.getProperties().get(HCAT_METASTORE_PRINCIPAL) == null
048                    ? props.getProperties().get(HIVE_METASTORE_PRINCIPAL)
049                    : props.getProperties().get(HCAT_METASTORE_PRINCIPAL);
050            if (principal == null || principal.isEmpty()) {
051                throw new CredentialException(ErrorCode.E0510,
052                        HCAT_METASTORE_PRINCIPAL + " is required to get hcat credential");
053            }
054            String server = props.getProperties().get(HCAT_METASTORE_URI) == null
055                    ? props.getProperties().get(HIVE_METASTORE_URI) : props.getProperties().get(HCAT_METASTORE_URI);
056            if (server == null || server.isEmpty()) {
057                throw new CredentialException(ErrorCode.E0510,
058                        HCAT_METASTORE_URI + " is required to get hcat credential");
059            }
060            HCatCredentialHelper hcch = new HCatCredentialHelper();
061            hcch.set(jobconf, principal, server);
062        }
063        catch (Exception e) {
064            XLog.getLog(getClass()).warn("Exception in addtoJobConf", e);
065            throw e;
066        }
067    }
068}