HDDS-2228. Fix NPE in OzoneDelegationTokenManager#addPersistedDelegat… (#1571)

This commit is contained in:
Xiaoyu Yao 2019-10-02 23:09:06 -07:00 committed by GitHub
parent 4c24f2434d
commit c5665b23ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 6 deletions

View File

@ -84,13 +84,16 @@ public class OzoneDelegationTokenSecretManager
* milliseconds * milliseconds
* @param dtRemoverScanInterval how often the tokens are scanned for expired * @param dtRemoverScanInterval how often the tokens are scanned for expired
* tokens in milliseconds * tokens in milliseconds
* @param certClient certificate client to SCM CA
*/ */
public OzoneDelegationTokenSecretManager(OzoneConfiguration conf, public OzoneDelegationTokenSecretManager(OzoneConfiguration conf,
long tokenMaxLifetime, long tokenRenewInterval, long tokenMaxLifetime, long tokenRenewInterval,
long dtRemoverScanInterval, Text service, long dtRemoverScanInterval, Text service,
S3SecretManager s3SecretManager) throws IOException { S3SecretManager s3SecretManager, CertificateClient certClient)
throws IOException {
super(new SecurityConfig(conf), tokenMaxLifetime, tokenRenewInterval, super(new SecurityConfig(conf), tokenMaxLifetime, tokenRenewInterval,
service, LOG); service, LOG);
setCertClient(certClient);
currentTokens = new ConcurrentHashMap(); currentTokens = new ConcurrentHashMap();
this.tokenRemoverScanInterval = dtRemoverScanInterval; this.tokenRemoverScanInterval = dtRemoverScanInterval;
this.s3SecretManager = (S3SecretManagerImpl) s3SecretManager; this.s3SecretManager = (S3SecretManagerImpl) s3SecretManager;

View File

@ -70,6 +70,7 @@ public abstract class OzoneSecretManager<T extends TokenIdentifier>
* @param tokenRenewInterval how often the tokens must be renewed in * @param tokenRenewInterval how often the tokens must be renewed in
* milliseconds * milliseconds
* @param service name of service * @param service name of service
* @param logger logger for the secret manager
*/ */
public OzoneSecretManager(SecurityConfig secureConf, long tokenMaxLifetime, public OzoneSecretManager(SecurityConfig secureConf, long tokenMaxLifetime,
long tokenRenewInterval, Text service, Logger logger) { long tokenRenewInterval, Text service, Logger logger) {
@ -188,7 +189,7 @@ public String formatTokenId(T id) {
public synchronized void start(CertificateClient client) public synchronized void start(CertificateClient client)
throws IOException { throws IOException {
Preconditions.checkState(!isRunning()); Preconditions.checkState(!isRunning());
this.certClient = client; setCertClient(client);
updateCurrentKey(new KeyPair(certClient.getPublicKey(), updateCurrentKey(new KeyPair(certClient.getPublicKey(),
certClient.getPrivateKey())); certClient.getPrivateKey()));
setIsRunning(true); setIsRunning(true);
@ -247,5 +248,9 @@ public AtomicInteger getTokenSequenceNumber() {
public CertificateClient getCertClient() { public CertificateClient getCertClient() {
return certClient; return certClient;
} }
public void setCertClient(CertificateClient client) {
this.certClient = client;
}
} }

View File

@ -627,7 +627,7 @@ private OzoneDelegationTokenSecretManager createDelegationTokenSecretManager(
return new OzoneDelegationTokenSecretManager(conf, tokenMaxLifetime, return new OzoneDelegationTokenSecretManager(conf, tokenMaxLifetime,
tokenRenewInterval, tokenRemoverScanInterval, omRpcAddressTxt, tokenRenewInterval, tokenRemoverScanInterval, omRpcAddressTxt,
s3SecretManager); s3SecretManager, certClient);
} }
private OzoneBlockTokenSecretManager createBlockTokenSecretManager( private OzoneBlockTokenSecretManager createBlockTokenSecretManager(

View File

@ -169,8 +169,15 @@ public void testCreateToken() throws Exception {
validateHash(token.getPassword(), token.getIdentifier()); validateHash(token.getPassword(), token.getIdentifier());
} }
@Test private void restartSecretManager() throws IOException {
public void testRenewTokenSuccess() throws Exception { secretManager.stop();
secretManager = null;
secretManager = createSecretManager(conf, tokenMaxLifetime,
expiryTime, tokenRemoverScanInterval);
}
private void testRenewTokenSuccessHelper(boolean restartSecretManager)
throws Exception {
secretManager = createSecretManager(conf, tokenMaxLifetime, secretManager = createSecretManager(conf, tokenMaxLifetime,
expiryTime, tokenRemoverScanInterval); expiryTime, tokenRemoverScanInterval);
secretManager.start(certificateClient); secretManager.start(certificateClient);
@ -178,10 +185,25 @@ public void testRenewTokenSuccess() throws Exception {
TEST_USER, TEST_USER,
TEST_USER); TEST_USER);
Thread.sleep(10 * 5); Thread.sleep(10 * 5);
if (restartSecretManager) {
restartSecretManager();
}
long renewalTime = secretManager.renewToken(token, TEST_USER.toString()); long renewalTime = secretManager.renewToken(token, TEST_USER.toString());
Assert.assertTrue(renewalTime > 0); Assert.assertTrue(renewalTime > 0);
} }
@Test
public void testReloadAndRenewToken() throws Exception {
testRenewTokenSuccessHelper(true);
}
@Test
public void testRenewTokenSuccess() throws Exception {
testRenewTokenSuccessHelper(false);
}
/** /**
* Tests failure for mismatch in renewer. * Tests failure for mismatch in renewer.
*/ */
@ -375,6 +397,7 @@ private void validateHash(byte[] hash, byte[] identifier) throws Exception {
createSecretManager(OzoneConfiguration config, long tokenMaxLife, createSecretManager(OzoneConfiguration config, long tokenMaxLife,
long expiry, long tokenRemoverScanTime) throws IOException { long expiry, long tokenRemoverScanTime) throws IOException {
return new OzoneDelegationTokenSecretManager(config, tokenMaxLife, return new OzoneDelegationTokenSecretManager(config, tokenMaxLife,
expiry, tokenRemoverScanTime, serviceRpcAdd, s3SecretManager); expiry, tokenRemoverScanTime, serviceRpcAdd, s3SecretManager,
certificateClient);
} }
} }