inner-1376-supplement: change format to json & inner-1386: bug fix[ treat line-break as normal character] (#2868)

* inner-1376-supplement:  change format to json

Signed-off-by: dcy <dcy10000@gmail.com>

* inner-1376-supplement:  change json struct

Signed-off-by: dcy <dcy10000@gmail.com>

* inner-1376-supplement:  add to show @@sysparam

Signed-off-by: dcy <dcy10000@gmail.com>

* inner-1386: bug fix[ treat line-break as normal character]

Signed-off-by: dcy <dcy10000@gmail.com>
This commit is contained in:
Rico
2021-09-16 17:08:47 +08:00
committed by GitHub
parent 2c6f7ba50f
commit 387475606e
2 changed files with 82 additions and 24 deletions
@@ -151,7 +151,8 @@ public final class ShowSysParam {
paramValues.add(sysConfig.getMaxCharsPerColumn() + "");
paramValues.add(sysConfig.getMaxRowSizeToFile() + "");
paramValues.add(sysConfig.isUseOuterHa() + "");
paramValues.add(sysConfig.isEnableRoutePenetration() + "");
paramValues.add(sysConfig.getRoutePenetrationRules() + "");
for (int i = 0; i < PARAM_NAMES.length; i++) {
RowDataPacket row = new RowDataPacket(FIELD_COUNT);
@@ -252,6 +253,8 @@ public final class ShowSysParam {
"maxCharsPerColumn",
"maxRowSizeToFile",
"useOuterHa",
"enableRoutePenetration",
"routePenetrationRules",
};
private static final String[] PARAM_DESCRIPTION = {
@@ -335,6 +338,8 @@ public final class ShowSysParam {
"The maximum number of characters allowed for per column when load data.The default value is 65535",
"The maximum row size,if over this value,row data will be saved to file when load data.The default value is 10000",
"Whether use outer ha component ",
"Whether enable route penetration",
"The config of route penetration",
};
private static final String[] ISOLATION_LEVELS = {"", "READ_UNCOMMITTED", "READ_COMMITTED", "REPEATABLE_READ", "SERIALIZABLE"};
@@ -8,10 +8,14 @@ package com.actiontech.dble.singleton;
import com.actiontech.dble.DbleServer;
import com.actiontech.dble.config.model.SystemConfig;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
@@ -20,7 +24,7 @@ import java.util.regex.Pattern;
*/
public final class RoutePenetrationManager {
private static final RoutePenetrationManager INSTANCE = new RoutePenetrationManager();
private final List<PenetrationRule> list = Lists.newArrayList();
private List<PenetrationRule> rules = Lists.newArrayList();
private static final Logger LOGGER = LogManager.getLogger(RoutePenetrationManager.class);
private RoutePenetrationManager() {
@@ -34,28 +38,19 @@ public final class RoutePenetrationManager {
public void init() {
final SystemConfig config = DbleServer.getInstance().getConfig().getSystem();
try {
Gson gson = new Gson();
if (config.isEnableRoutePenetration()) {
final String routePenetrationRules = config.getRoutePenetrationRules();
//split with ; ,exclude the \;
final String[] rules = routePenetrationRules.trim().split("(?<!\\\\);");
if (rules.length % 2 != 0) {
throw new IllegalStateException("rule must be pairwise.");
}
for (int i = 0; i + 1 < rules.length; i += 2) {
String regex = rules[i];
//escape \; to ;
regex = regex.replaceAll("\\\\;", ";");
final boolean caseSensitive = (Integer.parseInt(rules[i + 1]) & 1) != 0;
int flag = 0;
if (!caseSensitive) {
flag |= Pattern.CASE_INSENSITIVE;
}
final Pattern pattern = Pattern.compile(regex, flag);
list.add(new PenetrationRule(pattern));
final PenetrationConfig penetrationConfig = gson.fromJson(routePenetrationRules, PenetrationConfig.class);
if (penetrationConfig.getRules() == null) {
throw new IllegalStateException("rules can't be null");
}
rules = Arrays.asList(penetrationConfig.getRules());
rules.forEach(PenetrationRule::init);
}
LOGGER.info("init {} route-penetration rules success", rules.size());
} catch (Exception e) {
LOGGER.error("can't parse the sql-penetration rule, detail exception is ", e);
LOGGER.error("can't parse the route-penetration rule, please check the 'routePenetrationRules', detail exception is " + e);
throw e;
}
}
@@ -65,18 +60,76 @@ public final class RoutePenetrationManager {
}
public boolean match(String sql) {
return list.stream().anyMatch((rule) -> rule.match(sql));
return rules.stream().anyMatch((rule) -> rule.match(sql));
}
private static final class PenetrationConfig {
private PenetrationRule[] rules = new PenetrationRule[0];
public PenetrationRule[] getRules() {
return rules;
}
public void setRules(PenetrationRule[] rules) {
this.rules = rules;
}
}
private static final class PenetrationRule {
private final Pattern pattern;
private Pattern pattern;
private String regex;
private boolean caseSensitive = true;
private boolean partMatch = true;
private PenetrationRule(Pattern pattern) {
this.pattern = pattern;
public String getRegex() {
return regex;
}
public void setRegex(String regex) {
this.regex = regex;
}
public boolean isCaseSensitive() {
return caseSensitive;
}
public void setCaseSensitive(boolean caseSensitive) {
this.caseSensitive = caseSensitive;
}
public boolean isPartMatch() {
return partMatch;
}
public void setPartMatch(boolean partMatch) {
this.partMatch = partMatch;
}
public void init() {
if (StringUtils.isBlank(regex)) {
throw new IllegalStateException("regex can't be null or empty.");
}
int flag = Pattern.DOTALL;
if (!caseSensitive) {
flag |= Pattern.CASE_INSENSITIVE;
}
pattern = Pattern.compile(regex, flag);
}
public boolean match(String sql) {
return pattern.matcher(sql).matches();
final Matcher matcher = pattern.matcher(sql);
return partMatch ? matcher.find() : matcher.matches();
}
@Override
public String toString() {
return "PenetrationRule{" +
", regex='" + regex + '\'' +
", caseSensitive=" + caseSensitive +
", partMatch=" + partMatch +
'}';
}
}
}