/** * Get the authentication token of the user for the cluster specified in the configuration * @return null if the user does not have the token, otherwise the auth token for the cluster. */ private static Token<AuthenticationTokenIdentifier> getAuthToken(Configuration conf, User user) throws IOException, InterruptedException { ZKWatcher zkw = new ZKWatcher(conf, "TokenUtil-getAuthToken", null); try { String clusterId = ZKClusterId.readClusterIdZNode(zkw); if (clusterId == null) { throw new IOException("Failed to get cluster ID"); } return new AuthenticationTokenSelector().selectToken(new Text(clusterId), user.getTokens()); } catch (KeeperException e) { throw new IOException(e); } finally { zkw.close(); } }
@Test public void testUseExistingToken() throws Exception { User user = User.createUserForTesting(TEST_UTIL.getConfiguration(), "testuser2", new String[]{"testgroup"}); Token<AuthenticationTokenIdentifier> token = secretManager.generateToken(user.getName()); assertNotNull(token); user.addToken(token); // make sure we got a token Token<AuthenticationTokenIdentifier> firstToken = new AuthenticationTokenSelector().selectToken(token.getService(), user.getTokens()); assertNotNull(firstToken); assertEquals(token, firstToken); Connection conn = ConnectionFactory.createConnection(TEST_UTIL.getConfiguration()); try { assertFalse(TokenUtil.addTokenIfMissing(conn, user)); // make sure we still have the same token Token<AuthenticationTokenIdentifier> secondToken = new AuthenticationTokenSelector().selectToken(token.getService(), user.getTokens()); assertEquals(firstToken, secondToken); } finally { conn.close(); } }
/** * Get the authentication token of the user for the cluster specified in the configuration * @return null if the user does not have the token, otherwise the auth token for the cluster. */ private static Token<AuthenticationTokenIdentifier> getAuthToken(Configuration conf, User user) throws IOException, InterruptedException { ZooKeeperWatcher zkw = new ZooKeeperWatcher(conf, "TokenUtil-getAuthToken", null); try { String clusterId = ZKClusterId.readClusterIdZNode(zkw); if (clusterId == null) { throw new IOException("Failed to get cluster ID"); } return new AuthenticationTokenSelector().selectToken(new Text(clusterId), user.getTokens()); } catch (KeeperException e) { throw new IOException(e); } finally { zkw.close(); } } }
@Test public void testUseExistingToken() throws Exception { User user = User.createUserForTesting(TEST_UTIL.getConfiguration(), "testuser2", new String[]{"testgroup"}); Token<AuthenticationTokenIdentifier> token = secretManager.generateToken(user.getName()); assertNotNull(token); user.addToken(token); // make sure we got a token Token<AuthenticationTokenIdentifier> firstToken = new AuthenticationTokenSelector().selectToken(token.getService(), user.getTokens()); assertNotNull(firstToken); assertEquals(token, firstToken); Connection conn = ConnectionFactory.createConnection(TEST_UTIL.getConfiguration()); try { assertFalse(TokenUtil.addTokenIfMissing(conn, user)); // make sure we still have the same token Token<AuthenticationTokenIdentifier> secondToken = new AuthenticationTokenSelector().selectToken(token.getService(), user.getTokens()); assertEquals(firstToken, secondToken); } finally { conn.close(); } }