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

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

Introduction

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

Prototype

@InterfaceAudience.Private
    @InterfaceStability.Unstable
    @VisibleForTesting
    public static void setLoginUser(UserGroupInformation ugi) 

Source Link

Usage

From source file:com.github.sakserv.minicluster.impl.KdcLocalCluster.java

License:Apache License

@Override
public void start() throws Exception {

    LOG.info("KDC: Starting MiniKdc");
    configure();//from w ww .jav a  2s .  c o  m
    miniKdc = new MiniKdc(conf, new File(baseDir));
    miniKdc.start();

    UserGroupInformation ugi = UserGroupInformation.createRemoteUser("guest");
    UserGroupInformation.setLoginUser(ugi);
    String username = UserGroupInformation.getLoginUser().getShortUserName();

    List<String> temp = new ArrayList<>(principals);
    temp.add(username);
    this.principals = Collections.unmodifiableList(temp);

    principals.forEach(p -> {
        try {
            File keytab = new File(baseDir, p + ".keytab");
            LOG.info("KDC: Creating keytab for {} in {}", p, keytab);
            miniKdc.createPrincipal(keytab, p, getKrbPrincipal(p), getKrbPrincipalWithRealm(p));
        } catch (Exception e) {
            throw Throwables.propagate(e);
        }
    });
    refreshDefaultRealm();
    prepareSecureConfiguration(username);
}

From source file:com.github.sakserv.minicluster.yarn.InJvmContainerExecutor.java

License:Apache License

/**
 * Will launch containers within the same JVM as this Container Executor. It
 * will do so by: - extracting Container's class name and program arguments
 * from the launch script (e.g., launch_container.sh) - Creating an isolated
 * ClassLoader for each container - Calling doLaunchContainer(..) method to
 * launch Container/*from ww  w.j a v a  2s  .c o  m*/
 */
private int doLaunch(Container container, Path containerWorkDir) throws Exception {
    Map<String, String> environment = container.getLaunchContext().getEnvironment();
    EnvironmentUtils.putAll(environment);

    Set<URL> additionalClassPathUrls = this.filterAndBuildUserClasspath(container);

    ExecJavaCliParser javaCliParser = this.createExecCommandParser(containerWorkDir.toString());

    UserGroupInformation.setLoginUser(null);
    try {
        // create Isolated Class Loader for each container and set it as context
        // class loader
        URLClassLoader containerCl = new URLClassLoader(
                additionalClassPathUrls.toArray(additionalClassPathUrls.toArray(new URL[] {})), null);
        Thread.currentThread().setContextClassLoader(containerCl);
        String containerLauncher = javaCliParser.getMain();

        Class<?> containerClass = Class.forName(containerLauncher, true, containerCl);
        Method mainMethod = containerClass.getMethod("main", new Class[] { String[].class });
        mainMethod.setAccessible(true);
        String[] arguments = javaCliParser.getMainArguments();

        this.doLaunchContainer(containerClass, mainMethod, arguments);

    } catch (Exception e) {
        logger.error("Failed to launch container " + container, e);
        container.handle(new ContainerDiagnosticsUpdateEvent(container.getContainerId(), e.getMessage()));
        return -1;
    } finally {
        logger.info("Removing symlinks");
        this.cleanUp();
    }
    return 0;
}

From source file:com.hortonworks.minicluster.InJvmContainerExecutor.java

License:Apache License

/**
 * Will launch containers within the same JVM as this Container Executor. It
 * will do so by: - extracting Container's class name and program arguments
 * from the launch script (e.g., launch_container.sh) - Creating an isolated
 * ClassLoader for each container - Calling doLaunchContainer(..) method to
 * launch Container/*ww w .  ja va2s. c  o m*/
 */
private int doLaunch(Container container, Path containerWorkDir) {
    Map<String, String> environment = container.getLaunchContext().getEnvironment();
    EnvironmentUtils.putAll(environment);

    Set<URL> additionalClassPathUrls = this.filterAndBuildUserClasspath(container);

    ExecJavaCliParser javaCliParser = this.createExecCommandParser(containerWorkDir.toString());

    UserGroupInformation.setLoginUser(null);
    try {
        // create Isolated Class Loader for each container and set it as context
        // class loader
        URLClassLoader containerCl = new URLClassLoader(
                additionalClassPathUrls.toArray(additionalClassPathUrls.toArray(new URL[] {})), null);
        Thread.currentThread().setContextClassLoader(containerCl);
        String containerLauncher = javaCliParser.getMain();
        Class<?> containerClass = Class.forName(containerLauncher, true, containerCl);
        Method mainMethod = containerClass.getMethod("main", new Class[] { String[].class });
        mainMethod.setAccessible(true);
        String[] arguments = javaCliParser.getMainArguments();

        this.doLaunchContainer(containerClass, mainMethod, arguments);
    } catch (Exception e) {
        logger.error("Failed to launch container " + container, e);
        container.handle(new ContainerDiagnosticsUpdateEvent(container.getContainerId(), e.getMessage()));
        return -1;
    } finally {
        logger.info("Removing symlinks");
        this.cleanUp();
    }
    return 0;
}

