-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move HTTP Security Policies and Permissions to runtime
- Loading branch information
1 parent
fded7c0
commit e32e52e
Showing
21 changed files
with
575 additions
and
488 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
205 changes: 44 additions & 161 deletions
205
...http/deployment/src/main/java/io/quarkus/vertx/http/deployment/HttpSecurityProcessor.java
Large diffs are not rendered by default.
Oops, something went wrong.
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
29 changes: 29 additions & 0 deletions
29
...ttp/deployment/src/test/java/io/quarkus/vertx/http/security/CustomNamedHttpSecPolicy.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,29 @@ | ||
package io.quarkus.vertx.http.security; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
|
||
import io.quarkus.security.identity.SecurityIdentity; | ||
import io.quarkus.vertx.http.runtime.security.HttpSecurityPolicy; | ||
import io.smallrye.mutiny.Uni; | ||
import io.vertx.ext.web.RoutingContext; | ||
|
||
@ApplicationScoped | ||
public class CustomNamedHttpSecPolicy implements HttpSecurityPolicy { | ||
@Override | ||
public Uni<CheckResult> checkPermission(RoutingContext request, Uni<SecurityIdentity> identity, | ||
AuthorizationRequestContext requestContext) { | ||
if (isRequestAuthorized(request)) { | ||
return Uni.createFrom().item(CheckResult.PERMIT); | ||
} | ||
return Uni.createFrom().item(CheckResult.DENY); | ||
} | ||
|
||
private static boolean isRequestAuthorized(RoutingContext request) { | ||
return request.request().headers().contains("hush-hush"); | ||
} | ||
|
||
@Override | ||
public String name() { | ||
return "custom123"; | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
...deployment/src/test/java/io/quarkus/vertx/http/security/CustomNamedHttpSecPolicyTest.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,83 @@ | ||
package io.quarkus.vertx.http.security; | ||
|
||
import java.util.function.Supplier; | ||
|
||
import org.hamcrest.Matchers; | ||
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.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.security.test.utils.TestIdentityController; | ||
import io.quarkus.security.test.utils.TestIdentityProvider; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.restassured.RestAssured; | ||
|
||
public class CustomNamedHttpSecPolicyTest { | ||
|
||
@BeforeAll | ||
public static void setup() { | ||
TestIdentityController.resetRoles().add("test", "test", "test"); | ||
} | ||
|
||
private static final String APP_PROPS = "" + | ||
"# Add your application.properties here, if applicable.\n" + | ||
"quarkus.http.auth.permission.authenticated.paths=admin\n" + | ||
"quarkus.http.auth.permission.authenticated.policy=custom123\n"; | ||
|
||
@RegisterExtension | ||
static QuarkusUnitTest test = new QuarkusUnitTest().setArchiveProducer(new Supplier<>() { | ||
@Override | ||
public JavaArchive get() { | ||
return ShrinkWrap.create(JavaArchive.class) | ||
.addClasses(TestIdentityController.class, TestIdentityProvider.class, AdminPathHandler.class, | ||
CustomNamedHttpSecPolicy.class) | ||
.addAsResource(new StringAsset(APP_PROPS), "application.properties"); | ||
} | ||
}); | ||
|
||
@Test | ||
public void testAdminPath() { | ||
RestAssured | ||
.given() | ||
.when() | ||
.get("/admin") | ||
.then() | ||
.assertThat() | ||
.statusCode(401); | ||
RestAssured | ||
.given() | ||
.when() | ||
.header("hush-hush", "ignored") | ||
.get("/admin") | ||
.then() | ||
.assertThat() | ||
.statusCode(200) | ||
.body(Matchers.equalTo(":/admin")); | ||
RestAssured | ||
.given() | ||
.auth() | ||
.preemptive() | ||
.basic("test", "test") | ||
.when() | ||
.header("hush-hush", "ignored") | ||
.get("/admin") | ||
.then() | ||
.assertThat() | ||
.statusCode(200) | ||
.body(Matchers.equalTo("test:/admin")); | ||
RestAssured | ||
.given() | ||
.auth() | ||
.preemptive() | ||
.basic("test", "test") | ||
.when() | ||
.get("/admin") | ||
.then() | ||
.assertThat() | ||
.statusCode(403); | ||
} | ||
|
||
} |
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
25 changes: 25 additions & 0 deletions
25
...ons/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/AuthRuntimeConfig.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,25 @@ | ||
package io.quarkus.vertx.http.runtime; | ||
|
||
import java.util.Map; | ||
|
||
import io.quarkus.runtime.annotations.ConfigGroup; | ||
import io.quarkus.runtime.annotations.ConfigItem; | ||
|
||
/** | ||
* Authentication mechanism information used for configuring HTTP auth instance for the deployment. | ||
*/ | ||
@ConfigGroup | ||
public class AuthRuntimeConfig { | ||
|
||
/** | ||
* The HTTP permissions | ||
*/ | ||
@ConfigItem(name = "permission") | ||
public Map<String, PolicyMappingConfig> permissions; | ||
|
||
/** | ||
* The HTTP role based policies | ||
*/ | ||
@ConfigItem(name = "policy") | ||
public Map<String, PolicyConfig> rolePolicy; | ||
} |
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.