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

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

Introduction

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

Prototype

public static boolean isSecurityEnabled() 

Source Link

Document

Determine if UserGroupInformation is using Kerberos to determine user identities or is relying on simple authentication

Usage

From source file:com.cloudera.hue.SudoFsShell.java

License:Apache License

public static void main(String[] args) throws Exception {
    if (args.length < 1) {
        usage();/*from  w w  w  . j a va  2  s  .  c om*/
        System.exit(1);
    }

    String username = args[0];
    final String shellArgs[] = new String[args.length - 1];
    System.arraycopy(args, 1, shellArgs, 0, args.length - 1);

    UserGroupInformation sudoUgi;
    if (UserGroupInformation.isSecurityEnabled()) {
        sudoUgi = UserGroupInformation.createProxyUser(username, UserGroupInformation.getCurrentUser());
    } else {
        sudoUgi = UserGroupInformation.createRemoteUser(username);
    }

    sudoUgi.doAs(new PrivilegedExceptionAction<Void>() {
        public Void run() throws Exception {
            FsShell.main(shellArgs);
            return null;
        }
    });
}

From source file:com.cloudera.llama.server.Security.java

License:Apache License

public static void loginToHadoop(ServerConfiguration conf) throws Exception {
    if (UserGroupInformation.isSecurityEnabled()) {
        String principalName = conf.getServerPrincipalName();
        String keytab = conf.getKeytabFile();
        if (!(keytab.charAt(0) == '/')) {
            String confDir = conf.getConfDir();
            keytab = new File(confDir, keytab).getAbsolutePath();
        }/* w w  w.java2s .  com*/
        File keytabFile = new File(keytab).getAbsoluteFile();
        if (!keytabFile.exists()) {
            throw new RuntimeException(FastFormat.format("Keytab file '{}' does not exist", keytabFile));
        }
        UserGroupInformation.loginUserFromKeytab(principalName, keytabFile.getPath());
    }
}

From source file:com.cloudera.sa.querykerberosauthhs2.QueryKerberosAuthHS2.java

public QueryKerberosAuthHS2() throws IOException {
    if (UserGroupInformation.isSecurityEnabled()) {

        UserGroupInformation.loginUserFromKeytab("vijay@US-WEST-2.COMPUTE.INTERNAL", "/etc/vijay.keytab");

    }//from   w  ww .j  av  a 2  s . c o m
}

From source file:com.continuuity.weave.internal.yarn.Hadoop20YarnAppClient.java

License:Apache License

private void addRMToken(ContainerLaunchContext context) {
    if (!UserGroupInformation.isSecurityEnabled()) {
        return;//from   w w w. j a v  a  2  s  .  c o m
    }

    try {
        Credentials credentials = YarnUtils.decodeCredentials(context.getContainerTokens());

        Configuration config = yarnClient.getConfig();
        Token<TokenIdentifier> token = convertToken(
                yarnClient.getRMDelegationToken(new Text(YarnUtils.getYarnTokenRenewer(config))),
                YarnUtils.getRMAddress(config));

        LOG.info("Added RM delegation token {}", token);
        credentials.addToken(token.getService(), token);

        context.setContainerTokens(YarnUtils.encodeCredentials(credentials));

    } catch (Exception e) {
        LOG.error("Fails to create credentials.", e);
        throw Throwables.propagate(e);
    }
}

From source file:com.continuuity.weave.internal.yarn.Hadoop21YarnAppClient.java

License:Apache License

private void addRMToken(ContainerLaunchContext context) {
    if (!UserGroupInformation.isSecurityEnabled()) {
        return;/*from   w w w .  j  a va  2s.  co  m*/
    }

    try {
        Credentials credentials = YarnUtils.decodeCredentials(context.getTokens());

        Configuration config = yarnClient.getConfig();
        Token<TokenIdentifier> token = ConverterUtils.convertFromYarn(
                yarnClient.getRMDelegationToken(new Text(YarnUtils.getYarnTokenRenewer(config))),
                YarnUtils.getRMAddress(config));

        LOG.info("Added RM delegation token {}", token);
        credentials.addToken(token.getService(), token);

        context.setTokens(YarnUtils.encodeCredentials(credentials));

    } catch (Exception e) {
        LOG.error("Fails to create credentials.", e);
        throw Throwables.propagate(e);
    }
}