From source file:com.kappaware.hbtools.common.Utils.java

License:Apache License

public static Configuration buildHBaseConfiguration(HBaseParameters parameters)
        throws ConfigurationException, IOException {
    Configuration config = HBaseConfiguration.create();
    for (String cf : parameters.getConfigFiles()) {
        File f = new File(cf);
        if (!f.canRead()) {
            throw new ConfigurationException(String.format("Unable to read file '%s'", cf));
        }//from   w  w w.jav a2  s. c o m
        log.debug(String.format("Will load '%s'", cf));
        config.addResource(new Path(cf));
    }
    config.set("hbase.client.retries.number", Integer.toString(parameters.getClientRetries()));
    //config.reloadConfiguration();
    if (Utils.hasText(parameters.getDumpConfigFile())) {
        Utils.dumpConfiguration(config, parameters.getDumpConfigFile());
    }
    if (Utils.hasText(parameters.getKeytab()) && Utils.hasText(parameters.getPrincipal())) {
        // Check if keytab file exists and is readable
        File f = new File(parameters.getKeytab());
        if (!f.canRead()) {
            throw new ConfigurationException(
                    String.format("Unable to read keytab file: '%s'", parameters.getKeytab()));
        }
        UserGroupInformation.setConfiguration(config);
        if (!UserGroupInformation.isSecurityEnabled()) {
            throw new ConfigurationException(
                    "Security is not enabled in core-site.xml while Kerberos principal and keytab are provided.");
        }
        try {
            UserGroupInformation userGroupInformation = UserGroupInformation
                    .loginUserFromKeytabAndReturnUGI(parameters.getPrincipal(), parameters.getKeytab());
            UserGroupInformation.setLoginUser(userGroupInformation);
        } catch (Exception e) {
            throw new ConfigurationException(
                    String.format("Kerberos: Unable to authenticate with principal='%s' and keytab='%s': %s.",
                            parameters.getPrincipal(), parameters.getKeytab(), e.getMessage()));
        }
    }
    return config;
}

From source file:com.splicemachine.yarn.test.BareYarnTest.java

License:Apache License

/**
 * All we really need to do here is to create a yarn client, configure it using the same
 * yarn-site.xml as was used by the server to start up.
 * @throws YarnException//from w  w w.j  a v a  2s  .co m
 * @throws IOException
 */
@Test(timeout = 60000)
@Ignore("Broken by dependency change")
public void testAMRMClientMatchingFitInferredRack() throws YarnException, IOException {
    // create, submit new app
    ApplicationSubmissionContext appContext = yarnClient.createApplication().getApplicationSubmissionContext();
    ApplicationId appId = appContext.getApplicationId();
    // set the application name
    appContext.setApplicationName("Test");
    // Set the priority for the application master
    Priority pri = Records.newRecord(Priority.class);
    pri.setPriority(0);
    appContext.setPriority(pri);
    // Set the queue to which this application is to be submitted in the RM
    appContext.setQueue("default");
    // Set up the container launch context for the application master
    ContainerLaunchContext amContainer = BuilderUtils.newContainerLaunchContext(
            Collections.<String, LocalResource>emptyMap(), new HashMap<String, String>(),
            Arrays.asList("sleep", "100"), new HashMap<String, ByteBuffer>(), null,
            new HashMap<ApplicationAccessType, String>());
    appContext.setAMContainerSpec(amContainer);
    appContext.setResource(Resource.newInstance(1024, 1));
    // Create the request to send to the applications manager
    SubmitApplicationRequest appRequest = Records.newRecord(SubmitApplicationRequest.class);
    appRequest.setApplicationSubmissionContext(appContext);
    // Submit the application to the applications manager
    yarnClient.submitApplication(appContext);

    // wait for app to start
    RMAppAttempt appAttempt;
    while (true) {
        ApplicationReport appReport = yarnClient.getApplicationReport(appId);
        if (appReport.getYarnApplicationState() == YarnApplicationState.ACCEPTED) {
            ApplicationAttemptId attemptId = appReport.getCurrentApplicationAttemptId();
            appAttempt = yarnPlatform.getResourceManager().getRMContext().getRMApps()
                    .get(attemptId.getApplicationId()).getCurrentAppAttempt();
            while (true) {
                if (appAttempt.getAppAttemptState() == RMAppAttemptState.LAUNCHED) {
                    break;
                }
            }
            break;
        }
    }
    // Just dig into the ResourceManager and get the AMRMToken just for the sake
    // of testing.
    UserGroupInformation.setLoginUser(
            UserGroupInformation.createRemoteUser(UserGroupInformation.getCurrentUser().getUserName()));
    UserGroupInformation.getCurrentUser().addToken(appAttempt.getAMRMToken());
}

