Example usage for org.apache.hadoop.security UserGroupInformation getTokens

List of usage examples for org.apache.hadoop.security UserGroupInformation getTokens

Introduction

In this page you can find the example usage for org.apache.hadoop.security UserGroupInformation getTokens.

Prototype

public Collection<Token<? extends TokenIdentifier>> getTokens() 

Source Link

Document

Obtain the collection of tokens associated with this user.

Usage

From source file:org.apache.hive.service.cli.session.SessionUtils.java

License:Apache License

/**
 * Get the string form of the token given a token signature. The signature is used as the value of
 * the "service" field in the token for lookup. Ref: AbstractDelegationTokenSelector in Hadoop. If
 * there exists such a token in the token cache (credential store) of the job, the lookup returns
 * that. This is relevant only when running against a "secure" hadoop release The method gets hold
 * of the tokens if they are set up by hadoop - this should happen on the map/reduce tasks if the
 * client added the tokens into hadoop's credential store in the front end during job submission.
 * The method will select the hive delegation token among the set of tokens and return the string
 * form of it//  w  ww . ja va  2 s .co m
 * 
 * @param tokenSignature
 * @return the string form of the token found
 * @throws IOException
 */
public static String getTokenStrForm(String tokenSignature) throws IOException {
    UserGroupInformation ugi = UserGroupInformation.getCurrentUser();
    TokenSelector<? extends TokenIdentifier> tokenSelector = new DelegationTokenSelector();

    Token<? extends TokenIdentifier> token = tokenSelector
            .selectToken(tokenSignature == null ? new Text() : new Text(tokenSignature), ugi.getTokens());
    return token != null ? token.encodeToUrlString() : null;
}

From source file:org.apache.oozie.action.hadoop.LauncherAM.java

License:Apache License

public static void main(String[] args) throws Exception {
    final LocalFsOperations localFsOperations = new LocalFsOperations();
    final Configuration launcherConf = readLauncherConfiguration(localFsOperations);
    UserGroupInformation.setConfiguration(launcherConf);
    // MRAppMaster adds this call as well, but it's included only in Hadoop 2.9+
    // SecurityUtil.setConfiguration(launcherConf);
    UserGroupInformation ugi = getUserGroupInformation(launcherConf);
    printTokens("Executing Oozie Launcher with tokens:", ugi.getTokens());
    // Executing code inside a doAs with an ugi equipped with correct tokens.
    ugi.doAs(new PrivilegedExceptionAction<Object>() {
        @Override//from  ww w  .j  av a 2 s.c om
        public Object run() throws Exception {
            LauncherAM launcher = new LauncherAM(new AMRMClientAsyncFactory(), new AMRMCallBackHandler(),
                    new HdfsOperations(new SequenceFileWriterFactory()), new LocalFsOperations(),
                    new PrepareActionsHandler(new LauncherURIHandlerFactory(null)),
                    new LauncherAMCallbackNotifierFactory(), new LauncherSecurityManager(),
                    sysenv.getenv(ApplicationConstants.Environment.CONTAINER_ID.name()), launcherConf);
            launcher.run();
            return null;
        }
    });

}

From source file:org.apache.oozie.action.hadoop.LauncherAM.java

License:Apache License

public void run() throws Exception {
    final ErrorHolder errorHolder = new ErrorHolder();
    OozieActionResult actionResult = OozieActionResult.FAILED;
    boolean backgroundAction = false;
    try {//from  w  w  w.j av  a 2s. c  o m

        actionDir = new Path(launcherConf.get(OOZIE_ACTION_DIR_PATH));

        registerWithRM(amrmCallBackHandler);
        // Run user code without the AM_RM_TOKEN so users can't request containers
        UserGroupInformation ugi = getUserGroupInformation(launcherConf, AMRMTokenIdentifier.KIND_NAME);
        printTokens("Executing Action Main with tokens:", ugi.getTokens());
        ugi.doAs(new PrivilegedExceptionAction<Object>() {
            @Override
            public Object run() throws Exception {
                executePrepare(errorHolder);
                printDebugInfo();
                setupMainConfiguration();
                runActionMain(errorHolder);
                return null;
            }
        });

        if (!errorHolder.isPopulated()) {
            handleActionData();
            if (actionData.get(ACTION_DATA_OUTPUT_PROPS) != null) {
                System.out.println();
                System.out.println("Oozie Launcher, capturing output data:");
                System.out.println("=======================");
                System.out.println(actionData.get(ACTION_DATA_OUTPUT_PROPS));
                System.out.println();
                System.out.println("=======================");
                System.out.println();
            }
            if (actionData.get(ACTION_DATA_NEW_ID) != null) {
                System.out.println();
                System.out.println("Oozie Launcher, propagating new Hadoop job id to Oozie");
                System.out.println("=======================");
                System.out.println(actionData.get(ACTION_DATA_NEW_ID));
                System.out.println("=======================");
                System.out.println();
                backgroundAction = true;
            }
        }
    } catch (Exception e) {
        System.out.println("Launcher AM execution failed");
        System.err.println("Launcher AM execution failed");
        e.printStackTrace(System.out);
        e.printStackTrace(System.err);
        if (!errorHolder.isPopulated()) {
            errorHolder.setErrorCause(e);
            errorHolder.setErrorMessage(e.getMessage());
        }
        throw e;
    } finally {
        try {
            ErrorHolder callbackErrorHolder = amrmCallBackHandler.getError();

            if (!errorHolder.isPopulated()) {
                actionResult = backgroundAction ? OozieActionResult.RUNNING : OozieActionResult.SUCCEEDED;
            }

            if (errorHolder.isPopulated()) {
                updateActionDataWithFailure(errorHolder, actionData);
            } else if (callbackErrorHolder != null) { // async error from the callback
                actionResult = OozieActionResult.FAILED;
                updateActionDataWithFailure(callbackErrorHolder, actionData);
            }

            actionData.put(ACTION_DATA_FINAL_STATUS, actionResult.toString());
            hdfsOperations.uploadActionDataToHDFS(launcherConf, actionDir, actionData);
        } finally {
            try {
                unregisterWithRM(actionResult, errorHolder.getErrorMessage());
            } finally {
                LauncherAMCallbackNotifier cn = callbackNotifierFactory.createCallbackNotifier(launcherConf);
                cn.notifyURL(actionResult);
            }
        }
    }
}