This is an automated email from the ASF dual-hosted git repository. rcordier pushed a commit to branch postgresql in repository https://gitbox.apache.org/repos/asf/james-project.git
commit d8dec2f997594d002725cb071f13edae470e3bbd Author: Tung Tran <[email protected]> AuthorDate: Mon Nov 13 13:17:23 2023 +0700 fixup! JAMES-2586 Fix row-level security implementation --- backends-common/postgres/pom.xml | 7 ------- .../backends/postgres/DockerPostgresSingleton.java | 2 +- .../james/backends/postgres/PostgresExtension.java | 18 +++++++++--------- .../james/backends/postgres/PostgresFixture.java | 6 +----- .../test/resources/postgres-rowlevelsecurity-init.sql | 5 ----- 5 files changed, 11 insertions(+), 27 deletions(-) diff --git a/backends-common/postgres/pom.xml b/backends-common/postgres/pom.xml index 499f3b42a7..2e87eb59ea 100644 --- a/backends-common/postgres/pom.xml +++ b/backends-common/postgres/pom.xml @@ -29,7 +29,6 @@ <name>Apache James :: Backends Common :: Postgres</name> <properties> - <postgresql.driver.version>42.5.1</postgresql.driver.version> <jooq.version>3.16.22</jooq.version> <r2dbc.postgresql.version>1.0.2.RELEASE</r2dbc.postgresql.version> </properties> @@ -71,12 +70,6 @@ <artifactId>jooq</artifactId> <version>${jooq.version}</version> </dependency> - <dependency> - <groupId>org.postgresql</groupId> - <artifactId>postgresql</artifactId> - <version>${postgresql.driver.version}</version> - <scope>test</scope> - </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>r2dbc-postgresql</artifactId> diff --git a/backends-common/postgres/src/test/java/org/apache/james/backends/postgres/DockerPostgresSingleton.java b/backends-common/postgres/src/test/java/org/apache/james/backends/postgres/DockerPostgresSingleton.java index 21046eb72f..d51fa29675 100644 --- a/backends-common/postgres/src/test/java/org/apache/james/backends/postgres/DockerPostgresSingleton.java +++ b/backends-common/postgres/src/test/java/org/apache/james/backends/postgres/DockerPostgresSingleton.java @@ -30,7 +30,7 @@ public class DockerPostgresSingleton { } private static final Logger LOGGER = LoggerFactory.getLogger(DockerPostgresSingleton.class); - public static final PostgreSQLContainer SINGLETON = PostgresFixture.PG_CONTAINER.get() + public static final PostgreSQLContainer<?> SINGLETON = PostgresFixture.PG_CONTAINER.get() .withLogConsumer(DockerPostgresSingleton::displayDockerLog); static { diff --git a/backends-common/postgres/src/test/java/org/apache/james/backends/postgres/PostgresExtension.java b/backends-common/postgres/src/test/java/org/apache/james/backends/postgres/PostgresExtension.java index b340a5f8ac..d6f65b6f7a 100644 --- a/backends-common/postgres/src/test/java/org/apache/james/backends/postgres/PostgresExtension.java +++ b/backends-common/postgres/src/test/java/org/apache/james/backends/postgres/PostgresExtension.java @@ -20,10 +20,9 @@ package org.apache.james.backends.postgres; import static org.apache.james.backends.postgres.PostgresFixture.Database.DEFAULT_DATABASE; -import static org.apache.james.backends.postgres.PostgresFixture.SCRIPT_ROW_LEVEL_SECURITY_INIT_PATH; +import static org.apache.james.backends.postgres.PostgresFixture.Database.ROW_LEVEL_SECURITY_DATABASE; import java.net.URISyntaxException; -import java.util.Optional; import org.apache.http.client.utils.URIBuilder; import org.apache.james.GuiceModuleTestExtension; @@ -59,7 +58,6 @@ public class PostgresExtension implements GuiceModuleTestExtension { public static PostgreSQLContainer<?> PG_CONTAINER = DockerPostgresSingleton.SINGLETON; private final PostgresModule postgresModule; private final boolean rlsEnabled; - private final Optional<String> initScriptPath; private final PostgresFixture.Database selectedDatabase; private PostgresConfiguration postgresConfiguration; private PostgresExecutor postgresExecutor; @@ -70,10 +68,8 @@ public class PostgresExtension implements GuiceModuleTestExtension { this.rlsEnabled = rlsEnabled; if (rlsEnabled) { this.selectedDatabase = PostgresFixture.Database.ROW_LEVEL_SECURITY_DATABASE; - this.initScriptPath = Optional.of(SCRIPT_ROW_LEVEL_SECURITY_INIT_PATH); } else { this.selectedDatabase = DEFAULT_DATABASE; - this.initScriptPath = Optional.empty(); } } @@ -82,13 +78,17 @@ public class PostgresExtension implements GuiceModuleTestExtension { if (!PG_CONTAINER.isRunning()) { PG_CONTAINER.start(); } - runInitScriptIfNeed(); + querySettingRowLevelSecurityIfNeed(); initPostgresSession(); } - - private void runInitScriptIfNeed() { - initScriptPath.ifPresent(scriptPath -> Throwing.supplier(() -> PG_CONTAINER.execInContainer("psql", "-U", DEFAULT_DATABASE.dbUser(), "-f", scriptPath)).get()); + private void querySettingRowLevelSecurityIfNeed() { + Throwing.runnable(() -> { + PG_CONTAINER.execInContainer("psql", "-U", DEFAULT_DATABASE.dbUser(), "-c", "create user " + ROW_LEVEL_SECURITY_DATABASE.dbUser() + " WITH PASSWORD '" + ROW_LEVEL_SECURITY_DATABASE.dbPassword() + "';"); + PG_CONTAINER.execInContainer("psql", "-U", DEFAULT_DATABASE.dbUser(), "-c", "create database " + ROW_LEVEL_SECURITY_DATABASE.dbName() + ";"); + PG_CONTAINER.execInContainer("psql", "-U", DEFAULT_DATABASE.dbUser(), "-c", "grant all privileges on database " + ROW_LEVEL_SECURITY_DATABASE.dbName() + " to " + ROW_LEVEL_SECURITY_DATABASE.dbUser() + ";"); + PG_CONTAINER.execInContainer("psql", "-U", ROW_LEVEL_SECURITY_DATABASE.dbUser(), "-d", ROW_LEVEL_SECURITY_DATABASE.dbName(), "-c", "create schema if not exists " + ROW_LEVEL_SECURITY_DATABASE.schema() + ";"); + }).sneakyThrow().run(); } private void initPostgresSession() throws URISyntaxException { diff --git a/backends-common/postgres/src/test/java/org/apache/james/backends/postgres/PostgresFixture.java b/backends-common/postgres/src/test/java/org/apache/james/backends/postgres/PostgresFixture.java index b7fed73163..6c003f7ad9 100644 --- a/backends-common/postgres/src/test/java/org/apache/james/backends/postgres/PostgresFixture.java +++ b/backends-common/postgres/src/test/java/org/apache/james/backends/postgres/PostgresFixture.java @@ -26,7 +26,6 @@ import java.util.UUID; import java.util.function.Supplier; import org.testcontainers.containers.PostgreSQLContainer; -import org.testcontainers.utility.MountableFile; public interface PostgresFixture { @@ -91,12 +90,9 @@ public interface PostgresFixture { String IMAGE = "postgres:16"; Integer PORT = POSTGRESQL_PORT; - String POSTGRES_ROW_LEVEL_SECURITY_INIT_FILE = "postgres-rowlevelsecurity-init.sql"; - String SCRIPT_ROW_LEVEL_SECURITY_INIT_PATH = "/tmp/" + POSTGRES_ROW_LEVEL_SECURITY_INIT_FILE; Supplier<PostgreSQLContainer<?>> PG_CONTAINER = () -> new PostgreSQLContainer<>(IMAGE) .withDatabaseName(DEFAULT_DATABASE.dbName()) .withUsername(DEFAULT_DATABASE.dbUser()) .withPassword(DEFAULT_DATABASE.dbPassword()) - .withCreateContainerCmdModifier(cmd -> cmd.withName("james-postgres-test-" + UUID.randomUUID())) - .withCopyFileToContainer(MountableFile.forClasspathResource(POSTGRES_ROW_LEVEL_SECURITY_INIT_FILE), "/tmp/"); + .withCreateContainerCmdModifier(cmd -> cmd.withName("james-postgres-test-" + UUID.randomUUID())); } diff --git a/backends-common/postgres/src/test/resources/postgres-rowlevelsecurity-init.sql b/backends-common/postgres/src/test/resources/postgres-rowlevelsecurity-init.sql deleted file mode 100644 index 7a18723041..0000000000 --- a/backends-common/postgres/src/test/resources/postgres-rowlevelsecurity-init.sql +++ /dev/null @@ -1,5 +0,0 @@ -create user rlsuser WITH PASSWORD 'secret1'; -create database rlsdb; -grant all privileges on database rlsdb to rlsuser; -\c rlsdb; -create schema if not exists rlsschema authorization rlsuser; \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