From source file:org.apache.druid.indexer.path.GranularityPathSpecTest.java

License:Apache License

@Test
public void testAddInputPath() throws Exception {
    UserGroupInformation
            .setLoginUser(UserGroupInformation.createUserForTesting("test", new String[] { "testGroup" }));
    HadoopIngestionSpec spec = new HadoopIngestionSpec(new DataSchema("foo", null, new AggregatorFactory[0],
            new UniformGranularitySpec(Granularities.DAY, Granularities.MINUTE,
                    ImmutableList.of(Intervals.of("2015-11-06T00:00Z/2015-11-07T00:00Z"))),
            null, jsonMapper), new HadoopIOConfig(null, null, null), DEFAULT_TUNING_CONFIG);

    granularityPathSpec.setDataGranularity(Granularities.HOUR);
    granularityPathSpec.setFilePattern(".*");
    granularityPathSpec.setInputFormat(TextInputFormat.class);

    Job job = Job.getInstance();//from   w  w  w . ja  va2 s.  c  o m
    String formatStr = "file:%s/%s;org.apache.hadoop.mapreduce.lib.input.TextInputFormat";

    testFolder.newFolder("test", "y=2015", "m=11", "d=06", "H=00");
    testFolder.newFolder("test", "y=2015", "m=11", "d=06", "H=02");
    testFolder.newFolder("test", "y=2015", "m=11", "d=06", "H=05");
    testFolder.newFile("test/y=2015/m=11/d=06/H=00/file1");
    testFolder.newFile("test/y=2015/m=11/d=06/H=02/file2");
    testFolder.newFile("test/y=2015/m=11/d=06/H=05/file3");
    testFolder.newFile("test/y=2015/m=11/d=06/H=05/file4");

    granularityPathSpec.setInputPath(testFolder.getRoot().getPath() + "/test");

    granularityPathSpec.addInputPaths(HadoopDruidIndexerConfig.fromSpec(spec), job);

    String actual = job.getConfiguration().get("mapreduce.input.multipleinputs.dir.formats");

    String expected = Joiner.on(",")
            .join(Lists.newArrayList(
                    StringUtils.format(formatStr, testFolder.getRoot(), "test/y=2015/m=11/d=06/H=00/file1"),
                    StringUtils.format(formatStr, testFolder.getRoot(), "test/y=2015/m=11/d=06/H=02/file2"),
                    StringUtils.format(formatStr, testFolder.getRoot(), "test/y=2015/m=11/d=06/H=05/file3"),
                    StringUtils.format(formatStr, testFolder.getRoot(), "test/y=2015/m=11/d=06/H=05/file4")));

    Assert.assertEquals("Did not find expected input paths", expected, actual);
}

From source file:org.apache.druid.indexer.path.GranularityPathSpecTest.java

License:Apache License

@Test
public void testIntervalTrimming() throws Exception {
    UserGroupInformation
            .setLoginUser(UserGroupInformation.createUserForTesting("test", new String[] { "testGroup" }));
    HadoopIngestionSpec spec = new HadoopIngestionSpec(
            new DataSchema("foo", null, new AggregatorFactory[0],
                    new UniformGranularitySpec(Granularities.DAY, Granularities.ALL,
                            ImmutableList.of(Intervals.of("2015-01-01T11Z/2015-01-02T05Z"))),
                    null, jsonMapper),/*from   w  w w .  j  ava2s. c o  m*/
            new HadoopIOConfig(null, null, null), DEFAULT_TUNING_CONFIG);

    granularityPathSpec.setDataGranularity(Granularities.HOUR);
    granularityPathSpec.setPathFormat("yyyy/MM/dd/HH");
    granularityPathSpec.setFilePattern(".*");
    granularityPathSpec.setInputFormat(TextInputFormat.class);

    Job job = Job.getInstance();
    String formatStr = "file:%s/%s;org.apache.hadoop.mapreduce.lib.input.TextInputFormat";

    createFile(testFolder, "test/2015/01/01/00/file1", "test/2015/01/01/10/file2", "test/2015/01/01/18/file3",
            "test/2015/01/02/00/file1", "test/2015/01/02/03/file2", "test/2015/01/02/05/file3",
            "test/2015/01/02/07/file4", "test/2015/01/02/09/file5");

    granularityPathSpec.setInputPath(testFolder.getRoot().getPath() + "/test");

    granularityPathSpec.addInputPaths(HadoopDruidIndexerConfig.fromSpec(spec), job);

    String actual = job.getConfiguration().get("mapreduce.input.multipleinputs.dir.formats");

    String expected = Joiner.on(",")
            .join(Lists.newArrayList(
                    StringUtils.format(formatStr, testFolder.getRoot(), "test/2015/01/01/18/file3"),
                    StringUtils.format(formatStr, testFolder.getRoot(), "test/2015/01/02/00/file1"),
                    StringUtils.format(formatStr, testFolder.getRoot(), "test/2015/01/02/03/file2")));

    Assert.assertEquals("Did not find expected input paths", expected, actual);
}

