Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
Loading...
Searching...
No Matches
kcenon::logger::security::log_sanitizer Class Reference

Log sanitizer for masking sensitive data. More...

#include <log_sanitizer.h>

Collaboration diagram for kcenon::logger::security::log_sanitizer:
Collaboration graph

Public Member Functions

log_sanitizeradd_pattern (sensitive_data_type type)
 Add a built-in pattern for sensitive data detection.
 
log_sanitizeradd_custom_pattern (std::string_view name, std::string_view regex_pattern, std::string_view replacement="[REDACTED]", bool preserve_partial=false)
 Add a custom sanitization pattern.
 
log_sanitizerremove_pattern (std::string_view name)
 Remove a pattern by name.
 
std::string sanitize (std::string_view input) const
 Sanitize a string by masking all detected sensitive data.
 
bool contains_sensitive_data (std::string_view input) const
 Check if a string contains sensitive data.
 
std::vector< std::string > active_patterns () const
 Get list of active pattern names.
 
log_sanitizerclear ()
 Clear all patterns.
 
log_sanitizeradd_common_patterns ()
 Add all common patterns at once.
 

Private Member Functions

void add_credit_card_pattern ()
 
void add_ssn_pattern ()
 
void add_api_key_pattern ()
 
void add_password_pattern ()
 
void add_email_pattern ()
 
void add_ip_address_pattern ()
 
void add_phone_number_pattern ()
 

Static Private Member Functions

static std::string apply_rule (const std::string &input, const sanitization_rule &rule)
 

Private Attributes

std::vector< sanitization_rulerules_
 

Detailed Description

Log sanitizer for masking sensitive data.

Thread-safe sanitizer that detects and masks sensitive information in log messages. Supports both built-in patterns for common sensitive data types and custom patterns for organization-specific needs.

Built-in patterns:

  • Credit cards: Masks all but last 4 digits
  • SSN: Masks all but last 4 digits
  • API keys: Fully redacted
  • Passwords: Fully redacted
  • Emails: Partial masking of local part
  • IP addresses: Masks last two octets
  • Phone numbers: Masks middle digits

Definition at line 82 of file log_sanitizer.h.

Member Function Documentation

◆ active_patterns()

std::vector< std::string > kcenon::logger::security::log_sanitizer::active_patterns ( ) const
inline

Get list of active pattern names.

Returns
Vector of pattern names currently active
Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/security/log_sanitizer.h.

Definition at line 190 of file log_sanitizer.h.

190 {
191 std::vector<std::string> names;
192 names.reserve(rules_.size());
193 for (const auto& rule : rules_) {
194 names.push_back(rule.name);
195 }
196 return names;
197 }
std::vector< sanitization_rule > rules_

References rules_.

◆ add_api_key_pattern()

void kcenon::logger::security::log_sanitizer::add_api_key_pattern ( )
inlineprivate
Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/security/log_sanitizer.h.

Definition at line 244 of file log_sanitizer.h.

244 {
245 // Matches common API key formats
246 rules_.emplace_back(
247 "api_key",
248 R"(\b(sk[-_]|api[-_]|key[-_]|token[-_]|bearer\s+)([a-zA-Z0-9]{16,})\b)",
249 "$1[REDACTED]",
250 false);
251 }

References rules_.

Referenced by add_pattern().

Here is the caller graph for this function:

◆ add_common_patterns()

log_sanitizer & kcenon::logger::security::log_sanitizer::add_common_patterns ( )
inline

Add all common patterns at once.

Returns
Reference to this sanitizer for method chaining
Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/security/log_sanitizer.h.

Definition at line 212 of file log_sanitizer.h.

212 {
218 return *this;
219 }
log_sanitizer & add_pattern(sensitive_data_type type)
Add a built-in pattern for sensitive data detection.
@ api_key
API keys (sk-, api_, bearer tokens)
@ password
Password fields in key=value format.
@ credit_card
Credit card numbers (Visa, MC, Amex, etc.)
@ ssn
Social Security Numbers (US format)

References add_pattern(), kcenon::logger::security::api_key, kcenon::logger::security::credit_card, kcenon::logger::security::email, kcenon::logger::security::password, and kcenon::logger::security::ssn.

Referenced by kcenon::logger::security::make_default_sanitizer().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ add_credit_card_pattern()

