Skip to content

Commit

Permalink
add option to set fix masking len
Browse files Browse the repository at this point in the history
Signed-off-by: Tamar Galer <[email protected]>
  • Loading branch information
tamar-ox committed Jul 10, 2024
1 parent 749dfb5 commit d60a5a2
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 11 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Keep Your logs safe!
This formatter ensures the security of your logs and prevents sensitive data leaks.
For example -
Using this Formatter will print this line:
`looger.info(f'Dont Give Your {secrets} away')`
`logger.info(f'Dont Give Your {secrets} away')`
like this:
`Dont Give Your ****** away`

Expand Down Expand Up @@ -46,7 +46,16 @@ If, for some reason, you want to disable masking on a specific log line, use the
from masker_formatter import MaskerFormatter, SKIP_MASK
...
...
logger.info('Line you want tp skip', extra=SKIP_MASK)
logger.info('Line you want to skip', extra=SKIP_MASK)
```

#### fix len masking
If you want the masking to be in a fixed size (and not in the secret len),
please set the `fix_masking_len`:
```
handler.setFormatter(
MaskerFormatter("%(asctime)s %(name)s %(levelname)s %(message)s",
fix_masking_len=30))
```

## The Config File
Expand Down
2 changes: 1 addition & 1 deletion maskerlogger/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
Init file for oxformatter package.
"""
from maskerlogger.masker_formatter import MaskerFormatter # noqa
__version__ = '0.1.0'
__version__ = '0.2.0'
5 changes: 3 additions & 2 deletions maskerlogger/ahocorasick_regex_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def _get_match_regex(self, line, matched_regex) -> List[re.Match]:
matches.append(match)
return matches

def match_regex_to_line(self, line) -> re.Match:
if matched_regxes := self._filter_by_keywords(line):
def match_regex_to_line(self, line: str) -> re.Match:
lower_case_line = line.lower()
if matched_regxes := self._filter_by_keywords(lower_case_line):
return self._get_match_regex(line, matched_regxes)
10 changes: 7 additions & 3 deletions maskerlogger/masker_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
class MaskerFormatter(logging.Formatter):
def __init__(self, fmt=None, datefmt=None, style='%', validate=True,
defaults=None,
regex_config_path=DEFAULT_SECRETS_CONFIG_PATH):
regex_config_path=DEFAULT_SECRETS_CONFIG_PATH,
fix_masking_len=-1):
super().__init__(fmt, datefmt, style, validate=validate,
defaults=defaults)
self.fix_masking_len = fix_masking_len
self.regex_matcher = RegexMatcher(regex_config_path)

def format(self, record: logging.LogRecord) -> str:
Expand All @@ -23,8 +25,10 @@ def format(self, record: logging.LogRecord) -> str:

def _mask_secret(self, msg: str, matches: List[re.Match]) -> str:
for match in matches:
for group in match.groups():
msg = msg.replace(group, "*" * len(group))
match_groups = match.groups() if match.groups() else [match.group()] # noqa
for group in match_groups:
replace_len = len(group) if self.fix_masking_len < 0 else self.fix_masking_len # noqa
msg = msg.replace(group, "*" * replace_len)
return msg

def _mask_sensitive_data(self, record: logging.LogRecord) -> None:
Expand Down
4 changes: 2 additions & 2 deletions maskerlogger/secrets_in_logs_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ def main():
handler = logging.StreamHandler()
handler.setFormatter(
MaskerFormatter("%(asctime)s %(name)s %(levelname)s %(message)s",
regex_config_path="your/config/gitleaks.toml"))
fix_masking_len=30))
logger.addHandler(handler)

logger.info('"current_key": "AIzaSOHbouG6DDa6DOcRGEgOMayAXYXcw6la3c"', extra=SKIP_MASK) # noqa
logger.info('AWS secret: "AKIAI44QH8DHBEXAMPLE" and then more text.')
logger.info('"AKIAI44QH8DHBEXAMPLE" and then more text.')
logger.info("Datadog access token: 'abcdef1234567890abcdef1234567890'")
logger.info('"password": "password123"')

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "maskerlogger"
version = "0.1.0"
version = "0.2.0"
description = "mask your secrets from your logs"
authors = ["Tamar Galer <[email protected]>"]
readme = "README.md"
Expand Down

0 comments on commit d60a5a2

Please sign in to comment.