Search in sources :

Example 1 with MockServerLogger

use of org.mockserver.logging.MockServerLogger in project signers by ConsenSys.

the class HashicorpIntegrationTest method hashicorpVaultReturnsEncryptionKeyOverTls.

@Test
void hashicorpVaultReturnsEncryptionKeyOverTls() throws IOException {
    KeyStoreFactory keyStoreFactory = new KeyStoreFactory(new MockServerLogger());
    keyStoreFactory.loadOrCreateKeyStore();
    HttpsURLConnection.setDefaultSSLSocketFactory(keyStoreFactory.sslContext().getSocketFactory());
    final ClientAndServer clientAndServer = new ClientAndServer(0);
    clientAndServer.when(request().withPath(".*")).respond(response().withStatusCode(200).withBody("{\"data\":{\"data\":{\"value\":\"" + EXPECTED_KEY_STRING + "\"}}}"));
    final Path configFile = createConfigFile(DEFAULT_HOST, clientAndServer.getLocalPort(), ROOT_TOKEN, KEY_PATH, null, TIMEOUT_MILLISECONDS, true, "JKS", keyStoreFactory.keyStoreFileName, KeyStoreFactory.KEY_STORE_PASSWORD);
    final HashicorpKeyConfig keyConfig = TomlConfigLoader.fromToml(configFile, null);
    final HashicorpConnection connection = factory.create(keyConfig.getConnectionParams());
    final String keyFetched = connection.fetchKey(keyConfig.getKeyDefinition());
    assertThat(keyFetched).isEqualTo(EXPECTED_KEY_STRING);
}
Also used : Path(java.nio.file.Path) KeyStoreFactory(org.mockserver.socket.tls.KeyStoreFactory) MockServerLogger(org.mockserver.logging.MockServerLogger) HashicorpKeyConfig(tech.pegasys.signers.hashicorp.config.HashicorpKeyConfig) ClientAndServer(org.mockserver.integration.ClientAndServer) Test(org.junit.jupiter.api.Test)

Example 2 with MockServerLogger

use of org.mockserver.logging.MockServerLogger in project mockserver by mock-server.

the class X509GeneratorTest method shouldCreateClientCertificateWithDomainsComponentsWithNumbersInSANs.

@Test
public void shouldCreateClientCertificateWithDomainsComponentsWithNumbersInSANs() throws Exception {
    // given
    X509Generator x509Generator = new X509Generator(new MockServerLogger());
    String[] domainNames = { "57bob.com", "bob57.com", "localhost.23foo.com", "127.0.0.1" };
    // and - a certificate siging request with SANs
    CertificateSigningRequest csr = new CertificateSigningRequest().setCommonName(ROOT_COMMON_NAME).setKeyPairSize(KEY_SIZE);
    csr.addSubjectAlternativeNames(domainNames);
    // and - and a root keypair
    X509AndPrivateKey pemRootKeyPair = x509Generator.generateRootX509AndPrivateKey(csr);
    // when - a certificate has been successfully generated
    X509AndPrivateKey keyPair = x509Generator.generateLeafX509AndPrivateKey(csr, buildDistinguishedName(ROOT_COMMON_NAME), pemRootKeyPair.getPrivateKey(), x509FromPEM(pemRootKeyPair.getCert()));
    X509Certificate x509Certificate = x509FromPEM(keyPair.getCert());
    // then - the correct number of SANs should be present
    Collection<List<?>> subjectAlternativeNames = x509Certificate.getSubjectAlternativeNames();
    assertEquals(4, subjectAlternativeNames.size());
    // and - the correct values are contained in the correct order
    List<?> collect = subjectAlternativeNames.stream().map(subjectAlternativeName -> subjectAlternativeName.get(1)).collect(Collectors.toList());
    assertThat(collect, containsInAnyOrder(domainNames));
}
Also used : X509Certificate(java.security.cert.X509Certificate) Date(java.util.Date) LocalDateTime(java.time.LocalDateTime) CertificateNotYetValidException(java.security.cert.CertificateNotYetValidException) CertificateExpiredException(java.security.cert.CertificateExpiredException) RSAPublicKey(java.security.interfaces.RSAPublicKey) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) EMPTY(org.apache.commons.lang3.StringUtils.EMPTY) BigInteger(java.math.BigInteger) TestCase(junit.framework.TestCase) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) IS_CA(sun.security.x509.BasicConstraintsExtension.IS_CA) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) CertificateSigningRequest(org.mockserver.socket.tls.jdk.CertificateSigningRequest) Collection(java.util.Collection) Test(org.junit.Test) X509CertImpl(sun.security.x509.X509CertImpl) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) Base64(java.util.Base64) List(java.util.List) ChronoUnit(java.time.temporal.ChronoUnit) Matchers.contains(org.hamcrest.Matchers.contains) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) PEMToFile(org.mockserver.socket.tls.PEMToFile) MockServerLogger(org.mockserver.logging.MockServerLogger) MockServerLogger(org.mockserver.logging.MockServerLogger) CertificateSigningRequest(org.mockserver.socket.tls.jdk.CertificateSigningRequest) List(java.util.List) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) X509Certificate(java.security.cert.X509Certificate) Test(org.junit.Test)

