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

KAFKA-8774: Regex can be found anywhere in config value #7197

Merged
merged 3 commits into from Aug 13, 2019
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
Expand Up @@ -598,12 +598,13 @@ public static List<Map<String, String>> reverseTransform(String connName,
return result;
}

private static Set<String> keysWithVariableValues(Map<String, String> rawConfig, Pattern pattern) {
// Visible for testing
static Set<String> keysWithVariableValues(Map<String, String> rawConfig, Pattern pattern) {
Set<String> keys = new HashSet<>();
for (Map.Entry<String, String> config : rawConfig.entrySet()) {
if (config.getValue() != null) {
Matcher matcher = pattern.matcher(config.getValue());
if (matcher.matches()) {
if (matcher.find()) {
keys.add(config.getKey());
}
}
Expand Down
Expand Up @@ -20,6 +20,7 @@
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.config.ConfigDef;
import org.apache.kafka.common.config.ConfigException;
import org.apache.kafka.common.config.ConfigTransformer;
import org.apache.kafka.common.config.SaslConfigs;
import org.apache.kafka.common.security.oauthbearer.internals.unsecured.OAuthBearerUnsecuredLoginCallbackHandler;
import org.apache.kafka.connect.connector.ConnectRecord;
Expand Down Expand Up @@ -62,6 +63,7 @@
import java.util.Set;
import java.util.stream.Collectors;

import static org.apache.kafka.connect.runtime.AbstractHerder.keysWithVariableValues;
import static org.powermock.api.easymock.PowerMock.verifyAll;
import static org.powermock.api.easymock.PowerMock.replayAll;
import static org.easymock.EasyMock.strictMock;
Expand Down Expand Up @@ -474,6 +476,25 @@ public void testReverseTransformConfigs() {
assertFalse(reverseTransformed.get(0).containsKey(TEST_KEY3));
}

@Test
public void testConfigProviderRegex() {
testConfigProviderRegex("${file:/tmp/somefile.txt:somevar}");
testConfigProviderRegex("\"${file:/tmp/somefile.txt:somevar}\"");
testConfigProviderRegex("plain.PlainLoginModule required username=\"${file:/tmp/somefile.txt:somevar}\"");
testConfigProviderRegex("plain.PlainLoginModule required username=${file:/tmp/somefile.txt:somevar}");
rhauch marked this conversation as resolved.
Show resolved Hide resolved
testConfigProviderRegex("plain.PlainLoginModule required username", false);
}

private void testConfigProviderRegex(String rawConnConfig) {
testConfigProviderRegex(rawConnConfig, true);
}

private void testConfigProviderRegex(String rawConnConfig, boolean expected) {
Set<String> keys = keysWithVariableValues(Collections.singletonMap("key", rawConnConfig), ConfigTransformer.DEFAULT_PATTERN);
boolean actual = keys != null && !keys.isEmpty() && keys.contains("key");
assertEquals(String.format("%s should have matched regex", rawConnConfig), expected, actual);
}

private AbstractHerder createConfigValidationHerder(Class<? extends Connector> connectorClass,
ConnectorClientConfigOverridePolicy connectorClientConfigOverridePolicy) {

Expand Down