-
Notifications
You must be signed in to change notification settings - Fork 569
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
Environment variable resolution in configs #1198
Merged
efeg
merged 2 commits into
linkedin:master
from
viktorsomogyi:env-var-config-resolution
Jun 18, 2020
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
59 changes: 59 additions & 0 deletions
59
cruise-control/src/main/java/com/linkedin/kafka/cruisecontrol/config/EnvConfigProvider.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,59 @@ | ||
/* | ||
* Copyright 2020 LinkedIn Corp. Licensed under the BSD 2-Clause License (the "License"). See License in the project root for license information. | ||
*/ | ||
|
||
package com.linkedin.kafka.cruisecontrol.config; | ||
|
||
import org.apache.kafka.common.config.ConfigData; | ||
import org.apache.kafka.common.config.ConfigException; | ||
import org.apache.kafka.common.config.provider.ConfigProvider; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
public class EnvConfigProvider implements ConfigProvider { | ||
|
||
private Map<String, String> _preConfiguredEnvironmentVariables; | ||
|
||
@Override | ||
public ConfigData get(String path) { | ||
assertNoPath(path); | ||
return new ConfigData(getenv()); | ||
} | ||
|
||
@Override | ||
public ConfigData get(String path, Set<String> keys) { | ||
assertNoPath(path); | ||
Map<String, String> filtered = new HashMap<>(getenv()); | ||
filtered.keySet().retainAll(keys); | ||
return new ConfigData(filtered); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
// nop | ||
viktorsomogyi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
@Override | ||
public void configure(Map<String, ?> configs) { | ||
_preConfiguredEnvironmentVariables = configs.entrySet() | ||
.stream() | ||
.collect(Collectors.toMap(Map.Entry::getKey, kv -> (String) kv.getValue())); | ||
} | ||
|
||
private static void assertNoPath(String path) { | ||
if (path != null && !path.isEmpty()) { | ||
throw new ConfigException("EnvConfigProvider does not support paths. Found: " + path); | ||
} | ||
} | ||
|
||
private Map<String, String> getenv() { | ||
if (_preConfiguredEnvironmentVariables == null) { | ||
return System.getenv(); | ||
} else { | ||
return _preConfiguredEnvironmentVariables; | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...-control/src/test/java/com/linkedin/kafka/cruisecontrol/config/EnvConfigProviderTest.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,37 @@ | ||
/* | ||
* Copyright 2020 LinkedIn Corp. Licensed under the BSD 2-Clause License (the "License"). See License in the project root for license information. | ||
*/ | ||
|
||
package com.linkedin.kafka.cruisecontrol.config; | ||
|
||
import com.linkedin.kafka.cruisecontrol.KafkaCruiseControlUtils; | ||
import com.linkedin.kafka.cruisecontrol.config.constants.WebServerConfig; | ||
import org.apache.kafka.common.config.types.Password; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
import java.util.Objects; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
public class EnvConfigProviderTest { | ||
|
||
public static final String TEST_PASSWORD = "testPassword123"; | ||
public static final String NOT_SUBSTITUTED_CONFIG = "${env:SSL_KEY_PASSWORD}"; | ||
|
||
@Test | ||
public void testEnvConfigProvider() throws IOException { | ||
KafkaCruiseControlConfig configs = KafkaCruiseControlUtils.readConfig( | ||
Objects.requireNonNull(this.getClass().getClassLoader().getResource("envConfigProviderTest.properties")).getPath()); | ||
viktorsomogyi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Test password substitution | ||
Password actualSslKeystorePassword = configs.getPassword(WebServerConfig.WEBSERVER_SSL_KEYSTORE_PASSWORD_CONFIG); | ||
Password expectedSslKeystorePassword = new Password(TEST_PASSWORD); | ||
assertEquals(expectedSslKeystorePassword, actualSslKeystorePassword); | ||
|
||
// Test when the environment variable is not defined and the password isn't substituted | ||
Password actualSslKeyPassword = configs.getPassword(WebServerConfig.WEBSERVER_SSL_KEY_PASSWORD_CONFIG); | ||
Password expectedSslKeyPassword = new Password(NOT_SUBSTITUTED_CONFIG); | ||
assertEquals(expectedSslKeyPassword, actualSslKeyPassword); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
cruise-control/src/test/resources/envConfigProviderTest.properties
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,7 @@ | ||
bootstrap.servers=localhost:9092 | ||
zookeeper.connect=localhost:2181/ | ||
webserver.ssl.keystore.password=${env:SSL_KEYSTORE_PASSWORD} | ||
webserver.ssl.key.password=${env:SSL_KEY_PASSWORD} | ||
# the only way to initialize this for testing is to pass a preconfigured list of environment variables | ||
# but this shouldn't be configured in production but taken from System.getenv() | ||
config.providers.env.param.SSL_KEYSTORE_PASSWORD=testPassword123 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we also mention the properties that enable configs to be provided out of the configuration file?
Is it intentional to override these hardcoded properties if they were also defined in the configuration file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1st: acked, will do
2nd: well, I treat them as defaults here so if people want to do something else than what CC does by default, then they should be free to. What can I improve here?