-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
146 lines (125 loc) · 3.41 KB
/
main.go
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"context"
"crypto/rand"
"encoding/base64"
"fmt"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
var (
ctx = context.Background()
kmsProviders map[string]map[string]interface{}
)
// データキーの生成
func createDataKey() primitive.Binary {
// mongoに接続
kvClient, err := mongo.Connect(ctx, options.Client().ApplyURI("<MONGO_URI>").SetAuth(
options.Credential{
Username: "root",
Password: "password",
AuthSource: "admin",
}))
if err != nil {
panic(err)
}
localKey := make([]byte, 96)
if _, err := rand.Read(localKey); err != nil {
panic(err)
}
kmsProviders = map[string]map[string]interface{}{
"local": {
"key": localKey,
},
}
clientEncryptionOpts := options.ClientEncryption().SetKeyVaultNamespace("keyvault.datakeys").SetKmsProviders(kmsProviders)
// データキー用のdbクライアントを生成
clientEncryption, err := mongo.NewClientEncryption(kvClient, clientEncryptionOpts)
if err != nil {
panic(err)
}
defer clientEncryption.Close(ctx)
// データキーの生成
dataKeyId, err := clientEncryption.CreateDataKey(ctx, "local", options.DataKey().SetKeyAltNames([]string{"example"}))
return dataKeyId
}
func readSchemaMap(dataKeyIdBase64 string) bson.M {
content :=
`{
"fle-example.user": {
"encryptMetadata": {
"keyId": [
{
"$binary":
{
"base64": "%s",
"subType": "04"
}
}
]
},
"properties": {
"accountNumber": {
"encrypt": {
"bsonType": "string",
"algorithm": "AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic"
}
}
},
"bsonType": "object"
}
}`
schema := fmt.Sprintf(content, dataKeyIdBase64)
var doc bson.M
if err := bson.UnmarshalExtJSON([]byte(schema), false, &doc); err != nil {
panic(err)
}
return doc
}
// 暗号化用のdbクライアントを生成
func createEncryptedClient(schemaMap bson.M) *mongo.Client {
mongocryptdOpts := map[string]interface{}{
"mongodcryptdBypassSpawn": true,
}
// 自動暗号化のオプション
autoEncryptionOpts := options.AutoEncryption().
SetKeyVaultNamespace("keyvault.datakeys").
SetKmsProviders(kmsProviders).
SetSchemaMap(schemaMap).
SetExtraOptions(mongocryptdOpts)
// 暗号化用のdbクライアントを生成
mongoClient, err := mongo.Connect(ctx, options.Client().ApplyURI("<MONGO_URI>").SetAuth(options.Credential{
Username: "root",
Password: "password",
AuthSource: "admin",
}).SetAutoEncryptionOptions(autoEncryptionOpts))
if err != nil {
panic(err)
}
return mongoClient
}
func main() {
// データキーの生成
dataKey := createDataKey()
// スキーママップを定義
schemaMap := readSchemaMap(base64.StdEncoding.EncodeToString(dataKey.Data))
// 暗号化用のdbクライアントを生成
client := createEncryptedClient(schemaMap)
defer client.Disconnect(ctx)
collection := client.Database("fle-example").Collection("user")
// insert
if _, err := collection.InsertOne(context.TODO(), bson.M{"name": "TAROOO", "accountNumber": "123456"}); err != nil {
panic(err)
}
// find
var user interface{}
err := collection.FindOne(context.TODO(), bson.M{"accountNumber": "123456"}).Decode(&user)
if err != nil {
panic(err)
}
fmt.Printf("結果:%v", user)
}
// 起動コマンド
// go run -tags cse main.go