Skip to content

Commit 8954aca

Browse files
(DOCSP-22761) CSFLE Go Review (#1175)
* Go feedback (#75) * ka - prose suggestions * remove unused godotenv * ka - variable casing Co-authored-by: Kevin Albertson <[email protected]>
1 parent 0280737 commit 8954aca

File tree

17 files changed

+38
-63
lines changed

17 files changed

+38
-63
lines changed

source/core/csfle/fundamentals/manual-encryption.txt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,11 @@ Automatic Decryption
105105
To decrypt your fields automatically, you must configure your
106106
``MongoClient`` instance as follows:
107107

108-
- Set the ``bypass_auto_encryption`` option to ``True``
109108
- Specify your {+key-vault-long+}
110109
- Specify a ``kmsProviders`` object
110+
- If you use MongoDB Community Server, set the ``bypassAutoEncryption``
111+
option to ``True``
112+
111113

112114
.. note:: Automatic Decryption is Available in MongoDB Community Server
113115

@@ -177,10 +179,6 @@ your MongoDB instance:
177179
.. literalinclude:: /includes/fundamentals/manual-encryption/sample.json
178180
:language: json
179181

180-
You use the ``encryption.___keyVault`` collection as your
181-
{+key-vault-long+}, and you read and write data to the
182-
``employees.food`` collection.
183-
184182
.. procedure::
185183
:style: normal
186184

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
autoEncryptionOpts := options.AutoEncryption().
2-
SetKmsProviders(provider.Credentials()).
3-
SetKeyVaultNamespace(keyVaultNamespace).
4-
SetSchemaMap(schemaMap).
5-
SetExtraOptions(extraOptions)
2+
SetKmsProviders(provider.Credentials()).
3+
SetKeyVaultNamespace(keyVaultNamespace).
4+
SetSchemaMap(schemaMap).
5+
SetExtraOptions(extraOptions)
66
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri).SetAutoEncryptionOptions(autoEncryptionOpts))
77

