Skip to content

Commit

Permalink
Add a TopicNameRegexValidation to validate topic names
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonardoBonacci authored and purbon committed May 1, 2021
1 parent 83afe9a commit dddf8b2
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,4 @@ private Class<?> getValidationClazz(String validationClass) {
return null;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.purbon.kafka.topology.validation.topology;

import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.purbon.kafka.topology.exceptions.ConfigurationException;
import com.purbon.kafka.topology.exceptions.ValidationException;
import com.purbon.kafka.topology.model.Project;
import com.purbon.kafka.topology.model.Topic;
import com.purbon.kafka.topology.model.Topology;
import com.purbon.kafka.topology.validation.TopologyValidation;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigException;
import com.typesafe.config.ConfigFactory;

public class TopicNameRegexValidation implements TopologyValidation {

private static final Logger LOGGER = LogManager.getLogger(TopicNameRegexValidation.class);

private String topicNamePattern;

public TopicNameRegexValidation() throws ConfigurationException {
Config config = ConfigFactory.load();
try {
this.topicNamePattern = config.getString("topology.validations.regexp");
} catch (ConfigException e) {
throw new ConfigurationException(
"TopicNameRegexValidation requires you to define your regex in config 'topology.validations.regexp'");
}

if (StringUtils.isBlank(topicNamePattern)) {
throw new ConfigurationException(
"TopicNameRegexValidation is configured without specifying a topic name pattern. Use config 'topology.validations.regexp'");
}

try {
Pattern.compile(topicNamePattern);
} catch (PatternSyntaxException exception) {
throw new ConfigurationException(
String.format("TopicNameRegexValidation configured with unvalid regex '%s'", topicNamePattern));
}
}

public TopicNameRegexValidation(String pattern) {
this.topicNamePattern = pattern;
LOGGER.info(String.format("Applying Topic Name Regex Validation [%s]", pattern));
}

@Override
public void valid(Topology topology) throws ValidationException {

for (Project project : topology.getProjects()) {
for (Topic topic : project.getTopics()) {
matches(topic.toString());
}
}
}

public void matches(String topicName) throws ValidationException {
if (!topicName.matches(topicNamePattern)) {
String msg =
String.format(
"Topic name '%s' does not follow regex: %s", topicName, topicNamePattern);
throw new ValidationException(msg);
}
}
}

0 comments on commit dddf8b2

Please sign in to comment.