Skip to content

Commit 7b939f9

Browse files
authored
go csfle content (#706)
* go csfle content * fix tab name * correct note formatting * pr feedback Co-authored-by: Nathan Leniz <[email protected]>
1 parent 61c4391 commit 7b939f9

File tree

2 files changed

+234
-0
lines changed

2 files changed

+234
-0
lines changed

source/includes/steps-fle-configure-the-mongodb-client.yaml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ content: |
3232
.. code-block:: csharp
3333
3434
var keyVaultNamespace = CollectionNamespace.FromFullName("encryption.__keyVault");
35+
.. tab::
36+
:tabid: go
37+
38+
.. code-block:: go
39+
40+
keyVaultNamespace := "encryption.__keyVault"
41+
3542
---
3643
title: Specify the Local Master Encryption Key
3744
ref: specify-the-local-master-encryption-key
@@ -89,6 +96,22 @@ content: |
8996
{ "key", localMasterKeyBytes }
9097
};
9198
kmsProviders.Add("local", localOptions);
99+
.. tab::
100+
:tabid: go
101+
102+
.. code-block:: go
103+
104+
localMasterKey := getMasterKey()
105+
kmsProviders := map[string]map[string]interface{
106+
"local":
107+
"key": localMasterKey
108+
}
109+
110+
.. note::
111+
112+
In the companion project, the KMS provider information is represented by a struct.
113+
114+
92115
---
93116
title: Map the JSON Schema to the Patients Collection
94117
ref: map-the-json-schema-to-the-patients-collection
@@ -135,6 +158,18 @@ content: |
135158
// JsonSchemaCreator is a utility class found in the C# companion
136159
// project to this guide
137160
var schema = JsonSchemaCreator.CreateJsonSchema(keyIdBase64);
161+
162+
.. tab::
163+
:tabid: go
164+
165+
.. code-block:: go
166+
167+
// schema.CreateJSONSchema(dataKeyBase64) is a helper funciton found in the
168+
// Go companion project to this guide
169+
schema := schema.CreateJSONSchema(dataKeyBase64)
170+
schemaMap := map[string]interface{}{
171+
"medicalRecords.patients": schema,
172+
}
138173
---
139174
title: Specify the Location of the Encryption Binary
140175
ref: specify-the-location-of-the-encryption-binary
@@ -227,6 +262,33 @@ content: |
227262
{
228263
{ "mongocryptdSpawnPath", $@"{MongoBinariesPath}\mongocryptd.exe" },
229264
};
265+
266+
.. tab::
267+
:tabid: go
268+
269+
.. note::
270+
271+
It is only necessary to specify the spawn path if ``mongocryptd`` is not
272+
present in the system path.
273+
274+
.. code-block:: go
275+
276+
extraOptions := map[string]interface{}{
277+
"mongocryptdSpawnPath": "/usr/local/bin/mongocryptd",
278+
}
279+
280+
.. note:: Encryption Binary Daemon
281+
282+
If the ``mongocryptd`` daemon is already running, you can
283+
configure the client to skip starting it by passing the
284+
following option:
285+
286+
.. code-block:: go
287+
288+
extraOptions := map[string]interface{}{
289+
"mongocryptdBypassSpawn": true,
290+
}
291+
230292
---
231293
title: Create the MongoClient
232294
ref: create-the-mongoclient
@@ -298,3 +360,15 @@ content: |
298360
extraOptions: extraOptions);
299361
clientSettings.AutoEncryptionOptions = autoEncryptionOptions;
300362
var client = new MongoClient(clientSettings);
363+
364+
.. tab::
365+
:tabid: go
366+
367+
.. code-block:: go
368+
369+
autoEncryptionOpts := options.AutoEncryption().
370+
SetKmsProviders(provider.Credentials()).
371+
SetKeyVaultNamespace(keyVaultNamespace).
372+
SetSchemaMap(schemaMap).
373+
SetExtraOptions(extraOptions)
374+
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri).SetAutoEncryptionOptions(autoEncryptionOpts))

source/security/client-side-field-level-encryption-guide.txt

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,23 @@ Additional Dependencies
272272
* - x64 Support
273273
- x64 support is required for CSFLE
274274

275+
.. tab::
276+
:tabid: go
277+
278+
.. list-table::
279+
:header-rows: 1
280+
281+
* - Dependency Name
282+
- Description
283+
284+
* - Go version 1.10 or later
285+
- A Go version of 1.10 or later is required. Go 1.11 or later is recommended for support
286+
`go module support <https://blog.golang.org/using-go-modules>__`.
287+
288+
* - `libmongocrypt <https://github.com/mongodb/libmongocrypt#libmongocrypt>`__ version 1.1.0 or later
289+
-
290+
291+
275292
.. _fle-create-a-master-key:
276293

277294
A. Create a Master Key
@@ -403,6 +420,34 @@ to a file with the **fully runnable code below**:
403420
}
404421
}
405422

423+
.. tab::
424+
:tabid: go
425+
426+
The following script generates a 96-byte locally-managed master key and
427+
saves it to a file called ``master-key.txt`` in the directory
428+
from which the program is executed.
429+
430+
.. code-block:: go
431+
432+
package main
433+
434+
import (
435+
"crypto/rand"
436+
"io/ioutil"
437+
"log"
438+
)
439+
440+
func main() {
441+
key := make([]byte, 96)
442+
if _, err := rand.Read(key); err != nil {
443+
log.Fatalf("Unable to create a random 96 byte data key: %v", err)
444+
}
445+
if err := ioutil.WriteFile("master-key.txt", key, 0644); err != nil {
446+
log.Fatalf("Unable to write key to file: %v", err)
447+
}
448+
}
449+
450+
406451
B. Create a Data Encryption Key
407452
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
408453

