improve WallFilter

This commit is contained in:
wenshao
2013-03-17 11:04:28 +08:00
parent 2548d88fc6
commit 80bbefa088
6 changed files with 87 additions and 11 deletions
@@ -49,6 +49,7 @@ public abstract class WallProvider {
private final ConcurrentMap<String, WallDenyStat> deniedTables = new ConcurrentHashMap<String, WallDenyStat>();
private final ConcurrentMap<String, WallDenyStat> deniedFunctions = new ConcurrentHashMap<String, WallDenyStat>();
private final ConcurrentMap<String, WallDenyStat> deniedSchemas = new ConcurrentHashMap<String, WallDenyStat>();
public WallProvider(WallConfig config){
this.config = config;
@@ -146,6 +147,25 @@ public abstract class WallProvider {
return true;
}
public boolean checkDenySchema(String schemaName) {
if (schemaName == null) {
return true;
}
schemaName = schemaName.toLowerCase();
if (getConfig().getDenySchemas().contains(schemaName)) {
WallDenyStat denyStat = this.deniedSchemas.get(schemaName);
if (denyStat == null) {
this.deniedSchemas.putIfAbsent(schemaName, new WallDenyStat());
denyStat = this.deniedSchemas.get(schemaName);
}
denyStat.incrementAndGetDenyCount();
return false;
}
return true;
}
public boolean checkDenyTable(String tableName) {
if (tableName == null) {
@@ -193,6 +213,15 @@ public abstract class WallProvider {
String formName = WallVisitorUtils.form(tableName);
return deniedTables.get(formName);
}
public WallDenyStat getDenniedSchemaStat(String schemaName) {
if (schemaName == null) {
return null;
}
String formName = WallVisitorUtils.form(schemaName);
return deniedSchemas.get(formName);
}
public WallCheckResult check(String sql) {
WallCheckResult result = new WallCheckResult();
@@ -112,8 +112,7 @@ public class OracleWallVisitor extends OracleASTVisitorAdapter implements WallVi
}
public boolean visit(OracleSelectTableReference x) {
WallVisitorUtils.check(this, x);
return true;
return WallVisitorUtils.check(this, x);
}
public boolean visit(SQLExprTableSource x) {
@@ -237,10 +237,10 @@ public class SQLServerWallVisitor extends SQLServerASTVisitorAdapter implements
@Override
public boolean visit(SQLServerObjectReferenceExpr x) {
if (x.getSchema() != null && config.isDenySchema(x.getSchema())) {
if (x.getSchema() != null && !provider.checkDenySchema(x.getSchema())) {
this.getViolations().add(new IllegalSQLObjectViolation(this.toSQL(x)));
}
if (x.getDatabase() != null && config.isDenySchema(x.getDatabase())) {
if (x.getDatabase() != null && !provider.checkDenySchema(x.getDatabase())) {
this.getViolations().add(new IllegalSQLObjectViolation(this.toSQL(x)));
}
return true;
@@ -741,38 +741,48 @@ public class WallVisitorUtils {
}
private static void checkSchema(WallVisitor visitor, SQLExpr x) {
private static boolean checkSchema(WallVisitor visitor, SQLExpr x) {
if (x instanceof SQLName) {
String owner = ((SQLName) x).getSimleName();
owner = WallVisitorUtils.form(owner);
if (visitor.getConfig().isDenySchema(owner)) {
if (!visitor.getProvider().checkDenySchema(owner)) {
addViolation(visitor, x);
return false;
}
if (visitor.getConfig().isDenyObjects(owner)) {
addViolation(visitor, x);
return false;
}
}
// if (ownerExpr instanceof SQLPropertyExpr) {
if (x instanceof SQLPropertyExpr) {
checkSchema(visitor, ((SQLPropertyExpr) x).getOwner());
return checkSchema(visitor, ((SQLPropertyExpr) x).getOwner());
}
return true;
}
public static void check(WallVisitor visitor, SQLExprTableSource x) {
public static boolean check(WallVisitor visitor, SQLExprTableSource x) {
SQLExpr expr = x.getExpr();
if (expr instanceof SQLPropertyExpr) {
checkSchema(visitor, ((SQLPropertyExpr) expr).getOwner());
boolean checkResult = checkSchema(visitor, ((SQLPropertyExpr) expr).getOwner());
if (!checkResult) {
return false;
}
}
if (expr instanceof SQLName) {
String tableName = ((SQLName) expr).getSimleName();
if (visitor.isDenyTable(tableName)) {
addViolation(visitor, x);
return false;
}
}
return true;
}
private static void addViolation(WallVisitor visitor, SQLObject x) {