forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[fixes quarkusio#8508] - mTLS client authentication support
- Loading branch information
Showing
19 changed files
with
317 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
...ions/security/runtime/src/main/java/io/quarkus/security/runtime/X509IdentityProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package io.quarkus.security.runtime; | ||
|
||
import java.security.cert.X509Certificate; | ||
|
||
import javax.inject.Singleton; | ||
|
||
import io.quarkus.security.identity.AuthenticationRequestContext; | ||
import io.quarkus.security.identity.IdentityProvider; | ||
import io.quarkus.security.identity.SecurityIdentity; | ||
import io.quarkus.security.identity.request.CertificateAuthenticationRequest; | ||
import io.smallrye.mutiny.Uni; | ||
|
||
@Singleton | ||
public class X509IdentityProvider implements IdentityProvider<CertificateAuthenticationRequest> { | ||
|
||
@Override | ||
public Class<CertificateAuthenticationRequest> getRequestType() { | ||
return CertificateAuthenticationRequest.class; | ||
} | ||
|
||
@Override | ||
public Uni<SecurityIdentity> authenticate(CertificateAuthenticationRequest request, AuthenticationRequestContext context) { | ||
X509Certificate certificate = request.getCertificate().getCertificate(); | ||
|
||
return Uni.createFrom().item(QuarkusSecurityIdentity.builder() | ||
.setPrincipal(certificate.getSubjectX500Principal()) | ||
.addCredential(request.getCertificate()) | ||
.build()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...s/vertx-http/deployment/src/test/java/io/quarkus/vertx/http/security/MtlsRequestTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package io.quarkus.vertx.http.security; | ||
|
||
import static org.hamcrest.Matchers.is; | ||
|
||
import java.io.File; | ||
import java.net.URL; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.enterprise.event.Observes; | ||
|
||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.quarkus.test.common.http.TestHTTPResource; | ||
import io.quarkus.vertx.http.runtime.security.QuarkusHttpUser; | ||
import io.restassured.RestAssured; | ||
import io.vertx.ext.web.Router; | ||
|
||
public class MtlsRequestTest { | ||
|
||
@TestHTTPResource(value = "/mtls", ssl = true) | ||
URL url; | ||
|
||
@TestHTTPResource(value = "/mtls", ssl = false) | ||
URL urlNoTls; | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) | ||
.addClasses(MyBean.class) | ||
.addAsResource(new File("src/test/resources/conf/mtls/mtls-no-auth-jks.conf"), "application.properties") | ||
.addAsResource(new File("src/test/resources/conf/mtls/server-keystore.jks"), "server-keystore.jks") | ||
.addAsResource(new File("src/test/resources/conf/mtls/server-truststore.jks"), "server-truststore.jks")); | ||
|
||
@Test | ||
public void testClientAuthentication() { | ||
RestAssured.given() | ||
.keyStore(new File("src/test/resources/conf/mtls/client-keystore.jks"), "password") | ||
.trustStore(new File("src/test/resources/conf/mtls/client-truststore.jks"), "password") | ||
.get(url).then().statusCode(200).body(is("CN=client,OU=cert,O=quarkus,L=city,ST=state,C=AU")); | ||
} | ||
|
||
@Test | ||
public void testNoClientCert() { | ||
RestAssured.given() | ||
.trustStore(new File("src/test/resources/conf/mtls/client-truststore.jks"), "password") | ||
.get(url).then().statusCode(401); | ||
} | ||
|
||
@ApplicationScoped | ||
static class MyBean { | ||
|
||
public void register(@Observes Router router) { | ||
router.get("/mtls").handler(rc -> { | ||
rc.response().end(QuarkusHttpUser.class.cast(rc.user()).getSecurityIdentity().getPrincipal().getName()); | ||
}); | ||
} | ||
|
||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
.../vertx-http/deployment/src/test/java/io/quarkus/vertx/http/security/MtlsRequiredTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package io.quarkus.vertx.http.security; | ||
|
||
import static org.hamcrest.Matchers.is; | ||
|
||
import java.io.File; | ||
import java.net.URL; | ||
|
||
import javax.enterprise.context.ApplicationScoped; | ||
import javax.enterprise.event.Observes; | ||
|
||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.quarkus.test.common.http.TestHTTPResource; | ||
import io.quarkus.vertx.http.runtime.security.QuarkusHttpUser; | ||
import io.restassured.RestAssured; | ||
import io.vertx.ext.web.Router; | ||
|
||
public class MtlsRequiredTest { | ||
|
||
@TestHTTPResource(value = "/mtls", ssl = true) | ||
URL url; | ||
|
||
@TestHTTPResource(value = "/mtls", ssl = false) | ||
URL urlNoTls; | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) | ||
.addClasses(MyBean.class) | ||
.addAsResource(new File("src/test/resources/conf/mtls/mtls-jks.conf"), "application.properties") | ||
.addAsResource(new File("src/test/resources/conf/mtls/server-keystore.jks"), "server-keystore.jks") | ||
.addAsResource(new File("src/test/resources/conf/mtls/server-truststore.jks"), "server-truststore.jks")); | ||
|
||
@Test | ||
public void testClientAuthentication() { | ||
RestAssured.given() | ||
.keyStore(new File("src/test/resources/conf/mtls/client-keystore.jks"), "password") | ||
.trustStore(new File("src/test/resources/conf/mtls/client-truststore.jks"), "password") | ||
.get(url).then().statusCode(200).body(is("CN=client,OU=cert,O=quarkus,L=city,ST=state,C=AU")); | ||
} | ||
|
||
@Test | ||
public void testNoSslServerWithJKS() { | ||
RestAssured.given() | ||
.get(urlNoTls).then().statusCode(401); | ||
} | ||
|
||
@Test | ||
public void testNoClientCert() { | ||
RestAssured.given() | ||
.trustStore(new File("src/test/resources/conf/mtls/client-truststore.jks"), "password") | ||
.get(url).then().statusCode(401); | ||
} | ||
|
||
@ApplicationScoped | ||
static class MyBean { | ||
|
||
public void register(@Observes Router router) { | ||
router.get("/mtls").handler(rc -> { | ||
rc.response().end(QuarkusHttpUser.class.cast(rc.user()).getSecurityIdentity().getPrincipal().getName()); | ||
}); | ||
} | ||
|
||
} | ||
} |
Binary file added
BIN
+2.16 KB
extensions/vertx-http/deployment/src/test/resources/conf/mtls/client-keystore.jks
Binary file not shown.
Binary file added
BIN
+1.63 KB
extensions/vertx-http/deployment/src/test/resources/conf/mtls/client-truststore.jks
Binary file not shown.
5 changes: 5 additions & 0 deletions
5
extensions/vertx-http/deployment/src/test/resources/conf/mtls/mtls-jks.conf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
quarkus.http.ssl.certificate.key-store-file=server-keystore.jks | ||
quarkus.http.ssl.certificate.key-store-password=secret | ||
quarkus.http.ssl.certificate.trust-store-file=server-truststore.jks | ||
quarkus.http.ssl.certificate.trust-store-password=password | ||
quarkus.http.ssl.client-auth=REQUIRED |
7 changes: 7 additions & 0 deletions
7
extensions/vertx-http/deployment/src/test/resources/conf/mtls/mtls-no-auth-jks.conf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
quarkus.http.ssl.certificate.key-store-file=server-keystore.jks | ||
quarkus.http.ssl.certificate.key-store-password=secret | ||
quarkus.http.ssl.certificate.trust-store-file=server-truststore.jks | ||
quarkus.http.ssl.certificate.trust-store-password=password | ||
quarkus.http.ssl.client-auth=REQUEST | ||
quarkus.http.auth.permission.all.paths=/* | ||
quarkus.http.auth.permission.all.policy=authenticated |
Binary file added
BIN
+2.37 KB
extensions/vertx-http/deployment/src/test/resources/conf/mtls/server-keystore.jks
Binary file not shown.
Binary file added
BIN
+925 Bytes
extensions/vertx-http/deployment/src/test/resources/conf/mtls/server-truststore.jks
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.