Skip to content

Commit 4f4770c

Browse files
authored
GODRIVER-2454 use string for QueryType (#988)
* print hex for binary mismatch * drop encrypted collection before creating * updating bindings for string index_type and query_type * use string for EncryptOptions.QueryType * remove incorrect rm * pin libmongocrypt to 1.5.0-rc2 for Windows * add QueryTypeEquality string constant * free other CStrings
1 parent 48027d6 commit 4f4770c

File tree

7 files changed

+54
-51
lines changed

7 files changed

+54
-51
lines changed

.evergreen/config.yml

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,38 +94,45 @@ functions:
9494
go version
9595
go env
9696
97+
LIBMONGOCRYPT_TAG="1.5.0-rc2"
98+
# LIBMONGOCRYPT_COMMIT is the commit on libmongocrypt for the tag LIBMONGOCRYPT_TAG.
99+
LIBMONGOCRYPT_COMMIT="1756c5120814152c15a2eee7a679a33a4764ba63"
97100
# Install libmongocrypt based on OS.
98101
if [ "Windows_NT" = "$OS" ]; then
99102
mkdir -p c:/libmongocrypt/include
100103
mkdir -p c:/libmongocrypt/bin
101104
# TODO (GODRIVER-2436): do not use pre-release of libmongocrypt.
102-
curl https://s3.amazonaws.com/mciuploads/libmongocrypt/windows/latest_release/libmongocrypt_unstable.tar.gz --output libmongocrypt.tar.gz
103-
tar -xvzf libmongocrypt.tar.gz
104-
cp ./bin/mongocrypt.dll c:/libmongocrypt/bin
105-
cp ./include/mongocrypt/*.h c:/libmongocrypt/include
105+
echo "fetching build for Windows ... begin"
106+
mkdir libmongocrypt-all
107+
cd libmongocrypt-all
108+
# The following URL is published from the upload-all task in the libmongocrypt Evergreen project.
109+
curl https://mciuploads.s3.amazonaws.com/libmongocrypt/all/master/$LIBMONGOCRYPT_COMMIT/libmongocrypt-all.tar.gz -o libmongocrypt-all.tar.gz
110+
tar -xf libmongocrypt-all.tar.gz
111+
cd ..
112+
cp libmongocrypt-all/windows-test/bin/mongocrypt.dll c:/libmongocrypt/bin
113+
cp libmongocrypt-all/windows-test/include/mongocrypt/*.h c:/libmongocrypt/include
106114
export PATH=$PATH:/cygdrive/c/libmongocrypt/bin
115+
rm -rf libmongocrypt-all
116+
echo "fetching build for Windows ... end"
107117
elif [ "Darwin" = "$(uname -s)" ]; then
108118
# TODO (GODRIVER-2442): Once libmongocrypt compiles on macOS 10.15, remove this elif block.
109119
echo "fetching build for Darwin ... begin"
110120
mkdir -p install/libmongocrypt
111121
mkdir libmongocrypt-all
112122
cd libmongocrypt-all
113-
# LIBMONGOCRYPT_COMMIT is for 1.5.0-rc1.
114-
LIBMONGOCRYPT_COMMIT="6e100b087376d448534cb2ad1b4dc50cb7cbc1f6"
115123
# The following URL is published from the upload-all task in the libmongocrypt Evergreen project.
116124
curl https://mciuploads.s3.amazonaws.com/libmongocrypt/all/master/$LIBMONGOCRYPT_COMMIT/libmongocrypt-all.tar.gz -o libmongocrypt-all.tar.gz
117125
tar -xf libmongocrypt-all.tar.gz
118126
cd ..
119127
mv libmongocrypt-all/macos/include ./install/libmongocrypt
120128
mv libmongocrypt-all/macos/lib ./install/libmongocrypt
121129
rm -rf libmongocrypt-all
122-
rm libmongocrypt-all.tar.gz
123130
# Fix prefix in pkg-config prefix path.
124131
sed -i "" -E "s+prefix=.*+prefix=$(pwd)/install/libmongocrypt+" ./install/libmongocrypt/lib/pkgconfig/libmongocrypt.pc
125132
echo "fetching build for Darwin ... end"
126133
else
127134
# TODO (GODRIVER-2436): do not use pre-release of libmongocrypt.
128-
git clone https://github.com/mongodb/libmongocrypt --branch 1.5.0-rc1
135+
git clone https://github.com/mongodb/libmongocrypt --branch $LIBMONGOCRYPT_TAG
129136
./libmongocrypt/.evergreen/compile.sh
130137
fi
131138

mongo/client_encryption.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,7 @@ func (ce *ClientEncryption) Encrypt(ctx context.Context, val bson.RawValue, opts
113113
transformed.SetKeyAltName(*eo.KeyAltName)
114114
}
115115
transformed.SetAlgorithm(eo.Algorithm)
116-
if eo.QueryType != nil {
117-
switch *eo.QueryType {
118-
case options.QueryTypeEquality:
119-
transformed.SetQueryType(mcopts.QueryTypeEquality)
120-
default:
121-
return primitive.Binary{}, fmt.Errorf("unsupported value for QueryType: %v", *eo.QueryType)
122-
}
123-
}
116+
transformed.SetQueryType(eo.QueryType)
124117

125118
if eo.ContentionFactor != nil {
126119
transformed.SetContentionFactor(*eo.ContentionFactor)

mongo/integration/cmd_monitoring_helpers_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package integration
88

99
import (
1010
"bytes"
11+
"encoding/hex"
1112
"errors"
1213
"fmt"
1314
"strings"
@@ -95,7 +96,13 @@ func compareValues(mt *mtest.T, key string, expected, actual bson.RawValue) erro
9596
return fmt.Errorf("type mismatch for key %s; expected %s, got %s", key, expected.Type, actual.Type)
9697
}
9798
if !bytes.Equal(expected.Value, actual.Value) {
98-
return fmt.Errorf("value mismatch for key %s; expected %s, got %s", key, expected.Value, actual.Value)
99+
return fmt.Errorf(
100+
"value mismatch for key %s; expected %s (hex=%s), got %s (hex=%s)",
101+
key,
102+
expected.Value,
103+
hex.EncodeToString(expected.Value),
104+
actual.Value,
105+
hex.EncodeToString(actual.Value))
99106
}
100107
return nil
101108
}

mongo/integration/mtest/mongotest.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,13 @@ func (t *T) CreateCollection(coll Collection, createOnServer bool) *mongo.Collec
449449

450450
db := coll.Client.Database(coll.DB)
451451

452+
if coll.CreateOpts != nil && coll.CreateOpts.EncryptedFields != nil {
453+
// An encrypted collection consists of a data collection and three state collections.
454+
// Aborted test runs may leave these collections.
455+
// Drop all four collections to avoid a quiet failure to create all collections.
456+
DropEncryptedCollection(t, db.Collection(coll.Name), coll.CreateOpts.EncryptedFields)
457+
}
458+
452459
if createOnServer && t.clientType != Mock {
453460
var err error
454461
if coll.ViewOn != "" {

mongo/options/encryptoptions.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,17 @@ import (
1010
"go.mongodb.org/mongo-driver/bson/primitive"
1111
)
1212

13-
// QueryType describes the type of query the result of Encrypt is used for.
14-
type QueryType int
15-
1613
// These constants specify valid values for QueryType
1714
const (
18-
QueryTypeEquality QueryType = 1
15+
QueryTypeEquality string = "equality"
1916
)
2017

2118
// EncryptOptions represents options to explicitly encrypt a value.
2219
type EncryptOptions struct {
2320
KeyID *primitive.Binary
2421
KeyAltName *string
2522
Algorithm string
26-
QueryType *QueryType
23+
QueryType string
2724
ContentionFactor *int64
2825
}
2926

@@ -56,8 +53,10 @@ func (e *EncryptOptions) SetAlgorithm(algorithm string) *EncryptOptions {
5653
}
5754

5855
// SetQueryType specifies the intended query type. It is only valid to set if algorithm is "Indexed".
59-
func (e *EncryptOptions) SetQueryType(queryType QueryType) *EncryptOptions {
60-
e.QueryType = &queryType
56+
// This should be one of the following:
57+
// - equality
58+
func (e *EncryptOptions) SetQueryType(queryType string) *EncryptOptions {
59+
e.QueryType = queryType
6160
return e
6261
}
6362

@@ -84,7 +83,7 @@ func MergeEncryptOptions(opts ...*EncryptOptions) *EncryptOptions {
8483
if opt.Algorithm != "" {
8584
eo.Algorithm = opt.Algorithm
8685
}
87-
if opt.QueryType != nil {
86+
if opt.QueryType != "" {
8887
eo.QueryType = opt.QueryType
8988
}
9089
if opt.ContentionFactor != nil {

x/mongo/driver/mongocrypt/mongocrypt.go

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,14 @@ func NewMongoCrypt(opts *options.MongoCryptOptions) (*MongoCrypt, error) {
5858
// If loading the crypt_shared library isn't disabled, set the default library search path "$SYSTEM"
5959
// and set a library override path if one was provided.
6060
if !opts.CryptSharedLibDisabled {
61-
C.mongocrypt_setopt_append_crypt_shared_lib_search_path(crypt.wrapped, C.CString("$SYSTEM"))
61+
systemStr := C.CString("$SYSTEM")
62+
defer C.free(unsafe.Pointer(systemStr))
63+
C.mongocrypt_setopt_append_crypt_shared_lib_search_path(crypt.wrapped, systemStr)
6264

6365
if opts.CryptSharedLibOverridePath != "" {
64-
C.mongocrypt_setopt_set_crypt_shared_lib_path_override(crypt.wrapped, C.CString(opts.CryptSharedLibOverridePath))
66+
cryptSharedLibOverridePathStr := C.CString(opts.CryptSharedLibOverridePath)
67+
defer C.free(unsafe.Pointer(cryptSharedLibOverridePathStr))
68+
C.mongocrypt_setopt_set_crypt_shared_lib_path_override(crypt.wrapped, cryptSharedLibOverridePathStr)
6569
}
6670
}
6771

@@ -198,29 +202,15 @@ func (m *MongoCrypt) CreateExplicitEncryptionContext(doc bsoncore.Document, opts
198202
algoStr := C.CString(opts.Algorithm)
199203
defer C.free(unsafe.Pointer(algoStr))
200204

201-
switch opts.Algorithm {
202-
case "Indexed":
203-
if ok := C.mongocrypt_ctx_setopt_index_type(ctx.wrapped, IndexTypeIndexed); !ok {
204-
return nil, ctx.createErrorFromStatus()
205-
}
206-
case "Unindexed":
207-
if ok := C.mongocrypt_ctx_setopt_index_type(ctx.wrapped, IndexTypeUnindexed); !ok {
208-
return nil, ctx.createErrorFromStatus()
209-
}
210-
default:
211-
if ok := C.mongocrypt_ctx_setopt_algorithm(ctx.wrapped, algoStr, -1); !ok {
212-
return nil, ctx.createErrorFromStatus()
213-
}
205+
if ok := C.mongocrypt_ctx_setopt_algorithm(ctx.wrapped, algoStr, -1); !ok {
206+
return nil, ctx.createErrorFromStatus()
214207
}
215208

216-
if opts.QueryType != nil {
217-
switch *opts.QueryType {
218-
case options.QueryTypeEquality:
219-
if ok := C.mongocrypt_ctx_setopt_query_type(ctx.wrapped, 1); !ok {
220-
return nil, ctx.createErrorFromStatus()
221-
}
222-
default:
223-
return nil, fmt.Errorf("unsupported value for QueryType: %v", opts.QueryType)
209+
if opts.QueryType != "" {
210+
queryStr := C.CString(opts.QueryType)
211+
defer C.free(unsafe.Pointer(queryStr))
212+
if ok := C.mongocrypt_ctx_setopt_query_type(ctx.wrapped, queryStr, -1); !ok {
213+
return nil, ctx.createErrorFromStatus()
224214
}
225215
}
226216

x/mongo/driver/mongocrypt/options/mongocrypt_context_options.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ type ExplicitEncryptionOptions struct {
4747
KeyID *primitive.Binary
4848
KeyAltName *string
4949
Algorithm string
50-
QueryType *QueryType
50+
QueryType string
5151
ContentionFactor *int64
5252
}
5353

@@ -75,8 +75,8 @@ func (eeo *ExplicitEncryptionOptions) SetAlgorithm(algorithm string) *ExplicitEn
7575
}
7676

7777
// SetQueryType specifies the query type.
78-
func (eeo *ExplicitEncryptionOptions) SetQueryType(queryType QueryType) *ExplicitEncryptionOptions {
79-
eeo.QueryType = &queryType
78+
func (eeo *ExplicitEncryptionOptions) SetQueryType(queryType string) *ExplicitEncryptionOptions {
79+
eeo.QueryType = queryType
8080
return eeo
8181
}
8282

0 commit comments

Comments
 (0)