Skip to content

Commit

Permalink
Simplified code, incorporate feedback
Browse files Browse the repository at this point in the history
Add Certificate property to HttpAttributes
Remove Attribute when certificate is not set
Log instead of throwing an exception if message is not an HttpMessage
  • Loading branch information
BjoernAkAManf committed Oct 8, 2019
1 parent f2ac56a commit b4295b5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@
package io.micronaut.http.server.netty.ssl;

import io.micronaut.core.annotation.Internal;
import io.micronaut.http.HttpAttributes;
import io.micronaut.http.HttpMessage;
import io.micronaut.http.server.netty.decoders.HttpRequestDecoder;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.ssl.SslHandler;
import io.netty.handler.ssl.SslHandshakeCompletionEvent;
import io.netty.util.AttributeKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.net.ssl.SSLPeerUnverifiedException;
import java.security.cert.Certificate;
Expand All @@ -34,8 +37,8 @@
@Internal
public class SSLCertificateProviderHandler extends ChannelInboundHandlerAdapter {
private static final String ID = SSLCertificateProviderHandler.class.getSimpleName();
private static final String CERT_KEY = "javax.servlet.request.X509Certificate";
private static final AttributeKey<Certificate> CERT_ATTRIBUTE = AttributeKey.newInstance(CERT_KEY);
private static final AttributeKey<Certificate> CERT_ATTRIBUTE = AttributeKey.newInstance(ID);
private static final Logger LOG = LoggerFactory.getLogger(SSLCertificateProviderHandler.class);

@Override
public void channelRegistered(final ChannelHandlerContext ctx) throws Exception {
Expand Down Expand Up @@ -84,11 +87,16 @@ private static class AddCertificateToRequest extends ChannelInboundHandlerAdapte
public void channelRead(final ChannelHandlerContext ctx, final Object msg) throws Exception {
if (msg instanceof HttpMessage) {
HttpMessage<?> request = (HttpMessage<?>) msg;
request.setAttribute(CERT_KEY, ctx.channel().attr(CERT_ATTRIBUTE).get());
Certificate certificate = ctx.channel().attr(CERT_ATTRIBUTE).get();
if (certificate == null) {
request.removeAttribute(HttpAttributes.X509_CERTIFICATE, Certificate.class);
} else {
request.setAttribute(HttpAttributes.X509_CERTIFICATE, certificate);
}
super.channelRead(ctx, msg);
return;
}
throw new UnsupportedOperationException("Message must implement HttpMessage in order to set Certificate!");
LOG.warn("Message does not implement HttpMessage. Client Certificate can therefore not be set.");
}
}
}
7 changes: 6 additions & 1 deletion http/src/main/java/io/micronaut/http/HttpAttributes.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,12 @@ public enum HttpAttributes implements CharSequence {
/**
* Attribute used to store the MethodInvocationContext by declarative client.
*/
INVOCATION_CONTEXT(Constants.PREFIX + ".invocationContext");
INVOCATION_CONTEXT(Constants.PREFIX + ".invocationContext"),

/**
* Attribute used to store a client Certificate (mutual authentication).
*/
X509_CERTIFICATE("javax.servlet.request.X509Certificate");

private final String name;

Expand Down
2 changes: 1 addition & 1 deletion http/src/main/java/io/micronaut/http/HttpRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ default Optional<Locale> getLocale() {
* @return A certificate used for authentication, if applicable.
*/
default Optional<Certificate> getCertificate() {
return this.getAttribute("javax.servlet.request.X509Certificate", Certificate.class);
return this.getAttribute(HttpAttributes.X509_CERTIFICATE, Certificate.class);
}

/**
Expand Down

0 comments on commit b4295b5

Please sign in to comment.