Skip to content

Commit

Permalink
[pulsar-broker] validate namespace isolation policy regex before upda…
Browse files Browse the repository at this point in the history
…ting (apache#8804)

[pulsar-broker] validate namespace isolation policy regex before updating
  • Loading branch information
rdhabalia authored Dec 23, 2020
1 parent e58a906 commit ab9c77a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,35 @@ public void clusterNamespaceIsolationPolicies() throws PulsarAdminException {
LOG.warn("TEST FAILED [{}]", e.getMessage());
throw e;
}

// validate regex: invlid regex for primary and seconday
NamespaceIsolationData nsRegexPolicy = new NamespaceIsolationData();
nsRegexPolicy.namespaces = new ArrayList<String>();
nsRegexPolicy.namespaces.add("other/use/other.*");
nsRegexPolicy.primary = new ArrayList<String>();
nsRegexPolicy.primary.add("prod1-broker[45-46].messaging.use.example.com");
nsRegexPolicy.auto_failover_policy = new AutoFailoverPolicyData();
nsRegexPolicy.auto_failover_policy.policy_type = AutoFailoverPolicyType.min_available;
nsRegexPolicy.auto_failover_policy.parameters = new HashMap<String, String>();
nsRegexPolicy.auto_failover_policy.parameters.put("min_limit", "1");
nsRegexPolicy.auto_failover_policy.parameters.put("usage_threshold", "100");
try {
admin.clusters().createNamespaceIsolationPolicy("test", "invalid_primary", nsRegexPolicy);
fail("should have failed with invalid regex");
}catch (PulsarAdminException e) {
//Ok
}

nsRegexPolicy.primary.add("prod1-broker[4-5].messaging.use.example.com");
nsRegexPolicy.secondary = new ArrayList<String>();
nsRegexPolicy.secondary.add("prod1-broker[45-46].messaging.use.example.com");
try {
admin.clusters().createNamespaceIsolationPolicy("test", "invalid_primary", nsRegexPolicy);
fail("should have failed with invalid regex");
} catch (PulsarAdminException e) {
// Ok
}

}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
import io.swagger.annotations.ApiModelProperty;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import org.apache.commons.lang3.StringUtils;

/**
* The data of namespace isolation configuration.
Expand Down Expand Up @@ -83,10 +87,25 @@ public boolean equals(Object obj) {

public void validate() {
checkArgument(namespaces != null && !namespaces.isEmpty() && primary != null && !primary.isEmpty()
&& secondary != null && auto_failover_policy != null);
&& validateRegex(primary) && secondary != null && validateRegex(secondary) && auto_failover_policy != null);
auto_failover_policy.validate();
}

private boolean validateRegex(List<String> policies) {
if (policies != null && !policies.isEmpty()) {
policies.forEach((policy) -> {
try {
if (StringUtils.isNotBlank(policy)) {
Pattern.compile(policy);
}
} catch (PatternSyntaxException exception) {
throw new IllegalArgumentException("invalid policy regex " + policy);
}
});
}
return true;
}

@Override
public String toString() {
return String.format("namespaces=%s primary=%s secondary=%s auto_failover_policy=%s", namespaces, primary,
Expand Down

0 comments on commit ab9c77a

Please sign in to comment.