From b590e8f7ae32dc481aebcba51d3bde2fcd3527f0 Mon Sep 17 00:00:00 2001 From: ashitsalesforce Date: Tue, 17 Sep 2024 16:50:44 -0700 Subject: [PATCH] save encrypted proxy password in config.properties save encrypted proxy password in config.properties so that user does not have to re-enter it across data loader invocations. --- .../salesforce/dataloader/config/Config.java | 62 +++++++++++++++++-- 1 file changed, 56 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/salesforce/dataloader/config/Config.java b/src/main/java/com/salesforce/dataloader/config/Config.java index 2291df98..b74f8148 100644 --- a/src/main/java/com/salesforce/dataloader/config/Config.java +++ b/src/main/java/com/salesforce/dataloader/config/Config.java @@ -59,6 +59,7 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import java.util.Properties; import java.util.Set; import java.util.StringJoiner; @@ -448,7 +449,6 @@ public class Config { // - Make sure to list all sensitive properties such as password because these properties are not saved. private static final String[] READ_ONLY_PROPERTY_NAMES = { PASSWORD, - PROXY_PASSWORD, IDLOOKUP_FIELD, MAPPING_FILE, EXTRACT_SOQL, @@ -489,6 +489,13 @@ public class Config { RESET_URL_ON_LOGIN, }; + private static final String[] ENCRYPTED_PROPERTY_NAMES = { + PASSWORD, + PROXY_PASSWORD, + OAUTH_ACCESSTOKEN, + OAUTH_REFRESHTOKEN + }; + /** * Creates an empty config that loads from and saves to the a file.

Use the methods * load() and save() to load and store this preference store.

@@ -1074,7 +1081,38 @@ static private String decryptProperty(EncryptionAesUtil encrypter, Map propMap, String propName, boolean isBatch) + throws ParameterLoadException { + String propValue = propMap.get(propName); + if (propValue != null && propValue.length() > 0) { + try { + return encrypter.encryptMsg(propValue); + } catch (GeneralSecurityException e) { + // if running in the UI, we can ignore encryption errors + if (isBatch) { + String errMsg = Messages.getFormattedString("Config.errorParameterLoad", new String[]{propName, + String.class.getName()}); + logger.error(errMsg, e); + throw new ParameterLoadException(errMsg, e); + } else { + return null; + } + } catch (Exception e) { + String errMsg = Messages.getFormattedString("Config.errorParameterLoad", new String[]{propName, + String.class.getName()}); + logger.error(errMsg, e); + throw new ParameterLoadException(errMsg, e); + } + } + return propValue; + } /** * @throws ConfigInitializationException */ @@ -1177,6 +1215,21 @@ public void save() throws IOException, GeneralSecurityException { } } + for (String encryptedProp : ENCRYPTED_PROPERTY_NAMES) { + if (this.properties.containsKey(encryptedProp)) { + Map propMap = (Map)this.properties; + try { + @SuppressWarnings("unchecked") + String propValue = encryptProperty(encrypter, + (Map)propMap, + encryptedProp, isBatchMode()); + this.properties.put(encryptedProp, propValue); + } catch (ParameterLoadException e) { + this.properties.remove(encryptedProp); // Encryption attempt failed. Do not save. + } + } + } + removeUnsupportedProperties(); removeDecryptedProperties(); removeCLIOptionsFromProperties(); @@ -1251,10 +1304,7 @@ private void removeUnsupportedProperties() { } private void removeDecryptedProperties() { - this.properties.remove(PASSWORD + DECRYPTED_SUFFIX); - this.properties.remove(PROXY_PASSWORD + DECRYPTED_SUFFIX); - this.properties.remove(OAUTH_ACCESSTOKEN + DECRYPTED_SUFFIX); - this.properties.remove(OAUTH_REFRESHTOKEN + DECRYPTED_SUFFIX); + this.properties.entrySet().removeIf(entry -> (entry.getKey().toString().endsWith(DECRYPTED_SUFFIX))); } private void removeCLIOptionsFromProperties() {