-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow setting config values from env variables
- Loading branch information
1 parent
e86bedf
commit 526fb52
Showing
4 changed files
with
136 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
gateway-ha/src/main/java/io/trino/gateway/ha/util/ConfigurationUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.trino.gateway.ha.util; | ||
|
||
import java.util.Map; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import static java.lang.String.format; | ||
import static java.util.regex.Matcher.quoteReplacement; | ||
|
||
public final class ConfigurationUtils | ||
{ | ||
private static final Pattern ENV_PATTERN = Pattern.compile("\\$\\{ENV:([a-zA-Z][a-zA-Z0-9_-]*)}"); | ||
|
||
private ConfigurationUtils() {} | ||
|
||
public static String replaceEnvironmentVariables(String original) | ||
{ | ||
return replaceEnvironmentVariables(original, System.getenv()); | ||
} | ||
|
||
public static String replaceEnvironmentVariables(String original, Map<String, String> environment) | ||
{ | ||
StringBuilder result = new StringBuilder(); | ||
Matcher matcher = ENV_PATTERN.matcher(original); | ||
while (matcher.find()) { | ||
String envName = matcher.group(1); | ||
String envValue = environment.get(envName); | ||
if (envValue == null) { | ||
throw new IllegalArgumentException(format("Configuration references unset environment variable '%s'", envName)); | ||
} | ||
matcher.appendReplacement(result, quoteReplacement(envValue)); | ||
} | ||
matcher.appendTail(result); | ||
return result.toString(); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
gateway-ha/src/test/java/io/trino/gateway/ha/util/TestConfigurationUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.trino.gateway.ha.util; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Map; | ||
|
||
import static io.trino.gateway.ha.util.ConfigurationUtils.replaceEnvironmentVariables; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
final class TestConfigurationUtils | ||
{ | ||
private static final String config = """ | ||
serverConfig: | ||
http-server.https.keystore.path: certificate.pem | ||
http-server.https.keystore.key: ${ENV:KEYSTORE_KEY} | ||
presetUsers: | ||
api: | ||
password: ${ENV:API_PASSWORD} | ||
privileges: API | ||
"""; | ||
private static final String expected = """ | ||
serverConfig: | ||
http-server.https.keystore.path: certificate.pem | ||
http-server.https.keystore.key: keystore_12345 | ||
presetUsers: | ||
api: | ||
password: api_passw0rd | ||
privileges: API | ||
"""; | ||
|
||
@Test | ||
void testReplaceEnvironmentVariables() | ||
{ | ||
Map<String, String> env = ImmutableMap.<String, String>builder() | ||
.put("KEYSTORE_KEY", "keystore_12345") | ||
.put("API_PASSWORD", "api_passw0rd") | ||
.build(); | ||
String result = replaceEnvironmentVariables(config, env); | ||
assertThat(result).isEqualTo(expected); | ||
} | ||
|
||
@Test | ||
void testMissingEnvironmentVariables() | ||
{ | ||
assertThatThrownBy(() -> replaceEnvironmentVariables(config, ImmutableMap.of())) | ||
.hasMessageStartingWith("Configuration references unset environment variable"); | ||
} | ||
} |