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

Refactor SecurityTransformerUtils to consider repeated annotations #44238

Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package io.quarkus.resteasy.reactive.server.test.security;

import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;

import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.security.PermissionsAllowed;
import io.quarkus.security.StringPermission;
import io.quarkus.security.test.utils.TestIdentityController;
import io.quarkus.security.test.utils.TestIdentityProvider;
import io.quarkus.test.QuarkusUnitTest;
import io.restassured.RestAssured;
import io.vertx.core.json.JsonObject;

public class RepeatedPermissionsAllowedTest {

@RegisterExtension
static QuarkusUnitTest runner = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addClasses(TestIdentityProvider.class, TestIdentityController.class, HelloResource.class)
.addAsResource(
new StringAsset(
"quarkus.log.category.\"io.quarkus.vertx.http.runtime.QuarkusErrorHandler\".level=OFF"
+ System.lineSeparator()),
"application.properties"));

@BeforeAll
public static void setupUsers() {
TestIdentityController.resetRoles()
.add("user", "user", new StringPermission("read"))
.add("admin", "admin", new StringPermission("read"), new StringPermission("write"));
}

@Test
public void testRepeatedPermissionsAllowedOnClass() {
// anonymous user
RestAssured.given()
.body("{%$$#!#@") // assures checks are eager
.post("/hello")
.then()
.statusCode(401);
// authenticated user, insufficient rights
RestAssured.given()
.auth().preemptive().basic("user", "user")
.body("{%$$#!#@") // assures checks are eager
.post("/hello")
.then()
.statusCode(403);
// authorized user, invalid payload
RestAssured.given()
.auth().preemptive().basic("admin", "admin")
.body("{%$$#!#@") // assures checks are eager
.post("/hello")
.then()
.statusCode(500);
}

@Test
public void testRepeatedPermissionsAllowedOnInterface() {
// anonymous user
RestAssured.given()
.body("{%$$#!#@") // assures checks are eager
.post("/hello-interface")
.then()
.statusCode(401);
// authenticated user, insufficient rights
RestAssured.given()
.auth().preemptive().basic("user", "user")
.body("{%$$#!#@") // assures checks are eager
.post("/hello-interface")
.then()
.statusCode(403);
// authorized user, invalid payload
RestAssured.given()
.auth().preemptive().basic("admin", "admin")
.body("{%$$#!#@") // assures checks are eager
.post("/hello-interface")
.then()
.statusCode(500);
}

@Path("/hello")
public static class HelloResource {

@PermissionsAllowed(value = "write")
@PermissionsAllowed(value = "read")
@POST
public String sayHello(JsonObject entity) {
return "ignored";
}
}

@Path("/hello-interface")
public interface HelloInterface {

@PermissionsAllowed(value = "write")
@PermissionsAllowed(value = "read")
@POST
String sayHello(JsonObject entity);
}

public static class HelloInterfaceImpl implements HelloInterface {

@Override
public String sayHello(JsonObject entity) {
return "ignored";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public final class SecurityTransformerUtils {
public static final DotName DENY_ALL = DotName.createSimple(DenyAll.class.getName());
private static final Set<DotName> SECURITY_ANNOTATIONS = Set.of(DotName.createSimple(RolesAllowed.class.getName()),
DotName.createSimple(PermissionsAllowed.class.getName()),
DotName.createSimple(PermissionsAllowed.List.class.getName()),
DotName.createSimple(Authenticated.class.getName()),
DotName.createSimple(DenyAll.class.getName()),
DotName.createSimple(PermitAll.class.getName()));
Expand Down
Loading