DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Top 10 Open Source Projects for SREs and DevOps
  • Mitigating DevOps Repository Risks
  • Cost-Aware Resilience: Implementing Chaos Engineering Without Breaking the Budget
  • Kata Containers: From Kubernetes Pods to Secure VMs

Trending

  • How to Use AWS Aurora Database for a Retail Point of Sale (POS) Transaction System
  • Operational Principles, Architecture, Benefits, and Limitations of Artificial Intelligence Large Language Models
  • How GitHub Copilot Helps You Write More Secure Code
  • After 9 Years, Microsoft Fulfills This Windows Feature Request
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. DevOps and CI/CD
  4. Guide: How to Mask Sensitive Information Using Fluent Bit

Guide: How to Mask Sensitive Information Using Fluent Bit

In this article, the author will discuss how to mask sensitive information using Fluent Bit, a popular open-source log processor.

By 
Sharad Regoti user avatar
Sharad Regoti
·
Jun. 19, 23 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
6.0K Views

Join the DZone community and get the full member experience.

Join For Free

Fluent Bit is a popular open-source log processor and forwarder that allows you to collect data from different sources, filter, and transform it before forwarding it to different destinations. In some cases, the data collected may contain sensitive information like passwords, credit card numbers, social security numbers, and other personally identifiable information (PII). To protect such information, you need to mask or obfuscate it before forwarding it to the destination. In this document, we will discuss how to mask sensitive information using Fluent Bit.

The goal of this guide is to convert structured logs that contain PII information like (mobile numbers, identity information, names, etc.)
{"timestamp":"2023-06-05T17:04:33.505+05:30","requestURI":"/api/user","message":"Sending SMS to mobileNumber=1234512345 registered on aadhaarNumber=1234512345"}
to a format where this information is masked.
{"@timestamp":"2023-06-05T17:04:33.505+05:30","requestURI":"/api/user","message":"Sending SMS to mobileNumber=******** registered on aadhaarNumber=********"}

Prerequisites and Constraints

This guide assumes the following:

  • Docker is installed on your machine
  • You have knowledge of fluent-bit concepts like inputs, outputs, parsers, filters, etc.
  • We will be constraining ourselves not introducing any third-party service other than fluent-bit.

Let’s start with an initial configuration on your machine that reproduces the behavior where sensitive information is not masked yet.

Create an empty directory on your computer and save the below fluent-bit configuration in a file name fluent-bit.conf.

[INPUT]
    Name   dummy
    dummy  {"@timestamp":"2023-06-05T17:04:33.505+05:30","message":"Staring server on port 8080"}
    Tag    dummy.log
 
[INPUT]
    Name   dummy
    dummy  {"@timestamp":"2023-06-05T17:04:33.505+05:30","requestURI":"/api/user","message":"Sending SMS to mobileNumber=1234512345, registered on aadhaarNumber=1234512345"}
    Tag    dummy.log
 
[INPUT]
    Name   dummy
    dummy  {"@timestamp":"2023-06-05T17:04:33.505+05:30","requestURI":"/api/bank","message":"Successfully registered mobileNumber=1234512345, to panNumber=1234512345"}
    Tag    dummy.log
 
[OUTPUT]
    Name   stdout
    Match  *


Now, run the below command from the same directory to start the fluent-bit process in a Docker container.

Dockerfile
 
docker run \\
  -v $(pwd)/fluent-bit.conf:/fluent-bit/etc/fluent-bit.conf \\
  -ti cr.fluentbit.io/fluent/fluent-bit:2.0 \\
  /fluent-bit/bin/fluent-bit \\
  -c /fluent-bit/etc/fluent-bit.conf


After running the command, the expected output should look like this:

initial-logs output

As you can see mobileNumber and aadhaarNumber are clearly visible in the application logs. Let’s start masking this information.

The problem of masking PII information can be logically solved by search and replace operation. When we first search the required information from logs, once found, we apply to replace operation where the value of replace can be as simple as replacing with ******** or hashing the value.

In Fluent-Bit, the above-mentioned operations can be performed at the Filter stage. Now, let’s select the right plugin for this stage.

Selecting the Right Fluent-Bit Plugin

Out of the box, fluent-bit provides Nightfall plugin, which interacts with a third-party component called Nightfall to process the PII information. We won’t be using Nightfall plugin as we don’t want to introduce any third-party component in our system.

Search and Replace can be performed by these three filter plugins:

  • Record Modifier: This plugin gives us the ability to modify our structured logs by replacing entire key, values with something else. But does not allow replacing a small part of the value. For example, take the below-structured log {"timestamp":"2023-06-05T17:04:33.505+05:30","requestURI":"/api/user","message":"Sending SMS to mobileNumber=1234512345 registered on aadhaarNumber=1234512345"} in our case the message field of type string has mobileNumber in it. We just want to replace this value. But this filter can only replace the entire message content. So, we won’t be using this filter plugin
  • Modify: This has the same drawback as Record Modifier plugin. So, we won’t be using this filter plugin
  • Lua: This filter allows you to modify the incoming records using custom Lua scripts. This helps us to extend Fluent Bit capabilities by writing custom filters using Lua programming language. We can use this to write a custom Lua script to perform the search and replace operation for us.

