diff --git a/gdpr/go.mod b/gdpr/go.mod new file mode 100644 index 0000000..b158cf2 --- /dev/null +++ b/gdpr/go.mod @@ -0,0 +1,16 @@ +module github.com/msales/gox/gdpr + +go 1.22.0 + +toolchain go1.22.1 + +require ( + github.com/msales/gox v0.17.0 + 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 +) diff --git a/gdpr/go.sum b/gdpr/go.sum new file mode 100644 index 0000000..b216b11 --- /dev/null +++ b/gdpr/go.sum @@ -0,0 +1,12 @@ +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/msales/gox v0.17.0 h1:8OFneSI7tSSbAPVLl+vZ9gmQgzaLIM8YZPeBc0M9Ax0= +github.com/msales/gox v0.17.0/go.mod h1:P7jJkgIpAYby7x6/GKnBGR1Q0Nzzan4gObI+2ydUEyQ= +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= diff --git a/gdpr/protect_device_id.go b/gdpr/protect_device_id.go new file mode 100644 index 0000000..b1cc1ee --- /dev/null +++ b/gdpr/protect_device_id.go @@ -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, "") +} diff --git a/gdpr/protect_device_id_test.go b/gdpr/protect_device_id_test.go new file mode 100644 index 0000000..55a787c --- /dev/null +++ b/gdpr/protect_device_id_test.go @@ -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) + }) + } +} diff --git a/gdpr/protect_ip.go b/gdpr/protect_ip.go new file mode 100644 index 0000000..c047276 --- /dev/null +++ b/gdpr/protect_ip.go @@ -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) +} diff --git a/gdpr/protect_ip_test.go b/gdpr/protect_ip_test.go new file mode 100644 index 0000000..a7a1f3f --- /dev/null +++ b/gdpr/protect_ip_test.go @@ -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) + }) + } +} diff --git a/go.mod b/go.mod index 4d11202..ecc4214 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module github.com/msales/gox -go 1.21 +go 1.22.0 + +toolchain go1.22.1