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 -
- zlog
- 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 -
- By specified field
- By specified field-prefix
- By specified value
- By custom type
- By struct tag
- By data regex pattern (e.g. personal information)
- 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]}
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}
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}
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
Top comments (4)
I'd like to use this!
Are there some bugs? I couldn't install it by using
go get
.Thanks for reporting this. There were some mismatch in package name in go.mod. I had changed the name but didn't recreate go.mod file just replaced the name. That's why it caused issues. Check now and let me know. I didn't notified about this message via email. Hence took time to reply. Apologies.
You should be able to use it now. I have confirmed it.
It worked!
Thanks for quick fixing!
interesting, thanks for share!