Skip to content

Commit 673fa6c

Browse files
authored
Pr/liven/fix placeholder subtitution (#531)
1 parent 8093559 commit 673fa6c

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

src/main/java/me/itzg/helpers/env/SimplePlaceholders.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ public String processPlaceholders(String value) {
3939
do {
4040
final String type = Optional.ofNullable(m.group("type"))
4141
.orElse("env");
42-
final String replacement = buildPlaceholderReplacement(type, m.group("var"), m.group());
42+
final String replacement = buildPlaceholderReplacement(type, m.group("var"));
43+
44+
if (replacement == null) {
45+
continue;
46+
}
4347

4448
m.appendReplacement(sb, replacement);
4549
} while (m.find());
@@ -50,18 +54,15 @@ public String processPlaceholders(String value) {
5054
return value;
5155
}
5256

53-
private String buildPlaceholderReplacement(String type, String var, String fallback) {
57+
private String buildPlaceholderReplacement(String type, String var) {
5458
log.debug("Building placeholder replacement from type={} with var='{}'", type, var);
5559
switch (type) {
5660
case "env":
5761
final String result = environmentVariablesProvider.get(var);
58-
if (result != null) {
59-
return result;
60-
}
61-
else {
62+
if (result == null) {
6263
log.warn("Unable to resolve environment variable {}", var);
63-
return fallback;
6464
}
65+
return result;
6566

6667
case "date":
6768
case "time":
@@ -70,11 +71,11 @@ private String buildPlaceholderReplacement(String type, String var, String fallb
7071
return ZonedDateTime.now(clock).format(f);
7172
} catch (IllegalArgumentException e) {
7273
log.error("Invalid date/time format in {}", var, e);
73-
return fallback;
74+
return null;
7475
}
7576
}
7677

77-
return fallback;
78+
return null;
7879
}
7980

8081
}

src/test/java/me/itzg/helpers/properties/SetPropertiesCommandTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,28 @@ void processesPlaceholders(String motd, String expected) {
289289

290290
}
291291

292+
@Test
293+
void complexSecretsProcessedCorrectly() throws Exception {
294+
final String secret = ":xDmW!T5Y%Jjam;$L9adz-tf%4BuhN,z#64pGx+;3R6^W$#PgGv!Nm-*)A}3^*/js*&)#";
295+
296+
final int exitCode = new CommandLine(new SetPropertiesCommand()
297+
.setEnvironmentVariablesProvider(MappedEnvVarProvider.of(
298+
"RCON_PASSWORD", secret
299+
))
300+
)
301+
.execute(
302+
"--definitions", definitionsFile.toString(),
303+
propertiesFile.toString()
304+
);
305+
306+
assertThat(exitCode).isEqualTo(ExitCode.OK);
307+
308+
final Properties properties = loadProperties();
309+
310+
assertThat(properties).containsEntry("rcon.password", secret);
311+
assertPropertiesEqualExcept(properties, "rcon.password");
312+
}
313+
292314
private void assertPropertiesEqualExcept(Properties properties, String... propertiesToIgnore) {
293315
final HashSet<Object> actualKeys = new HashSet<>(properties.keySet());
294316
Arrays.asList(propertiesToIgnore).forEach(actualKeys::remove);

src/test/resources/properties/property-definitions.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@
7272
"compression-threshold": {
7373
"env": "COMPRESSION_THRESHOLD"
7474
},
75+
"rcon.password": {
76+
"env": "RCON_PASSWORD"
77+
},
7578
"level-name": {
7679
"env": "LEVEL_NAME"
7780
},

0 commit comments

Comments
 (0)