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

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

Introduction

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

Prototype

@InterfaceAudience.Public
@InterfaceStability.Evolving
public String getUserName() 

Source Link

Document

Get the user's full principal name.

Usage

From source file:org.apache.blur.hive.BlurSerDeTest.java

License:Apache License

private int runLoad(boolean disableMrUpdate)
        throws IOException, InterruptedException, ClassNotFoundException, SQLException {

    Configuration configuration = miniCluster.getMRConfiguration();
    writeSiteFiles(configuration);/*from  ww w  .ja  va  2 s  .co  m*/
    HiveConf hiveConf = new HiveConf(configuration, getClass());
    hiveConf.set("hive.server2.thrift.port", "0");
    HiveServer2 hiveServer2 = new HiveServer2();
    hiveServer2.init(hiveConf);
    hiveServer2.start();

    int port = waitForStartupAndGetPort(hiveServer2);

    Class.forName(HiveDriver.class.getName());
    String userName = UserGroupInformation.getCurrentUser().getShortUserName();
    Connection connection = DriverManager.getConnection("jdbc:hive2://localhost:" + port, userName, "");

    UserGroupInformation currentUser = UserGroupInformation.getCurrentUser();

    run(connection, "set blur.user.name=" + currentUser.getUserName());
    run(connection, "set blur.mr.update.disabled=" + disableMrUpdate);
    run(connection, "set hive.metastore.warehouse.dir=" + WAREHOUSE.toURI().toString());
    run(connection, "create database if not exists testdb");
    run(connection, "use testdb");

    run(connection, "CREATE TABLE if not exists testtable ROW FORMAT SERDE 'org.apache.blur.hive.BlurSerDe' "
            + "WITH SERDEPROPERTIES ( 'blur.zookeeper.connection'='" + miniCluster.getZkConnectionString()
            + "', " + "'blur.table'='" + TEST + "', 'blur.family'='" + FAM + "' ) "
            + "STORED BY 'org.apache.blur.hive.BlurHiveStorageHandler'");

    run(connection, "desc testtable");

    String createLoadTable = buildCreateLoadTable(connection);
    run(connection, createLoadTable);
    File dbDir = new File(WAREHOUSE, "testdb.db");
    File tableDir = new File(dbDir, "loadtable");
    int totalRecords = 100;
    generateData(tableDir, totalRecords);

    run(connection, "select * from loadtable");
    run(connection, "set " + BlurSerDe.BLUR_BLOCKING_APPLY + "=true");
    run(connection, "insert into table testtable select * from loadtable");
    connection.close();
    hiveServer2.stop();
    return totalRecords;
}

From source file:org.apache.blur.mapreduce.lib.CsvBlurDriver.java

License:Apache License

