ServiceJWTManager extends AbstractService { @VisibleForTesting @InterfaceAudience.Private protected ExecutorService getExecutorService() { return tokenRenewer; } ServiceJWTManager(String name); void setHTTPClient(CloseableHttpClient client); String getMasterToken(); static final String JWT_MANAGER_PREFIX; static final String JWT_MANAGER_MASTER_TOKEN_KEY; static final String JWT_MANAGER_RENEW_TOKEN_PATTERN; }
@Test public void testUpdatingJWTConf() throws Exception { LocalDateTime now = DateUtils.getNow(); LocalDateTime expiresAt = now.plus(10L, ChronoUnit.MINUTES); Pair<String, String[]> tokens = generateMasterAndRenewTokens(5, now, expiresAt); jwtManager = new TestingServiceJWTManager("TestingJWTManager", tokens.getFirst(), tokens.getSecond(), expiresAt); jwtManager.init(conf); jwtManager.start(); TimeUnit.MILLISECONDS.sleep(500); Assert.assertTrue(((TestingServiceJWTManager)jwtManager).renewed); Configuration sslConf = new Configuration(false); sslConf.addResource(conf.get(SSLFactory.SSL_SERVER_CONF_KEY)); String newMasterTokenConf = sslConf.get(ServiceJWTManager.JWT_MANAGER_MASTER_TOKEN_KEY, ""); Assert.assertEquals(tokens.getFirst(), newMasterTokenConf); for (int i = 0; i < tokens.getSecond().length; i++) { String newRenewalTokenConf = sslConf.get( String.format(ServiceJWTManager.JWT_MANAGER_RENEW_TOKEN_PATTERN, i), ""); Assert.assertEquals(tokens.getSecond()[i], newRenewalTokenConf); } Assert.assertTrue(isUpdaterThreadStillRunning(jwtManager.getExecutorService())); }
@Test public void testUpdateRetryOnFailure() throws Exception { LocalDateTime now = DateUtils.getNow(); LocalDateTime expiresAt = now.plus(10L, ChronoUnit.MINUTES); Pair<String, String[]> tokens = generateMasterAndRenewTokens(5, now, expiresAt); jwtManager = new FailingTestingServiceJWTManager("FailingTestingJWTManager", tokens.getFirst(), tokens.getSecond(), expiresAt, 3); jwtManager.init(conf); jwtManager.start(); int secondsWaited = 0; while (!((TestingServiceJWTManager)jwtManager).renewed && secondsWaited++ < 10) { TimeUnit.SECONDS.sleep(1); } Assert.assertTrue(((TestingServiceJWTManager)jwtManager).renewed); Assert.assertTrue(((FailingTestingServiceJWTManager)jwtManager).usedOneTimeTokens.size() > 1); Assert.assertTrue(isUpdaterThreadStillRunning(jwtManager.getExecutorService())); }
@Test public void testGiveUpAfterRetries() throws Exception { LocalDateTime now = DateUtils.getNow(); LocalDateTime expiresAt = now.plus(10L, ChronoUnit.MINUTES); int numOfRenewTokens = 5; Pair<String, String[]> tokens = generateMasterAndRenewTokens(numOfRenewTokens, now, expiresAt); jwtManager = new FailingTestingServiceJWTManager("FailingTestingJWTManager", tokens.getFirst(), tokens.getSecond(), expiresAt, Integer.MAX_VALUE); jwtManager.init(conf); jwtManager.start(); int secondsWaited = 0; while (((FailingTestingServiceJWTManager)jwtManager).usedOneTimeTokens.size() < numOfRenewTokens && secondsWaited++ < 30) { TimeUnit.SECONDS.sleep(1); } Assert.assertFalse(((TestingServiceJWTManager)jwtManager).renewed); Assert.assertEquals(numOfRenewTokens, ((FailingTestingServiceJWTManager)jwtManager).usedOneTimeTokens.size()); Assert.assertFalse(isUpdaterThreadStillRunning(jwtManager.getExecutorService())); }