Skip to content

Commit

Permalink
HDDS-2079. Fix TestSecureOzoneManager. Contributed by Xiaoyu Yao. (#1400
Browse files Browse the repository at this point in the history
)

(cherry picked from commit ae28747)
  • Loading branch information
xiaoyuyao authored and nandakumar131 committed Sep 5, 2019
1 parent 99e8e05 commit 58c626f
Showing 1 changed file with 14 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import org.junit.Test;
import org.junit.rules.Timeout;

import java.net.ConnectException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.KeyPair;
Expand Down Expand Up @@ -122,61 +121,44 @@ public void testSecureOmInitFailures() throws Exception {
omLogs.clearOutput();

// Case 1: When keypair as well as certificate is missing. Initial keypair
// boot-up. Get certificate will fail no SCM is not running.
LambdaTestUtils.intercept(ConnectException.class, "Connection " +
"refused; For more detail",
() -> OzoneManager.initializeSecurity(conf, omStorage));
// boot-up. Get certificate will fail when SCM is not running.
SecurityConfig securityConfig = new SecurityConfig(conf);
CertificateClient client =
new OMCertificateClient(securityConfig);
CertificateClient client = new OMCertificateClient(securityConfig,
omStorage.getOmCertSerialId());
Assert.assertEquals(CertificateClient.InitResponse.GETCERT, client.init());
privateKey = client.getPrivateKey();
publicKey = client.getPublicKey();
Assert.assertNotNull(client.getPrivateKey());
Assert.assertNotNull(client.getPublicKey());
Assert.assertNull(client.getCertificate());
Assert.assertTrue(omLogs.getOutput().contains("Init response: GETCERT"));
omLogs.clearOutput();

// Case 2: If key pair already exist than response should be RECOVER.
client = new OMCertificateClient(securityConfig);
LambdaTestUtils.intercept(RuntimeException.class, " OM security" +
" initialization failed",
() -> OzoneManager.initializeSecurity(conf, omStorage));
client = new OMCertificateClient(securityConfig,
omStorage.getOmCertSerialId());
Assert.assertEquals(CertificateClient.InitResponse.RECOVER, client.init());
Assert.assertNotNull(client.getPrivateKey());
Assert.assertNotNull(client.getPublicKey());
Assert.assertNull(client.getCertificate());
Assert.assertTrue(omLogs.getOutput().contains("Init response: RECOVER"));
Assert.assertTrue(omLogs.getOutput().contains(" OM certificate is " +
"missing"));
omLogs.clearOutput();

// Case 3: When public key as well as certificate is missing.
client = new OMCertificateClient(securityConfig);
FileUtils.deleteQuietly(Paths.get(securityConfig.getKeyLocation(COMPONENT)
.toString(), securityConfig.getPublicKeyFileName()).toFile());
LambdaTestUtils.intercept(RuntimeException.class, " OM security" +
" initialization failed",
() -> OzoneManager.initializeSecurity(conf, omStorage));
Assert.assertEquals(CertificateClient.InitResponse.FAILURE, client.init());
Assert.assertNotNull(client.getPrivateKey());
Assert.assertNull(client.getPublicKey());
Assert.assertNull(client.getCertificate());
Assert.assertTrue(omLogs.getOutput().contains("Init response: FAILURE"));
omLogs.clearOutput();

// Case 4: When private key and certificate is missing.
client = new OMCertificateClient(securityConfig);
FileUtils.deleteQuietly(Paths.get(securityConfig.getKeyLocation(COMPONENT)
.toString(), securityConfig.getPrivateKeyFileName()).toFile());
KeyCodec keyCodec = new KeyCodec(securityConfig, COMPONENT);
keyCodec.writePublicKey(publicKey);
LambdaTestUtils.intercept(RuntimeException.class, " OM security" +
" initialization failed",
() -> OzoneManager.initializeSecurity(conf, omStorage));
FileUtils.deleteQuietly(Paths.get(securityConfig.getKeyLocation(COMPONENT)
.toString(), securityConfig.getPrivateKeyFileName()).toFile());
Assert.assertEquals(CertificateClient.InitResponse.FAILURE, client.init());
Assert.assertNull(client.getPrivateKey());
Assert.assertNotNull(client.getPublicKey());
Assert.assertNull(client.getCertificate());
Assert.assertTrue(omLogs.getOutput().contains("Init response: FAILURE"));
omLogs.clearOutput();

// Case 5: When only certificate is present.
FileUtils.deleteQuietly(Paths.get(securityConfig.getKeyLocation(COMPONENT)
Expand All @@ -191,37 +173,29 @@ public void testSecureOmInitFailures() throws Exception {
client = new OMCertificateClient(securityConfig,
x509Certificate.getSerialNumber().toString());
omStorage.setOmCertSerialId(x509Certificate.getSerialNumber().toString());
LambdaTestUtils.intercept(RuntimeException.class, " OM security" +
" initialization failed",
() -> OzoneManager.initializeSecurity(conf, omStorage));
Assert.assertEquals(CertificateClient.InitResponse.FAILURE, client.init());
Assert.assertNull(client.getPrivateKey());
Assert.assertNull(client.getPublicKey());
Assert.assertNotNull(client.getCertificate());
Assert.assertTrue(omLogs.getOutput().contains("Init response: FAILURE"));
omLogs.clearOutput();

// Case 6: When private key and certificate is present.
client = new OMCertificateClient(securityConfig,
x509Certificate.getSerialNumber().toString());
FileUtils.deleteQuietly(Paths.get(securityConfig.getKeyLocation(COMPONENT)
.toString(), securityConfig.getPublicKeyFileName()).toFile());
keyCodec.writePrivateKey(privateKey);
OzoneManager.initializeSecurity(conf, omStorage);
Assert.assertEquals(CertificateClient.InitResponse.SUCCESS, client.init());
Assert.assertNotNull(client.getPrivateKey());
Assert.assertNotNull(client.getPublicKey());
Assert.assertNotNull(client.getCertificate());
Assert.assertTrue(omLogs.getOutput().contains("Init response: SUCCESS"));
omLogs.clearOutput();

// Case 7 When keypair and certificate is present.
client = new OMCertificateClient(securityConfig,
x509Certificate.getSerialNumber().toString());
OzoneManager.initializeSecurity(conf, omStorage);
Assert.assertEquals(CertificateClient.InitResponse.SUCCESS, client.init());
Assert.assertNotNull(client.getPrivateKey());
Assert.assertNotNull(client.getPublicKey());
Assert.assertNotNull(client.getCertificate());
Assert.assertTrue(omLogs.getOutput().contains("Init response: SUCCESS"));
omLogs.clearOutput();
}

/**
Expand Down

0 comments on commit 58c626f

Please sign in to comment.