From source file:com.continuuity.weave.internal.yarn.ports.AMRMClientImpl.java

License:Apache License

@Override
public synchronized void start() {
    final YarnConfiguration conf = new YarnConfiguration(getConfig());
    final YarnRPC rpc = YarnRPC.create(conf);
    final InetSocketAddress rmAddress = conf.getSocketAddr(YarnConfiguration.RM_SCHEDULER_ADDRESS,
            YarnConfiguration.DEFAULT_RM_SCHEDULER_ADDRESS, YarnConfiguration.DEFAULT_RM_SCHEDULER_PORT);

    UserGroupInformation currentUser;/*from  w  w w . j  a v  a  2 s  .  c o  m*/
    try {
        currentUser = UserGroupInformation.getCurrentUser();
    } catch (IOException e) {
        throw new YarnException(e);
    }

    if (UserGroupInformation.isSecurityEnabled()) {
        String tokenURLEncodedStr = System.getenv().get(ApplicationConstants.APPLICATION_MASTER_TOKEN_ENV_NAME);
        Token<? extends TokenIdentifier> token = new Token<TokenIdentifier>();

        try {
            token.decodeFromUrlString(tokenURLEncodedStr);
        } catch (IOException e) {
            throw new YarnException(e);
        }

        SecurityUtil.setTokenService(token, rmAddress);
        if (LOG.isDebugEnabled()) {
            LOG.debug("AppMasterToken is " + token);
        }
        currentUser.addToken(token);
    }

    rmClient = currentUser.doAs(new PrivilegedAction<AMRMProtocol>() {
        @Override
        public AMRMProtocol run() {
            return (AMRMProtocol) rpc.getProxy(AMRMProtocol.class, rmAddress, conf);
        }
    });
    LOG.debug("Connecting to ResourceManager at " + rmAddress);
    super.start();
}

From source file:com.datatorrent.stram.cli.ApexCli.java

License:Apache License

public static void main(final String[] args) throws Exception {
    final ApexCli shell = new ApexCli();
    shell.preImpersonationInit(args);//from www . j a va2s  .  co  m
    String hadoopUserName = System.getenv("HADOOP_USER_NAME");
    if (UserGroupInformation.isSecurityEnabled() && StringUtils.isNotBlank(hadoopUserName)
            && !hadoopUserName.equals(UserGroupInformation.getLoginUser().getUserName())) {
        LOG.info("You ({}) are running as user {}", UserGroupInformation.getLoginUser().getUserName(),
                hadoopUserName);
        UserGroupInformation ugi = UserGroupInformation.createProxyUser(hadoopUserName,
                UserGroupInformation.getLoginUser());
        ugi.doAs(new PrivilegedExceptionAction<Void>() {
            @Override
            public Void run() throws Exception {
                shell.mainHelper();
                return null;
            }
        });
    } else {
        shell.mainHelper();
    }
}

From source file:com.datatorrent.stram.cli.DTCli.java

License:Apache License

public static void main(final String[] args) throws Exception {
    final DTCli shell = new DTCli();
    shell.preImpersonationInit(args);// w ww  . ja  v a2  s.com
    String hadoopUserName = System.getenv("HADOOP_USER_NAME");
    if (UserGroupInformation.isSecurityEnabled() && StringUtils.isNotBlank(hadoopUserName)
            && !hadoopUserName.equals(UserGroupInformation.getLoginUser().getUserName())) {
        LOG.info("You ({}) are running as user {}", UserGroupInformation.getLoginUser().getUserName(),
                hadoopUserName);
        UserGroupInformation ugi = UserGroupInformation.createProxyUser(hadoopUserName,
                UserGroupInformation.getLoginUser());
        ugi.doAs(new PrivilegedExceptionAction<Void>() {
            @Override
            public Void run() throws Exception {
                shell.mainHelper();
                return null;
            }
        });
    } else {
        shell.mainHelper();
    }
}

From source file:com.datatorrent.stram.client.StramAgent.java

License:Apache License

