Skip to content
Merged
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
19 changes: 10 additions & 9 deletions src/main/java/me/itzg/helpers/env/SimplePlaceholders.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ public String processPlaceholders(String value) {
do {
final String type = Optional.ofNullable(m.group("type"))
.orElse("env");
final String replacement = buildPlaceholderReplacement(type, m.group("var"), m.group());
final String replacement = buildPlaceholderReplacement(type, m.group("var"));

if (replacement == null) {
continue;
}
Comment on lines +44 to +46
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good to know, that simply not calling appendReplacement effectively leaves that matched section unaltered.


m.appendReplacement(sb, replacement);
} while (m.find());
Expand All @@ -50,18 +54,15 @@ public String processPlaceholders(String value) {
return value;
}

private String buildPlaceholderReplacement(String type, String var, String fallback) {
private String buildPlaceholderReplacement(String type, String var) {
log.debug("Building placeholder replacement from type={} with var='{}'", type, var);
switch (type) {
case "env":
final String result = environmentVariablesProvider.get(var);
if (result != null) {
return result;
}
else {
if (result == null) {
log.warn("Unable to resolve environment variable {}", var);
return fallback;
}
return result;

case "date":
case "time":
Expand All @@ -70,11 +71,11 @@ private String buildPlaceholderReplacement(String type, String var, String fallb
return ZonedDateTime.now(clock).format(f);
} catch (IllegalArgumentException e) {
log.error("Invalid date/time format in {}", var, e);
return fallback;
return null;
}
}

return fallback;
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,28 @@ void processesPlaceholders(String motd, String expected) {

}

@Test
void complexSecretsProcessedCorrectly() throws Exception {
final String secret = ":xDmW!T5Y%Jjam;$L9adz-tf%4BuhN,z#64pGx+;3R6^W$#PgGv!Nm-*)A}3^*/js*&)#";

final int exitCode = new CommandLine(new SetPropertiesCommand()
.setEnvironmentVariablesProvider(MappedEnvVarProvider.of(
"RCON_PASSWORD", secret
))
)
.execute(
"--definitions", definitionsFile.toString(),
propertiesFile.toString()
);

assertThat(exitCode).isEqualTo(ExitCode.OK);

final Properties properties = loadProperties();

assertThat(properties).containsEntry("rcon.password", secret);
assertPropertiesEqualExcept(properties, "rcon.password");
}

private void assertPropertiesEqualExcept(Properties properties, String... propertiesToIgnore) {
final HashSet<Object> actualKeys = new HashSet<>(properties.keySet());
Arrays.asList(propertiesToIgnore).forEach(actualKeys::remove);
Expand Down
3 changes: 3 additions & 0 deletions src/test/resources/properties/property-definitions.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@
"compression-threshold": {
"env": "COMPRESSION_THRESHOLD"
},
"rcon.password": {
"env": "RCON_PASSWORD"
},
"level-name": {
"env": "LEVEL_NAME"
},
Expand Down