This is an automated email from the ASF dual-hosted git repository. ykinash pushed a commit to branch DATATALAB-2504 in repository https://gitbox.apache.org/repos/asf/incubator-datalab.git
commit e213ed49d94855470719ddb5db870385078daffc Author: KinashYurii <[email protected]> AuthorDate: Wed Jul 7 15:37:20 2021 +0300 [DATALAB-2504] -- fixed problem with wrong write on config page --- .../datalab/properties/ChangePropertiesConst.java | 12 +-- .../properties/ChangePropertiesService.java | 100 ++++++++++++++------- 2 files changed, 76 insertions(+), 36 deletions(-) diff --git a/services/datalab-webapp-common/src/main/java/com/epam/datalab/properties/ChangePropertiesConst.java b/services/datalab-webapp-common/src/main/java/com/epam/datalab/properties/ChangePropertiesConst.java index 2cfc997..1fa021c 100644 --- a/services/datalab-webapp-common/src/main/java/com/epam/datalab/properties/ChangePropertiesConst.java +++ b/services/datalab-webapp-common/src/main/java/com/epam/datalab/properties/ChangePropertiesConst.java @@ -23,17 +23,17 @@ public interface ChangePropertiesConst { String GKE_SELF_SERVICE_PATH = "/root/self-service.yaml"; String GKE_SELF_SERVICE = "self-service.yaml"; String SELF_SERVICE = "self-service.yml"; - // String SELF_SERVICE_PROP_PATH = "services/self-service/self-service.yml"; - String SELF_SERVICE_PROP_PATH = "/opt/datalab/conf/self-service.yml"; + String SELF_SERVICE_PROP_PATH = "services/self-service/self-service.yml"; +// String SELF_SERVICE_PROP_PATH = "/opt/datalab/conf/self-service.yml"; String PROVISIONING_SERVICE = "provisioning.yml"; - // String PROVISIONING_SERVICE_PROP_PATH = "services/provisioning-service/provisioning.yml"; - String PROVISIONING_SERVICE_PROP_PATH = "/opt/datalab/conf/provisioning.yml"; + String PROVISIONING_SERVICE_PROP_PATH = "services/provisioning-service/provisioning.yml"; +// String PROVISIONING_SERVICE_PROP_PATH = "/opt/datalab/conf/provisioning.yml"; String BILLING_SERVICE = "billing.yml"; + // String BILLING_SERVICE_PROP_PATH = "/opt/datalab/conf/billing.yml"; // String BILLING_SERVICE_PROP_PATH = "services/billing-gcp/billing.yml"; // String BILLING_SERVICE_PROP_PATH = "services/billing-azure/billing.yml"; -// String BILLING_SERVICE_PROP_PATH = "services/billing-aws/billing.yml"; - String BILLING_SERVICE_PROP_PATH = "/opt/datalab/conf/billing.yml"; + String BILLING_SERVICE_PROP_PATH = "services/billing-aws/billing.yml"; String GKE_BILLING_PATH = "/root/billing.yaml"; String GKE_BILLING_SERVICE = "billing.yml"; String RESTART_URL = "config/restart"; diff --git a/services/datalab-webapp-common/src/main/java/com/epam/datalab/properties/ChangePropertiesService.java b/services/datalab-webapp-common/src/main/java/com/epam/datalab/properties/ChangePropertiesService.java index e4944a5..80b000a 100644 --- a/services/datalab-webapp-common/src/main/java/com/epam/datalab/properties/ChangePropertiesService.java +++ b/services/datalab-webapp-common/src/main/java/com/epam/datalab/properties/ChangePropertiesService.java @@ -49,21 +49,39 @@ public class ChangePropertiesService { } } + public void writeFileFromString(String newPropFile, String serviceName, String servicePath) { + String oldFile = readFile(serviceName, servicePath); + try (BufferedWriter writer = new BufferedWriter(new FileWriter(servicePath))) { + try { + // changeCHMODE(serviceName, servicePath, ChangePropertiesConst.DEFAULT_CHMOD, ChangePropertiesConst.WRITE_CHMOD); + log.info("Trying to overwrite {}, file for path {} :", serviceName, servicePath); + writer.write(addLicence()); + writer.write(checkAndReplaceSecretIfEmpty(newPropFile, oldFile)); + log.info("{} overwritten successfully", serviceName); + writer.close(); + // changeCHMODE(serviceName, servicePath, ChangePropertiesConst.WRITE_CHMOD, ChangePropertiesConst.DEFAULT_CHMOD); + } catch (Exception e) { + log.error("Failed during overwriting {}", serviceName); + writer.write(oldFile); + throw new DynamicChangePropertiesException(String.format("Failed during overwriting %s", serviceName)); + } + } catch (IOException e) { + log.error("Failed to create writer with path {}", servicePath); + throw new DynamicChangePropertiesException(String.format("Failed during overwriting %s", serviceName)); + } + } + + private String readFile(String serviceName, String servicePath) { + String oldFile; try { - String oldFile = FileUtils.readFileToString(new File(servicePath), Charset.defaultCharset()); - changeCHMODE(serviceName, servicePath, ChangePropertiesConst.DEFAULT_CHMOD, ChangePropertiesConst.WRITE_CHMOD); - BufferedWriter writer = new BufferedWriter(new FileWriter(servicePath)); - log.info("Trying to overwrite {}, file for path {} :", serviceName, servicePath); - writer.write(addLicence()); - writer.write(checkAndReplaceSecretIfEmpty(newPropFile, oldFile)); - log.info("{} overwritten successfully", serviceName); - writer.close(); - changeCHMODE(serviceName, servicePath, ChangePropertiesConst.WRITE_CHMOD, ChangePropertiesConst.DEFAULT_CHMOD); + oldFile = FileUtils.readFileToString(new File(servicePath), Charset.defaultCharset()); } catch (IOException e) { - log.error("Failed during overwriting {}", serviceName); + log.error("Failed to read with path {}", servicePath); throw new DynamicChangePropertiesException(String.format("Failed during overwriting %s", serviceName)); + } + return oldFile; } public RestartAnswer restart(RestartForm restartForm) { @@ -94,24 +112,30 @@ public class ChangePropertiesService { private String hideSecretsAndRemoveLicence(String currentConf) { Matcher passMatcher = Pattern.compile(ChangePropertiesConst.SECRET_REGEX).matcher(currentConf); Matcher userMatcher = Pattern.compile(ChangePropertiesConst.USER_REGEX).matcher(currentConf); - List<String> secrets = new ArrayList<>(); - List<String> users = new ArrayList<>(); + List<String> secretsAndUsers = new ArrayList<>(); String confWithReplacedSecretConf = removeLicence(currentConf); while (passMatcher.find()) { String secret = passMatcher.group().split(":")[ChangePropertiesConst.DEFAULT_VALUE_PLACE]; if (!(secret.isEmpty() || secret.trim().isEmpty())) - secrets.add(secret); + secretsAndUsers.add(secret); } while (userMatcher.find()) { String user = userMatcher.group().split(":")[ChangePropertiesConst.DEFAULT_VALUE_PLACE]; if (!(user.isEmpty() || user.trim().isEmpty())) - users.add(user); + secretsAndUsers.add(user); } - for (String secret : secrets) { - confWithReplacedSecretConf = confWithReplacedSecretConf.replace(secret, ChangePropertiesConst.SECRET_REPLACEMENT_FORMAT); - } - for (String user : users) { - confWithReplacedSecretConf = confWithReplacedSecretConf.replace(user, ChangePropertiesConst.SECRET_REPLACEMENT_FORMAT); + for (String secretOrUser : secretsAndUsers) { + int start = confWithReplacedSecretConf.indexOf(secretOrUser); + int end = confWithReplacedSecretConf.indexOf(":", start); + boolean isTure; + try { + String s = confWithReplacedSecretConf.substring(start, end); + isTure = s.equals(secretOrUser); + } catch (StringIndexOutOfBoundsException e) { + isTure = true; + } + if (isTure) + confWithReplacedSecretConf = confWithReplacedSecretConf.replace(secretOrUser, ChangePropertiesConst.SECRET_REPLACEMENT_FORMAT); } return confWithReplacedSecretConf; } @@ -137,29 +161,45 @@ public class ChangePropertiesService { } private String checkAndReplaceSecretIfEmpty(String newPropFile, String oldProf) { - Map<String, String> emptySecrets = findEmptySecret(newPropFile); - return emptySecrets.isEmpty() ? newPropFile : replaceEmptySecret(newPropFile, oldProf, emptySecrets); + Map<String, String> emptySecretsAndUserNames = findEmptySecretAndNames(newPropFile); + return emptySecretsAndUserNames.isEmpty() ? newPropFile : replaceEmptySecret(newPropFile, oldProf, emptySecretsAndUserNames); } private String replaceEmptySecret(String newPropFile, String oldProf, Map<String, String> emptySecrets) { String fileWithReplacedEmptySecrets = newPropFile; - Matcher oldProfMatcher = Pattern.compile(ChangePropertiesConst.SECRET_REGEX).matcher(oldProf); - while (oldProfMatcher.find()) { - String[] s = oldProfMatcher.group().split(":"); + Matcher oldPassMatcher = Pattern.compile(ChangePropertiesConst.SECRET_REGEX).matcher(oldProf); + Matcher oldUserMatcher = Pattern.compile(ChangePropertiesConst.USER_REGEX).matcher(oldProf); + + while (oldPassMatcher.find()) { + String[] s = oldPassMatcher.group().split(":"); if (emptySecrets.containsKey(s[ChangePropertiesConst.DEFAULT_NAME_PLACE])) { - fileWithReplacedEmptySecrets = fileWithReplacedEmptySecrets.replace(emptySecrets.get(s[ChangePropertiesConst.DEFAULT_NAME_PLACE]), oldProfMatcher.group()); + fileWithReplacedEmptySecrets = fileWithReplacedEmptySecrets.replace(emptySecrets.get(s[ChangePropertiesConst.DEFAULT_NAME_PLACE]), oldPassMatcher.group()); + } + } + while (oldUserMatcher.find()) { + String[] s = oldUserMatcher.group().split(":"); + if (emptySecrets.containsKey(s[ChangePropertiesConst.DEFAULT_NAME_PLACE])) { + fileWithReplacedEmptySecrets = fileWithReplacedEmptySecrets.replace(emptySecrets.get(s[ChangePropertiesConst.DEFAULT_NAME_PLACE]), oldUserMatcher.group()); } } return fileWithReplacedEmptySecrets; } - private Map<String, String> findEmptySecret(String newPropFile) { - Matcher newPropFileMatcher = Pattern.compile(ChangePropertiesConst.SECRET_REGEX).matcher(newPropFile); + private Map<String, String> findEmptySecretAndNames(String newPropFile) { + Matcher passMatcher = Pattern.compile(ChangePropertiesConst.SECRET_REGEX).matcher(newPropFile); + Matcher userNameMatcher = Pattern.compile(ChangePropertiesConst.USER_REGEX).matcher(newPropFile); Map<String, String> emptySecrets = new HashMap<>(); - while (newPropFileMatcher.find()) { - String[] s = newPropFileMatcher.group().split(":"); + while (passMatcher.find()) { + String[] s = passMatcher.group().split(":"); + if (s[ChangePropertiesConst.DEFAULT_VALUE_PLACE].equals(ChangePropertiesConst.SECRET_REPLACEMENT_FORMAT)) { + emptySecrets.put(s[ChangePropertiesConst.DEFAULT_NAME_PLACE], passMatcher.group()); + } + } + + while (userNameMatcher.find()) { + String[] s = userNameMatcher.group().split(":"); if (s[ChangePropertiesConst.DEFAULT_VALUE_PLACE].equals(ChangePropertiesConst.SECRET_REPLACEMENT_FORMAT)) { - emptySecrets.put(s[ChangePropertiesConst.DEFAULT_NAME_PLACE], newPropFileMatcher.group()); + emptySecrets.put(s[ChangePropertiesConst.DEFAULT_NAME_PLACE], userNameMatcher.group()); } } return emptySecrets; --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
