|
31 | 31 | import java.util.concurrent.RejectedExecutionException; |
32 | 32 | import java.util.concurrent.ScheduledExecutorService; |
33 | 33 | import java.util.concurrent.TimeUnit; |
| 34 | +import java.util.regex.Matcher; |
| 35 | +import java.util.regex.Pattern; |
34 | 36 |
|
35 | 37 | import javax.inject.Inject; |
36 | 38 | import javax.naming.ConfigurationException; |
|
114 | 116 | import org.apache.logging.log4j.ThreadContext; |
115 | 117 |
|
116 | 118 | public class AsyncJobManagerImpl extends ManagerBase implements AsyncJobManager, ClusterManagerListener, Configurable { |
| 119 | + private static final Pattern PASSWORD_FIELD_PATTERN = Pattern.compile("\\\"password\\\":\\\"([^\\\"]*)\\\"+"); |
| 120 | + |
117 | 121 | // Advanced |
118 | 122 | public static final ConfigKey<Long> JobExpireMinutes = new ConfigKey<Long>("Advanced", Long.class, "job.expire.minutes", "1440", |
119 | 123 | "Time (in minutes) for async-jobs to be kept in system", true, ConfigKey.Scope.Global); |
@@ -517,22 +521,26 @@ public AsyncJob queryJob(final long jobId, final boolean updatePollTime) { |
517 | 521 | } |
518 | 522 |
|
519 | 523 | public String obfuscatePassword(String result, boolean hidePassword) { |
520 | | - if (hidePassword) { |
521 | | - String pattern = "\"password\":"; |
522 | | - if (result != null) { |
523 | | - if (result.contains(pattern)) { |
524 | | - String[] resp = result.split(pattern); |
525 | | - String psswd = resp[1].toString().split(",")[0]; |
526 | | - if (psswd.endsWith("}")) { |
527 | | - psswd = psswd.substring(0, psswd.length() - 1); |
528 | | - result = resp[0] + pattern + psswd.replace(psswd.substring(2, psswd.length() - 1), "*****") + "}," + resp[1].split(",", 2)[1]; |
529 | | - } else { |
530 | | - result = resp[0] + pattern + psswd.replace(psswd.substring(2, psswd.length() - 1), "*****") + "," + resp[1].split(",", 2)[1]; |
531 | | - } |
532 | | - } |
533 | | - } |
| 524 | + if (!hidePassword || StringUtils.isBlank(result)) { |
| 525 | + return result; |
| 526 | + } |
| 527 | + |
| 528 | + Matcher matcher = PASSWORD_FIELD_PATTERN.matcher(result); |
| 529 | + StringBuilder obfuscatedResult = new StringBuilder(); |
| 530 | + while (matcher.find()) { |
| 531 | + String password = matcher.group(1); |
| 532 | + String replacement = "\"password\":\"" + obfuscatePasswordValue(password) + "\""; |
| 533 | + matcher.appendReplacement(obfuscatedResult, Matcher.quoteReplacement(replacement)); |
| 534 | + } |
| 535 | + matcher.appendTail(obfuscatedResult); |
| 536 | + return obfuscatedResult.toString(); |
| 537 | + } |
| 538 | + |
| 539 | + private String obfuscatePasswordValue(String password) { |
| 540 | + if (StringUtils.isEmpty(password)) { |
| 541 | + return password; |
534 | 542 | } |
535 | | - return result; |
| 543 | + return password.charAt(0) + "*****"; |
536 | 544 | } |
537 | 545 |
|
538 | 546 | private void scheduleExecution(final AsyncJobVO job) { |
|
0 commit comments