Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

save encrypted proxy password in config.properties #1269

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 56 additions & 6 deletions src/main/java/com/salesforce/dataloader/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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. <p> Use the methods
* <code>load()</code> and <code>save()</code> to load and store this preference store. </p>
Expand Down Expand Up @@ -1074,7 +1081,38 @@ static private String decryptProperty(EncryptionAesUtil encrypter, Map<String, S
}
return propValue;
}


/**
* Decrypt property with propName using the encrypter. If decryption succeeds, return the
* decrypted value
*
* @return decrypted property value
*/
static private String encryptProperty(EncryptionAesUtil encrypter, Map<String, String> 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
*/
Expand Down Expand Up @@ -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<String, String>)propMap,
encryptedProp, isBatchMode());
this.properties.put(encryptedProp, propValue);
} catch (ParameterLoadException e) {
this.properties.remove(encryptedProp); // Encryption attempt failed. Do not save.
}
}
}

removeUnsupportedProperties();
removeDecryptedProperties();
removeCLIOptionsFromProperties();
Expand Down Expand Up @@ -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() {
Expand Down
Loading