-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provide the ability to remove final flag from methods of CDI beans
We need security annotations to result in the creation of an interceptor which is not possible when a method is final. The solution we follow is to remove the final modifier from methods that need intercepting. This is now configurable with the default value true, meaning that Arc will remove the final flag. If the value is set to false Arc throws an exception at build time. Fixes: #5051
- Loading branch information
Showing
11 changed files
with
269 additions
and
31 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
19 changes: 19 additions & 0 deletions
19
...ity/deployment/src/test/java/io/quarkus/security/test/cdi/BeanWithSecuredFinalMethod.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,19 @@ | ||
package io.quarkus.security.test.cdi; | ||
|
||
import javax.annotation.security.DenyAll; | ||
import javax.annotation.security.RolesAllowed; | ||
import javax.inject.Singleton; | ||
|
||
@Singleton | ||
public class BeanWithSecuredFinalMethod { | ||
|
||
@RolesAllowed("admin") | ||
public final String securedMethod() { | ||
return "accessibleForAdminOnly"; | ||
} | ||
|
||
@DenyAll | ||
public final String otherSecuredMethod(String input) { | ||
return "denied"; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...yment/src/test/java/io/quarkus/security/test/cdi/SecurityAnnotationOnFinalMethodTest.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,45 @@ | ||
package io.quarkus.security.test.cdi; | ||
|
||
import static io.quarkus.security.test.cdi.SecurityTestUtils.assertFailureFor; | ||
import static io.quarkus.security.test.cdi.SecurityTestUtils.assertSuccess; | ||
import static io.quarkus.security.test.utils.IdentityMock.ADMIN; | ||
import static io.quarkus.security.test.utils.IdentityMock.ANONYMOUS; | ||
import static io.quarkus.security.test.utils.IdentityMock.USER; | ||
|
||
import javax.inject.Inject; | ||
|
||
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.security.ForbiddenException; | ||
import io.quarkus.security.UnauthorizedException; | ||
import io.quarkus.security.test.utils.AuthData; | ||
import io.quarkus.security.test.utils.IdentityMock; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class SecurityAnnotationOnFinalMethodTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) | ||
.addClasses(BeanWithSecuredFinalMethod.class, IdentityMock.class, | ||
AuthData.class, SecurityTestUtils.class)); | ||
|
||
@Inject | ||
BeanWithSecuredFinalMethod bean; | ||
|
||
@Test | ||
public void shouldRestrictAccessToSpecificRole() { | ||
assertFailureFor(() -> bean.securedMethod(), UnauthorizedException.class, ANONYMOUS); | ||
assertSuccess(() -> bean.securedMethod(), "accessibleForAdminOnly", ADMIN); | ||
} | ||
|
||
@Test | ||
public void shouldFailToAccessCompletely() { | ||
assertFailureFor(() -> bean.otherSecuredMethod("whatever"), UnauthorizedException.class, ANONYMOUS); | ||
assertFailureFor(() -> bean.otherSecuredMethod("whatever"), ForbiddenException.class, USER, ADMIN); | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
...quarkus/security/test/cdi/SecurityAnnotationOnFinalMethodWithDisableFinalRemovalTest.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,39 @@ | ||
package io.quarkus.security.test.cdi; | ||
|
||
import static org.junit.jupiter.api.Assertions.fail; | ||
|
||
import javax.enterprise.inject.spi.DeploymentException; | ||
import javax.inject.Inject; | ||
|
||
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.security.test.utils.AuthData; | ||
import io.quarkus.security.test.utils.IdentityMock; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class SecurityAnnotationOnFinalMethodWithDisableFinalRemovalTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) | ||
.addClasses(BeanWithSecuredFinalMethod.class, IdentityMock.class, | ||
AuthData.class, SecurityTestUtils.class) | ||
.addAsResource(new StringAsset( | ||
"quarkus.arc.remove-final-for-proxyable-methods=false"), | ||
"application.properties")) | ||
.setExpectedException(DeploymentException.class); | ||
|
||
@Inject | ||
BeanWithSecuredFinalMethod bean; | ||
|
||
@Test | ||
public void test() { | ||
// should never be executed since the application should not be built | ||
fail(); | ||
} | ||
|
||
} |
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.