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.
Showing
11 changed files
with
356 additions
and
4 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
18 changes: 18 additions & 0 deletions
18
extensions/undertow/deployment/src/test/java/io/quarkus/undertow/test/SecuredServlet.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,18 @@ | ||
package io.quarkus.undertow.test; | ||
|
||
import java.io.IOException; | ||
|
||
import javax.servlet.ServletException; | ||
import javax.servlet.annotation.WebServlet; | ||
import javax.servlet.http.HttpServlet; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
@WebServlet(urlPatterns = "/secure/servlet") | ||
public class SecuredServlet extends HttpServlet { | ||
|
||
@Override | ||
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { | ||
resp.getWriter().write(req.getUserPrincipal().getName()); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...rtow/deployment/src/test/java/io/quarkus/undertow/test/ServletWebXmlSecurityTestCase.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,53 @@ | ||
package io.quarkus.undertow.test; | ||
|
||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
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.restassured.RestAssured; | ||
|
||
/** | ||
* tests that basic web.xml security is applied. We don't actually have | ||
* the security subsystem installed here, so this is the fallback behaviour that | ||
* will always deny | ||
*/ | ||
public class ServletWebXmlSecurityTestCase { | ||
|
||
static final String WEB_XML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + | ||
"\n" + | ||
"<web-app version=\"3.0\"\n" + | ||
" xmlns=\"http://java.sun.com/xml/ns/javaee\"\n" + | ||
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" + | ||
" xsi:schemaLocation=\"http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd\"\n" | ||
+ | ||
" metadata-complete=\"false\">\n" + | ||
"\n" + | ||
"<security-constraint>\n" + | ||
" <web-resource-collection>\n" + | ||
" <web-resource-name>test</web-resource-name>\n" + | ||
" <url-pattern>/secure/*</url-pattern>\n" + | ||
" <http-method>GET</http-method>\n" + | ||
" <http-method>POST</http-method>\n" + | ||
" </web-resource-collection>\n" + | ||
" <auth-constraint>\n" + | ||
" <role-name>admin</role-name>\n" + | ||
" </auth-constraint>\n" + | ||
"</security-constraint>" + | ||
"</web-app>"; | ||
|
||
@RegisterExtension | ||
static QuarkusUnitTest runner = new QuarkusUnitTest() | ||
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) | ||
.addClasses(SecuredServlet.class) | ||
.addAsManifestResource(new StringAsset(WEB_XML), "web.xml")); | ||
|
||
@Test | ||
public void testWebXmlSecurityConstraints() { | ||
RestAssured.when().get("/secure/servlet").then() | ||
.statusCode(401); | ||
} | ||
|
||
} |
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
79 changes: 79 additions & 0 deletions
79
...undertow/runtime/src/main/java/io/quarkus/undertow/runtime/ServletHttpSecurityPolicy.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,79 @@ | ||
package io.quarkus.undertow.runtime; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.CompletionStage; | ||
|
||
import javax.inject.Singleton; | ||
|
||
import io.quarkus.security.identity.SecurityIdentity; | ||
import io.quarkus.vertx.http.runtime.security.HttpSecurityPolicy; | ||
import io.undertow.servlet.api.Deployment; | ||
import io.undertow.servlet.api.SecurityInfo; | ||
import io.undertow.servlet.api.SingleConstraintMatch; | ||
import io.undertow.servlet.handlers.security.SecurityPathMatch; | ||
import io.vertx.ext.web.RoutingContext; | ||
|
||
@Singleton | ||
public class ServletHttpSecurityPolicy implements HttpSecurityPolicy { | ||
|
||
private volatile Deployment deployment; | ||
|
||
//the context path, guaranteed to have a trailing / | ||
private volatile String contextPath; | ||
|
||
@Override | ||
public CompletionStage<CheckResult> checkPermission(RoutingContext request, SecurityIdentity identity, | ||
AuthorizationRequestContext requestContext) { | ||
|
||
String requestPath = request.request().path(); | ||
if (!requestPath.startsWith(contextPath)) { | ||
//anything outside the context path we don't have anything to do with | ||
return CompletableFuture.completedFuture(CheckResult.PERMIT); | ||
} | ||
if (!contextPath.equals("/")) { | ||
requestPath = requestPath.substring(contextPath.length() - 1); | ||
} | ||
SecurityPathMatch match = deployment.getSecurityPathMatches().getSecurityInfo(requestPath, | ||
request.request().rawMethod()); | ||
|
||
SingleConstraintMatch mergedConstraint = match.getMergedConstraint(); | ||
if (mergedConstraint.getRequiredRoles().isEmpty()) { | ||
SecurityInfo.EmptyRoleSemantic emptyRoleSemantic = mergedConstraint.getEmptyRoleSemantic(); | ||
if (emptyRoleSemantic == SecurityInfo.EmptyRoleSemantic.PERMIT) { | ||
return CompletableFuture.completedFuture(CheckResult.PERMIT); | ||
} else if (emptyRoleSemantic == SecurityInfo.EmptyRoleSemantic.DENY) { | ||
return CompletableFuture.completedFuture(CheckResult.DENY); | ||
} else if (emptyRoleSemantic == SecurityInfo.EmptyRoleSemantic.AUTHENTICATE) { | ||
if (identity.isAnonymous()) { | ||
return CompletableFuture.completedFuture(CheckResult.DENY); | ||
} else { | ||
return CompletableFuture.completedFuture(CheckResult.PERMIT); | ||
} | ||
} else { | ||
CompletableFuture<CheckResult> c = new CompletableFuture<>(); | ||
c.completeExceptionally(new RuntimeException("Unknown empty role semantic " + emptyRoleSemantic)); | ||
return c; | ||
} | ||
} else { | ||
for (String i : mergedConstraint.getRequiredRoles()) { | ||
if (identity.hasRole(i)) { | ||
return CompletableFuture.completedFuture(CheckResult.PERMIT); | ||
} | ||
} | ||
return CompletableFuture.completedFuture(CheckResult.DENY); | ||
} | ||
} | ||
|
||
public Deployment getDeployment() { | ||
return deployment; | ||
} | ||
|
||
public ServletHttpSecurityPolicy setDeployment(Deployment deployment) { | ||
this.deployment = deployment; | ||
contextPath = deployment.getDeploymentInfo().getContextPath(); | ||
if (!contextPath.endsWith("/")) { | ||
contextPath = contextPath + "/"; | ||
} | ||
return this; | ||
} | ||
} |
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.