private StramWebServicesInfo retrieveWebServicesInfo(String appId) {
    YarnClient yarnClient = YarnClient.createYarnClient();
    String url;/*from   ww w .  j av a  2s  . co m*/
    try {
        yarnClient.init(conf);
        yarnClient.start();
        ApplicationReport ar = yarnClient.getApplicationReport(ConverterUtils.toApplicationId(appId));
        String trackingUrl = ar.getTrackingUrl();
        if (!trackingUrl.startsWith("http://") && !trackingUrl.startsWith("https://")) {
            url = "http://" + trackingUrl;
        } else {
            url = trackingUrl;
        }
        if (StringUtils.isBlank(url)) {
            LOG.error("Cannot get tracking url from YARN");
            return null;
        }
        if (url.endsWith("/")) {
            url = url.substring(0, url.length() - 1);
        }
        url += WebServices.PATH;
    } catch (Exception ex) {
        //LOG.error("Caught exception when retrieving web services info", ex);
        return null;
    } finally {
        yarnClient.stop();
    }

    WebServicesClient webServicesClient = new WebServicesClient();
    try {
        JSONObject response;
        String secToken = null;
        ClientResponse clientResponse;
        int i = 0;
        while (true) {
            LOG.debug("Accessing url {}", url);
            clientResponse = webServicesClient.process(url, ClientResponse.class,
                    new WebServicesClient.GetWebServicesHandler<ClientResponse>());
            String val = clientResponse.getHeaders().getFirst("Refresh");
            if (val == null) {
                break;
            }
            int index = val.indexOf("url=");
            if (index < 0) {
                break;
            }
            url = val.substring(index + 4);
            if (i++ > MAX_REDIRECTS) {
                LOG.error("Cannot get web service info -- exceeded the max number of redirects");
                return null;
            }
        }

        if (!UserGroupInformation.isSecurityEnabled()) {
            response = new JSONObject(clientResponse.getEntity(String.class));
        } else {
            if (UserGroupInformation.isSecurityEnabled()) {
                for (NewCookie nc : clientResponse.getCookies()) {
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Cookie " + nc.getName() + " " + nc.getValue());
                    }
                    if (nc.getName().equals(StramWSFilter.CLIENT_COOKIE)) {
                        secToken = nc.getValue();
                    }
                }
            }
            response = new JSONObject(clientResponse.getEntity(String.class));
        }
        String version = response.getString("version");
        response = webServicesClient.process(url + "/" + version + "/stram/info", JSONObject.class,
                new WebServicesClient.GetWebServicesHandler<JSONObject>());
        String appMasterUrl = response.getString("appMasterTrackingUrl");
        String appPath = response.getString("appPath");
        String user = response.getString("user");
        JSONObject permissionsInfo = null;
        FSDataInputStream is = null;
        try {
            is = fileSystem.open(new Path(appPath, "permissions.json"));
            permissionsInfo = new JSONObject(IOUtils.toString(is));
        } catch (JSONException ex) {
            LOG.error("Error reading from the permissions info. Ignoring", ex);
        } catch (IOException ex) {
            // ignore
        } finally {
            IOUtils.closeQuietly(is);
        }
        return new StramWebServicesInfo(appMasterUrl, version, appPath, user, secToken, permissionsInfo);
    } catch (Exception ex) {
        LOG.debug("Caught exception when retrieving web service info for app " + appId, ex);
        return null;
    }
}

From source file:com.datatorrent.stram.engine.StreamingContainer.java

License:Apache License

