Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: demo of wrong placeholder resolution in secretmanager sample #3439

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
import java.util.StringTokenizer;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

import org.apache.commons.lang3.SystemUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.jupiter.api.extension.AfterAllCallback;
Expand All @@ -55,9 +57,7 @@
*/
public class PubSubEmulator implements BeforeAllCallback, AfterAllCallback, ParameterResolver {

private static final Path EMULATOR_CONFIG_DIR =
Paths.get(System.getProperty("user.home"))
.resolve(Paths.get(".config", "gcloud", "emulators", "pubsub"));
private static final Path EMULATOR_CONFIG_DIR = getEmulatorConfigDir();

private static final String ENV_FILE_NAME = "env.yaml";

Expand Down Expand Up @@ -198,13 +198,13 @@ private void startEmulator() throws IOException, InterruptedException {

try {
this.emulatorProcess =
new ProcessBuilder("gcloud", "beta", "emulators", "pubsub", "start").start();
new ProcessBuilder("bash", "-c", "gcloud beta emulators pubsub start").start();
} catch (IOException ex) {
fail("Gcloud not found; leaving host/port uninitialized.");
}

if (configPresent) {
updateConfig(watchService);
waitForConfigUpdates(watchService);
watchService.close();
} else {
createConfig();
Expand Down Expand Up @@ -240,7 +240,7 @@ private void determineHostPort() throws IOException, InterruptedException {
private void createConfig() {
await()
.pollInterval(Duration.ofSeconds(1))
.atMost(Duration.ofSeconds(5))
.atMost(Duration.ofSeconds(10))
.untilAsserted(() -> assertThat(EMULATOR_CONFIG_PATH.toFile()).exists());
}

Expand All @@ -252,7 +252,7 @@ private void createConfig() {
* @throws InterruptedException which should interrupt the peaceful slumber and bubble up to fail
* the test.
*/
private void updateConfig(WatchService watchService) throws InterruptedException {
private void waitForConfigUpdates(WatchService watchService) throws InterruptedException {
int attempts = 10;
while (--attempts >= 0) {
WatchKey key = watchService.poll(1000, TimeUnit.MILLISECONDS);
Expand Down Expand Up @@ -285,4 +285,13 @@ private void killProcess(String pid) {
LOGGER.warn("Failed to clean up PID " + pid);
}
}

private static Path getEmulatorConfigDir() {
if (SystemUtils.IS_OS_WINDOWS) {
return Paths.get(System.getenv("APPDATA"))
.resolve(Paths.get("gcloud", "emulators", "pubsub"));
}
return Paths.get(System.getProperty("user.home"))
.resolve(Paths.get(".config", "gcloud", "emulators", "pubsub"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.cloud.stream.binder.AbstractBinderTests;
import org.springframework.cloud.stream.binder.Binding;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,17 @@ public class SecretManagerWebController {
// in property files.
@Value("${${sm://application-fake}:DEFAULT}")
private String defaultSecret;

// Application secrets can be accessed using @Value syntax.
@Value("${sm://application-secret}")
private String appSecretFromValue;

@Value("${sm://application-secret:DEFAULT}")
private String appSecretFromValueWithDefault;

@Value("${${sm://application-secret}:DEFAULT}")
private String appSecretFromValueWithDefaultUsingNestedPlaceholder;

public SecretManagerWebController(SecretManagerTemplate secretManagerTemplate,
SecretConfiguration configuration
) {
Expand All @@ -57,6 +64,8 @@ public SecretManagerWebController(SecretManagerTemplate secretManagerTemplate,
@GetMapping("/")
public ModelAndView renderIndex(ModelMap map) {
map.put("applicationDefaultSecret", defaultSecret);
map.put("appSecretFromValueWithDefault", appSecretFromValueWithDefault);
map.put("appSecretFromValueWithDefaultUsingNestedPlaceholder", appSecretFromValueWithDefaultUsingNestedPlaceholder);
map.put(APPLICATION_SECRET_FROM_VALUE, appSecretFromValue);
map.put("applicationSecretFromConfigurationProperties", configuration.getSecret());
return new ModelAndView(INDEX_PAGE, map);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ <h3>Secret Manager Property Source</h3>
<br/>
<b>Default Application secret if not found:</b> <i>[[${applicationDefaultSecret}]]</i><br/>
<b>Application secret from @Value:</b> <i>[[${applicationSecretFromValue}]]</i><br/>
<b>Application secret from @Value with default value:</b> <i>[[${appSecretFromValueWithDefault}]]</i><br/>
<b>Application secret from @Value with default value using nested placeholder:</b> <i>[[${appSecretFromValueWithDefaultUsingNestedPlaceholder}]]</i><br/>
<b>Application secret from @ConfigurationProperties:</b> <i>[[${applicationSecretFromConfigurationProperties}]]</i>
</div>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ void testApplicationStartupSecretLoadsCorrectly() {
assertThat(response.getStatusCode().is2xxSuccessful()).isTrue();
assertThat(response.getBody())
.contains("<b>Application secret from @Value:</b> <i>" + SECRET_CONTENT + "</i>");
assertThat(response.getBody())
.contains("<b>Application secret from @Value with default value:</b> <i>" + SECRET_CONTENT + "</i>");
assertThat(response.getBody())
.contains("<b>Application secret from @Value with default value using nested placeholder:</b> <i>" + SECRET_CONTENT + "</i>");
assertThat(response.getBody())
.contains(
"<b>Application secret from @ConfigurationProperties:</b> <i>"
Expand Down
Loading