You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@fineract.apache.org by "galovics (via GitHub)" <gi...@apache.org> on 2023/02/27 12:15:29 UTC

[GitHub] [fineract] galovics commented on a diff in pull request #3011: FINERACT-1831 - Encrypted tenant passwords

galovics commented on code in PR #3011:
URL: https://github.com/apache/fineract/pull/3011#discussion_r1118654993


##########
fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/service/migration/TenantPasswordEncryptionTask.java:
##########
@@ -0,0 +1,90 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.fineract.infrastructure.core.service.migration;
+
+import java.sql.ResultSet;
+import java.sql.Statement;
+import java.util.Optional;
+import liquibase.change.custom.CustomTaskChange;
+import liquibase.database.Database;
+import liquibase.database.jvm.JdbcConnection;
+import liquibase.exception.CustomChangeException;
+import liquibase.exception.SetupException;
+import liquibase.exception.ValidationErrors;
+import liquibase.resource.ResourceAccessor;
+import org.apache.fineract.infrastructure.security.utils.EncryptionUtil;
+
+public class TenantPasswordEncryptionTask implements CustomTaskChange {
+
+    private final String encryptionType;
+    private final String masterKey;
+
+    public TenantPasswordEncryptionTask() {
+        this.encryptionType = Optional.ofNullable(System.getenv("FINERACT_DEFAULT_TENANTDB_ENCRYPTION"))
+                .orElse(EncryptionUtil.DEFAULT_CIPHER);
+        this.masterKey = Optional.ofNullable(System.getenv("FINERACT_DEFAULT_TENANTDB_KEY")).orElse(EncryptionUtil.DEFAULT_MASTER_PASSWORD);
+    }
+
+    @Override
+    public void execute(Database database) throws CustomChangeException {
+        var dbConn = (JdbcConnection) database.getConnection(); // autocommit is false

Review Comment:
   No var pls. Thanks ;-)



##########
fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/service/migration/TenantPasswordEncryptionTask.java:
##########
@@ -0,0 +1,90 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.fineract.infrastructure.core.service.migration;
+
+import java.sql.ResultSet;
+import java.sql.Statement;
+import java.util.Optional;
+import liquibase.change.custom.CustomTaskChange;
+import liquibase.database.Database;
+import liquibase.database.jvm.JdbcConnection;
+import liquibase.exception.CustomChangeException;
+import liquibase.exception.SetupException;
+import liquibase.exception.ValidationErrors;
+import liquibase.resource.ResourceAccessor;
+import org.apache.fineract.infrastructure.security.utils.EncryptionUtil;
+
+public class TenantPasswordEncryptionTask implements CustomTaskChange {
+
+    private final String encryptionType;
+    private final String masterKey;
+
+    public TenantPasswordEncryptionTask() {
+        this.encryptionType = Optional.ofNullable(System.getenv("FINERACT_DEFAULT_TENANTDB_ENCRYPTION"))

Review Comment:
   Unfortunately this is not enough. The properties can be overridden directly via program arguments for example so this is not going to be in an environment variable necessarily.
   I know this object will be managed by Liquibase so no Spring DI at this point but you can do the ugly way to do it, look at TemporaryConfigurationServiceContainer



##########
fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/service/database/DataSourcePerTenantServiceFactory.java:
##########
@@ -67,18 +68,13 @@ public DataSource createNewDataSourceFor(final FineractPlatformTenantConnection
         String schemaConnectionParameters = tenantConnection.getSchemaConnectionParameters();
         // Properties to ReadOnly case
         if (fineractProperties.getMode().isReadOnlyMode()) {
-            schemaServer = getPropertyValue(tenantConnection.getReadOnlySchemaServer(), TenantConstants.PROPERTY_RO_SCHEMA_SERVER_NAME,
-                    schemaServer);
-            schemaPort = getPropertyValue(tenantConnection.getReadOnlySchemaServerPort(), TenantConstants.PROPERTY_RO_SCHEMA_SERVER_PORT,
-                    schemaPort);
-            schemaName = getPropertyValue(tenantConnection.getReadOnlySchemaName(), TenantConstants.PROPERTY_RO_SCHEMA_SCHEMA_NAME,
-                    schemaName);
-            schemaUsername = getPropertyValue(tenantConnection.getReadOnlySchemaUsername(), TenantConstants.PROPERTY_RO_SCHEMA_USERNAME,
-                    schemaUsername);
-            schemaPassword = getPropertyValue(tenantConnection.getReadOnlySchemaPassword(), TenantConstants.PROPERTY_RO_SCHEMA_PASSWORD,
-                    schemaPassword);
+            schemaServer = getPropertyValue(tenantConnection.getReadOnlySchemaServer(), schemaServer);

Review Comment:
   I'd rather go with org.apache.commons.lang3.StringUtils#defaultIfBlank.



##########
fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/service/migration/TenantPasswordEncryptionTask.java:
##########
@@ -0,0 +1,90 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.fineract.infrastructure.core.service.migration;
+
+import java.sql.ResultSet;
+import java.sql.Statement;
+import java.util.Optional;
+import liquibase.change.custom.CustomTaskChange;
+import liquibase.database.Database;
+import liquibase.database.jvm.JdbcConnection;
+import liquibase.exception.CustomChangeException;
+import liquibase.exception.SetupException;
+import liquibase.exception.ValidationErrors;
+import liquibase.resource.ResourceAccessor;
+import org.apache.fineract.infrastructure.security.utils.EncryptionUtil;
+
+public class TenantPasswordEncryptionTask implements CustomTaskChange {
+
+    private final String encryptionType;
+    private final String masterKey;
+
+    public TenantPasswordEncryptionTask() {
+        this.encryptionType = Optional.ofNullable(System.getenv("FINERACT_DEFAULT_TENANTDB_ENCRYPTION"))
+                .orElse(EncryptionUtil.DEFAULT_CIPHER);
+        this.masterKey = Optional.ofNullable(System.getenv("FINERACT_DEFAULT_TENANTDB_KEY")).orElse(EncryptionUtil.DEFAULT_MASTER_PASSWORD);
+    }
+
+    @Override
+    public void execute(Database database) throws CustomChangeException {
+        var dbConn = (JdbcConnection) database.getConnection(); // autocommit is false
+
+        try {
+            Statement selectStatement = dbConn.createStatement();
+            Statement updateStatement = dbConn.createStatement();
+
+            ResultSet rs = selectStatement.executeQuery("SELECT id, schema_password FROM tenant_server_connections");
+            while (rs.next()) {
+                String id = rs.getString("id");
+                String schemaPassword = rs.getString("schema_password");
+                String encryptedPassword = EncryptionUtil.encrypt(encryptionType, masterKey, schemaPassword);
+
+                var updateSql = String.format("update tenant_server_connections set schema_password = '%s' where id = %s",
+                        encryptedPassword, id);
+                updateStatement.execute(updateSql);
+            }
+
+            selectStatement.close();

Review Comment:
   Resource leaking potentially. You can use try with resources instead.



##########
fineract-provider/src/main/java/org/apache/fineract/infrastructure/core/service/migration/TenantPasswordEncryptionTask.java:
##########
@@ -0,0 +1,90 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.fineract.infrastructure.core.service.migration;
+
+import java.sql.ResultSet;
+import java.sql.Statement;
+import java.util.Optional;
+import liquibase.change.custom.CustomTaskChange;
+import liquibase.database.Database;
+import liquibase.database.jvm.JdbcConnection;
+import liquibase.exception.CustomChangeException;
+import liquibase.exception.SetupException;
+import liquibase.exception.ValidationErrors;
+import liquibase.resource.ResourceAccessor;
+import org.apache.fineract.infrastructure.security.utils.EncryptionUtil;
+
+public class TenantPasswordEncryptionTask implements CustomTaskChange {
+
+    private final String encryptionType;
+    private final String masterKey;
+
+    public TenantPasswordEncryptionTask() {
+        this.encryptionType = Optional.ofNullable(System.getenv("FINERACT_DEFAULT_TENANTDB_ENCRYPTION"))
+                .orElse(EncryptionUtil.DEFAULT_CIPHER);
+        this.masterKey = Optional.ofNullable(System.getenv("FINERACT_DEFAULT_TENANTDB_KEY")).orElse(EncryptionUtil.DEFAULT_MASTER_PASSWORD);
+    }
+
+    @Override
+    public void execute(Database database) throws CustomChangeException {
+        var dbConn = (JdbcConnection) database.getConnection(); // autocommit is false
+
+        try {
+            Statement selectStatement = dbConn.createStatement();
+            Statement updateStatement = dbConn.createStatement();
+
+            ResultSet rs = selectStatement.executeQuery("SELECT id, schema_password FROM tenant_server_connections");
+            while (rs.next()) {
+                String id = rs.getString("id");
+                String schemaPassword = rs.getString("schema_password");
+                String encryptedPassword = EncryptionUtil.encrypt(encryptionType, masterKey, schemaPassword);
+
+                var updateSql = String.format("update tenant_server_connections set schema_password = '%s' where id = %s",

Review Comment:
   Same here.



##########
fineract-provider/src/main/java/org/apache/fineract/infrastructure/security/utils/EncryptionUtil.java:
##########
@@ -0,0 +1,104 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.fineract.infrastructure.security.utils;
+
+import java.nio.charset.StandardCharsets;
+import java.security.NoSuchAlgorithmException;
+import java.security.SecureRandom;
+import java.security.spec.InvalidKeySpecException;
+import java.security.spec.KeySpec;
+import java.util.Base64;
+import java.util.Optional;
+import javax.crypto.Cipher;
+import javax.crypto.SecretKeyFactory;
+import javax.crypto.spec.IvParameterSpec;
+import javax.crypto.spec.PBEKeySpec;
+import javax.crypto.spec.SecretKeySpec;
+import org.jetbrains.annotations.NotNull;
+
+public final class EncryptionUtil {

Review Comment:
   Can we make this class a Spring bean instead so it can be easily mocked out?



##########
fineract-provider/src/main/java/org/apache/fineract/infrastructure/security/utils/EncryptionUtil.java:
##########
@@ -0,0 +1,104 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.fineract.infrastructure.security.utils;
+
+import java.nio.charset.StandardCharsets;
+import java.security.NoSuchAlgorithmException;
+import java.security.SecureRandom;
+import java.security.spec.InvalidKeySpecException;
+import java.security.spec.KeySpec;
+import java.util.Base64;
+import java.util.Optional;
+import javax.crypto.Cipher;
+import javax.crypto.SecretKeyFactory;
+import javax.crypto.spec.IvParameterSpec;
+import javax.crypto.spec.PBEKeySpec;
+import javax.crypto.spec.SecretKeySpec;
+import org.jetbrains.annotations.NotNull;
+
+public final class EncryptionUtil {
+
+    public static final String DEFAULT_CIPHER = "AES/CBC/PKCS5Padding";
+
+    public static final String DEFAULT_MASTER_PASSWORD = "fineract";
+
+    private EncryptionUtil() {
+    }
+
+    public static String encrypt(String cipherType, String masterPassword, String data) {

Review Comment:
   Do we need this level of abstraction to be able to provide the cipherType and masterPassword? If it's a Spring bean you can easily take these from the environment and there'll be no need to pass them.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@fineract.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org