public void heartbeatLoop() throws Exception {
    umbilical.log(containerId, "[" + containerId + "] Entering heartbeat loop..");
    logger.debug("Entering heartbeat loop (interval is {} ms)", this.heartbeatIntervalMillis);
    final YarnConfiguration conf = new YarnConfiguration();
    long tokenLifeTime = (long) (containerContext.getValue(LogicalPlan.TOKEN_REFRESH_ANTICIPATORY_FACTOR)
            * containerContext.getValue(LogicalPlan.HDFS_TOKEN_LIFE_TIME));
    long expiryTime = System.currentTimeMillis();
    final Credentials credentials = UserGroupInformation.getCurrentUser().getCredentials();
    Iterator<Token<?>> iter = credentials.getAllTokens().iterator();
    while (iter.hasNext()) {
        Token<?> token = iter.next();
        logger.debug("token: {}", token);
    }//  w w  w  . java  2  s  .c  om
    String hdfsKeyTabFile = containerContext.getValue(LogicalPlan.KEY_TAB_FILE);
    while (!exitHeartbeatLoop) {

        if (UserGroupInformation.isSecurityEnabled() && System.currentTimeMillis() >= expiryTime
                && hdfsKeyTabFile != null) {
            expiryTime = StramUserLogin.refreshTokens(tokenLifeTime, "." + File.separator + "tmp", containerId,
                    conf, hdfsKeyTabFile, credentials, null, false);
        }
        synchronized (this.heartbeatTrigger) {
            try {
                this.heartbeatTrigger.wait(heartbeatIntervalMillis);
            } catch (InterruptedException e1) {
                logger.warn("Interrupted in heartbeat loop, exiting..");
                break;
            }
        }

        long currentTime = System.currentTimeMillis();
        ContainerHeartbeat msg = new ContainerHeartbeat();
        msg.jvmName = jvmName;
        if (this.bufferServerAddress != null) {
            msg.bufferServerHost = this.bufferServerAddress.getHostName();
            msg.bufferServerPort = this.bufferServerAddress.getPort();
            if (bufferServer != null && !eventloop.isActive()) {
                logger.warn("Requesting restart due to terminated event loop");
                msg.restartRequested = true;
            }
        }
        msg.memoryMBFree = ((int) (Runtime.getRuntime().freeMemory() / (1024 * 1024)));
        garbageCollectorMXBeans = ManagementFactory.getGarbageCollectorMXBeans();
        for (GarbageCollectorMXBean bean : garbageCollectorMXBeans) {
            msg.gcCollectionTime += bean.getCollectionTime();
            msg.gcCollectionCount += bean.getCollectionCount();
        }

        ContainerHeartbeatResponse rsp;
        do {
            ContainerStats stats = new ContainerStats(containerId);
            // gather heartbeat info for all operators
            for (Map.Entry<Integer, Node<?>> e : nodes.entrySet()) {
                OperatorHeartbeat hb = new OperatorHeartbeat();
                hb.setNodeId(e.getKey());
                hb.setGeneratedTms(currentTime);
                hb.setIntervalMs(heartbeatIntervalMillis);
                if (e.getValue().commandResponse.size() > 0) {
                    BlockingQueue<StatsListener.OperatorResponse> commandResponse = e
                            .getValue().commandResponse;
                    ArrayList<StatsListener.OperatorResponse> response = new ArrayList<StatsListener.OperatorResponse>();
                    for (int i = 0; i < commandResponse.size(); i++) {
                        response.add(commandResponse.poll());
                    }
                    hb.requestResponse = response;
                }
                OperatorContext context = e.getValue().context;
                context.drainStats(hb.getOperatorStatsContainer());

                if (context.getThread() == null || context.getThread().getState() != Thread.State.TERMINATED) {
                    hb.setState(DeployState.ACTIVE);
                } else if (failedNodes.contains(hb.nodeId)) {
                    hb.setState(DeployState.FAILED);
                } else {
                    logger.debug("Reporting SHUTDOWN state because thread is {} and failedNodes is {}",
                            context.getThread(), failedNodes);
                    hb.setState(DeployState.SHUTDOWN);
                }

                stats.addNodeStats(hb);
            }

            /**
             * Container stats published for whoever is interested in listening.
             * Currently interested candidates are TupleRecorderCollection and BufferServerStatsSubscriber
             */
            eventBus.publish(new ContainerStatsEvent(stats));

            msg.setContainerStats(stats);

            // heartbeat call and follow-up processing
            //logger.debug("Sending heartbeat for {} operators.", msg.getContainerStats().size());
            msg.sentTms = System.currentTimeMillis();
            rsp = umbilical.processHeartbeat(msg);
            processHeartbeatResponse(rsp);
            if (rsp.hasPendingRequests) {
                logger.info("Waiting for pending request.");
                synchronized (this.heartbeatTrigger) {
                    try {
                        this.heartbeatTrigger.wait(500);
                    } catch (InterruptedException ie) {
                        logger.warn("Interrupted in heartbeat loop", ie);
                        break;
                    }
                }
            }
        } while (rsp.hasPendingRequests);

    }
    logger.debug("Exiting hearbeat loop");
    umbilical.log(containerId, "[" + containerId + "] Exiting heartbeat loop..");
}