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.
Merge pull request quarkusio#36759 from michalvavrik/feature/security…
…-jpa-multitenancy-support Security JPA: support Hibernate multitenancy
- Loading branch information
Showing
9 changed files
with
259 additions
and
33 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
35 changes: 35 additions & 0 deletions
35
...y-jpa/deployment/src/test/java/io/quarkus/security/jpa/CustomHibernateTenantResolver.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,35 @@ | ||
package io.quarkus.security.jpa; | ||
|
||
import jakarta.enterprise.context.RequestScoped; | ||
import jakarta.inject.Inject; | ||
|
||
import io.quarkus.hibernate.orm.PersistenceUnitExtension; | ||
import io.quarkus.hibernate.orm.runtime.tenant.TenantResolver; | ||
import io.vertx.ext.web.RoutingContext; | ||
|
||
@PersistenceUnitExtension | ||
@RequestScoped | ||
public class CustomHibernateTenantResolver implements TenantResolver { | ||
|
||
static volatile boolean useRoutingContext = false; | ||
|
||
@Inject | ||
RoutingContext routingContext; | ||
|
||
@Override | ||
public String getDefaultTenantId() { | ||
return "one"; | ||
} | ||
|
||
@Override | ||
public String resolveTenantId() { | ||
if (useRoutingContext) { | ||
var tenant = routingContext.queryParam("tenant"); | ||
if (!tenant.isEmpty()) { | ||
return tenant.get(0); | ||
} | ||
} | ||
return "two"; | ||
} | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
...oyment/src/test/java/io/quarkus/security/jpa/EagerAuthMultiTenantPersistenceUnitTest.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,37 @@ | ||
package io.quarkus.security.jpa; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.restassured.RestAssured; | ||
|
||
public class EagerAuthMultiTenantPersistenceUnitTest extends JpaSecurityRealmTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(testClasses) | ||
.addClass(MinimalUserEntity.class) | ||
.addClass(CustomHibernateTenantResolver.class) | ||
.addAsResource("minimal-config/import.sql", "import.sql") | ||
.addAsResource("multitenant-persistence-unit/application.properties", "application.properties")); | ||
|
||
@Test | ||
public void testRoutingCtxAccessInsideTenantResolver() { | ||
// RoutingContext is not used inside TenantResolver to resolve tenant | ||
RestAssured.given().auth().preemptive().basic("user", "user").when().get("/jaxrs-secured/roles-class/routing-context") | ||
.then().statusCode(200); | ||
|
||
// RoutingContext is used and proactive auth is enabled => expect error | ||
CustomHibernateTenantResolver.useRoutingContext = true; | ||
try { | ||
RestAssured.given().auth().preemptive().basic("user", "user").queryParam("tenant", "two").when() | ||
.get("/jaxrs-secured/roles-class") | ||
.then().statusCode(500); | ||
} finally { | ||
CustomHibernateTenantResolver.useRoutingContext = false; | ||
} | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
...loyment/src/test/java/io/quarkus/security/jpa/LazyAuthMultiTenantPersistenceUnitTest.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,44 @@ | ||
package io.quarkus.security.jpa; | ||
|
||
import org.hamcrest.Matchers; | ||
import org.jboss.shrinkwrap.api.asset.StringAsset; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.restassured.RestAssured; | ||
|
||
public class LazyAuthMultiTenantPersistenceUnitTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest config = new QuarkusUnitTest() | ||
.withApplicationRoot((jar) -> jar | ||
.addClasses(MinimalUserEntity.class, CustomHibernateTenantResolver.class, RolesEndpointClassLevel.class) | ||
.addAsResource("minimal-config/import.sql", "import.sql") | ||
.addAsResource("multitenant-persistence-unit/application.properties", "application.properties") | ||
.addAsResource(new StringAsset("quarkus.http.auth.proactive=false\n"), | ||
"META-INF/microprofile-config.properties")); | ||
|
||
@Test | ||
public void testRoutingCtxAccessInsideTenantResolver() { | ||
// RoutingContext is used and proactive auth is disabled => no issues | ||
CustomHibernateTenantResolver.useRoutingContext = true; | ||
try { | ||
// tenant 'one' | ||
RestAssured.given().auth().preemptive().basic("user", "user") | ||
.queryParam("tenant", "one").when().get("/roles-class/routing-context").then() | ||
.statusCode(200).body(Matchers.is("true")); | ||
// tenant 'two' | ||
RestAssured.given().auth().preemptive().basic("user", "user") | ||
.queryParam("tenant", "two").when().get("/roles-class/routing-context").then() | ||
.statusCode(200).body(Matchers.is("true")); | ||
// tenant 'unknown' | ||
RestAssured.given().auth().preemptive().basic("user", "user") | ||
.queryParam("tenant", "unknown").when().get("/roles-class/routing-context").then() | ||
.statusCode(500); | ||
} finally { | ||
CustomHibernateTenantResolver.useRoutingContext = false; | ||
} | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
...ecurity-jpa/deployment/src/test/java/io/quarkus/security/jpa/RolesEndpointClassLevel.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 |
---|---|---|
@@ -1,20 +1,33 @@ | ||
package io.quarkus.security.jpa; | ||
|
||
import jakarta.annotation.security.RolesAllowed; | ||
import jakarta.inject.Inject; | ||
import jakarta.ws.rs.GET; | ||
import jakarta.ws.rs.Path; | ||
import jakarta.ws.rs.core.Context; | ||
import jakarta.ws.rs.core.SecurityContext; | ||
|
||
import io.vertx.ext.web.RoutingContext; | ||
|
||
/** | ||
* Test JAXRS endpoint with RolesAllowed specified at the class level | ||
*/ | ||
@Path("/roles-class") | ||
@RolesAllowed("user") | ||
public class RolesEndpointClassLevel { | ||
|
||
@Inject | ||
RoutingContext routingContext; | ||
|
||
@GET | ||
public String echo(@Context SecurityContext sec) { | ||
return "Hello " + sec.getUserPrincipal().getName(); | ||
} | ||
|
||
@Path("routing-context") | ||
@GET | ||
public boolean hasRoutingContext() { | ||
return routingContext != null; | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
...ity-jpa/deployment/src/test/resources/multitenant-persistence-unit/application.properties
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 @@ | ||
quarkus.datasource.db-kind=h2 | ||
quarkus.datasource.username=sa | ||
quarkus.datasource.password=sa | ||
quarkus.datasource.jdbc.url=jdbc:h2:mem:default | ||
|
||
quarkus.datasource.one.db-kind=h2 | ||
quarkus.datasource.one.username=sa | ||
quarkus.datasource.one.password=sa | ||
quarkus.datasource.one.jdbc.url=jdbc:h2:mem:shared | ||
|
||
quarkus.datasource.two.db-kind=h2 | ||
quarkus.datasource.two.username=sa | ||
quarkus.datasource.two.password=sa | ||
quarkus.datasource.two.jdbc.url=jdbc:h2:mem:shared | ||
|
||
quarkus.hibernate-orm.multitenant=DATABASE | ||
quarkus.hibernate-orm.sql-load-script=import.sql | ||
quarkus.hibernate-orm.database.generation=drop-and-create | ||
quarkus.hibernate-orm.packages=io.quarkus.security.jpa |
Oops, something went wrong.