public static Job setupJob(Configuration configuration, ControllerPool controllerPool,
        AtomicReference<Callable<Void>> ref, String... otherArgs) throws Exception {
    CommandLine cmd = parse(otherArgs);//  w  w  w.  jav a  2  s .  c om
    if (cmd == null) {
        return null;
    }

    final String controllerConnectionStr = cmd.getOptionValue("c");
    final String tableName = cmd.getOptionValue("t");

    final Iface client = controllerPool.getClient(controllerConnectionStr);
    TableDescriptor tableDescriptor = client.describe(tableName);

    Job job = Job.getInstance(configuration, "Blur indexer [" + tableName + "]");
    job.setJarByClass(CsvBlurDriver.class);
    job.setMapperClass(CsvBlurMapper.class);

    if (cmd.hasOption("p")) {
        job.getConfiguration().set(MAPRED_COMPRESS_MAP_OUTPUT, "true");
        String codecStr = cmd.getOptionValue("p");
        COMPRESSION compression;
        try {
            compression = COMPRESSION.valueOf(codecStr.trim().toUpperCase());
        } catch (IllegalArgumentException e) {
            compression = null;
        }
        if (compression == null) {
            job.getConfiguration().set(MAPRED_MAP_OUTPUT_COMPRESSION_CODEC, codecStr.trim());
        } else {
            job.getConfiguration().set(MAPRED_MAP_OUTPUT_COMPRESSION_CODEC, compression.getClassName());
        }
    }
    if (cmd.hasOption("a")) {
        CsvBlurMapper.setAutoGenerateRecordIdAsHashOfData(job, true);
    }
    if (cmd.hasOption("A")) {
        CsvBlurMapper.setAutoGenerateRowIdAsHashOfData(job, true);
    }
    if (cmd.hasOption("S")) {
        job.setInputFormatClass(SequenceFileInputFormat.class);
    } else {
        job.setInputFormatClass(TextInputFormat.class);
    }

    if (cmd.hasOption("C")) {
        if (cmd.hasOption("S")) {
            String[] optionValues = cmd.getOptionValues("C");
            job.setInputFormatClass(CsvBlurCombineSequenceFileInputFormat.class);
            CombineFileInputFormat.setMinInputSplitSize(job, Long.parseLong(optionValues[0]));
            CombineFileInputFormat.setMaxInputSplitSize(job, Long.parseLong(optionValues[1]));
        } else {
            System.err.println("'C' can only be used with option 'S'");
            return null;
        }
    }

    if (cmd.hasOption("i")) {
        for (String input : cmd.getOptionValues("i")) {
            Path path = new Path(input);
            Set<Path> pathSet = recurisvelyGetPathesContainingFiles(path, job.getConfiguration());
            if (pathSet.isEmpty()) {
                FileInputFormat.addInputPath(job, path);
            } else {
                for (Path p : pathSet) {
                    FileInputFormat.addInputPath(job, p);
                }
            }
        }
    }
    // processing the 'I' option
    if (cmd.hasOption("I")) {
        if (cmd.hasOption("C")) {
            System.err.println("'I' and 'C' both parameters can not be used together.");
            return null;
        }
        Option[] options = cmd.getOptions();
        for (Option option : options) {
            if (option.getOpt().equals("I")) {
                String[] values = option.getValues();
                if (values.length < 2) {
                    System.err.println("'I' parameter missing minimum args of (family path*)");
                    return null;
                }
                for (String p : getSubArray(values, 1)) {
                    Path path = new Path(p);
                    CsvBlurMapper.addFamilyPath(job, values[0], path);
                    FileInputFormat.addInputPath(job, path);
                }
            }
        }
    }

    if (cmd.hasOption("s")) {
        CsvBlurMapper.setSeparator(job, StringEscapeUtils.unescapeJava(cmd.getOptionValue("s")));
    }
    if (cmd.hasOption("o")) {
        BlurOutputFormat.setOptimizeInFlight(job, false);
    }
    if (cmd.hasOption("l")) {
        BlurOutputFormat.setIndexLocally(job, false);
    }
    if (cmd.hasOption("b")) {
        int maxDocumentBufferSize = Integer.parseInt(cmd.getOptionValue("b"));
        BlurOutputFormat.setMaxDocumentBufferSize(job, maxDocumentBufferSize);
    }
    // processing the 'd' option
    Option[] options = cmd.getOptions();
    for (Option option : options) {
        if (option.getOpt().equals("d")) {
            String[] values = option.getValues();
            if (values.length < 2) {
                System.err.println("'d' parameter missing minimum args of (family columname*)");
                return null;
            }
            CsvBlurMapper.addColumns(job, values[0], getSubArray(values, 1));
        }
    }
    BlurOutputFormat.setupJob(job, tableDescriptor);
    BlurMapReduceUtil.addDependencyJars(job.getConfiguration(), Splitter.class);
    if (cmd.hasOption("r")) {
        int reducerMultiplier = Integer.parseInt(cmd.getOptionValue("r"));
        BlurOutputFormat.setReducerMultiplier(job, reducerMultiplier);
    }
    final Path output;
    if (cmd.hasOption("out")) {
        output = new Path(cmd.getOptionValue("out"));
    } else {
        UserGroupInformation currentUser = UserGroupInformation.getCurrentUser();
        String userName = currentUser.getUserName();
        output = new Path("/user/" + userName + "/.blur-" + System.currentTimeMillis());
    }
    BlurOutputFormat.setOutputPath(job, output);
    if (cmd.hasOption("import")) {
        ref.set(new Callable<Void>() {
            @Override
            public Void call() throws Exception {
                client.loadData(tableName, output.toUri().toString());
                return null;
            }
        });
    }
    return job;
}

From source file:org.apache.drill.exec.ops.OperatorContextImpl.java

License:Apache License

public <RESULT> ListenableFuture<RESULT> runCallableAs(final UserGroupInformation proxyUgi,
        final Callable<RESULT> callable) {
    synchronized (this) {
        if (delegatePool == null) {
            delegatePool = MoreExecutors.listeningDecorator(executor);
        }//w w w.j a  va  2 s .c om
    }
    return delegatePool.submit(new Callable<RESULT>() {
        @Override
        public RESULT call() throws Exception {
            final Thread currentThread = Thread.currentThread();
            final String originalThreadName = currentThread.getName();
            currentThread.setName(proxyUgi.getUserName() + ":task-delegate-thread");
            final RESULT result;
            try {
                result = proxyUgi.doAs(new PrivilegedExceptionAction<RESULT>() {
                    @Override
                    public RESULT run() throws Exception {
                        return callable.call();
                    }
                });
            } finally {
                currentThread.setName(originalThreadName);
            }
            return result;
        }
    });
}

From source file:org.apache.drill.exec.planner.index.MapRDBIndexDiscover.java

