Example usage for org.apache.hadoop.yarn.client ClientRMProxy getRMDelegationTokenService

List of usage examples for org.apache.hadoop.yarn.client ClientRMProxy getRMDelegationTokenService

Introduction

In this page you can find the example usage for org.apache.hadoop.yarn.client ClientRMProxy getRMDelegationTokenService.

Prototype

@Unstable
public static Text getRMDelegationTokenService(Configuration conf) 

Source Link

Document

Get the token service name to be used for RMDelegationToken.

Usage

From source file:alluxio.yarn.Client.java

License:Apache License

private void setupContainerLaunchContext() throws IOException, YarnException {
    Map<String, String> applicationMasterArgs = ImmutableMap.<String, String>of("-num_workers",
            Integer.toString(mNumWorkers), "-master_address", mMasterAddress, "-resource_path", mResourcePath);

    final String amCommand = YarnUtils.buildCommand(YarnContainerType.APPLICATION_MASTER,
            applicationMasterArgs);/*from  w w w  .ja v a  2 s  .  co  m*/

    System.out.println("ApplicationMaster command: " + amCommand);
    mAmContainer.setCommands(Collections.singletonList(amCommand));

    // Setup local resources
    Map<String, LocalResource> localResources = new HashMap<String, LocalResource>();
    localResources.put("alluxio.tar.gz",
            YarnUtils.createLocalResourceOfFile(mYarnConf, mResourcePath + "/alluxio.tar.gz"));
    localResources.put("alluxio-yarn-setup.sh",
            YarnUtils.createLocalResourceOfFile(mYarnConf, mResourcePath + "/alluxio-yarn-setup.sh"));
    localResources.put("alluxio.jar",
            YarnUtils.createLocalResourceOfFile(mYarnConf, mResourcePath + "/alluxio.jar"));
    mAmContainer.setLocalResources(localResources);

    // Setup CLASSPATH for ApplicationMaster
    Map<String, String> appMasterEnv = new HashMap<String, String>();
    setupAppMasterEnv(appMasterEnv);
    mAmContainer.setEnvironment(appMasterEnv);

    // Set up security tokens for launching our ApplicationMaster container.
    if (UserGroupInformation.isSecurityEnabled()) {
        Credentials credentials = new Credentials();
        String tokenRenewer = mYarnConf.get(YarnConfiguration.RM_PRINCIPAL);
        if (tokenRenewer == null || tokenRenewer.length() == 0) {
            throw new IOException("Can't get Master Kerberos principal for the RM to use as renewer");
        }
        org.apache.hadoop.fs.FileSystem fs = org.apache.hadoop.fs.FileSystem.get(mYarnConf);
        // getting tokens for the default file-system.
        final Token<?>[] tokens = fs.addDelegationTokens(tokenRenewer, credentials);
        if (tokens != null) {
            for (Token<?> token : tokens) {
                LOG.info("Got dt for " + fs.getUri() + "; " + token);
            }
        }
        // getting yarn resource manager token
        org.apache.hadoop.conf.Configuration config = mYarnClient.getConfig();
        Token<TokenIdentifier> token = ConverterUtils.convertFromYarn(
                mYarnClient.getRMDelegationToken(new org.apache.hadoop.io.Text(tokenRenewer)),
                ClientRMProxy.getRMDelegationTokenService(config));
        LOG.info("Added RM delegation token: " + token);
        credentials.addToken(token.getService(), token);

        DataOutputBuffer dob = new DataOutputBuffer();
        credentials.writeTokenStorageToStream(dob);
        ByteBuffer buffer = ByteBuffer.wrap(dob.getData(), 0, dob.getLength());
        mAmContainer.setTokens(buffer);
    }
}

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

License:Apache License

/**
 * Add an RM_DELEGATION_TOKEN to the {@link Credentials} provided.
 *
 * @param credentials the credentials object which is updated
 * @param config launcher AM configuration
 * @param props properties for getting credential token or certificate
 * @param context workflow context/*  www  . j  ava 2  s. c  om*/
 * @throws Exception thrown if failed
 */
@Override
public void updateCredentials(Credentials credentials, Configuration config, CredentialsProperties props,
        ActionExecutor.Context context) throws Exception {
    Text rmDelegationTokenService = ClientRMProxy.getRMDelegationTokenService(config);
    if (rmDelegationTokenService == null) {
        throw new CredentialException(ErrorCode.E0512, "Can't create RMDelegationTokenService");
    }
    try (YarnClient yarnClient = Services.get().get(HadoopAccessorService.class)
            .createYarnClient(context.getWorkflow().getUser(), config)) {
        org.apache.hadoop.yarn.api.records.Token rmDelegationToken = yarnClient
                .getRMDelegationToken(new Text(new HadoopTokenHelper().getServerPrincipal(config)));
        if (rmDelegationToken == null) {
            throw new CredentialException(ErrorCode.E0512, "Returned token is null");
        }
        Token<TokenIdentifier> rmToken = ConverterUtils.convertFromYarn(rmDelegationToken,
                rmDelegationTokenService);
        credentials.addToken(rmDelegationTokenService, rmToken);
    } catch (Exception e) {
        XLog.getLog(getClass()).debug("Exception in updateCredentials", e);
        throw e;
    }
}

From source file:org.apache.slider.core.launch.CredentialUtils.java

License:Apache License

/**
 * Create and add an RM delegation token to the credentials
 * @param yarnClient Yarn Client// ww  w  .java 2 s .  c  o m
 * @param credentials to add token to
 * @return the token which was added
 * @throws IOException
 * @throws YarnException
 */
public static Token<TokenIdentifier> addRMDelegationToken(YarnClient yarnClient, Credentials credentials)
        throws IOException, YarnException {
    Configuration conf = yarnClient.getConfig();
    Text rmPrincipal = new Text(CredentialUtils.getRMPrincipal(conf));
    Text rmDTService = ClientRMProxy.getRMDelegationTokenService(conf);
    Token<TokenIdentifier> rmDelegationToken = ConverterUtils
            .convertFromYarn(yarnClient.getRMDelegationToken(rmPrincipal), rmDTService);
    credentials.addToken(rmDelegationToken.getService(), rmDelegationToken);
    return rmDelegationToken;
}