Example usage for org.apache.hadoop.security.token Token renew

List of usage examples for org.apache.hadoop.security.token Token renew

Introduction

In this page you can find the example usage for org.apache.hadoop.security.token Token renew.

Prototype

public long renew(Configuration conf) throws IOException, InterruptedException 

Source Link

Document

Renew this delegation token.

Usage

From source file:com.alibaba.jstorm.hdfs.common.security.AutoHDFS.java

License:Apache License

/**
 * {@inheritDoc}/*from w ww  .jav a 2 s .  com*/
 */
@Override
@SuppressWarnings("unchecked")
public void renew(Map<String, String> credentials, Map topologyConf) {
    try {
        Credentials credential = getCredentials(credentials);
        if (credential != null) {
            Configuration configuration = new Configuration();
            Collection<Token<? extends TokenIdentifier>> tokens = credential.getAllTokens();

            if (tokens != null && tokens.isEmpty() == false) {
                for (Token token : tokens) {
                    //We need to re-login some other thread might have logged into hadoop using
                    // their credentials (e.g. AutoHBase might be also part of nimbu auto creds)
                    login(configuration);
                    long expiration = (Long) token.renew(configuration);
                    LOG.info("HDFS delegation token renewed, new expiration time {}", expiration);
                }
            } else {
                LOG.debug("No tokens found for credentials, skipping renewal.");
            }
        }
    } catch (Exception e) {
        LOG.warn("could not renew the credentials, one of the possible reason is tokens are beyond "
                + "renewal period so attempting to get new tokens.", e);
        populateCredentials(credentials, topologyConf);
    }
}

From source file:com.mellanox.r4h.DFSClient.java

License:Apache License

/**
 * Renew a delegation token//from   w  w w .ja  va 2 s.co m
 * 
 * @param token
 *            the token to renew
 * @return the new expiration time
 * @throws InvalidToken
 * @throws IOException
 * @deprecated Use Token.renew instead.
 */
@Deprecated
public long renewDelegationToken(Token<DelegationTokenIdentifier> token) throws InvalidToken, IOException {
    LOG.info("Renewing " + DelegationTokenIdentifier.stringifyToken(token));
    try {
        return token.renew(conf);
    } catch (InterruptedException ie) {
        throw new RuntimeException("caught interrupted", ie);
    } catch (RemoteException re) {
        throw re.unwrapRemoteException(InvalidToken.class, AccessControlException.class);
    }
}

From source file:org.apache.apex.engine.security.TokenRenewer.java

License:Apache License

private long renewTokens(final boolean refresh, boolean checkOnly) throws IOException {
    logger.info("{}", checkOnly ? "Checking renewal" : (refresh ? "Refreshing tokens" : "Renewing tokens"));
    long expiryTime = System.currentTimeMillis() + (refresh ? tokenLifeTime : tokenRenewalInterval);

    final String tokenRenewer = UserGroupInformation.getCurrentUser().getUserName();
    logger.debug("Token renewer {}", tokenRenewer);

    File keyTabFile = null;/* w  w w  .  j av a 2s .com*/
    try (FileSystem fs = FileSystem.newInstance(conf)) {
        String destinationDir = FileUtils.getTempDirectoryPath();
        keyTabFile = FSUtil.copyToLocalFileSystem(fs, destinationDir, destinationFile, hdfsKeyTabFile, conf);

        if (principal == null) {
            //principal = UserGroupInformation.getCurrentUser().getUserName();
            principal = UserGroupInformation.getLoginUser().getUserName();
        }
        logger.debug("Principal {}", principal);
        UserGroupInformation ugi = UserGroupInformation.loginUserFromKeytabAndReturnUGI(principal,
                keyTabFile.getAbsolutePath());
        if (!checkOnly) {
            try {
                UserGroupInformation currUGI = UserGroupInformation.createProxyUser(tokenRenewer, ugi);
                currUGI.doAs(new PrivilegedExceptionAction<Object>() {
                    @Override
                    public Object run() throws Exception {

                        if (refresh) {
                            Credentials creds = new Credentials();
                            try (FileSystem fs1 = FileSystem.newInstance(conf)) {
                                logger.info("Refreshing fs tokens");
                                fs1.addDelegationTokens(tokenRenewer, creds);
                                logger.info("Refreshed tokens");
                            }
                            if (renewRMToken) {
                                try (YarnClient yarnClient = StramClientUtils.createYarnClient(conf)) {
                                    logger.info("Refreshing rm tokens");
                                    new StramClientUtils.ClientRMHelper(yarnClient, conf)
                                            .addRMDelegationToken(tokenRenewer, creds);
                                    logger.info("Refreshed tokens");
                                }
                            }
                            credentials.addAll(creds);
                        } else {
                            Collection<Token<? extends TokenIdentifier>> tokens = credentials.getAllTokens();
                            for (Token<? extends TokenIdentifier> token : tokens) {
                                logger.debug("Token {}", token);
                                if (token.getKind().equals(HDFS_TOKEN_KIND) || (renewRMToken
                                        && token.getKind().equals(RMDelegationTokenIdentifier.KIND_NAME))) {
                                    logger.info("Renewing token {}", token.getKind());
                                    token.renew(conf);
                                    logger.info("Renewed token");
                                }
                            }
                        }

                        return null;
                    }
                });
                UserGroupInformation.getCurrentUser().addCredentials(credentials);
            } catch (InterruptedException e) {
                logger.error("Error while renewing tokens ", e);
                expiryTime = System.currentTimeMillis();
            } catch (IOException e) {
                logger.error("Error while renewing tokens ", e);
                expiryTime = System.currentTimeMillis();
            }
        }
        if (logger.isDebugEnabled()) {
            logger.debug("number of tokens: {}", credentials.getAllTokens().size());
            Iterator<Token<?>> iter = credentials.getAllTokens().iterator();
            while (iter.hasNext()) {
                Token<?> token = iter.next();
                logger.debug("updated token: {}", token);
            }
        }
    } finally {
        if (keyTabFile != null) {
            keyTabFile.delete();
        }
    }
    return expiryTime;
}

