Repository navigation
Expand file tree
/
Copy pathexample_test.go
More file actions
122 lines (97 loc) · 3.29 KB
/
Copy pathexample_test.go
File metadata and controls
122 lines (97 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package privacy_test
import (
"context"
"log"
"time"
"github.com/ln80/privacy-engine"
"github.com/ln80/privacy-engine/aes"
"github.com/ln80/privacy-engine/memory"
)
type Profile struct {
Email string `pii:"data"`
Fullname string `pii:"data"`
Role string
}
type User struct {
ID string `pii:"subjectID"`
Profile `pii:"dive"`
}
// Example of Tokenization and Client-side encryption
func Example() {
ctx := context.Background()
newProtector := func(namespace string) privacy.Protector {
return privacy.NewProtector(namespace, memory.NewKeyEngine(), func(pc *privacy.ProtectorConfig) {
// Token engine is optional.
// If not provided, tokenization methods return core.ErrTokenEngineNotConfigured.
pc.TokenEngine = memory.NewTokenEngine()
// If cache is enabled then the service will decorates engines
// using an in-memory cache layer.
pc.CacheEnabled = true
pc.CacheTTL = 15 * time.Minute
// AES is 256GCM is the default encryptor.
pc.Encryptor = aes.New256GCMEncryptor()
// This Allow a soft crypto-shredding of personal data,
// so the data can be recovered within the grace period.
pc.GracefulMode = true
})
}
factory := privacy.NewFactory(newProtector)
// Spin up a goroutine to particularly monitor and evict caches
factory.Monitor(ctx)
// tenant ID is used as namespace to ensure some degree of isolation.
tenant := "tnt-a20873"
protector, clear := factory.Instance(tenant)
// Force the cache cleanup at the end of the function call.
// Always be cautious about the cached encryption materials.
defer clear()
// A user profile data sent from the client-site
p := Profile{
Email: "Samanta_Murray25@hotmail.com",
Fullname: "Samanta Murray",
Role: "Teacher",
}
// Assuming the example function is a system entrypoint, an important step is
// to tokenize the user canonical identifier (i.e., the email address)
// as soon as possible in the process and use the token as a surrogate ID
tokens, err := protector.Tokenize(ctx, privacy.TokenDataSlice(p.Email), privacy.WithPrefix("sub_"))
if err != nil {
log.Fatal(err)
}
id := tokens.Get(p.Email).Token
// Note that the token (aka the surrogate ID) is used as PII subjectID,
// Which means all user PII data will be encrypted using the same encryption key
// that is uniquely associated to this token.
user := User{ID: id, Profile: p}
err = protector.Encrypt(ctx, &user)
if err != nil {
log.Fatal(err)
}
// Encrypted Output ex:
// Profile{
// Email: "ENC..NDQ1ZDRhYTMtNWUwNS00MDcxLWEwNzAtMDlhMTM5MTFkM2Ex.7Q61HTCUT+XZtzzGp3HsVoHk6o74kwdEHqY46kB4eflXnRwswgHRVlApRg7mp4bNH5zSppV2u40=",
// Fullname: "ENC..NDQ1ZDRhYTMtNWUwNS00MDcxLWEwNzAtMDlhMTM5MTFkM2Ex.mNyZmcvUHTAKTMC+uY6f77bJ3sZ5+NYBZwWKj8zZ0sA4j8mOPz8188sV",
// Role: "Teacher",
// }
err = protector.Decrypt(ctx, &user)
if err != nil {
log.Fatal(err)
}
print(user.Profile)
// If the encryption key is lost, PII can't be decrypted,
// and data is removed from the structure.
_ = protector.Encrypt(ctx, &user)
_ = protector.Forget(ctx, user.ID)
_ = protector.Decrypt(ctx, &user)
print(user.Profile)
// Output:
// Profile{
// Email: "Samanta_Murray25@hotmail.com",
// Fullname: "Samanta Murray",
// Role: "Teacher",
// }
// Profile{
// Email: "",
// Fullname: "",
// Role: "Teacher",
// }
}