Skip to content

Commit

Permalink
[TASK] TRK-3577 N - Device ID masking GDPR
Browse files Browse the repository at this point in the history
  • Loading branch information
tsknadaj committed Oct 8, 2024
1 parent 73a283c commit a991bf8
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 0 deletions.
25 changes: 25 additions & 0 deletions gdpr/protect_device_id.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package gdpr

import "strings"

const (
ipSeparator = "."
gdprStringHideValue = "*"
gdprIPHideValue = "0"
unknownIPValue = "unknown"
emptyIP = "0"
)

// ProtectDeviceID hides last two character from passed device id and returns string with protected value
func ProtectDeviceID(deviceIDValue string) string {
if deviceIDValue == "" {
return deviceIDValue
}

splitted := strings.Split(deviceIDValue, "")
l := len(splitted)
splitted[l-1] = gdprStringHideValue
splitted[l-2] = gdprStringHideValue

return strings.Join(splitted, "")
}
35 changes: 35 additions & 0 deletions gdpr/protect_device_id_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package gdpr_test

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/msales/gox/gdpr"
)

func Test_protector_ProtectDeviceID(t *testing.T) {
tests := []struct {
name string
value string
wantProtectedValue string
}{
{
name: "Empty value",
value: "",
wantProtectedValue: "",
},
{
name: "Value to protect",
value: "some_value",
wantProtectedValue: "some_val**",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
protectedValue := gdpr.ProtectDeviceID(tt.value)

assert.Equal(t, tt.wantProtectedValue, protectedValue)
})
}
}
31 changes: 31 additions & 0 deletions gdpr/protect_ip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package gdpr

import (
"strings"

"github.com/msales/gox/netx"
)

// ProtectIPV4 hides last octet from passed uint32 IPV4 value and returns string with protected value
func ProtectIPV4(ipValue uint32) string {
if ipValue == 0 {
return netx.UintToIP(ipValue).String()
}

ip := netx.UintToIP(ipValue).String()

return ProtectRawIP(ip)
}

// ProtectRawIP hides last octet from ip string value and returns string with protected value
func ProtectRawIP(ipValue string) string {
if ipValue == emptyIP || ipValue == "" || ipValue == unknownIPValue {
return ipValue
}

splitted := strings.Split(ipValue, ".")
l := len(splitted)
splitted[l-1] = gdprIPHideValue

return strings.Join(splitted, ipSeparator)
}
61 changes: 61 additions & 0 deletions gdpr/protect_ip_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package gdpr_test

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/msales/gox/gdpr"
)

func Test_protector_ProtectRawIP(t *testing.T) {
tests := []struct {
name string
value string
wantProtectedValue string
}{
{
name: "Empty value",
value: "",
wantProtectedValue: "",
},
{
name: "Value to protect",
value: "42.21.37.213",
wantProtectedValue: "42.21.37.0",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
protectedValue := gdpr.ProtectRawIP(tt.value)

assert.Equal(t, tt.wantProtectedValue, protectedValue)
})
}
}

func Test_protector_ProtectIPV4(t *testing.T) {
tests := []struct {
name string
value uint32
wantProtectedValue string
}{
{
name: "Empty value",
value: 0,
wantProtectedValue: "0.0.0.0",
},
{
name: "Value to protect",
value: 706029013, // 42.21.37.213
wantProtectedValue: "42.21.37.0",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
protectedValue := gdpr.ProtectIPV4(tt.value)

assert.Equal(t, tt.wantProtectedValue, protectedValue)
})
}
}
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
module github.com/msales/gox

go 1.21

require github.com/stretchr/testify v1.9.0

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit a991bf8

Please sign in to comment.