Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,13 @@
import org.bouncycastle.cert.X509CertificateHolder;
import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter;
import org.bouncycastle.pkcs.PKCS10CertificationRequest;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.IOException;
import java.math.BigInteger;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.KeyPair;
import java.security.NoSuchAlgorithmException;
Expand All @@ -61,30 +60,29 @@
import java.util.concurrent.Future;
import java.util.function.Consumer;

import static junit.framework.TestCase.assertTrue;
import static org.apache.hadoop.hdds.HddsConfigKeys.OZONE_METADATA_DIRS;
import static org.apache.hadoop.hdds.protocol.proto.HddsProtos.NodeType.OM;
import static org.apache.hadoop.hdds.protocol.proto.HddsProtos.NodeType.SCM;
import static org.apache.hadoop.hdds.security.x509.certificate.authority.CertificateServer.CAType.INTERMEDIARY_CA;
import static org.apache.hadoop.hdds.security.x509.certificate.authority.CertificateServer.CAType.SELF_SIGNED_CA;
import static org.apache.hadoop.ozone.OzoneConsts.SCM_CA_CERT_STORAGE_DIR;
import static org.apache.hadoop.ozone.OzoneConsts.SCM_CA_PATH;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

/**
* Tests the Default CA Server.
*/
public class TestDefaultCAServer {
private static OzoneConfiguration conf = new OzoneConfiguration();
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
private MockCAStore caStore;

@Before
public void init() throws IOException {
conf.set(OZONE_METADATA_DIRS, temporaryFolder.newFolder().toString());
@BeforeEach
public void init(@TempDir Path tempDir) throws IOException {
conf.set(OZONE_METADATA_DIRS, tempDir.toString());
caStore = new MockCAStore();
}

Expand Down Expand Up @@ -331,16 +329,16 @@ public void testRequestCertificateWithInvalidSubjectFailure()
});
}

