MXS-1302: the keyword in the Json is now "match" and MatchRule class substitutes CaptureRule

The keyword in the Json is now "match" and MatchRule class substitutes
CaptureRule

"replace": {
"column": "d_code",
"match": "(?<=aaa).*(?=-12)|(?<=-12).*"
},…
This commit is contained in:
MassimilianoPinto
2017-07-14 11:52:59 +02:00
parent 85d7d67ab8
commit f1b2257383
2 changed files with 55 additions and 55 deletions

View File

@ -46,7 +46,7 @@ static const char KEY_TABLE[] = "table";
static const char KEY_VALUE[] = "value"; static const char KEY_VALUE[] = "value";
static const char KEY_WITH[] = "with"; static const char KEY_WITH[] = "with";
static const char KEY_OBFUSCATE[] = "obfuscate"; static const char KEY_OBFUSCATE[] = "obfuscate";
static const char KEY_CAPTURE[] = "capture"; static const char KEY_MATCH[] = "match";
/** /**
* @class AccountVerbatim * @class AccountVerbatim
@ -367,10 +367,10 @@ bool create_rules_from_array(json_t* pRules, vector<shared_ptr<MaskingRules::Rul
} }
else else
{ {
json_t* pCapture = json_object_get(pReplace, KEY_CAPTURE); json_t* pMatch = json_object_get(pReplace, KEY_MATCH);
// Capture takes the precedence // Match takes the precedence
sRule = pCapture ? sRule = pMatch ?
MaskingRules::CaptureRule::create_from(pRule) : MaskingRules::MatchRule::create_from(pRule) :
MaskingRules::ReplaceRule::create_from(pRule); MaskingRules::ReplaceRule::create_from(pRule);
} }
@ -479,13 +479,13 @@ MaskingRules::ObfuscateRule::ObfuscateRule(const std::string& column,
{ {
} }
MaskingRules::CaptureRule::CaptureRule(const std::string& column, MaskingRules::MatchRule::MatchRule(const std::string& column,
const std::string& table, const std::string& table,
const std::string& database, const std::string& database,
const std::vector<SAccount>& applies_to, const std::vector<SAccount>& applies_to,
const std::vector<SAccount>& exempted, const std::vector<SAccount>& exempted,
pcre2_code* regexp, pcre2_code* regexp,
const std::string& fill) const std::string& fill)
: MaskingRules::Rule::Rule(column, table, database, applies_to, exempted) : MaskingRules::Rule::Rule(column, table, database, applies_to, exempted)
, m_regexp(regexp) , m_regexp(regexp)
, m_fill(fill) , m_fill(fill)
@ -504,7 +504,7 @@ MaskingRules::ObfuscateRule::~ObfuscateRule()
{ {
} }
MaskingRules::CaptureRule::~CaptureRule() MaskingRules::MatchRule::~MatchRule()
{ {
pcre2_code_free(m_regexp); pcre2_code_free(m_regexp);
} }
@ -758,17 +758,17 @@ bool rule_get_values(json_t* pRule,
} }
/** /**
* Returns 'capture' regexp & 'fill' value from a 'replace' rule * Returns 'match' regexp & 'fill' value from a 'replace' rule
* *
* @param pRule The Json rule doc * @param pRule The Json rule doc
* @param pCapture The string buffer for 'capture'value * @param pMatch The string buffer for 'match'value
* @param pFill The string buffer for 'fill' value * @param pFill The string buffer for 'fill' value
* *
* @return True on success, false on errors * @return True on success, false on errors
*/ */
bool rule_get_capture_fill(json_t* pRule, bool rule_get_match_fill(json_t* pRule,
std::string *pCapture, std::string *pMatch,
std::string* pFill) std::string* pFill)
{ {
// Get the 'with' key from the rule // Get the 'with' key from the rule
json_t* pWith = json_object_get(pRule, KEY_WITH); json_t* pWith = json_object_get(pRule, KEY_WITH);
@ -789,17 +789,17 @@ bool rule_get_capture_fill(json_t* pRule,
// Get fill from 'with' object // Get fill from 'with' object
json_t* pTheFill = rule_get_fill(pWith); json_t* pTheFill = rule_get_fill(pWith);
// Get 'capture' from 'replace' ojbect // Get 'match' from 'replace' ojbect
json_t* pTheCapture = json_object_get(pKeyObj, KEY_CAPTURE); json_t* pTheMatch = json_object_get(pKeyObj, KEY_MATCH);
// Check values // Check values
if ((!pTheFill || !json_is_string(pTheFill)) || if ((!pTheFill || !json_is_string(pTheFill)) ||
((!pTheCapture || !json_is_string(pTheCapture)))) ((!pTheMatch || !json_is_string(pTheMatch))))
{ {
MXS_ERROR("A masking '%s' rule has '%s' and/or '%s' " MXS_ERROR("A masking '%s' rule has '%s' and/or '%s' "
"invalid Json strings.", "invalid Json strings.",
KEY_REPLACE, KEY_REPLACE,
KEY_CAPTURE, KEY_MATCH,
KEY_FILL); KEY_FILL);
return false; return false;
} }
@ -807,7 +807,7 @@ bool rule_get_capture_fill(json_t* pRule,
{ {
// Update the string buffers // Update the string buffers
pFill->assign(json_string_value(pTheFill)); pFill->assign(json_string_value(pTheFill));
pCapture->assign(json_string_value(pTheCapture)); pMatch->assign(json_string_value(pTheMatch));
return true; return true;
} }
@ -968,17 +968,17 @@ static pcre2_code* rule_compile_pcre2_match(const char* match_string)
} }
//static //static
auto_ptr<MaskingRules::Rule> MaskingRules::CaptureRule::create_from(json_t* pRule) auto_ptr<MaskingRules::Rule> MaskingRules::MatchRule::create_from(json_t* pRule)
{ {
ss_dassert(json_is_object(pRule)); ss_dassert(json_is_object(pRule));
std::string column, table, database, value, fill, capture; std::string column, table, database, value, fill, match;
vector<shared_ptr<MaskingRules::Rule::Account> > applies_to; vector<shared_ptr<MaskingRules::Rule::Account> > applies_to;
vector<shared_ptr<MaskingRules::Rule::Account> > exempted; vector<shared_ptr<MaskingRules::Rule::Account> > exempted;
auto_ptr<MaskingRules::Rule> sRule; auto_ptr<MaskingRules::Rule> sRule;
// Check rule, extract base values // Check rule, extract base values
// Note: the capture rule has same rule_type of "replace" // Note: the match rule has same rule_type of "replace"
if (rule_get_values(pRule, if (rule_get_values(pRule,
&applies_to, &applies_to,
&exempted, &exempted,
@ -986,29 +986,29 @@ auto_ptr<MaskingRules::Rule> MaskingRules::CaptureRule::create_from(json_t* pRul
&table, &table,
&database, &database,
KEY_REPLACE) && KEY_REPLACE) &&
rule_get_capture_fill(pRule, // get capture/fill rule_get_match_fill(pRule, // get match/fill
&capture, &match,
&fill)) &fill))
{ {
if (!capture.empty() && !fill.empty()) if (!match.empty() && !fill.empty())
{ {
// Compile the regexp capture // Compile the regexp
pcre2_code* pCode = rule_compile_pcre2_match(capture.c_str()); pcre2_code* pCode = rule_compile_pcre2_match(match.c_str());
if (pCode) if (pCode)
{ {
Closer<pcre2_code*> code(pCode); Closer<pcre2_code*> code(pCode);
// Instantiate the CaptureRule class // Instantiate the MatchRule class
sRule = auto_ptr<MaskingRules::CaptureRule>(new MaskingRules::CaptureRule(column, sRule = auto_ptr<MaskingRules::MatchRule>(new MaskingRules::MatchRule(column,
table, table,
database, database,
applies_to, applies_to,
exempted, exempted,
pCode, pCode,
fill)); fill));
// Ownership of pCode has been moved to the CaptureRule object. // Ownership of pCode has been moved to the MatchRule object.
code.release(); code.release();
} }
} }
@ -1147,7 +1147,7 @@ inline void fill_buffer(FillIter f_first,
} }
} }
void MaskingRules::CaptureRule::rewrite(LEncString& s) const void MaskingRules::MatchRule::rewrite(LEncString& s) const
{ {
int rv = 0; int rv = 0;
uint32_t n_matches = 0; uint32_t n_matches = 0;

View File

@ -243,11 +243,11 @@ public:
ObfuscateRule& operator = (const ObfuscateRule&); ObfuscateRule& operator = (const ObfuscateRule&);
}; };
class CaptureRule : public Rule class MatchRule : public Rule
{ {
public: public:
/** /**
* Constructor of CaptureRule * Constructor of MatchRule
* *
* @param column The column value from the json file. * @param column The column value from the json file.
* @param table The table value from the json file. * @param table The table value from the json file.
@ -259,15 +259,15 @@ public:
* @param regexp The compiled capture regexp from the json file. * @param regexp The compiled capture regexp from the json file.
* @param fill The fill value from the json file. * @param fill The fill value from the json file.
*/ */
CaptureRule(const std::string& column, MatchRule(const std::string& column,
const std::string& table, const std::string& table,
const std::string& database, const std::string& database,
const std::vector<SAccount>& applies_to, const std::vector<SAccount>& applies_to,
const std::vector<SAccount>& exempted, const std::vector<SAccount>& exempted,
pcre2_code* regexp, pcre2_code* regexp,
const std::string& fill); const std::string& fill);
~CaptureRule(); ~MatchRule();
const pcre2_code& capture() const const pcre2_code& capture() const
{ {
@ -280,7 +280,7 @@ public:
} }
/** /**
* Create a CaptureRule instance * Create a MatchRule instance
* *
* @param pRule A json object corresponding to a single * @param pRule A json object corresponding to a single
* rule in the rules json file. * rule in the rules json file.
@ -301,8 +301,8 @@ public:
std::string m_fill; std::string m_fill;
private: private:
CaptureRule(const CaptureRule&); MatchRule(const MatchRule&);
CaptureRule& operator = (const CaptureRule&); MatchRule& operator = (const MatchRule&);
}; };
~MaskingRules(); ~MaskingRules();