Writing the Lua Script

Create a file called mask.lua in the same directory where fluent-bit.conf exists. Copy the below content inside mask.lua file.

Lua
 
function mask_sensitive_info(tag, timestamp, record)
    message = record["message"]
    if message then
        -- Match "aadhaarNumber:xxxx," and replace with "aadhaarNumber:****,"
        local masked_message = string.gsub(message, 'aadhaarNumber=[^,]*', 'aadhaarNumber=****')
 
        -- Match "mobileNumber:xxxx," and replace with "mobileNumber:****,"
        masked_message = string.gsub(masked_message, 'mobileNumber=[^,]*', 'mobileNumber=****')
 
        record["message"] = masked_message
    end
    return 2, timestamp, record
 end


Here's a breakdown of what this Lua function does:

  1. function mask_sensitive_info(tag, timestamp, record): This line defines a new Lua function named mask_sensitive_info. This function takes three parameters: tag, timestamp, and record.
  2. message = record["message"]: This line retrieves the value of the "message" field from the record parameter and assigns it to a local variable named message.
  3. if message then: This line starts an if statement that only executes the enclosed block of code if message is not nil or false.
  4. local masked_message = string.gsub(message, 'aadhaarNumber[^,]*', 'aadhaarNumber:****'): This line creates a new local variable named masked_message. It assigns to this variable the result of calling the string.gsub function on message. The string.gsub function is used to replace all occurrences of the pattern 'aadhaarNumber[^,]' (which matches the string "aadhaarNumber" followed by any sequence of characters that are not a comma) with the string 'aadhaarNumber:**.'
  5. masked_message = string.gsub(masked_message, 'mobileNumber[^,]*', 'mobileNumber:****'): This line reassigns masked_message with the result of another call to string.gsub, this time replacing all occurrences of the pattern 'mobileNumber[^,]' (which matches the string "mobileNumber" followed by any sequence of characters that are not a comma) with the string 'mobileNumber:**.'
  6. record["message"] = masked_message: This line updates the "message" field of record with the value of masked_message, which at this point should have all sensitive data replaced with asterisks.
  7. end: This line closes the if statement.
  8. return 2, timestamp, record: This line specifies what the function should return when called. In this case, it returns three values: the number 2, the value of timestamp, and the updated record.
  9. end: This line closes the function definition.

So, the overall purpose of this function is to replace any sensitive data (like Aadhaar numbers and mobile numbers) in the "message" field of a record with asterisks for privacy reasons.

Using the Lua Script in Lua Plugin

To enable the lua plugin, you need to add it to the Fluent Bit configuration file. Below is an example of how to configure the lua plugin to mask the PII field in the log data.

Lua
 
[INPUT]
    Name   dummy
    dummy  {"@timestamp":"2023-06-05T17:04:33.505+05:30","message":"Staring server on port 8080"}
    Tag    dummy.log
 
[INPUT]
    Name   dummy
    dummy  {"@timestamp":"2023-06-05T17:04:33.505+05:30","requestURI":"/api/user","message":"Sending SMS to mobileNumber=1234512345, registered on aadhaarNumber=1234512345"}
    Tag    dummy.log
 
[INPUT]
    Name   dummy
    dummy  {"@timestamp":"2023-06-05T17:04:33.505+05:30","requestURI":"/api/bank","message":"Successfully registered mobileNumber=1234512345, to panNumber=1234512345"}
    Tag    dummy.log
 
[FILTER]
    Name    lua
    Match   *
    call    mask_sensitive_info
    script  /fluent-bit/scripts/mask.lua
 
[OUTPUT]
    Name   stdout
    Match  *


In the above configuration, we have added the Lua plugin in the filter state. We set the Match parameter to * to match all incoming log data. We then specify the script parameter to /fluent-bit/scripts/mask.lua which specifies the path to the Lua script file. We also set the call parameter to mask_sensitive_info to specify the function name which has to be loaded from the Lua script file.

Testing

To test the above configuration, run the below command:

Dockerfile
 
docker run \\
  -v $(pwd)/fluent-bit.conf:/fluent-bit/etc/fluent-bit.conf \\
  -v $(pwd)/mask.lua:/fluent-bit/scripts/mask.lua \\
  -ti cr.fluentbit.io/fluent/fluent-bit:2.0 \\
  /fluent-bit/bin/fluent-bit \\
  -c /fluent-bit/etc/fluent-bit.conf


The expected output should contain masked values like this:

masked values

Conclusion

Masking sensitive information is an important security measure that helps protect personally identifiable information from unauthorized access or disclosure. Fluent Bit provides a simple and effective way of masking sensitive information using the lua filter plugin. By configuring the plugin to mask specific fields or patterns in the log data, you can ensure that no sensitive information is leaked to external systems or logs.

Docker (software) Lua (programming language) DevOps Observability Open source

Published at DZone with permission of Sharad Regoti. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Top 10 Open Source Projects for SREs and DevOps
  • Mitigating DevOps Repository Risks
  • Cost-Aware Resilience: Implementing Chaos Engineering Without Breaking the Budget
  • Kata Containers: From Kubernetes Pods to Secure VMs

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!