void kcenon::logger::security::log_sanitizer::add_credit_card_pattern ( )
inlineprivate
Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/security/log_sanitizer.h.

Definition at line 224 of file log_sanitizer.h.

224 {
225 // Matches credit card numbers with or without separators
226 // Preserves last 4 digits
227 rules_.emplace_back(
228 "credit_card",
229 R"(\b(\d{4}[-\s]?\d{4}[-\s]?\d{4}[-\s]?)(\d{4})\b)",
230 "****-****-****-$2",
231 true);
232 }

References rules_.

Referenced by add_pattern().

Here is the caller graph for this function:

◆ add_custom_pattern()

log_sanitizer & kcenon::logger::security::log_sanitizer::add_custom_pattern ( std::string_view name,
std::string_view regex_pattern,
std::string_view replacement = "[REDACTED]",
bool preserve_partial = false )
inline

Add a custom sanitization pattern.

Parameters
nameIdentifier for the pattern
regex_patternRegular expression to match sensitive data
replacementReplacement string (use $1, $2 for capture groups)
preserve_partialIf true, preserve last few characters
Returns
Reference to this sanitizer for method chaining
Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/security/log_sanitizer.h.

Definition at line 127 of file log_sanitizer.h.

131 {
132 rules_.emplace_back(
133 std::string(name),
134 std::string(regex_pattern),
135 std::string(replacement),
136 preserve_partial);
137 return *this;
138 }

References rules_.

◆ add_email_pattern()

void kcenon::logger::security::log_sanitizer::add_email_pattern ( )
inlineprivate
Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/security/log_sanitizer.h.

Definition at line 262 of file log_sanitizer.h.

262 {
263 // Matches email addresses, masks local part partially
264 rules_.emplace_back(
265 "email",
266 R"(\b([a-zA-Z0-9._%+-])([a-zA-Z0-9._%+-]*)(@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})\b)",
267 "$1***$3",
268 true);
269 }

References rules_.

Referenced by add_pattern().

Here is the caller graph for this function:

◆ add_ip_address_pattern()

void kcenon::logger::security::log_sanitizer::add_ip_address_pattern ( )
inlineprivate
Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/security/log_sanitizer.h.

Definition at line 271 of file log_sanitizer.h.

271 {
272 // Matches IPv4 addresses, masks last two octets
273 rules_.emplace_back(
274 "ip_address",
275 R"(\b(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\b)",
276 "$1.$2.x.x",
277 true);
278 }

References rules_.

Referenced by add_pattern().

Here is the caller graph for this function:

◆ add_password_pattern()

void kcenon::logger::security::log_sanitizer::add_password_pattern ( )
inlineprivate
Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/security/log_sanitizer.h.

Definition at line 253 of file log_sanitizer.h.

253 {
254 // Matches password=xxx, pwd=xxx, passwd=xxx patterns
255 rules_.emplace_back(
256 "password",
257 R"(((?:password|passwd|pwd|secret|credential)[\s]*[=:]\s*)([^\s&]+))",
258 "$1[REDACTED]",
259 false);
260 }

References rules_.

Referenced by add_pattern().

Here is the caller graph for this function:

◆ add_pattern()

log_sanitizer & kcenon::logger::security::log_sanitizer::add_pattern ( sensitive_data_type type)
inline

Add a built-in pattern for sensitive data detection.

Parameters
typeThe type of sensitive data to detect
Returns
Reference to this sanitizer for method chaining
Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/security/log_sanitizer.h.

Definition at line 89 of file log_sanitizer.h.

89 {
90 switch (type) {
93 break;
96 break;
99 break;
102 break;
105 break;
108 break;
111 break;
113 // Custom patterns are added via add_custom_pattern()
114 break;
115 }
116 return *this;
117 }
@ phone_number
Phone numbers (various formats)
@ custom
Custom user-defined patterns.

References add_api_key_pattern(), add_credit_card_pattern(), add_email_pattern(), add_ip_address_pattern(), add_password_pattern(), add_phone_number_pattern(), add_ssn_pattern(), kcenon::logger::security::api_key, kcenon::logger::security::credit_card, kcenon::logger::security::custom, kcenon::logger::security::email, kcenon::logger::security::ip_address, kcenon::logger::security::password, kcenon::logger::security::phone_number, and kcenon::logger::security::ssn.