@@ -656,6 +701,13 @@ full `JSON Schema for the Medical Care Management System
656701
View the **complete runnable** `helper code in C#
657702
<https://github.com/mongodb-university/csfle-guides/blob/master/dotnet/CSFLE/JsonSchemaCreator.cs>`_.
658703

704+
.. tab::
705+
:tabid: go
706+
707+
View the **complete runnable** `helper code in Go
708+
<https://github.com/mongodb-university/csfle-guides/blob/master/schema/json_schema.go>`_.
709+
710+
659711
D. Create the MongoDB Client
660712
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
661713

@@ -863,6 +915,50 @@ can provide configurable parameters including:
863915

864916
| **Default**: ``60``
865917

918+
.. tab::
919+
:tabid: go
920+
921+
.. list-table::
922+
:header-rows: 1
923+
:stub-columns: 1
924+
925+
* - Name
926+
- Description
927+
928+
929+
* - port
930+
- | Listening port.
931+
| Specify this value as follows:
932+
933+
.. example::
934+
935+
.. code-block:: go
936+
937+
extraOptions := map[string]interface{}{
938+
"mongocryptdSpawnArgs": []string{
939+
"--port=30000",
940+
},
941+
}
942+
943+
| **Default**: ``27020``
944+
945+
* - idleShutdownTimeoutSecs
946+
- | Number of idle seconds in which the ``mongocryptd`` process should wait before exiting.
947+
| Specify this value as follows:
948+
949+
.. example::
950+
951+
.. code-block:: go
952+
953+
extraOptions := map[string]interface{}{
954+
"mongocryptdSpawnArgs": []string{
955+
"--idleShutdownTimeoutSecs=75",
956+
},
957+
}
958+
959+
| **Default**: ``60``
960+
961+
866962

867963
.. note::
868964

@@ -1019,6 +1115,58 @@ following **code snippet**:
10191115

10201116
Console.WriteLine($"Encrypted client query by the SSN (deterministically-encrypted) field:\n {result}\n");
10211117

1118+
.. tab::
1119+
:tabid: go
1120+
1121+
.. code-block:: go
1122+
1123+
package patient
1124+
1125+
type medicalRecord struct {
1126+
Weight int `bson:"weight"`
1127+
BloodPressure string `bson:"bloodPressure"`
1128+
}
1129+
1130+
type insurance struct {
1131+
Provider string `bson:"provider"`
1132+
PolicyNumber int `bson:"policyNumber"`
1133+
}
1134+
1135+
type Patient struct {
1136+
Name string `bson:"name"`
1137+
SSN int `bson:"ssn"`
1138+
BloodType string `bson:"bloodType"`
1139+
medicalRecords []medicalRecord `bson:"medicalRecords"`
1140+
insurance insurance `bson:"insurance"`
1141+
}
1142+
1143+
func GetExamplePatient() Patient {
1144+
1145+
return Patient{
1146+
Name: "Jon Doe",
1147+
SSN: 241014209,
1148+
BloodType: "AB+",
1149+
medicalRecords: []medicalRecord{{
1150+
Weight: 180,
1151+
BloodPressure: "120/80",
1152+
}},
1153+
insurance: insurance{
1154+
Provider: "MaestCare",
1155+
PolicyNumber: 123142,
1156+
},
1157+
}
1158+
}
1159+
1160+
doc := GetExamplePatient()
1161+
collection := client.Database(dbName).Collection(collName)
1162+
if _, err := collection.InsertOne(context.TODO(), doc); err != nil {
1163+
return fmt.Errorf("InsertOne error: %v", err)
1164+
}
1165+
1166+
.. note::
1167+
1168+
Rather than creating a raw BSON document, you can pass a struct with ``bson`` tags directly
1169+
to the driver for encoding.
10221170

10231171

10241172
When a CSFLE-enabled client inserts a new patient record into the Medical Care
@@ -1190,6 +1338,12 @@ To view and download a runnable example of CSFLE, select your driver below:
11901338
**GitHub:** `C# CSFLE runnable example
11911339
<https://github.com/mongodb-university/csfle-guides/tree/master/dotnet>`_
11921340

1341+
.. tab::
1342+
:tabid: go
1343+
1344+
**GitHub:** `Go CSFLE runnable example
1345+
<https://github.com/mongodb-university/csfle-guides/tree/master/gocse>`_
1346+
11931347
Move to Production
11941348
~~~~~~~~~~~~~~~~~~
11951349

@@ -1238,3 +1392,9 @@ check out the reference docs in the server manual:
12381392

12391393
For additional information on MongoDB CSFLE API, see the
12401394
`official C# driver documentation <https://mongodb.github.io/mongo-csharp-driver/2.11/reference/driver/crud/client_side_encryption/>`__.
1395+
1396+
.. tab::
1397+
:tabid: go
1398+
1399+
For additional information on the MongoDB CSFLE API, see the
1400+
`official Go driver documentation <https://pkg.go.dev/go.mongodb.org/[email protected]/mongo#hdr-Client_Side_Encryption>`__

0 commit comments

Comments
 (0)