From source file:org.apache.gobblin.yarn.YarnServiceTest.java

License:Apache License

private void startApp() throws Exception {
    // submit a dummy app
    ApplicationSubmissionContext appSubmissionContext = yarnClient.createApplication()
            .getApplicationSubmissionContext();
    this.applicationId = appSubmissionContext.getApplicationId();

    ContainerLaunchContext containerLaunchContext = BuilderUtils.newContainerLaunchContext(
            Collections.emptyMap(), Collections.emptyMap(), Arrays.asList("sleep", "100"),
            Collections.emptyMap(), null, Collections.emptyMap());

    // Setup the application submission context
    appSubmissionContext.setApplicationName("TestApp");
    appSubmissionContext.setResource(Resource.newInstance(128, 1));
    appSubmissionContext.setPriority(Priority.newInstance(0));
    appSubmissionContext.setAMContainerSpec(containerLaunchContext);

    this.yarnClient.submitApplication(appSubmissionContext);

    // wait for application to be accepted
    int i;//from w  w w.java  2  s  .  c  om
    RMAppAttempt attempt = null;
    for (i = 0; i < 120; i++) {
        ApplicationReport appReport = yarnClient.getApplicationReport(applicationId);

        if (appReport.getYarnApplicationState() == YarnApplicationState.ACCEPTED) {
            this.applicationAttemptId = appReport.getCurrentApplicationAttemptId();
            attempt = yarnCluster.getResourceManager().getRMContext().getRMApps()
                    .get(appReport.getCurrentApplicationAttemptId().getApplicationId()).getCurrentAppAttempt();
            break;
        }
        Thread.sleep(1000);
    }

    Assert.assertTrue(i < 120, "timed out waiting for ACCEPTED state");

    // Set the AM-RM token in the UGI for access during testing
    UserGroupInformation.setLoginUser(
            UserGroupInformation.createRemoteUser(UserGroupInformation.getCurrentUser().getUserName()));
    UserGroupInformation.getCurrentUser().addToken(attempt.getAMRMToken());
}

From source file:org.apache.ranger.audit.provider.MiscUtil.java

License:Apache License

/**
 * @param ugiLoginUser//from w  w w.  j a v  a 2 s . c om
 */
public static void setUGILoginUser(UserGroupInformation newUGI, Subject newSubject) {
    if (newUGI != null) {
        UserGroupInformation.setLoginUser(newUGI);
        ugiLoginUser = newUGI;
        logger.info("Setting UGI=" + newUGI);
    } else {
        logger.error("UGI is null. Not setting it.");
    }
    if (newSubject != null) {
        logger.info("Setting SUBJECT");
        subjectLoginUser = newSubject;
    }
}

From source file:org.apache.sentry.tests.e2e.solr.db.integration.AbstractSolrSentryTestWithDbProvider.java

License:Apache License

public static void configureWithSolr() throws Exception {
    conf.set(ServerConfig.SECURITY_USE_UGI_TRANSPORT, "true");
    //save configuration to sentry-site.xml
    conf.writeXml(new FileOutputStream(sentrySitePath));
    setSystemProperties();//from w w w  .jav a  2  s. com
    extraRequestFilters = new TreeMap<Class, String>(new Comparator<Class>() {
        // There's only one class, make this as simple as possible
        @Override
        public int compare(Class o1, Class o2) {
            return 0;
        }

        @Override
        public boolean equals(Object obj) {
            return true;
        }
    });
    extraRequestFilters.put(ModifiableUserAuthenticationFilter.class, "*");

    //set the solr for the loginUser and belongs to solr group
    addGroupsToUser("solr", "solr");
    UserGroupInformation
            .setLoginUser(UserGroupInformation.createUserForTesting("solr", new String[] { "solr" }));
}