Skip to content

Commit ec2bdcf

Browse files
committed
feat(config): introduce constants for configuration data types and refactor validation logic
1 parent 6f26f08 commit ec2bdcf

File tree

3 files changed

+30
-32
lines changed

3 files changed

+30
-32
lines changed

backend/src/main/java/com/park/utmstack/config/Constants.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,11 @@ public final class Constants {
151151
public static final String ENV_TFA_ENABLE = "APP_TFA_ENABLED";
152152
public static final String TFA_EXEMPTION_HEADER = "X-Bypass-TFA";
153153

154+
// Configuration data types for moduleGroupConfiguration
155+
156+
public static final String CONF_TYPE_PASSWORD = "password";
157+
public static final String CONF_TYPE_FILE = "file";
158+
154159
private Constants() {
155160
}
156161
}

backend/src/main/java/com/park/utmstack/domain/application_modules/validators/UtmModuleConfigValidator.java

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,47 +21,40 @@ public class UtmModuleConfigValidator {
2121
private final UtmModuleGroupConfigurationRepository moduleGroupConfigurationRepository;
2222
private final UtmStackConnectionService utmStackConnectionService;
2323

24-
/**
25-
* Validates if the given configuration allows a successful connection to UTMStack.
26-
*
27-
* @param keys A list of configuration keys that should include at least `ipAddress` and `connectionKey`.
28-
* @return true if login and ping are successful; false otherwise.
29-
* @throws Exception If required fields are missing or the connection fails.
30-
*/
3124
public boolean validate(UtmModule module, List<UtmModuleGroupConfiguration> keys) throws Exception {
3225
if (keys.isEmpty()) return false;
3326

34-
List<UtmModuleGroupConfiguration> configurations = moduleGroupConfigurationRepository
35-
.findAllByGroupId(keys.get(0).getGroupId())
36-
.stream()
37-
.map(c -> {
38-
if (this.containsConfigInKeys(keys, c.getConfKey()) != null) {
39-
return this.containsConfigInKeys(keys, c.getConfKey());
40-
} else {
41-
if ("password".equals(c.getConfDataType())) {
42-
c.setConfValue(CipherUtil.decrypt(
43-
c.getConfValue(),
44-
System.getenv(Constants.ENV_ENCRYPTION_KEY)
45-
));
46-
}
47-
return c;
48-
}
49-
})
50-
.collect(Collectors.toList());
27+
List<UtmModuleGroupConfiguration> dbConfigs = moduleGroupConfigurationRepository
28+
.findAllByGroupId(keys.get(0).getGroupId());
29+
30+
List<UtmModuleGroupConfDTO> configDTOs = dbConfigs.stream()
31+
.map(dbConf -> {
32+
UtmModuleGroupConfiguration override = findInKeys(keys, dbConf.getConfKey());
33+
UtmModuleGroupConfiguration source = override != null ? override : dbConf;
5134

52-
List<UtmModuleGroupConfDTO> configDTOs = configurations.stream()
53-
.map(entity -> new UtmModuleGroupConfDTO(entity.getConfKey(), entity.getConfValue()))
54-
.collect(Collectors.toList());
35+
return new UtmModuleGroupConfDTO(
36+
source.getConfKey(),
37+
decryptIfNeeded(source.getConfDataType(), source.getConfValue())
38+
);
39+
})
40+
.toList();
5541

5642
UtmModuleGroupConfWrapperDTO body = new UtmModuleGroupConfWrapperDTO(configDTOs);
5743

58-
return utmStackConnectionService.testConnection(module.getModuleName().name(),body);
44+
return utmStackConnectionService.testConnection(module.getModuleName().name(), body);
5945
}
6046

61-
private UtmModuleGroupConfiguration containsConfigInKeys(List<UtmModuleGroupConfiguration> keys, String confKey) {
47+
private UtmModuleGroupConfiguration findInKeys(List<UtmModuleGroupConfiguration> keys, String confKey) {
6248
return keys.stream()
63-
.filter(key -> key.getConfKey().equals(confKey))
49+
.filter(k -> k.getConfKey().equals(confKey))
6450
.findFirst()
6551
.orElse(null);
6652
}
53+
54+
private String decryptIfNeeded(String dataType, String value) {
55+
if (Constants.CONF_TYPE_PASSWORD.equals(dataType) || Constants.CONF_TYPE_FILE.equals(dataType)) {
56+
return CipherUtil.decrypt(value, System.getenv(Constants.ENV_ENCRYPTION_KEY));
57+
}
58+
return value;
59+
}
6760
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ public void decryptModuleConfig (UtmModule module){
6262
Set<UtmModuleGroup> groups = module.getModuleGroups();
6363
groups.forEach((gp) -> {
6464
gp.getModuleGroupConfigurations().forEach((gpc) -> {
65-
if ((gpc.getConfDataType().equals("password") && StringUtils.hasText(gpc.getConfValue()))
66-
|| (gpc.getConfDataType().equals("file") && StringUtils.hasText(gpc.getConfValue())) && typeFileNeedsDecryptList.contains(module.getModuleName())) {
65+
if ((gpc.getConfDataType().equals(Constants.CONF_TYPE_PASSWORD) && StringUtils.hasText(gpc.getConfValue()))
66+
|| (gpc.getConfDataType().equals(Constants.CONF_TYPE_FILE) && StringUtils.hasText(gpc.getConfValue())) && typeFileNeedsDecryptList.contains(module.getModuleName())) {
6767
gpc.setConfValue(CipherUtil.decrypt(gpc.getConfValue(), System.getenv(Constants.ENV_ENCRYPTION_KEY)));
6868
}
6969
});

0 commit comments

Comments
 (0)