@@ -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}
0 commit comments