Skip to content

Commit f0e6109

Browse files
feat[backend](modules-config): removed decryption type in all comunications with modules config service
1 parent a697ca0 commit f0e6109

5 files changed

Lines changed: 34 additions & 38 deletions

File tree

backend/src/main/java/com/park/utmstack/event_processor/EventProcessorManagerService.java

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,18 @@
11
package com.park.utmstack.event_processor;
22

33
import com.park.utmstack.config.Constants;
4-
import com.park.utmstack.domain.application_modules.UtmModule;
5-
import com.park.utmstack.domain.application_modules.UtmModuleGroup;
6-
import com.park.utmstack.domain.application_modules.enums.ModuleName;
74
import com.park.utmstack.service.dto.application_modules.ModuleDTO;
8-
import com.park.utmstack.service.dto.application_modules.UtmModuleMapper;
95
import com.park.utmstack.service.web_clients.rest_template.RestTemplateService;
10-
import com.park.utmstack.util.CipherUtil;
116
import lombok.RequiredArgsConstructor;
127
import org.slf4j.Logger;
138
import org.slf4j.LoggerFactory;
149
import org.springframework.http.HttpHeaders;
1510
import org.springframework.http.MediaType;
1611
import org.springframework.http.ResponseEntity;
1712
import org.springframework.stereotype.Service;
18-
import org.springframework.util.StringUtils;
1913
import org.springframework.web.util.UriComponentsBuilder;
2014

2115
import java.util.List;
22-
import java.util.Set;
2316

2417
@Service
2518
@RequiredArgsConstructor
@@ -30,8 +23,6 @@ public class EventProcessorManagerService {
3023

3124
private final RestTemplateService restTemplateService;
3225

33-
private final List<ModuleName> typeFileNeedsDecryptList = List.of(ModuleName.GCP);
34-
3526
public static final String EVENT_PROCESSOR_BASE_URL = "http://" +
3627
System.getenv(Constants.ENV_EVENT_PROCESSOR_HOST) + ":" +
3728
System.getenv(Constants.ENV_EVENT_PROCESSOR_PORT);
@@ -59,27 +50,6 @@ public void updateModule(ModuleDTO module) {
5950
}
6051
}
6152

