From c25c0d688b90bafe4e5c1f4690a3cbb7b5a9c702 Mon Sep 17 00:00:00 2001 From: Johan Wikman Date: Fri, 30 Dec 2016 14:19:11 +0200 Subject: [PATCH] Masking: Add startup handling to masking filter - Process parameters - Create config - Load rules --- .../modules/filter/masking/maskingfilter.cc | 67 +++++++++++++++++-- .../modules/filter/masking/maskingfilter.hh | 8 ++- .../filter/masking/maskingfilterconfig.hh | 16 ++++- 3 files changed, 81 insertions(+), 10 deletions(-) diff --git a/server/modules/filter/masking/maskingfilter.cc b/server/modules/filter/masking/maskingfilter.cc index ae949578a..7cf088c16 100644 --- a/server/modules/filter/masking/maskingfilter.cc +++ b/server/modules/filter/masking/maskingfilter.cc @@ -13,6 +13,11 @@ #define MXS_MODULE_NAME "masking" #include "maskingfilter.hh" +#include +#include "maskingrules.hh" + +using std::auto_ptr; +using std::string; // // Global symbols of the Module @@ -20,7 +25,7 @@ extern "C" MXS_MODULE* MXS_CREATE_MODULE() { - MXS_NOTICE("Initialized masking module."); + MXS_NOTICE("Masking module %s initialized.", VERSION_STRING); static MXS_MODULE info = { @@ -46,8 +51,11 @@ extern "C" MXS_MODULE* MXS_CREATE_MODULE() // MaskingFilter // -MaskingFilter::MaskingFilter() +MaskingFilter::MaskingFilter(const Config& config, auto_ptr sRules) + : m_config(config) + , m_sRules(sRules) { + MXS_NOTICE("Masking filter [%s] created.", m_config.name().c_str()); } MaskingFilter::~MaskingFilter() @@ -57,12 +65,17 @@ MaskingFilter::~MaskingFilter() // static MaskingFilter* MaskingFilter::create(const char* zName, char** pzOptions, CONFIG_PARAMETER* ppParams) { - MaskingFilter* pFilter = new MaskingFilter; + MaskingFilter* pFilter = NULL; - if (!process_params(pzOptions, ppParams, pFilter->m_config)) + MaskingFilter::Config config(zName); + if (process_params(pzOptions, ppParams, config)) { - delete pFilter; - pFilter = NULL; + auto_ptr sRules = MaskingRules::load(config.rules_file().c_str()); + + if (sRules.get()) + { + pFilter = new MaskingFilter(config, sRules); + } } return pFilter; @@ -89,5 +102,45 @@ uint64_t MaskingFilter::getCapabilities() // static bool MaskingFilter::process_params(char **pzOptions, CONFIG_PARAMETER *ppParams, Config& config) { - return true; + bool error = false; + + for (int i = 0; ppParams[i]; ++i) + { + const FILTER_PARAMETER *pParam = ppParams[i]; + + if (strcmp(pParam->name, "rules_file") == 0) + { + string rules_file; + + if (*pParam->value != '/') + { + // A relative path is interpreted relative to the data directory. + rules_file += get_datadir(); + rules_file += "/"; + } + + rules_file += pParam->value; + + config.set_rules_file(rules_file); + } + else if (!filter_standard_parameter(pParam->name)) + { + MXS_ERROR("Unknown configuration entry '%s'.", pParam->name); + error = true; + } + } + + if (!error) + { + if (config.rules_file().empty()) + { + MXS_ERROR("In order to use the masking filter, the location of the rules file " + "must be specified. Add a configuration entry 'rules_file=...' in " + "the section [%s], in the MariaDB MaxScale configuration file.", + config.name().c_str()); + error = true; + } + } + + return !error; } diff --git a/server/modules/filter/masking/maskingfilter.hh b/server/modules/filter/masking/maskingfilter.hh index fe774f496..1a547bf93 100644 --- a/server/modules/filter/masking/maskingfilter.hh +++ b/server/modules/filter/masking/maskingfilter.hh @@ -13,10 +13,13 @@ */ #include +#include #include #include "maskingfilterconfig.hh" #include "maskingfiltersession.hh" +class MaskingRules; + class MaskingFilter : public maxscale::Filter { @@ -33,7 +36,7 @@ public: static uint64_t getCapabilities(); private: - MaskingFilter(); + MaskingFilter(const Config& config, std::auto_ptr sRules); MaskingFilter(const MaskingFilter&); MaskingFilter& operator = (const MaskingFilter&); @@ -41,5 +44,6 @@ private: static bool process_params(char **pzOptions, CONFIG_PARAMETER *ppParams, Config& config); private: - Config m_config; + Config m_config; + std::auto_ptr m_sRules; }; diff --git a/server/modules/filter/masking/maskingfilterconfig.hh b/server/modules/filter/masking/maskingfilterconfig.hh index db58c6cb0..afccb2cbe 100644 --- a/server/modules/filter/masking/maskingfilterconfig.hh +++ b/server/modules/filter/masking/maskingfilterconfig.hh @@ -13,8 +13,22 @@ */ #include +#include class MaskingFilterConfig { - // Placeholder +public: + MaskingFilterConfig(const char* zName) + : m_name(zName) + {} + ~MaskingFilterConfig() {} + + const std::string& name() const { return m_name; } + const std::string& rules_file() const { return m_rules_file; } + + void set_rules_file(const std::string& s) { m_rules_file = s; } + +private: + std::string m_name; + std::string m_rules_file; };