License:Apache License

@SuppressWarnings("deprecation")
private Admin admin() {
    assert getOriginalScan() instanceof MapRDBGroupScan;

    final MapRDBGroupScan dbGroupScan = (MapRDBGroupScan) getOriginalScan();
    final UserGroupInformation currentUser = ImpersonationUtil.createProxyUgi(dbGroupScan.getUserName());
    final Configuration conf = dbGroupScan.getFormatPlugin().getFsConf();

    final Admin admin;
    try {/*from  www  .  ja va  2 s. c  o m*/
        admin = currentUser.doAs((PrivilegedExceptionAction<Admin>) () -> MapRDB.getAdmin(conf));
    } catch (Exception e) {
        throw new DrillRuntimeException("Failed to get Admin instance for user: " + currentUser.getUserName(),
                e);
    }
    return admin;
}

From source file:org.apache.drill.exec.rpc.BitConnectionConfig.java

License:Apache License

public Map<String, ?> getSaslClientProperties(final DrillbitEndpoint remoteEndpoint) throws IOException {
    final DrillProperties properties = DrillProperties.createEmpty();

    final UserGroupInformation loginUser = UserGroupInformation.getLoginUser();
    if (loginUser.getAuthenticationMethod() == UserGroupInformation.AuthenticationMethod.KERBEROS) {
        final HadoopKerberosName loginPrincipal = new HadoopKerberosName(loginUser.getUserName());
        if (!useLoginPrincipal) {
            properties.setProperty(DrillProperties.SERVICE_PRINCIPAL, KerberosUtil.getPrincipalFromParts(
                    loginPrincipal.getShortName(), remoteEndpoint.getAddress(), loginPrincipal.getRealm()));
        } else {/*from   w  w  w  . j a  va 2 s .  c  o m*/
            properties.setProperty(DrillProperties.SERVICE_PRINCIPAL, loginPrincipal.toString());
        }
    }
    return properties.stringPropertiesAsMap();
}

From source file:org.apache.drill.exec.rpc.security.kerberos.KerberosFactory.java

License:Apache License

@Override
public SaslServer createSaslServer(final UserGroupInformation ugi, final Map<String, ?> properties)
        throws SaslException {
    try {/*ww w . j a  v a  2 s. co  m*/
        final String primaryName = ugi.getShortUserName();
        final String instanceName = new HadoopKerberosName(ugi.getUserName()).getHostName();

        final SaslServer saslServer = ugi.doAs(new PrivilegedExceptionAction<SaslServer>() {
            @Override
            public SaslServer run() throws Exception {
                return FastSaslServerFactory.getInstance().createSaslServer(KerberosUtil.KERBEROS_SASL_NAME,
                        primaryName, instanceName, properties, new KerberosServerCallbackHandler());
            }
        });
        logger.trace("GSSAPI SaslServer created.");
        return saslServer;
    } catch (final UndeclaredThrowableException e) {
        final Throwable cause = e.getCause();
        logger.debug("Authentication failed.", cause);
        if (cause instanceof SaslException) {
            throw (SaslException) cause;
        } else {
            throw new SaslException("Unexpected failure trying to authenticate using Kerberos", cause);
        }
    } catch (final IOException | InterruptedException e) {
        logger.debug("Authentication failed.", e);
        throw new SaslException("Unexpected failure trying to authenticate using Kerberos", e);
    }
}

From source file:org.apache.drill.exec.server.rest.spnego.TestSpnegoConfig.java

License:Apache License

/**
 * Valid Configuration with both keytab & principal
 * @throws Exception/*from  ww  w  . j a va  2 s  .  com*/
 */
@Test
public void testValidSpnegoConfig() throws Exception {

    try {
        final DrillConfig newConfig = new DrillConfig(DrillConfig.create()
                .withValue(ExecConstants.USER_AUTHENTICATION_ENABLED, ConfigValueFactory.fromAnyRef(true))
                .withValue(ExecConstants.AUTHENTICATION_MECHANISMS,
                        ConfigValueFactory.fromIterable(Lists.newArrayList("plain")))
                .withValue(ExecConstants.HTTP_SPNEGO_PRINCIPAL,
                        ConfigValueFactory.fromAnyRef(spnegoHelper.SERVER_PRINCIPAL))
                .withValue(ExecConstants.HTTP_SPNEGO_KEYTAB,
                        ConfigValueFactory.fromAnyRef(spnegoHelper.serverKeytab.toString()))
                .withValue(ExecConstants.USER_AUTHENTICATOR_IMPL,
                        ConfigValueFactory.fromAnyRef(UserAuthenticatorTestImpl.TYPE)));

        final SpnegoConfig spnegoConfig = new SpnegoConfig(newConfig);
        spnegoConfig.validateSpnegoConfig();
        UserGroupInformation ugi = spnegoConfig.getLoggedInUgi();
        assertEquals(primaryName, ugi.getShortUserName());
        assertEquals(spnegoHelper.SERVER_PRINCIPAL, ugi.getUserName());
    } catch (Exception ex) {
        fail();
    }
}