62-
public void decryptModuleConfig (UtmModule module){
63-
Set<UtmModuleGroup> groups = module.getModuleGroups();
64-
decryptModuleGroupsConfig(groups, module.getModuleName());
65-
}
66-
67-
public void decryptModuleConfig (ModuleDTO moduleDTO){
68-
Set<UtmModuleGroup> groups = moduleDTO.getModuleGroups();
69-
decryptModuleGroupsConfig(groups, moduleDTO.getModuleName());
70-
}
71-
72-
private void decryptModuleGroupsConfig(Set<UtmModuleGroup> groups, ModuleName moduleName) {
73-
groups.forEach((gp) -> {
74-
gp.getModuleGroupConfigurations().forEach((gpc) -> {
75-
if ((gpc.getConfDataType().equals(Constants.CONF_TYPE_PASSWORD) && StringUtils.hasText(gpc.getConfValue()))
76-
|| (gpc.getConfDataType().equals(Constants.CONF_TYPE_FILE) && StringUtils.hasText(gpc.getConfValue())) && typeFileNeedsDecryptList.contains(moduleName)) {
77-
gpc.setConfValue(CipherUtil.decrypt(gpc.getConfValue(), System.getenv(Constants.ENV_ENCRYPTION_KEY)));
78-
}
79-
});
80-
});
81-
}
82-
8353
private HttpHeaders buildEventProcessorHeaders() {
8454
HttpHeaders headers = new HttpHeaders();
8555
headers.setContentType(MediaType.APPLICATION_JSON);

backend/src/main/java/com/park/utmstack/service/application_modules/UtmModuleGroupConfigurationService.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ public void createConfigurationKeys(List<UtmModuleGroupConfiguration> keys) thro
4343
try {
4444
if (CollectionUtils.isEmpty(keys))
4545
return;
46+
for (UtmModuleGroupConfiguration key : keys) {
47+
if (isSensitiveType(key.getConfDataType()) && StringUtils.hasText(key.getConfValue())) {
48+
key.setConfValue(CipherUtil.encrypt(key.getConfValue(), System.getenv(Constants.ENV_ENCRYPTION_KEY)));
49+
}
50+
}
4651
moduleConfigurationRepository.saveAll(keys);
4752
} catch (Exception e) {
4853
throw new Exception(ctx + ": " + e.getMessage());

backend/src/main/java/com/park/utmstack/service/application_modules/UtmModuleGroupService.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -249,13 +249,19 @@ public void updateCollectorConfigurationKeys(CollectorConfigDTO collectorConfig)
249249
moduleGroupRepository.deleteAll(dbConfigs);
250250
} else {
251251
for (UtmModuleGroupConfiguration key : keys) {
252-
if (key.getConfDataType().equals("password")) {
253-
if (Constants.MASKED_VALUE.equals(key.getConfValue())) {
254-
continue;
255-
}
252+
boolean isSensitive = isSensitiveType(key.getConfDataType());
253+
if (isSensitive && Constants.MASKED_VALUE.equals(key.getConfValue())) {
254+
continue;
255+
}
256+
if (isSensitive) {
256257
key.setConfValue(CipherUtil.encrypt(key.getConfValue(), System.getenv(Constants.ENV_ENCRYPTION_KEY)));
257258
}
258259
}
260+
261+
List<UtmModuleGroupConfiguration> toSave = keys.stream()
262+
.filter(k -> !(isSensitiveType(k.getConfDataType()) && Constants.MASKED_VALUE.equals(k.getConfValue())))
263+
.collect(Collectors.toList());
264+
259265
List<Long> keyGroupIds = keys.stream()
260266
.map(UtmModuleGroupConfiguration::getGroupId)
261267
.toList();
@@ -265,7 +271,9 @@ public void updateCollectorConfigurationKeys(CollectorConfigDTO collectorConfig)
265271
.collect(Collectors.toList());
266272

267273
moduleGroupRepository.deleteAll(groupsToDelete);
268-
moduleGroupConfigurationRepository.saveAll(keys);
274+
if (!toSave.isEmpty()) {
275+
moduleGroupConfigurationRepository.saveAll(toSave);
276+
}
269277
}
270278

271279
} catch (Exception e) {
@@ -274,4 +282,8 @@ public void updateCollectorConfigurationKeys(CollectorConfigDTO collectorConfig)
274282
}
275283
}
276284

285+
private boolean isSensitiveType(String dataType) {
286+
return Constants.CONF_TYPE_PASSWORD.equals(dataType) || Constants.CONF_TYPE_FILE.equals(dataType);
287+
}
288+
277289
}

backend/src/main/java/com/park/utmstack/web/rest/application_modules/UtmModuleGroupResource.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,17 @@ public ResponseEntity<UtmModuleGroup> getConfigurationGroup(@PathVariable Long g
159159
final String ctx = CLASSNAME + ".getConfigurationGroups";
160160

161161
Optional<UtmModuleGroup> group = moduleGroupService.findOne(groupId);
162+
group.ifPresent(g -> {
163+
if (g.getModuleGroupConfigurations() != null) {
164+
for (UtmModuleGroupConfiguration conf : g.getModuleGroupConfigurations()) {
165+
if ((Constants.CONF_TYPE_PASSWORD.equals(conf.getConfDataType())
166+
|| Constants.CONF_TYPE_FILE.equals(conf.getConfDataType()))
167+
&& conf.getConfValue() != null) {
168+
conf.setConfValue(Constants.MASKED_VALUE);
169+
}
170+
}
171+
}
172+
});
162173
return ResponseUtil.wrapOrNotFound(group);
163174

164175
}

backend/src/main/java/com/park/utmstack/web/rest/application_modules/UtmModuleResource.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,7 @@ public ResponseEntity<UtmModule> getModuleDetailsDecrypted(@RequestParam ModuleN
124124
final String ctx = CLASSNAME + ".getModuleDetailsDecrypted";
125125
try {
126126
UtmModule module = moduleFactory.getInstance(nameShort).getDetails(utmServerRepository.getUtmServer());
127-
if (InternalApiKeyFilter.isApiKeyHeaderInUse()) {
128-
this.eventProcessorManagerService.decryptModuleConfig(module);
129-
} else {
127+
if (!InternalApiKeyFilter.isApiKeyHeaderInUse()) {
130128
String msg = ctx + ": You must provide the header used to communicate internally with this resource";
131129
log.error(msg);
132130
eventService.createEvent(msg, ApplicationEventType.ERROR);

0 commit comments

Comments
 (0)