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 java.io.IOException;
021    import java.util.Map;
022    
023    import org.apache.hadoop.conf.Configuration;
024    import org.apache.hadoop.hbase.HBaseConfiguration;
025    import org.apache.hadoop.hbase.security.User;
026    import org.apache.hadoop.mapred.JobConf;
027    import org.apache.oozie.action.ActionExecutor.Context;
028    import org.apache.oozie.action.hadoop.Credentials;
029    import org.apache.oozie.action.hadoop.CredentialsProperties;
030    import org.apache.oozie.util.XLog;
031    import org.apache.hadoop.security.UserGroupInformation;
032    
033    
034    /**
035     * Hbase Credentials implementation to store in jobConf
036     * The jobConf is used further to pass credentials to the tasks while running
037     * Oozie server should be configured to use this Credentials class by including it via property 'oozie.credentials.credentialclasses'
038     *
039     */
040    public class HbaseCredentials extends Credentials {
041    
042    
043        /* (non-Javadoc)
044         * @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)
045         */
046        @Override
047        public void addtoJobConf(JobConf jobConf, CredentialsProperties props, Context context) throws Exception {
048            try {
049                copyHbaseConfToJobConf(jobConf, props);
050                obtainToken(jobConf, context);
051            }
052            catch (Exception e) {
053                XLog.getLog(getClass()).warn("Exception in receiving hbase credentials", e);
054                throw e;
055            }
056        }
057    
058        void copyHbaseConfToJobConf(JobConf jobConf, CredentialsProperties props) {
059            // Create configuration using hbase-site.xml/hbase-default.xml
060            Configuration hbaseConf = HBaseConfiguration.create();
061            // copy cred props to hbaseconf and override if values already exists
062            addPropsConf(props, hbaseConf);
063            // copy conf from hbaseConf to jobConf without overriding the
064            // already existing values of jobConf
065            injectConf(hbaseConf, jobConf);
066        }
067    
068        private void obtainToken(JobConf jobConf, Context context) throws IOException, InterruptedException {
069            String user = context.getWorkflow().getUser();
070            UserGroupInformation ugi =  UserGroupInformation.createProxyUser(user, UserGroupInformation.getLoginUser());
071            User u = User.create(ugi);
072            u.obtainAuthTokenForJob(jobConf);
073        }
074    
075        private void addPropsConf(CredentialsProperties props, Configuration destConf) {
076            for (Map.Entry<String, String> entry : props.getProperties().entrySet()) {
077                destConf.set(entry.getKey(), entry.getValue());
078            }
079        }
080    
081        private void injectConf(Configuration srcConf, Configuration destConf) {
082            for (Map.Entry<String, String> entry : srcConf) {
083                String name = entry.getKey();
084                if (destConf.get(name) == null) {
085                    String value = entry.getValue();
086                    destConf.set(name, value);
087                }
088            }
089        }
090    }