@Test(expected = IllegalStateException.class)
public void testIntermediaryCAWithEmpty()
throws Exception {
@Test
public void testIntermediaryCAWithEmpty() {

CertificateServer scmCA = new DefaultCAServer("testCA",
RandomStringUtils.randomAlphabetic(4),
RandomStringUtils.randomAlphabetic(4), caStore,
new DefaultProfile(), Paths.get("scm").toString());

scmCA.init(new SecurityConfig(conf), INTERMEDIARY_CA);
assertThrows(IllegalStateException.class,
() -> scmCA.init(new SecurityConfig(conf), INTERMEDIARY_CA));
}

@Test
Expand All @@ -362,7 +360,7 @@ clusterId, scmId, caStore, new DefaultProfile(),
new SCMCertificateClient(new SecurityConfig(conf));

CertificateClient.InitResponse response = scmCertificateClient.init();
Assert.assertEquals(CertificateClient.InitResponse.GETCERT, response);
assertEquals(CertificateClient.InitResponse.GETCERT, response);

// Generate cert
KeyPair keyPair =
Expand All @@ -378,12 +376,12 @@ clusterId, scmId, caStore, new DefaultProfile(),

Future<X509CertificateHolder> holder = rootCA.requestCertificate(csr,
CertificateApprover.ApprovalType.TESTING_AUTOMATIC, SCM);
Assert.assertTrue(holder.isDone());
assertTrue(holder.isDone());

X509CertificateHolder certificateHolder = holder.get();


Assert.assertNotNull(certificateHolder);
assertNotNull(certificateHolder);
LocalDate invalidAfterDate = certificateHolder.getNotAfter().toInstant()
.atZone(ZoneId.systemDefault())
.toLocalDate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,38 +43,34 @@
import org.bouncycastle.pkcs.PKCS10CertificationRequestBuilder;
import org.bouncycastle.pkcs.PKCSException;
import org.bouncycastle.pkcs.jcajce.JcaPKCS10CertificationRequestBuilder;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.IOException;
import java.nio.file.Path;
import java.security.KeyPair;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

import static org.apache.hadoop.hdds.HddsConfigKeys.OZONE_METADATA_DIRS;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Tests for the default PKI Profile.
*/
public class TestDefaultProfile {
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();

private OzoneConfiguration configuration;
private SecurityConfig securityConfig;
private DefaultProfile defaultProfile;
private MockApprover testApprover;
private KeyPair keyPair;

@Before
public void setUp() throws Exception {
@BeforeEach
public void setUp(@TempDir Path tempDir) throws Exception {
configuration = new OzoneConfiguration();
configuration.set(OZONE_METADATA_DIRS,
temporaryFolder.newFolder().toString());
configuration.set(OZONE_METADATA_DIRS, tempDir.toString());
securityConfig = new SecurityConfig(configuration);
defaultProfile = new DefaultProfile();
testApprover = new MockApprover(defaultProfile,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.apache.hadoop.hdds.HddsConfigKeys.OZONE_METADATA_DIRS;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
Expand All @@ -33,6 +33,7 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.KeyPair;
import java.security.NoSuchAlgorithmException;
Expand Down Expand Up @@ -60,12 +61,9 @@
import org.bouncycastle.cert.X509v2CRLBuilder;
import org.bouncycastle.operator.OperatorCreationException;
import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

/**
* Tests for the CRLCodec.
Expand All @@ -79,8 +77,6 @@ public class TestCRLCodec {
private KeyPair keyPair;
private static final String TMP_CERT_FILE_NAME = "pemcertificate.crt";

@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
private File basePath;
private static final String TMP_CRL_ENTRY =
"-----BEGIN X509 CRL-----\n" +
Expand All @@ -95,12 +91,12 @@ public class TestCRLCodec {
"tPiRCAUQLW9BACm17xc=\n" +
"-----END X509 CRL-----\n";

@Before
public void init() throws NoSuchProviderException,
@BeforeEach
public void init(@TempDir Path tempDir) throws NoSuchProviderException,
NoSuchAlgorithmException, IOException,
CertificateException, OperatorCreationException {

conf.set(OZONE_METADATA_DIRS, temporaryFolder.newFolder().toString());
conf.set(OZONE_METADATA_DIRS, tempDir.toString());
securityConfig = new SecurityConfig(conf);
writeTempCert();
x509CertificateHolder = readTempCert();
Expand Down Expand Up @@ -272,7 +268,7 @@ private void writeTempCert() throws NoSuchProviderException,
securityConfig.getCertificateLocation("scm")));

if (!basePath.exists()) {
Assert.assertTrue(basePath.mkdirs());
assertTrue(basePath.mkdirs());
}
codec.writeCertificate(basePath.toPath(), TMP_CERT_FILE_NAME,
pemString, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,12 @@
import org.apache.hadoop.hdds.security.x509.certificates.utils.SelfSignedCertificate;
import org.apache.hadoop.hdds.security.x509.keys.HDDSKeyGenerator;
import org.bouncycastle.cert.X509CertificateHolder;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.cert.CertificateException;
Expand All @@ -42,9 +40,9 @@
import java.time.temporal.ChronoUnit;

import static org.apache.hadoop.hdds.HddsConfigKeys.OZONE_METADATA_DIRS;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Tests the Certificate codecs.
Expand All @@ -53,12 +51,10 @@ public class TestCertificateCodec {
private static OzoneConfiguration conf = new OzoneConfiguration();
private static final String COMPONENT = "test";
private SecurityConfig securityConfig;
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();

@Before
public void init() throws IOException {
conf.set(OZONE_METADATA_DIRS, temporaryFolder.newFolder().toString());
@BeforeEach
public void init(@TempDir Path tempDir) {
conf.set(OZONE_METADATA_DIRS, tempDir.toString());
securityConfig = new SecurityConfig(conf);
}

Expand Down Expand Up @@ -118,9 +114,9 @@ public void testGetPEMEncodedString()
* @throws CertificateException - on Error.
*/
@Test
public void testwriteCertificate() throws NoSuchProviderException,
NoSuchAlgorithmException, IOException, SCMSecurityException,
CertificateException {
public void testWriteCertificate(@TempDir Path basePath)
throws NoSuchProviderException, NoSuchAlgorithmException,
IOException, SCMSecurityException, CertificateException {
HDDSKeyGenerator keyGenerator =
new HDDSKeyGenerator(conf);
X509CertificateHolder cert =
Expand All @@ -137,16 +133,13 @@ public void testwriteCertificate() throws NoSuchProviderException,
.build();
CertificateCodec codec = new CertificateCodec(securityConfig, COMPONENT);
String pemString = codec.getPEMEncodedString(cert);
File basePath = temporaryFolder.newFolder();
if (!basePath.exists()) {
Assert.assertTrue(basePath.mkdirs());
}
codec.writeCertificate(basePath.toPath(), "pemcertificate.crt",
codec.writeCertificate(basePath, "pemcertificate.crt",
pemString, false);
X509CertificateHolder certHolder =
codec.readCertificate(basePath.toPath(), "pemcertificate.crt");
codec.readCertificate(basePath, "pemcertificate.crt");
assertNotNull(certHolder);
assertEquals(cert.getSerialNumber(), certHolder.getSerialNumber());
assertEquals(cert.getSerialNumber(),
certHolder.getSerialNumber());
}

/**
Expand Down
Loading