Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow hot reloading of certs through KeyCertOptions #4519

Closed
Closed
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
16 changes: 16 additions & 0 deletions src/main/generated/io/vertx/core/net/JksOptionsConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public static void fromJson(Iterable<java.util.Map.Entry<String, Object>> json,
obj.setAliasPassword((String)member.getValue());
}
break;
case "certRefreshRateInSeconds":
if (member.getValue() instanceof Number) {
obj.setCertRefreshRateInSeconds(((Number)member.getValue()).longValue());
}
break;
case "password":
if (member.getValue() instanceof String) {
obj.setPassword((String)member.getValue());
Expand All @@ -40,6 +45,11 @@ public static void fromJson(Iterable<java.util.Map.Entry<String, Object>> json,
obj.setPath((String)member.getValue());
}
break;
case "reloadCerts":
if (member.getValue() instanceof Boolean) {
obj.setReloadCerts((Boolean)member.getValue());
}
break;
case "value":
if (member.getValue() instanceof String) {
obj.setValue(io.vertx.core.buffer.Buffer.buffer(BASE64_DECODER.decode((String)member.getValue())));
Expand All @@ -60,12 +70,18 @@ public static void toJson(JksOptions obj, java.util.Map<String, Object> json) {
if (obj.getAliasPassword() != null) {
json.put("aliasPassword", obj.getAliasPassword());
}
if (obj.getCertRefreshRateInSeconds() != null) {
json.put("certRefreshRateInSeconds", obj.getCertRefreshRateInSeconds());
}
if (obj.getPassword() != null) {
json.put("password", obj.getPassword());
}
if (obj.getPath() != null) {
json.put("path", obj.getPath());
}
if (obj.getReloadCerts() != null) {
json.put("reloadCerts", obj.getReloadCerts());
}
if (obj.getValue() != null) {
json.put("value", BASE64_ENCODER.encodeToString(obj.getValue().getBytes()));
}
Expand Down
16 changes: 16 additions & 0 deletions src/main/generated/io/vertx/core/net/KeyStoreOptionsConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public static void fromJson(Iterable<java.util.Map.Entry<String, Object>> json,
obj.setAliasPassword((String)member.getValue());
}
break;
case "certRefreshRateInSeconds":
if (member.getValue() instanceof Number) {
obj.setCertRefreshRateInSeconds(((Number)member.getValue()).longValue());
}
break;
case "password":
if (member.getValue() instanceof String) {
obj.setPassword((String)member.getValue());
Expand All @@ -45,6 +50,11 @@ public static void fromJson(Iterable<java.util.Map.Entry<String, Object>> json,
obj.setProvider((String)member.getValue());
}
break;
case "reloadCerts":
if (member.getValue() instanceof Boolean) {
obj.setReloadCerts((Boolean)member.getValue());
}
break;
case "type":
if (member.getValue() instanceof String) {
obj.setType((String)member.getValue());
Expand All @@ -70,6 +80,9 @@ public static void toJson(KeyStoreOptions obj, java.util.Map<String, Object> jso
if (obj.getAliasPassword() != null) {
json.put("aliasPassword", obj.getAliasPassword());
}
if (obj.getCertRefreshRateInSeconds() != null) {
json.put("certRefreshRateInSeconds", obj.getCertRefreshRateInSeconds());
}
if (obj.getPassword() != null) {
json.put("password", obj.getPassword());
}
Expand All @@ -79,6 +92,9 @@ public static void toJson(KeyStoreOptions obj, java.util.Map<String, Object> jso
if (obj.getProvider() != null) {
json.put("provider", obj.getProvider());
}
if (obj.getReloadCerts() != null) {
json.put("reloadCerts", obj.getReloadCerts());
}
if (obj.getType() != null) {
json.put("type", obj.getType());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ static void fromJson(Iterable<java.util.Map.Entry<String, Object>> json, PemKeyC
obj.setCertPaths(list);
}
break;
case "certRefreshRateInSeconds":
if (member.getValue() instanceof Number) {
obj.setCertRefreshRateInSeconds(((Number)member.getValue()).longValue());
}
break;
case "certValue":
if (member.getValue() instanceof String) {
obj.setCertValue(io.vertx.core.buffer.Buffer.buffer(BASE64_DECODER.decode((String)member.getValue())));
Expand Down Expand Up @@ -80,6 +85,11 @@ static void fromJson(Iterable<java.util.Map.Entry<String, Object>> json, PemKeyC
obj.setKeyValues(list);
}
break;
case "reloadCerts":
if (member.getValue() instanceof Boolean) {
obj.setReloadCerts((Boolean)member.getValue());
}
break;
}
}
}
Expand All @@ -94,6 +104,9 @@ static void toJson(PemKeyCertOptions obj, java.util.Map<String, Object> json) {
obj.getCertPaths().forEach(item -> array.add(item));
json.put("certPaths", array);
}
if (obj.getCertRefreshRateInSeconds() != null) {
json.put("certRefreshRateInSeconds", obj.getCertRefreshRateInSeconds());
}
if (obj.getCertValues() != null) {
JsonArray array = new JsonArray();
obj.getCertValues().forEach(item -> array.add(BASE64_ENCODER.encodeToString(item.getBytes())));
Expand All @@ -109,5 +122,8 @@ static void toJson(PemKeyCertOptions obj, java.util.Map<String, Object> json) {
obj.getKeyValues().forEach(item -> array.add(BASE64_ENCODER.encodeToString(item.getBytes())));
json.put("keyValues", array);
}
if (obj.getReloadCerts() != null) {
json.put("reloadCerts", obj.getReloadCerts());
}
}
}
16 changes: 16 additions & 0 deletions src/main/generated/io/vertx/core/net/PfxOptionsConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ static void fromJson(Iterable<java.util.Map.Entry<String, Object>> json, PfxOpti
obj.setAliasPassword((String)member.getValue());
}
break;
case "certRefreshRateInSeconds":
if (member.getValue() instanceof Number) {
obj.setCertRefreshRateInSeconds(((Number)member.getValue()).longValue());
}
break;
case "password":
if (member.getValue() instanceof String) {
obj.setPassword((String)member.getValue());
Expand All @@ -40,6 +45,11 @@ static void fromJson(Iterable<java.util.Map.Entry<String, Object>> json, PfxOpti
obj.setPath((String)member.getValue());
}
break;
case "reloadCerts":
if (member.getValue() instanceof Boolean) {
obj.setReloadCerts((Boolean)member.getValue());
}
break;
case "value":
if (member.getValue() instanceof String) {
obj.setValue(io.vertx.core.buffer.Buffer.buffer(BASE64_DECODER.decode((String)member.getValue())));
Expand All @@ -60,12 +70,18 @@ static void toJson(PfxOptions obj, java.util.Map<String, Object> json) {
if (obj.getAliasPassword() != null) {
json.put("aliasPassword", obj.getAliasPassword());
}
if (obj.getCertRefreshRateInSeconds() != null) {
json.put("certRefreshRateInSeconds", obj.getCertRefreshRateInSeconds());
}
if (obj.getPassword() != null) {
json.put("password", obj.getPassword());
}
if (obj.getPath() != null) {
json.put("path", obj.getPath());
}
if (obj.getReloadCerts() != null) {
json.put("reloadCerts", obj.getReloadCerts());
}
if (obj.getValue() != null) {
json.put("value", BASE64_ENCODER.encodeToString(obj.getValue().getBytes()));
}
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/io/vertx/core/http/impl/HttpServerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.vertx.core.impl.VertxInternal;
import io.vertx.core.impl.logging.Logger;
import io.vertx.core.impl.logging.LoggerFactory;
import io.vertx.core.net.KeyCertOptions;
import io.vertx.core.net.SocketAddress;
import io.vertx.core.net.impl.*;
import io.vertx.core.spi.metrics.MetricsProvider;
Expand Down Expand Up @@ -200,11 +201,13 @@ protected Handler<Channel> childHandler(ContextInternal context, SocketAddress a

@Override
protected SSLHelper createSSLHelper() {
return new SSLHelper(options, options
.getAlpnVersions()
.stream()
.map(HttpVersion::alpnName)
.collect(Collectors.toList()));
KeyCertOptions keyCertOptions = options.getKeyCertOptions();
SSLHelper sslHelper = new SSLHelper(options, options.getAlpnVersions()
.stream()
.map(HttpVersion::alpnName)
.collect(Collectors.toList()));
startPeriodicSSLContextReload(keyCertOptions, sslHelper);
return sslHelper;
}

@Override
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/io/vertx/core/net/JksOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ public JksOptions setAliasPassword(String aliasPassword) {
return (JksOptions) super.setAliasPassword(aliasPassword);
}

@Override
public JksOptions setReloadCerts(Boolean reloadCerts) {
return (JksOptions) super.setReloadCerts(reloadCerts);
}

@Override
public JksOptions setCertRefreshRateInSeconds(Long certRefreshRateInSeconds) {
return (JksOptions) super.setCertRefreshRateInSeconds(certRefreshRateInSeconds);
}

@Override
public JksOptions copy() {
return new JksOptions(this);
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/io/vertx/core/net/KeyCertOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@

package io.vertx.core.net;

import io.vertx.codegen.annotations.GenIgnore;
import io.vertx.core.Vertx;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.X509KeyManager;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

/**
Expand All @@ -39,6 +41,35 @@ public interface KeyCertOptions {
*/
KeyManagerFactory getKeyManagerFactory(Vertx vertx) throws Exception;

/**
* This method is used to check if context reloading is enabled.
*/
default Boolean getReloadCerts() {
return Boolean.FALSE;
}

/**
* This method is used by {@link io.vertx.core.net.impl.SSLHelper} to check if context reloading is needed.
*/
@GenIgnore(GenIgnore.PERMITTED_TYPE)
default boolean isReloadNeeded() {
return false;
}

/**
* This method returns default certificate refresh rate. By default, set to 5 hrs.
*/
default Long getCertRefreshRateInSeconds() {
return TimeUnit.HOURS.toSeconds(5);
}

/**
* This method is used by {@link io.vertx.core.net.impl.SSLHelper} for reloading certs present in cert/keystore paths.
*/
default void reload() {
throw new UnsupportedOperationException("Default reload no-op method called");
}

/**
* Returns a function that maps SNI server names to {@link X509KeyManager} instance.
*
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/io/vertx/core/net/KeyStoreOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,16 @@ public KeyStoreOptions setAliasPassword(String aliasPassword) {
return (KeyStoreOptions) super.setAliasPassword(aliasPassword);
}

@Override
public KeyStoreOptions setReloadCerts(Boolean reloadCerts) {
return (KeyStoreOptions) super.setReloadCerts(reloadCerts);
}

@Override
public KeyStoreOptions setCertRefreshRateInSeconds(Long certRefreshRateInSeconds) {
return (KeyStoreOptions) super.setCertRefreshRateInSeconds(certRefreshRateInSeconds);
}

@Override
public KeyStoreOptions copy() {
return new KeyStoreOptions(this);
Expand Down
Loading