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

Environment variable resolution in configs #1198

Merged
merged 2 commits into from
Jun 18, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
import com.linkedin.kafka.cruisecontrol.config.KafkaCruiseControlConfig;
import com.linkedin.kafka.cruisecontrol.config.constants.WebServerConfig;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import static com.linkedin.kafka.cruisecontrol.KafkaCruiseControlUtils.readConfig;

/**
* The main class to run Kafka Cruise Control.
Expand All @@ -37,14 +34,6 @@ public static void main(String[] args) throws Exception {
app.start();
}

private static KafkaCruiseControlConfig readConfig(String propertiesFile) throws IOException {
Properties props = new Properties();
try (InputStream propStream = new FileInputStream(propertiesFile)) {
props.load(propStream);
}
return new KafkaCruiseControlConfig(props);
}

private static Integer parsePort(String[] args, KafkaCruiseControlConfig config) {
if (args.length > 1) {
return Integer.parseInt(args[1]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,24 @@
package com.linkedin.kafka.cruisecontrol;

import com.linkedin.kafka.cruisecontrol.analyzer.AnalyzerUtils;
import com.linkedin.kafka.cruisecontrol.analyzer.goals.Goal;
import com.linkedin.kafka.cruisecontrol.analyzer.goals.PreferredLeaderElectionGoal;
import com.linkedin.kafka.cruisecontrol.config.EnvConfigProvider;
import com.linkedin.kafka.cruisecontrol.config.KafkaCruiseControlConfig;
import com.linkedin.kafka.cruisecontrol.config.constants.AnalyzerConfig;
import com.linkedin.kafka.cruisecontrol.monitor.ModelCompletenessRequirements;
import com.linkedin.kafka.cruisecontrol.monitor.task.LoadMonitorTaskRunner;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import com.linkedin.kafka.cruisecontrol.analyzer.goals.Goal;
import java.util.List;
import java.util.Map;
import java.util.Set;
import kafka.zk.KafkaZkClient;
import org.apache.kafka.clients.admin.AdminClient;
import org.apache.kafka.clients.admin.AdminClientConfig;
import org.apache.kafka.clients.admin.DescribeLogDirsResult;
import java.util.TimeZone;
import java.util.stream.Collectors;
import org.apache.kafka.common.Cluster;
import org.apache.kafka.common.Node;
import org.apache.kafka.common.PartitionInfo;
import org.apache.kafka.common.TopicPartition;
import org.apache.kafka.common.config.ConfigException;
import org.apache.kafka.common.config.SslConfigs;
import org.apache.kafka.common.config.SaslConfigs;
import org.apache.kafka.common.config.SslConfigs;
import org.apache.kafka.common.message.MetadataResponseData;
import org.apache.kafka.common.record.RecordBatch;
import org.apache.kafka.common.requests.AbstractResponse;
Expand All @@ -41,6 +31,22 @@
import org.apache.kafka.common.utils.SystemTime;
import scala.Option;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.TimeZone;
import java.util.stream.Collectors;

import static com.linkedin.kafka.cruisecontrol.servlet.parameters.ParameterUtils.SKIP_HARD_GOAL_CHECK_PARAM;


Expand Down Expand Up @@ -541,4 +547,20 @@ public static Map<String, Double> balancednessCostByGoal(List<Goal> goals, doubl

return balancednessCostByGoal;
}

/**
* Reads the configuration file, parses and validates the configs.
Copy link
Collaborator

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?

Copy link
Contributor Author

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?

* @param propertiesFile is the file containing the Cruise Control configuration.
* @return a parsed {@link KafkaCruiseControlConfig}
* @throws IOException if the configuration file can't be read.
*/
public static KafkaCruiseControlConfig readConfig(String propertiesFile) throws IOException {
Properties props = new Properties();
try (InputStream propStream = new FileInputStream(propertiesFile)) {
props.put("config.providers", "env");
props.put("config.providers.env.class", EnvConfigProvider.class.getName());
viktorsomogyi marked this conversation as resolved.
Show resolved Hide resolved
props.load(propStream);
}
return new KafkaCruiseControlConfig(props);
}
}
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;
}
}
}
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);
}
}
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