2024-06-21 20:29:07 -03:00
2024-06-21 20:29:07 -03:00
2024-06-21 20:29:07 -03:00
2024-06-21 20:29:07 -03:00
2024-06-21 19:57:03 -03:00
2024-06-21 19:57:03 -03:00

Redact your Logs in Python

Introduction

It often becomes necessary to hide the values of certain attributes in a String. The term for this process is called REDACTION.

There are several ways to do this in a JSON or XML String. We can find several tutorials to do this, but I came across a different case.

Although part of the String looks like a JSON structure, this String can come from an Error Log, which messes up the entire JSON structure and the codes I found don't work well in these situations.

I found a really cool tutorial:

Secure Logging in Python: Redacting Sensitive Data with Filters

This material provides us with code that deals with REGEX patterns and allows, regardless of whether we have a poorly formatted JSON (or any other) structure, it works anyway.

However, the material is well aimed at finding REGEX patterns, such as dates, credit card numbers, telephone numbers and other String formatting patterns.

But what about when we need to hide specific attributes?

I didn't find anything about it, so I decided to build a code that would also add this form of REDACTION to the tutorial above.

Understand the Code

The code has two aspects:

  • Act based on REGEX patterns

img.png

  • Act based on attribute names

img_1.png

We can then, from a list of Strings (Logs for example), have a poorly formatted String:

img_2.png

What the algorithm performs:

  • The algorithm looks for REGEX patterns, therefore, no matter the name of the attribute or if there is an attribute associated with the name (in a JSON structure), the algorithm will always replace the value with .

  • The algorithm searches for the attribute name in the list (ATTRIBUTE_PATTERNS). This needs to be in the format:

    '': '' is the attribute name : need to respect a JSON association is the value between '', yes the value needs to be a String

So you can use the class Redaction.py in you code, like this:

img_3.png

Test

Execute the Python code redact.py

img_4.png

Description
No description provided
Readme 1.3 MiB
Languages
Python 100%