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