source/includes/fundamentals/manage-keys/key-alt-names.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
clientEncryptionOpts := options.ClientEncryption().SetKeyVaultNamespace(KeyVaultNamespace).SetKmsProviders(kmsProviders)
22
keyVaultClient, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(URI))
33
if err != nil {
4-
return fmt.Errorf("Client encryption connect error %v", err)
4+
return fmt.Errorf("Client connect error %v", err)
55
}
66
clientEnc, err := mongo.NewClientEncryption(keyVaultClient, clientEncryptionOpts)
77
if err != nil {
@@ -10,12 +10,12 @@ if err != nil {
1010
defer func() {
1111
_ = clientEnc.Close(context.TODO())
1212
}()
13-
masterKey := map[string]interface{} {
13+
masterKey := map[string]interface{}{
1414
"<Your dataKeyOpts Key>": "<Your dataKeyOpts Value>",
1515
}
1616
dataKeyOpts := options.DataKey().
17-
SetMasterKey(masterKey).
18-
SetKeyAltNames([]string{"<Your Key Alt Name>"})
17+
SetMasterKey(masterKey).
18+
SetKeyAltNames([]string{"<Your Key Alt Name>"})
1919
dataKeyID, err := clientEnc.CreateDataKey(context.TODO(), provider, dataKeyOpts)
2020
if err != nil {
2121
return fmt.Errorf("create data key error %v", err)

source/includes/fundamentals/manual-encryption/bypass-auto/bypass-auto.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
autoEncryptionOpts := options.AutoEncryption().
2-
SetKmsProviders(kmsProviders).
3-
SetKeyVaultNamespace(KeyVaultNamespace).
4-
SetBypassAutoEncryption(true)
2+
SetKmsProviders(kmsProviders).
3+
SetKeyVaultNamespace(KeyVaultNamespace).
4+
SetBypassAutoEncryption(true)
55
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(URI).SetAutoEncryptionOptions(autoEncryptionOpts))
66
if err != nil {
77
return fmt.Errorf("Connect error for encrypted client: %v", err)

source/includes/fundamentals/manual-encryption/manual-enc.go

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"go.mongodb.org/mongo-driver/mongo/options"
1313
)
1414

15-
1615
const (
1716
KeyVaultNamespace = "encryption.__keyVault"
1817
DbName = "medicalRecords"
@@ -22,25 +21,24 @@ const (
2221

2322
var URI string = os.Getenv("MONGODB_URI")
2423

25-
2624
func main() {
2725
err := godotenv.Load()
2826
provider := "aws"
29-
kmsProviders := map[string]map[string]interface{} {
30-
provider:{
31-
"accessKeyId": os.Getenv("AWS_ACCESS_KEY_ID"),
32-
"secretAccessKey" : os.Getenv("AWS_SECRET_ACCESS_KEY"),
27+
kmsProviders := map[string]map[string]interface{}{
28+
provider: {
29+
"accessKeyId": os.Getenv("AWS_ACCESS_KEY_ID"),
30+
"secretAccessKey": os.Getenv("AWS_SECRET_ACCESS_KEY"),
3331
},
3432
}
35-
masterKey := map[string]interface{} {
36-
"key": os.Getenv("AWS_KEY_ARN"),
33+
masterKey := map[string]interface{}{
34+
"key": os.Getenv("AWS_KEY_ARN"),
3735
"region": os.Getenv("AWS_KEY_REGION"),
3836
}
3937

4038
// start_mongoclient
4139
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(URI))
4240
if err != nil {
43-
panic(fmt.Errorf("Client encryption connect error %v", err))
41+
panic(fmt.Errorf("Client connect error %v", err))
4442
}
4543
// end_mongoclient
4644

@@ -57,8 +55,8 @@ func main() {
5755
// end_client_enc
5856

5957
dataKeyOpts := options.DataKey().
60-
SetMasterKey(masterKey).
61-
SetKeyAltNames([]string{KeyAltName})
58+
SetMasterKey(masterKey).
59+
SetKeyAltNames([]string{KeyAltName})
6260
dataKeyID, err := clientEnc.CreateDataKey(context.TODO(), provider, dataKeyOpts)
6361
if err != nil {
6462
panic(fmt.Errorf("create data key error %v", err))
@@ -69,7 +67,7 @@ func main() {
6967
if err != nil {
7068
panic(err)
7169
}
72-
nameRawValue := bson.RawValue{Type: nameRawValueType, Value: nameRawValueData}
70+
nameRawValue := bson.RawValue{Type: nameRawValueType, Value: nameRawValueData}
7371
nameEncryptionOpts := options.Encrypt().
7472
SetAlgorithm("AEAD_AES_256_CBC_HMAC_SHA_512-Deterministic").
7573
SetKeyID(dataKeyID)
@@ -80,7 +78,7 @@ func main() {
8078
if err != nil {
8179
panic(err)
8280
}
83-
foodsRawValueType, foodsRawValueData, err := bson.MarshalValue(bson.A{"Grapes","Cheese"})
81+
foodsRawValueType, foodsRawValueData, err := bson.MarshalValue(bson.A{"Grapes", "Cheese"})
8482
if err != nil {
8583
panic(err)
8684
}
@@ -137,10 +135,9 @@ func main() {
137135
foodsDecrypted, err := clientEnc.Decrypt(
138136
context.TODO(),
139137
result["foods"].(primitive.Binary))
140-
result["foods"] = foodsDecrypted;
138+
result["foods"] = foodsDecrypted
141139
result["name"] = nameDecrypted
142140
fmt.Printf("Decrypted Document: %s\n", result)
143141
// end_find_decrypt
144142
return
145143
}
146-

source/includes/sample_apps/csfle/build/go/aws/reader/go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ go 1.15
55
require (
66
github.com/aws/aws-sdk-go v1.34.28 // indirect
77
github.com/fatih/structs v1.1.0
8-
github.com/joho/godotenv v1.3.0
98
github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c // indirect
109
github.com/xdg/stringprep v0.0.0-20180714160509-73f8eece6fdc // indirect
1110
go.mongodb.org/mongo-driver v1.7.2

source/includes/sample_apps/csfle/build/go/aws/reader/insert-encrypted-document.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,15 @@ import (
44
"context"
55
"fmt"
66

7-
"github.com/joho/godotenv"
87
"go.mongodb.org/mongo-driver/bson"
98
"go.mongodb.org/mongo-driver/mongo"
109
"go.mongodb.org/mongo-driver/mongo/options"
1110
)
1211

1312
func Insert() error {
14-
err := godotenv.Load()
1513

1614
// start-key-vault
17-
KeyVaultNamespace := "encryption.__keyVault"
15+
keyVaultNamespace := "encryption.__keyVault"
1816
// end-key-vault
1917
dbName := "medicalRecords"
2018
collName := "patients"
@@ -95,7 +93,7 @@ func Insert() error {
9593
// start-client
9694
autoEncryptionOpts := options.AutoEncryption().
9795
SetKmsProviders(kmsProviders).
98-
SetKeyVaultNamespace(KeyVaultNamespace).
96+
SetKeyVaultNamespace(keyVaultNamespace).
9997
SetSchemaMap(schemaMap).
10098
SetExtraOptions(extraOptions)
10199
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri).SetAutoEncryptionOptions(autoEncryptionOpts))

source/includes/sample_apps/csfle/build/go/aws/reader/make-data-key.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ import (
55
"encoding/base64"
66
"fmt"
77

8-
"github.com/joho/godotenv"
98
"go.mongodb.org/mongo-driver/mongo"
109
"go.mongodb.org/mongo-driver/mongo/options"
1110
)
1211

1312
func MakeKey() error {
14-
err := godotenv.Load()
1513

1614
// start-kmsproviders
1715
provider := "aws"
@@ -36,7 +34,7 @@ func MakeKey() error {
3634
clientEncryptionOpts := options.ClientEncryption().SetKeyVaultNamespace(keyVaultNamespace).SetKmsProviders(kmsProviders)
3735
keyVaultClient, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
3836
if err != nil {
39-
return fmt.Errorf("Client encryption connect error %v", err)
37+
return fmt.Errorf("Client connect error %v", err)
4038
}
4139
clientEnc, err := mongo.NewClientEncryption(keyVaultClient, clientEncryptionOpts)
4240
if err != nil {

source/includes/sample_apps/csfle/build/go/azure/reader/go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ go 1.15
55
require (
66
github.com/aws/aws-sdk-go v1.34.28 // indirect
77
github.com/fatih/structs v1.1.0
8-
github.com/joho/godotenv v1.3.0
98
github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c // indirect
109
github.com/xdg/stringprep v0.0.0-20180714160509-73f8eece6fdc // indirect
1110
go.mongodb.org/mongo-driver v1.7.2

source/includes/sample_apps/csfle/build/go/azure/reader/insert-encrypted-document.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,15 @@ import (
44
"context"
55
"fmt"
66

7-
"github.com/joho/godotenv"
87
"go.mongodb.org/mongo-driver/bson"
98
"go.mongodb.org/mongo-driver/mongo"
109
"go.mongodb.org/mongo-driver/mongo/options"
1110
)
1211

1312
func Insert() error {
14-
err := godotenv.Load()
1513

1614
// start-key-vault
17-
KeyVaultNamespace := "encryption.__keyVault"
15+
keyVaultNamespace := "encryption.__keyVault"
1816
// end-key-vault
1917
dbName := "medicalRecords"
2018
collName := "patients"
@@ -96,7 +94,7 @@ func Insert() error {
9694
// start-client
9795
autoEncryptionOpts := options.AutoEncryption().
9896
SetKmsProviders(kmsProviders).
99-
SetKeyVaultNamespace(KeyVaultNamespace).
97+
SetKeyVaultNamespace(keyVaultNamespace).
10098
SetSchemaMap(schemaMap).
10199
SetExtraOptions(extraOptions)
102100
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri).SetAutoEncryptionOptions(autoEncryptionOpts))

source/includes/sample_apps/csfle/build/go/azure/reader/make-data-key.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ import (
55
"encoding/base64"
66
"fmt"
77

8-
"github.com/joho/godotenv"
98
"go.mongodb.org/mongo-driver/mongo"
109
"go.mongodb.org/mongo-driver/mongo/options"
1110
)
1211

1312
func MakeKey() error {
14-
err := godotenv.Load()
1513

1614
// start-kmsproviders
1715
provider := "azure"
@@ -37,7 +35,7 @@ func MakeKey() error {
3735
clientEncryptionOpts := options.ClientEncryption().SetKeyVaultNamespace(keyVaultNamespace).SetKmsProviders(kmsProviders)
3836
keyVaultClient, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
3937
if err != nil {
40-
return fmt.Errorf("Client encryption connect error %v", err)
38+
return fmt.Errorf("Client connect error %v", err)
4139
}
4240
clientEnc, err := mongo.NewClientEncryption(keyVaultClient, clientEncryptionOpts)
4341
if err != nil {

source/includes/sample_apps/csfle/build/go/gcp/reader/go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ go 1.15
55
require (
66
github.com/aws/aws-sdk-go v1.34.28 // indirect
77
github.com/fatih/structs v1.1.0
8-
github.com/joho/godotenv v1.3.0
98
github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c // indirect
109
github.com/xdg/stringprep v0.0.0-20180714160509-73f8eece6fdc // indirect
1110
go.mongodb.org/mongo-driver v1.7.2

source/includes/sample_apps/csfle/build/go/gcp/reader/insert-encrypted-document.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,15 @@ import (
44
"context"
55
"fmt"
66

7-
"github.com/joho/godotenv"
87
"go.mongodb.org/mongo-driver/bson"
98
"go.mongodb.org/mongo-driver/mongo"
109
"go.mongodb.org/mongo-driver/mongo/options"
1110
)
1211

1312
func Insert() error {
14-
err := godotenv.Load()
1513

1614
// start-key-vault
17-
KeyVaultNamespace := "encryption.__keyVault"
15+
keyVaultNamespace := "encryption.__keyVault"
1816
// end-key-vault
1917
dbName := "medicalRecords"
2018
collName := "patients"
@@ -95,7 +93,7 @@ func Insert() error {
9593
// start-client
9694
autoEncryptionOpts := options.AutoEncryption().
9795
SetKmsProviders(kmsProviders).
98-
SetKeyVaultNamespace(KeyVaultNamespace).
96+
SetKeyVaultNamespace(keyVaultNamespace).
9997
SetSchemaMap(schemaMap).
10098
SetExtraOptions(extraOptions)
10199
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri).SetAutoEncryptionOptions(autoEncryptionOpts))

source/includes/sample_apps/csfle/build/go/gcp/reader/make-data-key.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@ import (
55
"encoding/base64"
66
"fmt"
77

8-
"github.com/joho/godotenv"
98
"go.mongodb.org/mongo-driver/mongo"
109
"go.mongodb.org/mongo-driver/mongo/options"
1110
)
1211

1312
func MakeKey() error {
14-
err := godotenv.Load()
1513

1614
// start-kmsproviders
1715
provider := "gcp"
@@ -38,7 +36,7 @@ func MakeKey() error {
3836
clientEncryptionOpts := options.ClientEncryption().SetKeyVaultNamespace(keyVaultNamespace).SetKmsProviders(kmsProviders)
3937
keyVaultClient, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
4038
if err != nil {
41-
return fmt.Errorf("Client encryption connect error %v", err)
39+
return fmt.Errorf("Client connect error %v", err)
4240
}
4341
clientEnc, err := mongo.NewClientEncryption(keyVaultClient, clientEncryptionOpts)
4442
if err != nil {

source/includes/sample_apps/csfle/build/go/local/reader/go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ go 1.15
55
require (
66
github.com/aws/aws-sdk-go v1.34.28 // indirect
77
github.com/fatih/structs v1.1.0
8-
github.com/joho/godotenv v1.3.0
98
github.com/xdg/scram v0.0.0-20180814205039-7eeb5667e42c // indirect
109
github.com/xdg/stringprep v0.0.0-20180714160509-73f8eece6fdc // indirect
1110
go.mongodb.org/mongo-driver v1.7.2

source/includes/sample_apps/csfle/build/go/local/reader/insert-encrypted-document.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,15 @@ import (
77
"io/ioutil"
88
"log"
99

10-
"github.com/joho/godotenv"
1110
"go.mongodb.org/mongo-driver/bson"
1211
"go.mongodb.org/mongo-driver/mongo"
1312
"go.mongodb.org/mongo-driver/mongo/options"
1413
)
1514

1615
func Insert() error {
17-
err := godotenv.Load()
1816

1917
// start-key-vault
20-
KeyVaultNamespace := "encryption.__keyVault"
18+
keyVaultNamespace := "encryption.__keyVault"
2119
// end-key-vault
2220
dbName := "medicalRecords"
2321
collName := "patients"
@@ -97,7 +95,7 @@ func Insert() error {
9795
// start-client
9896
autoEncryptionOpts := options.AutoEncryption().
9997
SetKmsProviders(kmsProviders).
100-
SetKeyVaultNamespace(KeyVaultNamespace).
98+
SetKeyVaultNamespace(keyVaultNamespace).
10199
SetSchemaMap(schemaMap).
102100
SetExtraOptions(extraOptions)
103101
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri).SetAutoEncryptionOptions(autoEncryptionOpts))

source/includes/sample_apps/csfle/build/go/local/reader/make-data-key.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"io/ioutil"
1010
"log"
1111

12-
"github.com/joho/godotenv"
1312
"go.mongodb.org/mongo-driver/mongo"
1413
"go.mongodb.org/mongo-driver/mongo/options"
1514
)
@@ -26,7 +25,6 @@ func localMasterKey() []byte {
2625
}
2726

2827
func MakeKey() error {
29-
err := godotenv.Load()
3028

3129
localMasterKey()
3230

@@ -48,7 +46,7 @@ func MakeKey() error {
4846
clientEncryptionOpts := options.ClientEncryption().SetKeyVaultNamespace(keyVaultNamespace).SetKmsProviders(kmsProviders)
4947
keyVaultClient, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri))
5048
if err != nil {
51-
return fmt.Errorf("Client encryption connect error %v", err)
49+
return fmt.Errorf("Client connect error %v", err)
5250
}
5351
clientEnc, err := mongo.NewClientEncryption(keyVaultClient, clientEncryptionOpts)
5452
if err != nil {

0 commit comments

Comments
 (0)