From e7f9c1d183f7b2856d5de44a6dcc9eabb865fc1c Mon Sep 17 00:00:00 2001 From: waner Date: Thu, 14 May 2026 14:53:35 +0800 Subject: [PATCH] =?UTF-8?q?fix=EF=BC=9A=E9=85=8D=E7=BD=AE=E5=8A=A0?= =?UTF-8?q?=E5=AF=86=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...figDecryptingEnvironmentPostProcessor.java | 12 +++++-- .../ConfigurableInitStepExecutor.java | 18 +++++----- ...ecryptingEnvironmentPostProcessorTest.java | 33 +++++++++++++++++++ .../restore/RestoreCommandInvokerTest.java | 7 ++-- 4 files changed, 55 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/cisd/tms/common/crypto/config/ConfigDecryptingEnvironmentPostProcessor.java b/src/main/java/com/cisd/tms/common/crypto/config/ConfigDecryptingEnvironmentPostProcessor.java index 2c22eff..32b9d1d 100644 --- a/src/main/java/com/cisd/tms/common/crypto/config/ConfigDecryptingEnvironmentPostProcessor.java +++ b/src/main/java/com/cisd/tms/common/crypto/config/ConfigDecryptingEnvironmentPostProcessor.java @@ -1,7 +1,9 @@ package com.cisd.tms.common.crypto.config; import java.util.LinkedHashMap; +import java.util.HashSet; import java.util.Map; +import java.util.Set; import org.springframework.boot.SpringApplication; import org.springframework.boot.env.EnvironmentPostProcessor; import org.springframework.core.Ordered; @@ -27,14 +29,18 @@ public class ConfigDecryptingEnvironmentPostProcessor implements EnvironmentPost @Override public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { Map decryptedValues = new LinkedHashMap<>(); + Set visitedPropertyNames = new HashSet<>(); for (PropertySource propertySource : environment.getPropertySources()) { if (!(propertySource instanceof EnumerablePropertySource enumerablePropertySource)) { continue; } for (String propertyName : enumerablePropertySource.getPropertyNames()) { - Object rawValue = enumerablePropertySource.getProperty(propertyName); - if (rawValue instanceof String stringValue && encryptor.isEncrypted(stringValue)) { - decryptedValues.putIfAbsent(propertyName, encryptor.decryptIfNecessary(stringValue)); + if (!visitedPropertyNames.add(propertyName)) { + continue; + } + String resolvedValue = environment.getProperty(propertyName); + if (encryptor.isEncrypted(resolvedValue)) { + decryptedValues.put(propertyName, encryptor.decryptIfNecessary(resolvedValue)); } } } diff --git a/src/main/java/com/cisd/tms/modules/init/executor/ConfigurableInitStepExecutor.java b/src/main/java/com/cisd/tms/modules/init/executor/ConfigurableInitStepExecutor.java index a6713fb..b78c508 100644 --- a/src/main/java/com/cisd/tms/modules/init/executor/ConfigurableInitStepExecutor.java +++ b/src/main/java/com/cisd/tms/modules/init/executor/ConfigurableInitStepExecutor.java @@ -93,7 +93,7 @@ public class ConfigurableInitStepExecutor implements InitStepExecutor { Process process = null; try { - ProcessBuilder processBuilder = new ProcessBuilder("/bin/zsh", "-lc", command); + ProcessBuilder processBuilder = new ProcessBuilder("/bin/bash", "-lc", command); processBuilder.redirectErrorStream(true); processBuilder.redirectOutput(logFile.toFile()); process = processBuilder.start(); @@ -897,7 +897,7 @@ public class ConfigurableInitStepExecutor implements InitStepExecutor { try { int code = runCommand( logFile, - Arrays.asList("/bin/zsh", "-lc", applyCommand), + Arrays.asList("/bin/bash", "-lc", applyCommand), false, loadDir, buildStandardDbApplyEnvironment(loadDir, context) @@ -1031,7 +1031,7 @@ public class ConfigurableInitStepExecutor implements InitStepExecutor { try { int code = runCommand( logFile, - Arrays.asList("/bin/zsh", "-lc", command), + Arrays.asList("/bin/bash", "-lc", command), false, workingDirectory, buildStandardStartEnvironment(stepName) @@ -1070,7 +1070,7 @@ public class ConfigurableInitStepExecutor implements InitStepExecutor { try { int code = runCommand( logFile, - Arrays.asList("/bin/zsh", "-lc", command), + Arrays.asList("/bin/bash", "-lc", command), false, null, buildStandardDbApplyEnvironment(null, context) @@ -1246,7 +1246,7 @@ public class ConfigurableInitStepExecutor implements InitStepExecutor { .toAbsolutePath(); Path logFile = prepareLogPath(task, step); try { - int code = runCommand(logFile, Arrays.asList("/bin/zsh", "-lc", command), false, scriptDir); + int code = runCommand(logFile, Arrays.asList("/bin/bash", "-lc", command), false, scriptDir); if (code != 0) { return InitStepExecutionResult.failure("cae_update 执行失败", code, logFile.toString()); } @@ -1315,7 +1315,7 @@ public class ConfigurableInitStepExecutor implements InitStepExecutor { Path logFile = prepareLogPath(task, step); try { - int code = runCommand(logFile, Arrays.asList("/bin/zsh", "-lc", command), false); + int code = runCommand(logFile, Arrays.asList("/bin/bash", "-lc", command), false); if (code != 0) { return InitStepExecutionResult.failure("restart_tlq 执行失败", code, logFile.toString()); } @@ -2246,7 +2246,7 @@ public class ConfigurableInitStepExecutor implements InitStepExecutor { try { int code = runCommand( logFile, - Arrays.asList("/bin/zsh", "-lc", "pgrep -f " + quoteShellValue(pattern) + " >/dev/null"), + Arrays.asList("/bin/bash", "-lc", "pgrep -f " + quoteShellValue(pattern) + " >/dev/null"), true ); if (code == 0) { @@ -2270,7 +2270,7 @@ public class ConfigurableInitStepExecutor implements InitStepExecutor { try { int code = runCommand( logFile, - Arrays.asList("/bin/zsh", "-lc", command), + Arrays.asList("/bin/bash", "-lc", command), true, null, buildStandardDbApplyEnvironment(null, context) @@ -2301,7 +2301,7 @@ public class ConfigurableInitStepExecutor implements InitStepExecutor { private void verifyRabbitObjectAbsent(Path logFile, String shellCommand, String message, List failures) throws IOException { try { - int code = runCommand(logFile, Arrays.asList("/bin/zsh", "-lc", shellCommand), true); + int code = runCommand(logFile, Arrays.asList("/bin/bash", "-lc", shellCommand), true); if (code == 0) { failures.add(message); } diff --git a/src/test/java/com/cisd/tms/common/crypto/config/ConfigDecryptingEnvironmentPostProcessorTest.java b/src/test/java/com/cisd/tms/common/crypto/config/ConfigDecryptingEnvironmentPostProcessorTest.java index 9e63a2a..b7de3cb 100644 --- a/src/test/java/com/cisd/tms/common/crypto/config/ConfigDecryptingEnvironmentPostProcessorTest.java +++ b/src/test/java/com/cisd/tms/common/crypto/config/ConfigDecryptingEnvironmentPostProcessorTest.java @@ -3,7 +3,9 @@ package com.cisd.tms.common.crypto.config; import java.util.Map; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import org.springframework.boot.WebApplicationType; import org.springframework.boot.SpringApplication; +import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.StandardEnvironment; @@ -25,4 +27,35 @@ class ConfigDecryptingEnvironmentPostProcessorTest { Assertions.assertEquals("db-password", environment.getProperty("spring.datasource.password")); Assertions.assertEquals("8080", environment.getProperty("server.port")); } + + @Test + void decryptsEncryptedPlaceholderDefaultValuesBeforeBinding() { + ConfigTextEncryptor encryptor = ConfigTextEncryptor.withDefaultHardcodedKey(); + String encryptedUsername = encryptor.encrypt("db-user"); + StandardEnvironment environment = new StandardEnvironment(); + environment.getPropertySources().addFirst(new MapPropertySource("test", Map.of( + "spring.datasource.username", "${TMS_DB_USER:" + encryptedUsername + "}" + ))); + + new ConfigDecryptingEnvironmentPostProcessor() + .postProcessEnvironment(environment, new SpringApplication(Object.class)); + + Assertions.assertEquals("db-user", environment.getProperty("spring.datasource.username")); + } + + @Test + void springBootAutoLoadsPostProcessorFromFactories() { + ConfigTextEncryptor encryptor = ConfigTextEncryptor.withDefaultHardcodedKey(); + String encryptedUsername = encryptor.encrypt("dev-user"); + SpringApplication application = new SpringApplication(Object.class); + application.setWebApplicationType(WebApplicationType.NONE); + application.setDefaultProperties(Map.of( + "tms.crypto.test.username", encryptedUsername, + "spring.main.banner-mode", "off" + )); + + try (ConfigurableApplicationContext context = application.run()) { + Assertions.assertEquals("dev-user", context.getEnvironment().getProperty("tms.crypto.test.username")); + } + } } diff --git a/src/test/java/com/cisd/tms/modules/backup/restore/RestoreCommandInvokerTest.java b/src/test/java/com/cisd/tms/modules/backup/restore/RestoreCommandInvokerTest.java index eb73a3a..784f1ef 100644 --- a/src/test/java/com/cisd/tms/modules/backup/restore/RestoreCommandInvokerTest.java +++ b/src/test/java/com/cisd/tms/modules/backup/restore/RestoreCommandInvokerTest.java @@ -18,7 +18,7 @@ class RestoreCommandInvokerTest { Path logFile = tempDir.resolve("restore.log"); int exitCode = invoker.run( - List.of("/bin/zsh", "-lc", "printf 'hello-restore'"), + List.of("/bin/bash", "-lc", "printf 'hello-restore'"), logFile, tempDir ); @@ -35,7 +35,7 @@ class RestoreCommandInvokerTest { long started = System.nanoTime(); int exitCode = invoker.run( - List.of("/bin/zsh", "-lc", "sleep 2; printf 'done'"), + List.of("/bin/bash", "-lc", "sleep 2; printf 'done'"), logFile, tempDir ); @@ -46,12 +46,13 @@ class RestoreCommandInvokerTest { } private static void waitUntilLogContains(Path logFile, String expected) throws Exception { - long deadline = System.currentTimeMillis() + 1000; + long deadline = System.currentTimeMillis() + 5000; while (System.currentTimeMillis() < deadline) { if (Files.isRegularFile(logFile) && Files.readString(logFile).contains(expected)) { return; } Thread.sleep(20); } + Assertions.fail("restore log did not contain expected text: " + expected); } }