You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by rc...@apache.org on 2024/05/03 03:35:25 UTC

(james-project) 06/06: JAMES-2586 Create rls-bypass instance for PoolBackedPostgresConnectionFactory

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 cfeba0162d4fa7c8549069649976f2cac45b628b
Author: hung phan <hp...@linagora.com>
AuthorDate: Thu May 2 13:54:46 2024 +0700

    JAMES-2586 Create rls-bypass instance for PoolBackedPostgresConnectionFactory
---
 .../backends/postgres/PostgresConfiguration.java   | 59 +++++++++++++++++++---
 .../utils/PoolBackedPostgresConnectionFactory.java |  6 +--
 .../james/backends/postgres/PostgresExtension.java |  4 +-
 .../sample-configuration/postgres.properties       | 12 +++--
 .../james/modules/data/PostgresCommonModule.java   | 15 +++---
 5 files changed, 74 insertions(+), 22 deletions(-)

diff --git a/backends-common/postgres/src/main/java/org/apache/james/backends/postgres/PostgresConfiguration.java b/backends-common/postgres/src/main/java/org/apache/james/backends/postgres/PostgresConfiguration.java
index bfffc4ddee..9a5e2fa63c 100644
--- a/backends-common/postgres/src/main/java/org/apache/james/backends/postgres/PostgresConfiguration.java
+++ b/backends-common/postgres/src/main/java/org/apache/james/backends/postgres/PostgresConfiguration.java
@@ -46,7 +46,13 @@ public class PostgresConfiguration {
     public static final String NON_RLS_PASSWORD = "database.non-rls.password";
     public static final String RLS_ENABLED = "row.level.security.enabled";
     public static final String POOL_INITIAL_SIZE = "pool.initial.size";
+    public static final int POOL_INITIAL_SIZE_DEFAULT_VALUE = 10;
     public static final String POOL_MAX_SIZE = "pool.max.size";
+    public static final int POOL_MAX_SIZE_DEFAULT_VALUE = 15;
+    public static final String NON_RLS_POOL_INITIAL_SIZE = "non-rls.pool.initial.size";
+    public static final int NON_RLS_POOL_INITIAL_SIZE_DEFAULT_VALUE = 5;
+    public static final String NON_RLS_POOL_MAX_SIZE = "non-rls.pool.max.size";
+    public static final int NON_RLS_POOL_MAX_SIZE_DEFAULT_VALUE = 10;
     public static final String SSL_MODE = "ssl.mode";
     public static final String SSL_MODE_DEFAULT_VALUE = "allow";
     public static final String JOOQ_REACTIVE_TIMEOUT = "jooq.reactive.timeout";
@@ -83,6 +89,8 @@ public class PostgresConfiguration {
         private Optional<Boolean> rowLevelSecurityEnabled = Optional.empty();
         private Optional<Integer> poolInitialSize = Optional.empty();
         private Optional<Integer> poolMaxSize = Optional.empty();
+        private Optional<Integer> nonRLSPoolInitialSize = Optional.empty();
+        private Optional<Integer> nonRLSPoolMaxSize = Optional.empty();
         private Optional<String> sslMode = Optional.empty();
         private Optional<Duration> jooqReactiveTimeout = Optional.empty();
 
@@ -196,6 +204,26 @@ public class PostgresConfiguration {
             return this;
         }
 
+        public Builder nonRLSPoolInitialSize(Optional<Integer> nonRLSPoolInitialSize) {
+            this.nonRLSPoolInitialSize = nonRLSPoolInitialSize;
+            return this;
+        }
+
+        public Builder nonRLSPoolInitialSize(Integer nonRLSPoolInitialSize) {
+            this.nonRLSPoolInitialSize = Optional.of(nonRLSPoolInitialSize);
+            return this;
+        }
+
+        public Builder nonRLSPoolMaxSize(Optional<Integer> nonRLSPoolMaxSize) {
+            this.nonRLSPoolMaxSize = nonRLSPoolMaxSize;
+            return this;
+        }
+
+        public Builder nonRLSPoolMaxSize(Integer nonRLSPoolMaxSize) {
+            this.nonRLSPoolMaxSize = Optional.of(nonRLSPoolMaxSize);
+            return this;
+        }
+
         public Builder sslMode(Optional<String> sslMode) {
             this.sslMode = sslMode;
             return this;
@@ -227,8 +255,10 @@ public class PostgresConfiguration {
                 new Credential(username.get(), password.get()),
                 new Credential(nonRLSUser.orElse(username.get()), nonRLSPassword.orElse(password.get())),
                 rowLevelSecurityEnabled.orElse(false),
-                poolInitialSize,
-                poolMaxSize,
+                poolInitialSize.orElse(POOL_INITIAL_SIZE_DEFAULT_VALUE),
+                poolMaxSize.orElse(POOL_MAX_SIZE_DEFAULT_VALUE),
+                nonRLSPoolInitialSize.orElse(NON_RLS_POOL_INITIAL_SIZE_DEFAULT_VALUE),
+                nonRLSPoolMaxSize.orElse(NON_RLS_POOL_MAX_SIZE_DEFAULT_VALUE),
                 SSLMode.fromValue(sslMode.orElse(SSL_MODE_DEFAULT_VALUE)),
                 jooqReactiveTimeout.orElse(JOOQ_REACTIVE_TIMEOUT_DEFAULT_VALUE));
         }
@@ -251,6 +281,8 @@ public class PostgresConfiguration {
             .rowLevelSecurityEnabled(propertiesConfiguration.getBoolean(RLS_ENABLED, false))
             .poolInitialSize(Optional.ofNullable(propertiesConfiguration.getInteger(POOL_INITIAL_SIZE, null)))
             .poolMaxSize(Optional.ofNullable(propertiesConfiguration.getInteger(POOL_MAX_SIZE, null)))
+            .nonRLSPoolInitialSize(Optional.ofNullable(propertiesConfiguration.getInteger(NON_RLS_POOL_INITIAL_SIZE, null)))
+            .nonRLSPoolMaxSize(Optional.ofNullable(propertiesConfiguration.getInteger(NON_RLS_POOL_MAX_SIZE, null)))
             .sslMode(Optional.ofNullable(propertiesConfiguration.getString(SSL_MODE)))
             .jooqReactiveTimeout(Optional.ofNullable(propertiesConfiguration.getString(JOOQ_REACTIVE_TIMEOUT))
                 .map(value -> DurationParser.parse(value, ChronoUnit.SECONDS)))
@@ -264,14 +296,17 @@ public class PostgresConfiguration {
     private final Credential credential;
     private final Credential nonRLSCredential;
     private final boolean rowLevelSecurityEnabled;
-    private final Optional<Integer> poolInitialSize;
-    private final Optional<Integer> poolMaxSize;
+    private final Integer poolInitialSize;
+    private final Integer poolMaxSize;
+    private final Integer nonRLSPoolInitialSize;
+    private final Integer nonRLSPoolMaxSize;
     private final SSLMode sslMode;
     private final Duration jooqReactiveTimeout;
 
     private PostgresConfiguration(String host, int port, String databaseName, String databaseSchema,
                                   Credential credential, Credential nonRLSCredential, boolean rowLevelSecurityEnabled,
-                                  Optional<Integer> poolInitialSize, Optional<Integer> poolMaxSize,
+                                  Integer poolInitialSize, Integer poolMaxSize,
+                                  Integer nonRLSPoolInitialSize, Integer nonRLSPoolMaxSize,
                                   SSLMode sslMode, Duration jooqReactiveTimeout) {
         this.host = host;
         this.port = port;
@@ -282,6 +317,8 @@ public class PostgresConfiguration {
         this.rowLevelSecurityEnabled = rowLevelSecurityEnabled;
         this.poolInitialSize = poolInitialSize;
         this.poolMaxSize = poolMaxSize;
+        this.nonRLSPoolInitialSize = nonRLSPoolInitialSize;
+        this.nonRLSPoolMaxSize = nonRLSPoolMaxSize;
         this.sslMode = sslMode;
         this.jooqReactiveTimeout = jooqReactiveTimeout;
     }
@@ -314,14 +351,22 @@ public class PostgresConfiguration {
         return rowLevelSecurityEnabled;
     }
 
-    public Optional<Integer> poolInitialSize() {
+    public Integer poolInitialSize() {
         return poolInitialSize;
     }
 
-    public Optional<Integer> poolMaxSize() {
+    public Integer poolMaxSize() {
         return poolMaxSize;
     }
 
+    public Integer nonRLSPoolInitialSize() {
+        return nonRLSPoolInitialSize;
+    }
+
+    public Integer nonRLSPoolMaxSize() {
+        return nonRLSPoolMaxSize;
+    }
+
     public SSLMode getSslMode() {
         return sslMode;
     }
diff --git a/backends-common/postgres/src/main/java/org/apache/james/backends/postgres/utils/PoolBackedPostgresConnectionFactory.java b/backends-common/postgres/src/main/java/org/apache/james/backends/postgres/utils/PoolBackedPostgresConnectionFactory.java
index ea97e70627..ba558495b3 100644
--- a/backends-common/postgres/src/main/java/org/apache/james/backends/postgres/utils/PoolBackedPostgresConnectionFactory.java
+++ b/backends-common/postgres/src/main/java/org/apache/james/backends/postgres/utils/PoolBackedPostgresConnectionFactory.java
@@ -41,10 +41,8 @@ public class PoolBackedPostgresConnectionFactory implements JamesPostgresConnect
     private final boolean rowLevelSecurityEnabled;
     private final ConnectionPool pool;
 
-    public PoolBackedPostgresConnectionFactory(boolean rowLevelSecurityEnabled, Optional<Integer> maybeInitialSize, Optional<Integer> maybeMaxSize, ConnectionFactory connectionFactory) {
+    public PoolBackedPostgresConnectionFactory(boolean rowLevelSecurityEnabled, int initialSize, int maxSize, ConnectionFactory connectionFactory) {
         this.rowLevelSecurityEnabled = rowLevelSecurityEnabled;
-        int initialSize = maybeInitialSize.orElse(DEFAULT_INITIAL_SIZE);
-        int maxSize = maybeMaxSize.orElse(DEFAULT_MAX_SIZE);
         ConnectionPoolConfiguration configuration = ConnectionPoolConfiguration.builder(connectionFactory)
             .initialSize(initialSize)
             .maxSize(maxSize)
@@ -54,7 +52,7 @@ public class PoolBackedPostgresConnectionFactory implements JamesPostgresConnect
     }
 
     public PoolBackedPostgresConnectionFactory(boolean rowLevelSecurityEnabled, ConnectionFactory connectionFactory) {
-        this(rowLevelSecurityEnabled, Optional.empty(), Optional.empty(), connectionFactory);
+        this(rowLevelSecurityEnabled, DEFAULT_INITIAL_SIZE, DEFAULT_MAX_SIZE, connectionFactory);
     }
 
     @Override
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 8166d71d12..bfb5c3daf2 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
@@ -182,8 +182,8 @@ public class PostgresExtension implements GuiceModuleTestExtension {
         } else {
             executorFactory = new PostgresExecutor.Factory(
                 new PoolBackedPostgresConnectionFactory(false,
-                    Optional.of(poolSize.getMin()),
-                    Optional.of(poolSize.getMax()),
+                    poolSize.getMin(),
+                    poolSize.getMax(),
                     connectionFactory),
                 postgresConfiguration,
                 new RecordingMetricFactory());
diff --git a/server/apps/postgres-app/sample-configuration/postgres.properties b/server/apps/postgres-app/sample-configuration/postgres.properties
index a8780c5133..85cefb1ba3 100644
--- a/server/apps/postgres-app/sample-configuration/postgres.properties
+++ b/server/apps/postgres-app/sample-configuration/postgres.properties
@@ -25,11 +25,17 @@ row.level.security.enabled=false
 # String. It is required when row.level.security.enabled is true. Database password of non-rls user.
 #database.non-rls.password=secret1
 
-# Integer. Optional, default to 5432. Database connection pool initial size.
+# Integer. Optional, default to 10. Database connection pool initial size.
 pool.initial.size=10
 
-# Integer. Optional, default to 5432. Database connection pool max size.
-pool.max.size=20
+# Integer. Optional, default to 15. Database connection pool max size.
+pool.max.size=15
+
+# Integer. Optional, default to 5. rls-bypass database connection pool initial size.
+non-rls.pool.initial.size=5
+
+# Integer. Optional, default to 10. rls-bypass database connection pool max size.
+non-rls.pool.max.size=10
 
 # String. Optional, defaults to allow. SSLMode required to connect to the Postgresql db server.
 # Check https://www.postgresql.org/docs/current/libpq-ssl.html#LIBPQ-SSL-PROTECTION for a list of supported SSLModes.
diff --git a/server/container/guice/postgres-common/src/main/java/org/apache/james/modules/data/PostgresCommonModule.java b/server/container/guice/postgres-common/src/main/java/org/apache/james/modules/data/PostgresCommonModule.java
index 245748a6d1..592879b75e 100644
--- a/server/container/guice/postgres-common/src/main/java/org/apache/james/modules/data/PostgresCommonModule.java
+++ b/server/container/guice/postgres-common/src/main/java/org/apache/james/modules/data/PostgresCommonModule.java
@@ -33,7 +33,6 @@ import org.apache.james.backends.postgres.utils.PoolBackedPostgresConnectionFact
 import org.apache.james.backends.postgres.utils.PostgresConnectionClosure;
 import org.apache.james.backends.postgres.utils.PostgresExecutor;
 import org.apache.james.backends.postgres.utils.PostgresHealthCheck;
-import org.apache.james.backends.postgres.utils.SinglePostgresConnectionFactory;
 import org.apache.james.core.healthcheck.HealthCheck;
 import org.apache.james.metrics.api.MetricFactory;
 import org.apache.james.utils.InitializationOperation;
@@ -53,10 +52,10 @@ import com.google.inject.name.Named;
 import io.r2dbc.postgresql.PostgresqlConnectionConfiguration;
 import io.r2dbc.postgresql.PostgresqlConnectionFactory;
 import io.r2dbc.spi.ConnectionFactory;
-import reactor.core.publisher.Mono;
 
 public class PostgresCommonModule extends AbstractModule {
     private static final Logger LOGGER = LoggerFactory.getLogger("POSTGRES");
+    private static final boolean DISABLED_ROW_LEVEL_SECURITY = false;
 
     @Override
     public void configure() {
@@ -79,8 +78,6 @@ public class PostgresCommonModule extends AbstractModule {
     @Singleton
     JamesPostgresConnectionFactory provideJamesPostgresConnectionFactory(PostgresConfiguration postgresConfiguration,
                                                                          ConnectionFactory connectionFactory) {
-        LOGGER.info("Is PostgreSQL row level security enabled? {}", postgresConfiguration.rowLevelSecurityEnabled());
-        LOGGER.info("Implementation for PostgreSQL connection factory: {}", PoolBackedPostgresConnectionFactory.class.getName());
         return new PoolBackedPostgresConnectionFactory(postgresConfiguration.rowLevelSecurityEnabled(),
             postgresConfiguration.poolInitialSize(),
             postgresConfiguration.poolMaxSize(),
@@ -91,9 +88,15 @@ public class PostgresCommonModule extends AbstractModule {
     @Named(JamesPostgresConnectionFactory.NON_RLS_INJECT)
     @Singleton
     JamesPostgresConnectionFactory provideJamesPostgresConnectionFactoryWithRLSBypass(PostgresConfiguration postgresConfiguration,
+                                                                                      JamesPostgresConnectionFactory jamesPostgresConnectionFactory,
                                                                                       @Named(JamesPostgresConnectionFactory.NON_RLS_INJECT) ConnectionFactory connectionFactory) {
-        LOGGER.info("Implementation for PostgresSQL connection factory: {}", SinglePostgresConnectionFactory.class.getName());
-        return new SinglePostgresConnectionFactory(Mono.from(connectionFactory.create()).block());
+        if (!postgresConfiguration.rowLevelSecurityEnabled()) {
+            return jamesPostgresConnectionFactory;
+        }
+        return new PoolBackedPostgresConnectionFactory(DISABLED_ROW_LEVEL_SECURITY,
+            postgresConfiguration.nonRLSPoolInitialSize(),
+            postgresConfiguration.nonRLSPoolMaxSize(),
+            connectionFactory);
     }
 
     @Provides


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org