Referenced by add_common_patterns().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ add_phone_number_pattern()

void kcenon::logger::security::log_sanitizer::add_phone_number_pattern ( )
inlineprivate
Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/security/log_sanitizer.h.

Definition at line 280 of file log_sanitizer.h.

280 {
281 // Matches phone numbers in various formats
282 rules_.emplace_back(
283 "phone_number",
284 R"(\b(\+?\d{1,3}[-.\s]?)(\d{3})[-.\s]?(\d{3})[-.\s]?(\d{4})\b)",
285 "$1***-***-$4",
286 true);
287 }

References rules_.

Referenced by add_pattern().

Here is the caller graph for this function:

◆ add_ssn_pattern()

void kcenon::logger::security::log_sanitizer::add_ssn_pattern ( )
inlineprivate
Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/security/log_sanitizer.h.

Definition at line 234 of file log_sanitizer.h.

234 {
235 // Matches SSN format XXX-XX-XXXX
236 // Preserves last 4 digits
237 rules_.emplace_back(
238 "ssn",
239 R"(\b(\d{3})[-\s]?(\d{2})[-\s]?(\d{4})\b)",
240 "***-**-$3",
241 true);
242 }

References rules_.

Referenced by add_pattern().

Here is the caller graph for this function:

◆ apply_rule()

static std::string kcenon::logger::security::log_sanitizer::apply_rule ( const std::string & input,
const sanitization_rule & rule )
inlinestaticprivate
Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/security/log_sanitizer.h.

Definition at line 289 of file log_sanitizer.h.

289 {
290 return std::regex_replace(input, rule.pattern, rule.replacement);
291 }

References kcenon::logger::security::sanitization_rule::pattern, and kcenon::logger::security::sanitization_rule::replacement.

Referenced by sanitize().

Here is the caller graph for this function:

◆ clear()

log_sanitizer & kcenon::logger::security::log_sanitizer::clear ( )
inline

Clear all patterns.

Returns
Reference to this sanitizer for method chaining
Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/security/log_sanitizer.h.

Definition at line 203 of file log_sanitizer.h.

203 {
204 rules_.clear();
205 return *this;
206 }

References clear(), and rules_.

Referenced by clear().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ contains_sensitive_data()

bool kcenon::logger::security::log_sanitizer::contains_sensitive_data ( std::string_view input) const
inline

Check if a string contains sensitive data.

Parameters
inputThe string to check
Returns
true if sensitive data is detected, false otherwise
Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/security/log_sanitizer.h.

Definition at line 177 of file log_sanitizer.h.

177 {
178 for (const auto& rule : rules_) {
179 if (std::regex_search(input.begin(), input.end(), rule.pattern)) {
180 return true;
181 }
182 }
183 return false;
184 }

References rules_.

◆ remove_pattern()

log_sanitizer & kcenon::logger::security::log_sanitizer::remove_pattern ( std::string_view name)
inline

Remove a pattern by name.

Parameters
nameName of the pattern to remove
Returns
Reference to this sanitizer for method chaining
Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/security/log_sanitizer.h.

Definition at line 145 of file log_sanitizer.h.

145 {
146 rules_.erase(
147 std::remove_if(rules_.begin(), rules_.end(),
148 [name](const sanitization_rule& rule) {
149 return rule.name == name;
150 }),
151 rules_.end());
152 return *this;
153 }

References rules_.

◆ sanitize()

std::string kcenon::logger::security::log_sanitizer::sanitize ( std::string_view input) const
inline

Sanitize a string by masking all detected sensitive data.

Parameters
inputThe string to sanitize
Returns
Sanitized string with sensitive data masked
Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/security/log_sanitizer.h.

Definition at line 160 of file log_sanitizer.h.

160 {
161 if (rules_.empty()) {
162 return std::string(input);
163 }
164
165 std::string result(input);
166 for (const auto& rule : rules_) {
167 result = apply_rule(result, rule);
168 }
169 return result;
170 }
static std::string apply_rule(const std::string &input, const sanitization_rule &rule)

References apply_rule(), and rules_.

Here is the call graph for this function:

Member Data Documentation

◆ rules_


The documentation for this class was generated from the following file: