@Override public PemKeyCertOptions keyCertOptions() { return new PemKeyCertOptions() .setKeyPath(privateKeyPath()) .setCertPath(certificatePath()); }
@Test public void testCopyPemKeyCertOptions() throws Exception { PemKeyCertOptions options = new PemKeyCertOptions(new JsonObject()); String keyPath1 = TestUtils.randomAlphaString(100); Buffer keyValue1 = Buffer.buffer(TestUtils.randomAlphaString(100)); String certPath1 = TestUtils.randomAlphaString(100); Buffer certValue1 = Buffer.buffer(TestUtils.randomAlphaString(100)); options.setKeyPath(keyPath1); options.setKeyValue(keyValue1); options.setCertPath(certPath1); options.setCertValue(certValue1); options = new PemKeyCertOptions(options); assertEquals(keyPath1, options.getKeyPath()); assertEquals(keyValue1, options.getKeyValue()); assertEquals(certPath1, options.getCertPath()); assertEquals(certValue1, options.getCertValue()); assertEquals(Collections.singletonList(keyPath1), options.getKeyPaths()); assertEquals(Collections.singletonList(keyValue1), options.getKeyValues()); assertEquals(Collections.singletonList(certPath1), options.getCertPaths()); assertEquals(Collections.singletonList(certValue1), options.getCertValues()); options = new PemKeyCertOptions(options.toJson()); assertEquals(keyPath1, options.getKeyPath()); assertEquals(keyValue1, options.getKeyValue()); assertEquals(certPath1, options.getCertPath()); assertEquals(certValue1, options.getCertValue()); assertEquals(Collections.singletonList(keyPath1), options.getKeyPaths()); assertEquals(Collections.singletonList(keyValue1), options.getKeyValues()); assertEquals(Collections.singletonList(certPath1), options.getCertPaths()); assertEquals(Collections.singletonList(certValue1), options.getCertValues());
case "certPath": if (member.getValue() instanceof String) { obj.setCertPath((String)member.getValue()); list.add((String)item); }); obj.setCertPaths(list); obj.setCertValue(io.vertx.core.buffer.Buffer.buffer(java.util.Base64.getDecoder().decode((String)member.getValue()))); list.add(io.vertx.core.buffer.Buffer.buffer(java.util.Base64.getDecoder().decode((String)item))); }); obj.setCertValues(list); obj.setKeyPath((String)member.getValue()); list.add((String)item); }); obj.setKeyPaths(list); obj.setKeyValue(io.vertx.core.buffer.Buffer.buffer(java.util.Base64.getDecoder().decode((String)member.getValue()))); list.add(io.vertx.core.buffer.Buffer.buffer(java.util.Base64.getDecoder().decode((String)item))); }); obj.setKeyValues(list);
@Test public void testDefaultKeyCertOptionsJson() throws Exception { PemKeyCertOptions def = new PemKeyCertOptions(); PemKeyCertOptions json = new PemKeyCertOptions(new JsonObject()); assertEquals(def.getKeyPath(), json.getKeyPath()); assertEquals(def.getCertPath(), json.getCertPath()); assertEquals(def.getKeyValue(), json.getKeyValue()); assertEquals(def.getCertValue(), json.getCertValue()); }
case PEM: PEMKeyStore pemKeyStore = (PEMKeyStore) sslOptions.getKeyStore(); PemKeyCertOptions pemKeyCertOptions = new PemKeyCertOptions(); if (pemKeyStore.getCertPath() != null && !pemKeyStore.getCertPath().isEmpty()) { pemKeyCertOptions.setCertPath(pemKeyStore.getCertPath()); } else if (pemKeyStore.getCertContent() != null && !pemKeyStore.getCertContent().isEmpty()) { pemKeyCertOptions.setCertValue(io.vertx.core.buffer.Buffer.buffer(pemKeyStore.getCertContent())); pemKeyCertOptions.setKeyPath(pemKeyStore.getKeyPath()); } else if (pemKeyStore.getKeyContent() != null && !pemKeyStore.getKeyContent().isEmpty()) { pemKeyCertOptions.setKeyValue(io.vertx.core.buffer.Buffer.buffer(pemKeyStore.getKeyContent()));
@Test public void testKeyCertOptions() throws Exception { PemKeyCertOptions options = new PemKeyCertOptions(); assertNull(options.getKeyPath()); String randString = TestUtils.randomAlphaString(100); assertEquals(options, options.setKeyPath(randString)); assertEquals(randString, options.getKeyPath()); assertNull(options.getCertPath()); randString = TestUtils.randomAlphaString(100); assertEquals(options, options.setCertPath(randString)); assertEquals(randString, options.getCertPath()); }
public JsonObject getConfiguration() { JsonObject config = new JsonObject(); config.put("host", getHost()); config.put("port", getPort()); config.put("ssl", true); PemKeyCertOptions options = new PemKeyCertOptions() .addCertPath("target/vault/config/ssl/client-cert.pem") .addKeyPath("target/vault/config/ssl/client-privatekey.pem"); config.put("pemKeyCertOptions", options.toJson()); JksOptions jks = new JksOptions() .setPath("target/vault/config/ssl/truststore.jks"); config.put("trustStoreOptions", jks.toJson()); return config; }
@Override public PemKeyCertOptions clone() { return new PemKeyCertOptions(this); } }
@Test public void testCloseCompletionHandlerNotCalledWhenActualServerFailed() { server.close(); server = vertx.createNetServer( new NetServerOptions() .setSsl(true) .setPemKeyCertOptions(new PemKeyCertOptions().setKeyPath("invalid"))) .connectHandler(c -> { }); try { server.listen(10000, r -> fail()); } catch (Exception ignore) { // Expected } server.close(onSuccess(v -> { testComplete(); })); await(); }
/** * Create an options instance for the ProtonClient * * @return ProtonClient options instance */ private ProtonClientOptions createClientOptions() { ProtonClientOptions options = new ProtonClientOptions(); options.setConnectTimeout(5000); options.setReconnectAttempts(-1).setReconnectInterval(1000); // reconnect forever, every 1000 millisecs String certDir = this.options.getCertDir(); if (certDir != null) { options.setSsl(true) .addEnabledSaslMechanism("EXTERNAL") .setHostnameVerificationAlgorithm("") .setPemTrustOptions(new PemTrustOptions() .addCertPath(new File(certDir, "ca.crt").getAbsolutePath())) .setPemKeyCertOptions(new PemKeyCertOptions() .addCertPath(new File(certDir, "tls.crt").getAbsolutePath()) .addKeyPath(new File(certDir, "tls.key").getAbsolutePath())); } return options; }
@Test public void testPemKeyCertOptionsJson() throws Exception { PemKeyCertOptions options = new PemKeyCertOptions(new JsonObject()); assertEquals(null, options.getKeyPath()); assertEquals(null, options.getKeyValue()); assertEquals(null, options.getCertPath()); assertEquals(null, options.getCertValue()); String certPath1 = TestUtils.randomAlphaString(100); Buffer certValue1 = Buffer.buffer(TestUtils.randomAlphaString(100)); options = new PemKeyCertOptions(new JsonObject(). put("keyPath", keyPath1). put("keyValue", keyValue1.getBytes()). put("certPath", certPath1). put("certValue", certValue1.getBytes())); assertEquals(keyPath1, options.getKeyPath()); assertEquals(keyValue1, options.getKeyValue()); assertEquals(certPath1, options.getCertPath()); assertEquals(certValue1, options.getCertValue()); assertEquals(Collections.singletonList(keyPath1), options.getKeyPaths()); assertEquals(Collections.singletonList(keyValue1), options.getKeyValues()); assertEquals(Collections.singletonList(certPath1), options.getCertPaths()); assertEquals(Collections.singletonList(certValue1), options.getCertValues()); String certPath2 = TestUtils.randomAlphaString(100); Buffer certValue2 = Buffer.buffer(TestUtils.randomAlphaString(100)); options = new PemKeyCertOptions(new JsonObject(). put("keyPaths", new JsonArray().add(keyPath1).add(keyPath2)). put("keyValues", new JsonArray().add(keyValue1.getBytes()).add(keyValue2.getBytes())). put("certPaths", new JsonArray().add(certPath1).add(certPath2)).
@Test public void testKeyCertInvalidPem() throws IOException { String[] contents = { "", "-----BEGIN PRIVATE KEY-----", "-----BEGIN RSA PRIVATE KEY-----", "-----BEGIN PRIVATE KEY-----\n-----END PRIVATE KEY-----", "-----BEGIN RSA PRIVATE KEY-----\n-----END RSA PRIVATE KEY-----", "-----BEGIN PRIVATE KEY-----\n*\n-----END PRIVATE KEY-----", "-----BEGIN RSA PRIVATE KEY-----\n*\n-----END RSA PRIVATE KEY-----" }; String[] messages = { "Missing -----BEGIN PRIVATE KEY----- or -----BEGIN RSA PRIVATE KEY----- delimiter", "Missing -----END PRIVATE KEY----- delimiter", "Missing -----END RSA PRIVATE KEY----- delimiter", "Empty pem file", "Empty pem file", "Input byte[] should at least have 2 bytes for base64 bytes", "Input byte[] should at least have 2 bytes for base64 bytes" }; for (int i = 0;i < contents.length;i++) { Path file = testFolder.newFile("vertx" + UUID.randomUUID().toString() + ".pem").toPath(); Files.write(file, Collections.singleton(contents[i])); String expectedMessage = messages[i]; testInvalidKeyStore(Cert.SERVER_PEM.get().setKeyPath(file.toString()), expectedMessage, null); } }
public static KeyCertOptions randomKeyCertOptions() { KeyCertOptions keyCertOptions; switch (TestUtils.randomPositiveInt() % 3) { case 0: keyCertOptions = new JksOptions(); String jksPassword = TestUtils.randomAlphaString(100); ((JksOptions) keyCertOptions).setPassword(jksPassword); break; case 1: keyCertOptions = new PemKeyCertOptions(); Buffer keyValue = TestUtils.randomBuffer(100); ((PemKeyCertOptions) keyCertOptions).setKeyValue(keyValue); break; default: keyCertOptions = new PfxOptions(); String pfxPassword = TestUtils.randomAlphaString(100); ((PfxOptions) keyCertOptions).setPassword(pfxPassword); } return keyCertOptions; }
@Test public void testKeyCertInvalidCertPath() { testInvalidKeyStore(Cert.SERVER_PEM.get().setCertPath("/invalid.pem"), "java.nio.file.NoSuchFileException: ", "invalid.pem"); }
@Test public void testKeyOptionsEquality() { JksOptions jksOptions = Cert.SERVER_JKS.get(); JksOptions jksOptionsCopy = new JksOptions(jksOptions); PfxOptions pfxOptions = Cert.SERVER_PKCS12.get(); PfxOptions pfxOptionsCopy = new PfxOptions(pfxOptions); PemKeyCertOptions pemKeyCertOptions = Cert.SERVER_PEM.get(); PemKeyCertOptions pemKeyCertOptionsCopy = new PemKeyCertOptions(pemKeyCertOptions); assertEquals(jksOptions, jksOptionsCopy); assertEquals(jksOptions.hashCode(), jksOptionsCopy.hashCode()); assertEquals(pfxOptions, pfxOptionsCopy); assertEquals(pfxOptions.hashCode(), pfxOptionsCopy.hashCode()); assertEquals(pemKeyCertOptions, pemKeyCertOptionsCopy); assertEquals(pemKeyCertOptions.hashCode(), pemKeyCertOptionsCopy.hashCode()); }
json.put("pemKeyCertOptions", obj.getPemKeyCertOptions().toJson());
case PEM: PEMKeyStore pemKeyStore = (PEMKeyStore) sslOptions.getKeyStore(); PemKeyCertOptions pemKeyCertOptions = new PemKeyCertOptions(); if (pemKeyStore.getCertPath() != null && !pemKeyStore.getCertPath().isEmpty()) { pemKeyCertOptions.setCertPath(pemKeyStore.getCertPath()); } else if (pemKeyStore.getCertContent() != null && !pemKeyStore.getCertContent().isEmpty()) { pemKeyCertOptions.setCertValue(io.vertx.core.buffer.Buffer.buffer(pemKeyStore.getCertContent())); pemKeyCertOptions.setKeyPath(pemKeyStore.getKeyPath()); } else if (pemKeyStore.getKeyContent() != null && !pemKeyStore.getKeyContent().isEmpty()) { pemKeyCertOptions.setKeyValue(io.vertx.core.buffer.Buffer.buffer(pemKeyStore.getKeyContent()));
@Test public void testKeyCertOptions() throws Exception { PemKeyCertOptions options = new PemKeyCertOptions(); assertNull(options.getKeyPath()); String randString = TestUtils.randomAlphaString(100); assertEquals(options, options.setKeyPath(randString)); assertEquals(randString, options.getKeyPath()); assertNull(options.getCertPath()); randString = TestUtils.randomAlphaString(100); assertEquals(options, options.setCertPath(randString)); assertEquals(randString, options.getCertPath()); }
public JsonObject getConfiguration() { JsonObject config = new JsonObject(); config.put("host", getHost()); config.put("port", getPort()); config.put("ssl", true); PemKeyCertOptions options = new PemKeyCertOptions() .addCertPath("target/vault/config/ssl/client-cert.pem") .addKeyPath("target/vault/config/ssl/client-privatekey.pem"); config.put("pemKeyCertOptions", options.toJson()); JksOptions jks = new JksOptions() .setPath("target/vault/config/ssl/truststore.jks"); config.put("trustStoreOptions", jks.toJson()); return config; }
case "certPath": if (member.getValue() instanceof String) { obj.setCertPath((String)member.getValue()); list.add((String)item); }); obj.setCertPaths(list); obj.setCertValue(io.vertx.core.buffer.Buffer.buffer(java.util.Base64.getDecoder().decode((String)member.getValue()))); list.add(io.vertx.core.buffer.Buffer.buffer(java.util.Base64.getDecoder().decode((String)item))); }); obj.setCertValues(list); obj.setKeyPath((String)member.getValue()); list.add((String)item); }); obj.setKeyPaths(list); obj.setKeyValue(io.vertx.core.buffer.Buffer.buffer(java.util.Base64.getDecoder().decode((String)member.getValue()))); list.add(io.vertx.core.buffer.Buffer.buffer(java.util.Base64.getDecoder().decode((String)item))); }); obj.setKeyValues(list);