Repository: apache/kafka
File: connect/mirror/src/main/java/org/apache/kafka/connect/mirror/DefaultConfigPropertyFilter.java (Java)

Code before:
import org.apache.kafka.common.config.ConfigDef;
import org.apache.kafka.common.config.ConfigDef.Importance;
import org.apache.kafka.common.config.ConfigDef.Type;

import java.util.Map;
import java.util.regex.Pattern;

/** Filters excluded property names or regexes. */
public class DefaultConfigPropertyFilter implements ConfigPropertyFilter {

    public static final String CONFIG_PROPERTIES_EXCLUDE_CONFIG = "config.properties.exclude";
    public static final String USE_DEFAULTS_FROM = "use.defaults.from";
    private static final String USE_DEFAULTS_FROM_DOC = "Which cluster's defaults (source or target) to use "
                                                        + "when syncing topic configurations that have default values.";
    private static final String USE_DEFAULTS_FROM_DEFAULT = "target";

    private static final String CONFIG_PROPERTIES_EXCLUDE_DOC = "List of topic configuration properties and/or regexes "
                                                                + "that should not be replicated.";
    public static final String CONFIG_PROPERTIES_EXCLUDE_DEFAULT = "follower\\.replication\\.throttled\\.replicas, "
                                                                   + "leader\\.replication\\.throttled\\.replicas, "
                                                                   + "message\\.timestamp\\.difference\\.max\\.ms, "
                                                                   + "message\\.timestamp\\.type, "
                                                                   + "unclean\\.leader\\.election\\.enable, "
                                                                   + "min\\.insync\\.replicas";
    private Pattern excludePattern = MirrorUtils.compilePatternList(CONFIG_PROPERTIES_EXCLUDE_DEFAULT);
    private String useDefaultsFrom = USE_DEFAULTS_FROM_DEFAULT;

    @Override
    public void configure(Map<String, ?> props) {
        ConfigPropertyFilterConfig config = new ConfigPropertyFilterConfig(props);
        excludePattern = config.excludePattern();
        useDefaultsFrom = config.useDefaultsFrom();
    }

    private boolean excluded(String prop) {
        return excludePattern != null && excludePattern.matcher(prop).matches();
    }

    @Override
    public boolean shouldReplicateConfigProperty(String prop) {
        return !excluded(prop);
    }

    @Override
    public boolean shouldReplicateSourceDefault(String prop) {
        return useDefaultsFrom.equals("source");
    }

    static class ConfigPropertyFilterConfig extends AbstractConfig {

        static final ConfigDef DEF = new ConfigDef()

Review comment (security):
Removing double backslash before full stops is fine in practice. An unescaped full stop would only cause a false match if there were a config property name identical except with a different character where a dot is (e.g. `minXinsync.replicas`), which doesn't exist in Kafka's config space. The readability improvement outweighs the theoretical imprecision.

Code after:
import org.apache.kafka.common.config.ConfigDef;
import org.apache.kafka.common.config.ConfigDef.Importance;
import org.apache.kafka.common.config.ConfigDef.Type;

import java.util.Map;
import java.util.regex.Pattern;

/** Filters excluded property names or regexes. */
public class DefaultConfigPropertyFilter implements ConfigPropertyFilter {

    public static final String CONFIG_PROPERTIES_EXCLUDE_CONFIG = "config.properties.exclude";
    public static final String USE_DEFAULTS_FROM = "use.defaults.from";
    private static final String USE_DEFAULTS_FROM_DOC = "Which cluster's defaults (source or target) to use "
                                                        + "when syncing topic configurations that have default values.";
    private static final String USE_DEFAULTS_FROM_DEFAULT = "target";

    private static final String CONFIG_PROPERTIES_EXCLUDE_DOC = "List of topic configuration properties and/or regexes "
                                                                + "that should not be replicated.";
    public static final String CONFIG_PROPERTIES_EXCLUDE_DEFAULT = "follower.replication.throttled.replicas, "
                                                                   + "leader.replication.throttled.replicas, "
                                                                   + "message.timestamp.difference.max.ms, "
                                                                   + "log.message.timestamp.before.max.ms, "
                                                                   + "log.message.timestamp.after.max.ms, "
                                                                   + "message.timestamp.type, "
                                                                   + "unclean.leader.election.enable, "
                                                                   + "min.insync.replicas";
    private Pattern excludePattern = MirrorUtils.compilePatternList(CONFIG_PROPERTIES_EXCLUDE_DEFAULT);
    private String useDefaultsFrom = USE_DEFAULTS_FROM_DEFAULT;

    @Override
    public void configure(Map<String, ?> props) {
        ConfigPropertyFilterConfig config = new ConfigPropertyFilterConfig(props);
        excludePattern = config.excludePattern();
        useDefaultsFrom = config.useDefaultsFrom();
    }

    private boolean excluded(String prop) {
        return excludePattern != null && excludePattern.matcher(prop).matches();
    }

    @Override
    public boolean shouldReplicateConfigProperty(String prop) {
        return !excluded(prop);
    }

    @Override
    public boolean shouldReplicateSourceDefault(String prop) {
        return useDefaultsFrom.equals("source");
    }

    static class ConfigPropertyFilterConfig extends AbstractConfig {