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 users to specify the ssl context #123

Merged
merged 3 commits into from
Sep 24, 2022
Merged
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
17 changes: 14 additions & 3 deletions barista/src/main/java/com/markelliot/barista/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.nio.file.Paths;
import java.util.LinkedHashSet;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import javax.net.ssl.SSLContext;
import org.slf4j.Logger;
Expand Down Expand Up @@ -76,6 +77,7 @@ public static final class Builder {
private Authz authz = Authz.denyAll();
private boolean allowAllOrigins = false;
private boolean tls = true;
private Optional<SSLContext> sslContext = Optional.empty();
private double tracingRate = 0.2;

private Builder() {}
Expand Down Expand Up @@ -121,6 +123,12 @@ public Builder disableTls() {
return this;
}

public Builder sslContext(SSLContext context) {
Objects.requireNonNull(context);
this.sslContext = Optional.of(context);
return this;
}

/**
* Sets the sample rate to run tracing for incoming requests without a traceId header.
*
Expand Down Expand Up @@ -161,9 +169,12 @@ public Server start() {
private ListenerBuilder listener() {
ListenerBuilder lb = new ListenerBuilder().setPort(port).setHost("0.0.0.0");
if (tls) {
SSLContext sslContext =
TransportLayerSecurity.createSslContext(Paths.get("var", "security"));
lb.setType(ListenerType.HTTPS).setSslContext(sslContext);
SSLContext context =
sslContext.orElseGet(
() ->
TransportLayerSecurity.createSslContext(
Paths.get("var", "security")));
lb.setType(ListenerType.HTTPS).setSslContext(context);
} else {
lb.setType(ListenerType.HTTP);
}
Expand Down