-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TASK] TRK-3577 N - Device ID masking GDPR
- Loading branch information
Showing
7 changed files
with
183 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, "") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
module github.com/msales/gox | ||
|
||
go 1.21 | ||
go 1.22.0 | ||
|
||
toolchain go1.22.1 |