Introducing Golang Masking Tool To Mask Away your sensitive information

anu1097

Anuraag Gupta

Posted on March 11, 2022

Introducing Golang Masking Tool To Mask Away your sensitive information

Hi Guys,

I wanted to share this library I created for one of my use cases where I needed to mask sensitive information from the data I was logging and storing in Database.

I was searching for similar libraries which already solve this use case in golang projects. I found some but it didn't completely solve all my use-cases. So I created this using some existing solutions.

Inspired by two repositories -

  1. zlog
  2. Golang Masker Both libraries were solving similar usecase but didn't cover all use cases I was looking for. Zlog is a libray that focuses more on logging and its filterating features are not exposed to be used separately. While Golang Masker doesn't cover all data types. Hence it didn't solve my use-cases.

So I combined both of them to create this library and sharing the best of both them. Added some more uses cases too.

You can use variety of Filters to help identify sensitive information and replace it with masked string or a custom filter string -

  1. By specified field
  2. By specified field-prefix
  3. By specified value
  4. By custom type
  5. By struct tag
  6. By data regex pattern (e.g. personal information)
  7. All Fields Filter

Sample Example to show its power -

By Specified Field and Default Custom Filter String

    type myRecord struct {
        ID    string
        Phone string
    }
    record := myRecord{
        ID:    "userId",
        Phone: "090-0000-0000",
    }

    maskTool := NewMaskTool(filter.FieldFilter("Phone"))
    filteredData := maskTool.MaskDetails(record)

    // fmt.Println(filteredData)
    // {userId [filtered]}
Enter fullscreen mode Exit fullscreen mode

By Specified Field and custom masking

    type myRecord struct {
        ID         string
        Phone      string
        Url        string
        Email      string
        Name       string
        Address    string
        CreditCard string
    }
    record := myRecord{
        ID:         "userId",
        Phone:      "090-0000-0000",
        Url:        "http://admin:mysecretpassword@localhost:1234/uri",
        Email:      "dummy@dummy.com",
        Name:       "John Doe",
        Address:    "1 AB Road, Paradise",
        CreditCard: "4444-4444-4444-4444",
    }

    maskTool := NewMaskTool(
        filter.CustomFieldFilter("Phone", customMasker.MMobile),
        filter.CustomFieldFilter("Email", customMasker.MEmail),
        filter.CustomFieldFilter("Url", customMasker.MURL),
        filter.CustomFieldFilter("Name", customMasker.MName),
        filter.CustomFieldFilter("ID", customMasker.MID),
        filter.CustomFieldFilter("Address", customMasker.MAddress),
        filter.CustomFieldFilter("CreditCard", customMasker.MCreditCard),
    )
    filteredData := maskTool.MaskDetails(record)

    // fmt.Println(filteredData)
    // {userId**** 090-***0-0000 http://admin:xxxxx@localhost:1234/uri dum****@dummy.com J**n D**e 1 AB R****** 4444-4******44-4444}
Enter fullscreen mode Exit fullscreen mode

By Struct Tag and custom masking

    type myRecord struct {
        ID    string
        EMail string `mask:"email"`
        Phone string `mask:"mobile"`
    }
    record := myRecord{
        ID:    "userId",
        EMail: "dummy@dummy.com",
        Phone: "9191919191",
    }

    maskTool := NewMaskTool(filter.TagFilter(customMasker.MEmail, customMasker.MMobile))
    filteredData := maskTool.MaskDetails(record)

    // fmt.Println(filteredData)
    // {userId dum****@dummy.com 9191***191}
Enter fullscreen mode Exit fullscreen mode

There are 8 types of custom masking available currently. And you can customise the default Filter String and masking placeholder character.

More usage docs are present in Readme

Library Link - https://github.com/anu1097/golang-masking-tool

💖 💪 🙅 🚩
anu1097
Anuraag Gupta

Posted on March 11, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related