Skip to content

Commit

Permalink
ConfigurationServiceImpl: encryptPasswords() modifies configuration p…
Browse files Browse the repository at this point in the history
…roperties in place. No need to return the modified Map reference.

Avoid instanciating extra objects that are only a shallow copy of the original.
  • Loading branch information
cdealti committed May 19, 2015
1 parent 8c6e803 commit ff97a66
Showing 1 changed file with 13 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -639,12 +639,11 @@ void updateConfigurationInternal(String pid, Map<String, Object> properties, boo
s_logger.info("Snapshot on EventAdmin configuration will be taken for {}.", pid);
}

Map<String, Object> encryptedProperties= encryptPasswords(mergedProperties);
encryptPasswords(mergedProperties);
// Update the new properties
// use ConfigurationAdmin to do the update
Configuration config = m_configurationAdmin.getConfiguration(pid);
config.update(CollectionsUtil.mapToDictionary(encryptedProperties));
//mergedProperties));
config.update(CollectionsUtil.mapToDictionary(mergedProperties));

s_logger.info("Updating Configuration of ConfigurableComponent {} ... Done.", pid);
} catch (IOException e) {
Expand Down Expand Up @@ -718,7 +717,7 @@ Map<String, Object> decryptPasswords(ComponentConfiguration config) {
return configProperties;
}

private Map<String, Object> encryptPasswords(ComponentConfiguration config){
private void encryptPasswords(ComponentConfiguration config){
Map<String, Object> propertiesToUpdate = config.getConfigurationProperties();

Iterator<String> keys = propertiesToUpdate.keySet().iterator();
Expand All @@ -730,15 +729,15 @@ private Map<String, Object> encryptPasswords(ComponentConfiguration config){
try {
propertiesToUpdate.put(key, new Password(m_cryptoService.encryptAes(value.toString())));
} catch (Exception e) {
s_logger.warn("Failed to encrypt Password property: {}", key);
propertiesToUpdate.remove(key);
}
}
}
}
return propertiesToUpdate;
}
}

private Map<String, Object> encryptPasswords(Map<String, Object> propertiesToUpdate){
private void encryptPasswords(Map<String, Object> propertiesToUpdate){
Iterator<String> keys = propertiesToUpdate.keySet().iterator();
while (keys.hasNext()) {
String key = keys.next();
Expand All @@ -748,12 +747,12 @@ private Map<String, Object> encryptPasswords(Map<String, Object> propertiesToUpd
try {
propertiesToUpdate.put(key, new Password(m_cryptoService.encryptAes(value.toString())));
} catch (Exception e) {
s_logger.warn("Failed to encrypt Password property: {}", key);
propertiesToUpdate.remove(key);
}
}
}
}
return propertiesToUpdate;
}
}

// ----------------------------------------------------------------
Expand All @@ -762,15 +761,12 @@ private Map<String, Object> encryptPasswords(Map<String, Object> propertiesToUpd
//
// ----------------------------------------------------------------

private List<ComponentConfigurationImpl> encryptConfigs(List<? extends ComponentConfiguration> configs) {
List<ComponentConfigurationImpl> configImpls = new ArrayList<ComponentConfigurationImpl>();
private void encryptConfigs(List<? extends ComponentConfiguration> configs) {
for (ComponentConfiguration config : configs) {
if (config instanceof ComponentConfigurationImpl) {
((ComponentConfigurationImpl) config).setProperties(encryptPasswords(config));
configImpls.add((ComponentConfigurationImpl) config);
encryptPasswords(config);
}
}
return configImpls;
}

private boolean allSnapshotsUnencrypted() {
Expand Down Expand Up @@ -857,16 +853,14 @@ private void encryptPlainSnapshots() throws Exception {
}
}
List<ComponentConfigurationImpl> configs = xmlConfigs.getConfigurations();
List<ComponentConfigurationImpl> configImpls = encryptConfigs(configs);
XmlComponentConfigurations conf = new XmlComponentConfigurations();
conf.setConfigurations(configImpls);
encryptConfigs(configs);

FileOutputStream fos = null;
OutputStreamWriter osw = null;
try {
fos = new FileOutputStream(fSnapshot);
osw = new OutputStreamWriter(fos, "UTF-8");
String xmlResult = XmlUtil.marshal(conf);
String xmlResult = XmlUtil.marshal(xmlConfigs);
String encryptedXML = m_cryptoService.encryptAes(xmlResult);
osw.append(encryptedXML);
osw.flush();
Expand Down Expand Up @@ -1172,8 +1166,7 @@ private void loadLatestSnapshotInConfigAdmin() throws KuraException {
fr = new FileReader(fSnapshot);
xmlConfigs = XmlUtil.unmarshal(fr, XmlComponentConfigurations.class);
encryptPlainSnapshots();
List<ComponentConfigurationImpl> encryptedConfigs = encryptConfigs(xmlConfigs.getConfigurations());
xmlConfigs.setConfigurations(encryptedConfigs);
encryptConfigs(xmlConfigs.getConfigurations());
}
} catch (Exception ex) {
throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
Expand Down

0 comments on commit ff97a66

Please sign in to comment.