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 */
018package org.apache.oozie.action.hadoop;
019
020import java.io.IOException;
021import java.util.Map;
022
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.hbase.HBaseConfiguration;
025import org.apache.hadoop.hbase.security.User;
026import org.apache.hadoop.mapred.JobConf;
027import org.apache.oozie.action.ActionExecutor.Context;
028import org.apache.oozie.action.hadoop.Credentials;
029import org.apache.oozie.action.hadoop.CredentialsProperties;
030import org.apache.oozie.util.XLog;
031import 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 */
040public 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 cred props to jobconf and override if values already exist
064        addPropsConf(props, jobConf);
065        // copy conf from hbaseConf to jobConf without overriding the
066        // already existing values of jobConf
067        injectConf(hbaseConf, jobConf);
068    }
069
070    private void obtainToken(JobConf jobConf, Context context) throws IOException, InterruptedException {
071        String user = context.getWorkflow().getUser();
072        UserGroupInformation ugi =  UserGroupInformation.createProxyUser(user, UserGroupInformation.getLoginUser());
073        User u = User.create(ugi);
074        u.obtainAuthTokenForJob(jobConf);
075    }
076
077    private void addPropsConf(CredentialsProperties props, Configuration destConf) {
078        for (Map.Entry<String, String> entry : props.getProperties().entrySet()) {
079            destConf.set(entry.getKey(), entry.getValue());
080        }
081    }
082
083    private void injectConf(Configuration srcConf, Configuration destConf) {
084        for (Map.Entry<String, String> entry : srcConf) {
085            String name = entry.getKey();
086            if (destConf.get(name) == null) {
087                String value = entry.getValue();
088                destConf.set(name, value);
089            }
090        }
091    }
092}