FINERACT-2085: Implementation for decrypting database passwords with CLI tooling

This commit is contained in:
Arnold Galovics
2024-05-21 11:21:26 +02:00
committed by Arnold Gálovics
parent 55a67b1344
commit 63ee58aabd
2 changed files with 48 additions and 1 deletions
@@ -0,0 +1,42 @@
/**
* 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.database;
import static org.apache.fineract.infrastructure.core.service.database.DatabasePasswordEncryptor.DEFAULT_ENCRYPTION;
import java.text.MessageFormat;
import org.apache.fineract.infrastructure.security.utils.EncryptionUtil;
public final class DatabasePasswordDecryptor {
private DatabasePasswordDecryptor() {}
@SuppressWarnings("checkstyle:regexpsinglelinejava")
public static void main(String[] args) {
if (args.length < 2) {
System.out.println(
"Usage: java -cp fineract-provider.jar -Dloader.main=org.apache.fineract.infrastructure.core.service.database.DatabasePasswordDecryptor org.springframework.boot.loader.launch.PropertiesLauncher <masterPassword> <base64Password>");
System.exit(1);
}
String masterPassword = args[0];
String base64Password = args[1];
String decryptedPassword = EncryptionUtil.decryptFromBase64(DEFAULT_ENCRYPTION, masterPassword, base64Password);
System.out.println(MessageFormat.format("The decrypted password: {0}", decryptedPassword));
}
}
@@ -40,13 +40,14 @@ public class DatabasePasswordEncryptor implements PasswordEncryptor {
public static void main(String[] args) {
if (args.length < 2) {
System.out.println(
"Usage: java -cp fineract-provider.jar java -Dloader.main=org.apache.fineract.infrastructure.core.service.database.DatabasePasswordEncryptor org.springframework.boot.loader.PropertiesLauncher <masterPassword> <plainPassword>");
"Usage: java -cp fineract-provider.jar -Dloader.main=org.apache.fineract.infrastructure.core.service.database.DatabasePasswordEncryptor org.springframework.boot.loader.launch.PropertiesLauncher <masterPassword> <plainPassword>");
System.exit(1);
}
String masterPassword = args[0];
String plainPassword = args[1];
String encryptedPassword = EncryptionUtil.encryptToBase64(DEFAULT_ENCRYPTION, masterPassword, plainPassword);
System.out.println(MessageFormat.format("The encrypted password: {0}", encryptedPassword));
System.out.println(MessageFormat.format("The master password hash is: {0}", getPasswordHash(masterPassword)));
}
@Override
@@ -74,6 +75,10 @@ public class DatabasePasswordEncryptor implements PasswordEncryptor {
.map(FineractProperties::getTenant) //
.map(FineractProperties.FineractTenantProperties::getMasterPassword) //
.orElse(fineractProperties.getDatabase().getDefaultMasterPassword());
return getPasswordHash(masterPassword);
}
private static String getPasswordHash(String masterPassword) {
return BCrypt.hashpw(masterPassword.getBytes(StandardCharsets.UTF_8), BCrypt.gensalt());
}