From source file:org.apache.falcon.resource.channel.HTTPChannel.java

License:Apache License

@SuppressWarnings("unchecked")
@Override/* ww  w .  j  a  v a 2  s .c o m*/
public <T> T invoke(String methodName, Object... args) throws FalconException {
    HttpServletRequest incomingRequest = null;
    try {
        Method method = getMethod(service, methodName, args);
        String urlPrefix = getFalconEndPoint();
        final String url = urlPrefix + "/" + pathValue(method, args);
        LOG.debug("Executing {}", url);

        incomingRequest = getIncomingRequest(args);
        incomingRequest.getInputStream().reset();
        String httpMethod = getHttpMethod(method);
        String mimeType = getConsumes(method);
        String accept = MediaType.WILDCARD;
        final String user = CurrentUser.getUser();

        String doAsUser = incomingRequest.getParameter(DO_AS_PARAM);
        WebResource resource = getClient().resource(UriBuilder.fromUri(url).build().normalize())
                .queryParam("user.name", user);
        if (doAsUser != null) {
            resource = resource.queryParam("doAs", doAsUser);
        }

        AuthenticatedURL.Token authenticationToken = null;
        if (SecurityUtil.isSecurityEnabled()) {
            UserGroupInformation ugiLoginUser = UserGroupInformation.getCurrentUser();
            LOG.debug("Security is enabled. Using DoAs : " + ugiLoginUser.getUserName());
            authenticationToken = ugiLoginUser.doAs(new PrivilegedExceptionAction<AuthenticatedURL.Token>() {
                @Override
                public AuthenticatedURL.Token run() throws Exception {
                    return getToken(url + PseudoAuthenticator.USER_NAME + "=" + user, getClient());
                }
            });
        }

        ClientResponse response = resource.header("Cookie", AUTH_COOKIE_EQ + authenticationToken).accept(accept)
                .type(mimeType).method(httpMethod, ClientResponse.class,
                        (isPost(httpMethod) ? incomingRequest.getInputStream() : null));
        incomingRequest.getInputStream().reset();

        Family status = response.getClientResponseStatus().getFamily();
        if (status == Family.INFORMATIONAL || status == Family.SUCCESSFUL) {
            return (T) response.getEntity(method.getReturnType());
        } else if (response.getClientResponseStatus().getStatusCode() == Response.Status.BAD_REQUEST
                .getStatusCode()) {
            LOG.error("Request failed: {}", response.getClientResponseStatus().getStatusCode());
            throw FalconWebException.newAPIException(response.getEntity(APIResult.class).getMessage());
        } else {
            LOG.error("Request failed: {}", response.getClientResponseStatus().getStatusCode());
            throw new FalconException(response.getEntity(String.class));
        }
    } catch (FalconWebException falconWebException) {
        LOG.error("Request failed", falconWebException);
        throw falconWebException;
    } catch (Throwable e) {
        LOG.error("Request failed", e);
        throw new FalconException(e);
    } finally {
        try {
            if (incomingRequest != null) {
                incomingRequest.getInputStream().reset();
            }
        } catch (IOException e) {
            LOG.error("Error in HTTPChannel", e);
        }
    }
}

From source file:org.apache.falcon.security.AuthenticationInitializationServiceTest.java

License:Apache License

@Test
public void testInitForSimpleAuthenticationMethod() {
    try {//from w w  w  .j ava 2 s. c o m
        StartupProperties.get().setProperty(SecurityUtil.AUTHENTICATION_TYPE, PseudoAuthenticationHandler.TYPE);
        authenticationService.init();

        UserGroupInformation loginUser = UserGroupInformation.getLoginUser();
        Assert.assertFalse(loginUser.isFromKeytab());
        Assert.assertEquals(loginUser.getAuthenticationMethod().name().toLowerCase(),
                PseudoAuthenticationHandler.TYPE);
        Assert.assertEquals(System.getProperty("user.name"), loginUser.getUserName());
    } catch (Exception e) {
        Assert.fail("AuthenticationInitializationService init failed.", e);
    }
}

From source file:org.apache.falcon.security.CurrentUserTest.java

License:Apache License

@Test
public void testGetProxyUserForAuthenticatedUser() throws Exception {
    CurrentUser.authenticate("proxy");
    UserGroupInformation proxyUgi = CurrentUser.getProxyUGI();
    Assert.assertNotNull(proxyUgi);// www. j  a  v  a2s . c  o  m
    Assert.assertEquals(proxyUgi.getUserName(), "proxy");
}