From source file:org.apache.storm.hdfs.security.AutoHDFS.java

License:Apache License

/**
 * {@inheritDoc}//w w  w. j  a  v a  2s.  c  om
 */
@Override
@SuppressWarnings("unchecked")
public void doRenew(Map<String, String> credentials, Map<String, Object> topologyConf) {
    for (Pair<String, Credentials> cred : getCredentials(credentials)) {
        try {
            Configuration configuration = getHadoopConfiguration(topologyConf, cred.getFirst());
            Collection<Token<? extends TokenIdentifier>> tokens = cred.getSecond().getAllTokens();

            if (tokens != null && !tokens.isEmpty()) {
                for (Token token : tokens) {
                    //We need to re-login some other thread might have logged into hadoop using
                    // their credentials (e.g. AutoHBase might be also part of nimbu auto creds)
                    login(configuration);
                    long expiration = token.renew(configuration);
                    LOG.info("HDFS delegation token renewed, new expiration time {}", expiration);
                }
            } else {
                LOG.debug("No tokens found for credentials, skipping renewal.");
            }
        } catch (Exception e) {
            LOG.warn("could not renew the credentials, one of the possible reason is tokens are beyond "
                    + "renewal period so attempting to get new tokens.", e);
            populateCredentials(credentials, topologyConf);
        }
    }
}

From source file:org.apache.storm.hdfs.security.AutoHDFSNimbus.java

License:Apache License

/**
 * {@inheritDoc}/*  w w w .  j a v  a2s.c o m*/
 */
@Override
public void doRenew(Map<String, String> credentials, Map<String, Object> topologyConf,
        final String topologyOwnerPrincipal) {
    List<String> confKeys = getConfigKeys(topologyConf);
    for (Pair<String, Credentials> cred : getCredentials(credentials, confKeys)) {
        try {
            Configuration configuration = getHadoopConfiguration(topologyConf, cred.getFirst());
            Collection<Token<? extends TokenIdentifier>> tokens = cred.getSecond().getAllTokens();

            if (tokens != null && !tokens.isEmpty()) {
                for (Token<? extends TokenIdentifier> token : tokens) {
                    //We need to re-login some other thread might have logged into hadoop using
                    // their credentials (e.g. AutoHBase might be also part of nimbu auto creds)
                    login(configuration);
                    long expiration = token.renew(configuration);
                    LOG.info("HDFS delegation token renewed, new expiration time {}", expiration);
                }
            } else {
                LOG.debug("No tokens found for credentials, skipping renewal.");
            }
        } catch (Exception e) {
            LOG.warn("could not renew the credentials, one of the possible reason is tokens are beyond "
                    + "renewal period so attempting to get new tokens.", e);
            populateCredentials(credentials, topologyConf, topologyOwnerPrincipal);
        }
    }
}