From f7b978b2a2bd4aa1564d15526032b737d6c7befe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=A4kel=C3=A4?= Date: Thu, 31 Aug 2017 12:04:35 +0300 Subject: [PATCH] MXS-1346: Make User more like a class The User class now handles the appending of the rules by itself and it also provides a method for accessing the name instead of exposing the name itself. The rules matching is still done externally to the User class and moving it into the User class depends on other changes being made first. --- .../modules/filter/dbfwfilter/CMakeLists.txt | 6 +-- .../modules/filter/dbfwfilter/dbfwfilter.cc | 19 ++----- server/modules/filter/dbfwfilter/users.cc | 50 +++++++++++++++++++ server/modules/filter/dbfwfilter/users.hh | 32 +++++++----- 4 files changed, 75 insertions(+), 32 deletions(-) create mode 100644 server/modules/filter/dbfwfilter/users.cc diff --git a/server/modules/filter/dbfwfilter/CMakeLists.txt b/server/modules/filter/dbfwfilter/CMakeLists.txt index 4bcb40deb..55aac5c87 100644 --- a/server/modules/filter/dbfwfilter/CMakeLists.txt +++ b/server/modules/filter/dbfwfilter/CMakeLists.txt @@ -6,14 +6,14 @@ if(BISON_FOUND AND FLEX_FOUND) add_flex_bison_dependency(token ruleparser) include_directories(${CMAKE_CURRENT_BINARY_DIR}) include_directories(${CMAKE_CURRENT_SOURCE_DIR}) - add_library(dbfwfilter-core STATIC ${BISON_ruleparser_OUTPUTS} ${FLEX_token_OUTPUTS}) - add_library(dbfwfilter SHARED dbfwfilter.cc rules.cc) + add_library(dbfwfilter-core STATIC ${BISON_ruleparser_OUTPUTS} ${FLEX_token_OUTPUTS} rules.cc users.cc) + add_library(dbfwfilter SHARED dbfwfilter.cc) target_link_libraries(dbfwfilter maxscale-common MySQLCommon dbfwfilter-core) set_target_properties(dbfwfilter PROPERTIES VERSION "1.0.0") install_module(dbfwfilter core) # The offline rule check utility - add_executable(dbfwchk dbfw_rule_check.cc rules.cc) + add_executable(dbfwchk dbfw_rule_check.cc) target_link_libraries(dbfwchk maxscale-common MySQLCommon dbfwfilter-core) install_executable(dbfwchk core) diff --git a/server/modules/filter/dbfwfilter/dbfwfilter.cc b/server/modules/filter/dbfwfilter/dbfwfilter.cc index 573734033..f9092f571 100644 --- a/server/modules/filter/dbfwfilter/dbfwfilter.cc +++ b/server/modules/filter/dbfwfilter/dbfwfilter.cc @@ -1189,20 +1189,7 @@ static bool process_user_templates(UserMap& users, const TemplateList& templates if (newrules.size() > 0) { - switch (ut->type) - { - case FWTOK_MATCH_ANY: - user->rules_or.insert(user->rules_or.end(), newrules.begin(), newrules.end()); - break; - - case FWTOK_MATCH_ALL: - user->rules_and.insert(user->rules_and.end(), newrules.begin(), newrules.end()); - break; - - case FWTOK_MATCH_STRICT_ALL: - user->rules_strict_and.insert(user->rules_strict_and.end(), newrules.begin(), newrules.end()); - break; - } + user->append_rules(ut->type, newrules); } } @@ -2238,13 +2225,13 @@ routeQuery(MXS_FILTER *instance, MXS_FILTER_SESSION *session, GWBUF *queue) { ss_dassert(rname); MXS_NOTICE("[%s] Rule '%s' for '%s' matched by %s@%s: %.*s", - dcb->service->name, rname, user->name.c_str(), + dcb->service->name, rname, user->name(), dcb->user, dcb->remote, len, sql); } else if (!match && my_instance->log_match & FW_LOG_NO_MATCH) { MXS_NOTICE("[%s] Query for '%s' by %s@%s was not matched: %.*s", - dcb->service->name, user->name.c_str(), dcb->user, + dcb->service->name, user->name(), dcb->user, dcb->remote, len, sql); } } diff --git a/server/modules/filter/dbfwfilter/users.cc b/server/modules/filter/dbfwfilter/users.cc new file mode 100644 index 000000000..8be054f61 --- /dev/null +++ b/server/modules/filter/dbfwfilter/users.cc @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2016 MariaDB Corporation Ab + * + * Use of this software is governed by the Business Source License included + * in the LICENSE.TXT file and at www.mariadb.com/bsl11. + * + * Change Date: 2020-01-01 + * + * On the date above, in accordance with the Business Source License, use + * of this software will be governed by version 2 or later of the General + * Public License. + */ + +#include "users.hh" + +User::User(std::string name): + m_name(name) +{ +} + +User::~User() +{ +} + +const char* User::name() const +{ + return m_name.c_str(); +} + +void User::append_rules(match_type mode, const RuleList& rules) +{ + switch (mode) + { + case FWTOK_MATCH_ANY: + rules_or.insert(rules_or.end(), rules.begin(), rules.end()); + break; + + case FWTOK_MATCH_ALL: + rules_and.insert(rules_and.end(), rules.begin(), rules.end()); + break; + + case FWTOK_MATCH_STRICT_ALL: + rules_strict_and.insert(rules_strict_and.end(), rules.begin(), rules.end()); + break; + + default: + ss_dassert(false); + break; + } +} diff --git a/server/modules/filter/dbfwfilter/users.hh b/server/modules/filter/dbfwfilter/users.hh index c4d73e6cd..789bc144c 100644 --- a/server/modules/filter/dbfwfilter/users.hh +++ b/server/modules/filter/dbfwfilter/users.hh @@ -39,25 +39,31 @@ class User User& operator=(const User&); public: - User(std::string name): - name(name), - lock(SPINLOCK_INIT), - qs_limit(NULL) - { - } + User(std::string name); + ~User(); - ~User() - { - delete qs_limit; - } + /** + * Get the name of this user + * + * @return Name of the user + */ + const char* name() const; + + /** + * Append new rules to existing rules + * + * @param mode Matching mode for the rule + * @param rules Rules to append + */ + void append_rules(match_type mode, const RuleList& rules); - std::string name; /*< Name of the user */ - SPINLOCK lock; /*< User spinlock */ - QUERYSPEED* qs_limit; /*< The query speed structure unique to this user */ RuleList rules_or; /*< If any of these rules match the action is triggered */ RuleList rules_and; /*< All of these rules must match for the action to trigger */ RuleList rules_strict_and; /*< rules that skip the rest of the rules if one of them * fails. This is only for rules paired with 'match strict_all'. */ + +private: + std::string m_name; /*< Name of the user */ }; typedef std::tr1::shared_ptr SUser;