Example 3 with MockServerLogger

use of org.mockserver.logging.MockServerLogger in project mockserver by mock-server.

the class X509GeneratorTest method shouldCreateRootCertificateThatCanBePEMEncodedAndDecoded.

@Test
public void shouldCreateRootCertificateThatCanBePEMEncodedAndDecoded() throws Exception {
    // given
    X509Generator x509Generator = new X509Generator(new MockServerLogger());
    // when - a key pair is generated
    X509AndPrivateKey keyPair = x509Generator.generateRootX509AndPrivateKey(csr);
    // then - validate pem decoding/encoding of the private key
    assertEquals(keyPair.getPrivateKey(), privateKeyToPEM(privateKeyBytesFromPEM(keyPair.getPrivateKey())));
    // and - validate pem decoding/encoding of the cert
    assertEquals(keyPair.getCert(), certToPEM(certFromPem(keyPair.getCert())));
}
Also used : MockServerLogger(org.mockserver.logging.MockServerLogger) Test(org.junit.Test)

Example 4 with MockServerLogger

use of org.mockserver.logging.MockServerLogger in project mockserver by mock-server.

the class X509GeneratorTest method shouldCreateRootCertificateWithPositiveSerialNumber.

@Test
public void shouldCreateRootCertificateWithPositiveSerialNumber() throws Exception {
    // given
    X509Generator x509Generator = new X509Generator(new MockServerLogger());
    // when - a key pair is generated
    X509AndPrivateKey keyPair = x509Generator.generateRootX509AndPrivateKey(csr);
    X509Certificate x509Certificate = x509FromPEM(keyPair.getCert());
    assertTrue("The ca cert serial number is non-negative", x509Certificate.getSerialNumber().compareTo(BigInteger.ZERO) > 0);
}
Also used : MockServerLogger(org.mockserver.logging.MockServerLogger) X509Certificate(java.security.cert.X509Certificate) Test(org.junit.Test)

Example 5 with MockServerLogger

use of org.mockserver.logging.MockServerLogger in project mockserver by mock-server.

the class X509GeneratorTest method shouldCreateClientCertificateWithEmptySANs.

@Test
public void shouldCreateClientCertificateWithEmptySANs() throws Exception {
    // given
    X509Generator x509Generator = new X509Generator(new MockServerLogger());
    // and - a certificate signing request with SANs
    CertificateSigningRequest csr = new CertificateSigningRequest().setCommonName(ROOT_COMMON_NAME).setKeyPairSize(KEY_SIZE);
    csr.addSubjectAlternativeNames();
    // and - and a root keypair
    X509AndPrivateKey pemRootKeyPair = x509Generator.generateRootX509AndPrivateKey(csr);
    // when - a certificate has been successfully generated
    X509AndPrivateKey keyPair = x509Generator.generateLeafX509AndPrivateKey(csr, buildDistinguishedName(ROOT_COMMON_NAME), pemRootKeyPair.getPrivateKey(), x509FromPEM(pemRootKeyPair.getCert()));
    X509Certificate x509Certificate = x509FromPEM(keyPair.getCert());
    // then - the no SANs should be present
    Collection<List<?>> subjectAlternativeNames = x509Certificate.getSubjectAlternativeNames();
    assertNull(subjectAlternativeNames);
}
Also used : MockServerLogger(org.mockserver.logging.MockServerLogger) CertificateSigningRequest(org.mockserver.socket.tls.jdk.CertificateSigningRequest) List(java.util.List) X509Certificate(java.security.cert.X509Certificate) Test(org.junit.Test)

Aggregations

MockServerLogger (org.mockserver.logging.MockServerLogger)410 Test (org.junit.Test)327 Cookies (org.mockserver.model.Cookies)44 HttpResponse (org.mockserver.model.HttpResponse)41 Cookie (org.mockserver.model.Cookie)39 NettyHttpClient (org.mockserver.httpclient.NettyHttpClient)38 Expectation (org.mockserver.mock.Expectation)35 Before (org.junit.Before)33 HttpRequestDTO (org.mockserver.serialization.model.HttpRequestDTO)33 Scheduler (org.mockserver.scheduler.Scheduler)26 KeyStoreFactory (org.mockserver.socket.tls.KeyStoreFactory)24 LogEntry (org.mockserver.log.model.LogEntry)23 BeforeClass (org.junit.BeforeClass)22 Headers (org.mockserver.model.Headers)22 HttpState (org.mockserver.mock.HttpState)20 Header (org.mockserver.model.Header)18 X509Certificate (java.security.cert.X509Certificate)17 HttpRequest (org.mockserver.model.HttpRequest)17 InetSocketAddress (java.net.InetSocketAddress)15 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)14