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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
|
// Copyright (C) MongoDB, Inc. 2014-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
package mongorestore
import (
"encoding/hex"
"fmt"
"github.com/mongodb/mongo-tools-common/db"
"github.com/mongodb/mongo-tools-common/intents"
"github.com/mongodb/mongo-tools-common/log"
"github.com/mongodb/mongo-tools-common/util"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
)
// Specially treated restore collection types.
const (
Users = "users"
Roles = "roles"
)
// struct for working with auth versions
type authVersionPair struct {
// Dump is the auth version of the users/roles collection files in the target dump directory
Dump int
// Server is the auth version of the connected MongoDB server
Server int
}
// Metadata holds information about a collection's options and indexes.
type Metadata struct {
Options bson.D `bson:"options,omitempty"`
Indexes []IndexDocument `bson:"indexes"`
UUID string `bson:"uuid"`
}
// IndexDocument holds information about a collection's index.
type IndexDocument struct {
Options bson.M `bson:",inline"`
Key bson.D `bson:"key"`
PartialFilterExpression bson.D `bson:"partialFilterExpression,omitempty"`
}
// MetadataFromJSON takes a slice of JSON bytes and unmarshals them into usable
// collection options and indexes for restoring collections.
func (restore *MongoRestore) MetadataFromJSON(jsonBytes []byte) (*Metadata, error) {
if len(jsonBytes) == 0 {
// skip metadata parsing if the file is empty
return nil, nil
}
meta := &Metadata{}
err := bson.UnmarshalExtJSON(jsonBytes, true, meta)
if err != nil {
return nil, err
}
return meta, nil
}
// LoadIndexesFromBSON reads indexes from the index BSON files and
// caches them in the MongoRestore object.
func (restore *MongoRestore) LoadIndexesFromBSON() error {
dbCollectionIndexes := make(map[string]collectionIndexes)
for _, dbname := range restore.manager.SystemIndexDBs() {
dbCollectionIndexes[dbname] = make(collectionIndexes)
intent := restore.manager.SystemIndexes(dbname)
err := intent.BSONFile.Open()
if err != nil {
return err
}
defer intent.BSONFile.Close()
bsonSource := db.NewDecodedBSONSource(db.NewBSONSource(intent.BSONFile))
defer bsonSource.Close()
// iterate over stored indexes, saving all that match the collection
for {
indexDocument := IndexDocument{}
if !bsonSource.Next(&indexDocument) {
break
}
namespace := indexDocument.Options["ns"].(string)
dbCollectionIndexes[dbname][stripDBFromNS(namespace)] =
append(dbCollectionIndexes[dbname][stripDBFromNS(namespace)], indexDocument)
}
if err := bsonSource.Err(); err != nil {
return fmt.Errorf("error scanning system.indexes: %v", err)
}
}
restore.dbCollectionIndexes = dbCollectionIndexes
return nil
}
func stripDBFromNS(ns string) string {
_, c := util.SplitNamespace(ns)
return c
}
// CollectionExists returns true if the given intent's collection exists.
func (restore *MongoRestore) CollectionExists(intent *intents.Intent) (bool, error) {
restore.knownCollectionsMutex.Lock()
defer restore.knownCollectionsMutex.Unlock()
// make sure the map exists
if restore.knownCollections == nil {
restore.knownCollections = map[string][]string{}
}
// first check if we haven't done listCollections for this database already
if restore.knownCollections[intent.DB] == nil {
// if the database name isn't in the cache, grab collection
// names from the server
session, err := restore.SessionProvider.GetSession()
if err != nil {
return false, fmt.Errorf("error establishing connection: %v", err)
}
collections, err := session.Database(intent.DB).ListCollections(nil, bson.M{})
if err != nil {
return false, err
}
// update the cache
for collections.Next(nil) {
colNameRaw := collections.Current.Lookup("name")
colName, ok := colNameRaw.StringValueOK()
if !ok {
return false, fmt.Errorf("invalid collection name: %v", colNameRaw)
}
restore.knownCollections[intent.DB] = append(restore.knownCollections[intent.DB], colName)
}
}
// now check the cache for the given collection name
exists := util.StringSliceContains(restore.knownCollections[intent.DB], intent.C)
return exists, nil
}
// CreateIndexes takes in an intent and an array of index documents and
// attempts to create them using the createIndexes command. If that command
// fails, we fall back to individual index creation.
func (restore *MongoRestore) CreateIndexes(intent *intents.Intent, indexes []IndexDocument, hasNonSimpleCollation bool) error {
// first, sanitize the indexes
for _, index := range indexes {
// update the namespace of the index before inserting
index.Options["ns"] = intent.Namespace()
// check for length violations before building the command
fullIndexName := fmt.Sprintf("%v.$%v", index.Options["ns"], index.Options["name"])
if len(fullIndexName) > 127 {
return fmt.Errorf(
"cannot restore index with namespace '%v': "+
"namespace is too long (max size is 127 bytes)", fullIndexName)
}
// remove the index version, forcing an update,
// unless we specifically want to keep it
if !restore.OutputOptions.KeepIndexVersion {
delete(index.Options, "v")
}
// for non-simple default collation on the collection, indexes without
// a collation option need to add "collation:{locale:"simple"}}
if _, ok := index.Options["collation"]; hasNonSimpleCollation && !ok {
index.Options["collation"] = bson.D{{"locale", "simple"}}
}
}
session, err := restore.SessionProvider.GetSession()
if err != nil {
return fmt.Errorf("error establishing connection: %v", err)
}
// then attempt the createIndexes command
rawCommand := bson.D{
{"createIndexes", intent.C},
{"indexes", indexes},
}
err = session.Database(intent.DB).RunCommand(nil, rawCommand).Err()
if err == nil {
return nil
}
if err.Error() != "no such cmd: createIndexes" {
return fmt.Errorf("createIndex error: %v", err)
}
// if we're here, the connected server does not support the command, so we fall back
log.Logv(log.Info, "\tcreateIndexes command not supported, attemping legacy index insertion")
for _, idx := range indexes {
log.Logvf(log.Info, "\tmanually creating index %v", idx.Options["name"])
err = restore.LegacyInsertIndex(intent, idx)
if err != nil {
return fmt.Errorf("error creating index %v: %v", idx.Options["name"], err)
}
}
return nil
}
// LegacyInsertIndex takes in an intent and an index document and attempts to
// create the index on the "system.indexes" collection.
func (restore *MongoRestore) LegacyInsertIndex(intent *intents.Intent, index IndexDocument) error {
session, err := restore.SessionProvider.GetSession()
if err != nil {
return fmt.Errorf("error establishing connection: %v", err)
}
indexCollection := session.Database(intent.DB).Collection("system.indexes")
_, err = indexCollection.InsertOne(nil, index)
if err != nil {
return fmt.Errorf("insert error: %v", err)
}
return nil
}
// CreateCollection creates the collection specified in the intent with the
// given options.
func (restore *MongoRestore) CreateCollection(intent *intents.Intent, options bson.D, uuid string) error {
session, err := restore.SessionProvider.GetSession()
if err != nil {
return fmt.Errorf("error establishing connection: %v", err)
}
switch {
case uuid != "":
return restore.createCollectionWithApplyOps(session, intent, options, uuid)
default:
return restore.createCollectionWithCommand(session, intent, options)
}
}
func (restore *MongoRestore) createCollectionWithCommand(session *mongo.Client, intent *intents.Intent, options bson.D) error {
command := createCollectionCommand(intent, options)
// If there is no error, the result doesnt matter
singleRes := session.Database(intent.DB).RunCommand(nil, command, nil)
if err := singleRes.Err(); err != nil {
return fmt.Errorf("error running create command: %v", err)
}
res := bson.M{}
singleRes.Decode(&res)
if util.IsFalsy(res["ok"]) {
return fmt.Errorf("create command: %v", res["errmsg"])
}
return nil
}
func (restore *MongoRestore) createCollectionWithApplyOps(session *mongo.Client, intent *intents.Intent, options bson.D, uuidHex string) error {
command := createCollectionCommand(intent, options)
uuid, err := hex.DecodeString(uuidHex)
if err != nil {
return fmt.Errorf("Couldn't restore UUID because UUID was invalid: %s", err)
}
createOp := struct {
Operation string `bson:"op"`
Namespace string `bson:"ns"`
Object bson.D `bson:"o"`
UI *primitive.Binary `bson:"ui,omitempty"`
}{
Operation: "c",
Namespace: intent.DB + ".$cmd",
Object: command,
UI: &primitive.Binary{Subtype: 0x04, Data: uuid},
}
return restore.ApplyOps(session, []interface{}{createOp})
}
func createCollectionCommand(intent *intents.Intent, options bson.D) bson.D {
return append(bson.D{{"create", intent.C}}, options...)
}
// RestoreUsersOrRoles accepts a users intent and a roles intent, and restores
// them via _mergeAuthzCollections. Either or both can be nil. In the latter case
// nothing is done.
//
// _mergeAuthzCollections is an internal server command implemented specifically for mongorestore. Instead of inserting
// into the admin.system.{roles, users} collections (which isn't allowed due to some locking policies), we construct
// temporary collections that are then merged with or replace the existing ones.
//
// The "drop" argument that determines whether the merge replaces the existing users/roles or adds to them.
//
// The "db" argument determines which databases' users and roles are merged. If left blank, it merges users and roles
// from all databases. When the user restores the admin database, we assume they wish to restore the users and roles for
// all databases, not just the admin ones, so we leave the "db" field blank in that case.
//
// The "temp{Users,Roles}Collection" arguments determine which temporary collections to merge from, and the presence of
// either determines which collection to merge to. (e.g. if tempUsersCollection is defined, admin.system.users is merged
// into).
//
// This command must be run on the "admin" database. Thus, the temporary collections must be on the admin db as well.
// This command must also be run on the primary.
//
// Example command:
// {
// _mergeAuthzCollections: 1,
// db: "foo",
// tempUsersCollection: "myTempUsers"
// drop: true
// writeConcern: {w: "majority"}
// }
//
func (restore *MongoRestore) RestoreUsersOrRoles(users, roles *intents.Intent) error {
type loopArg struct {
intent *intents.Intent
intentType string
mergeParamName string
tempCollectionName string
}
if users == nil && roles == nil {
return nil
}
if users != nil && roles != nil && users.DB != roles.DB {
return fmt.Errorf("can't restore users and roles to different databases, %v and %v", users.DB, roles.DB)
}
args := []loopArg{}
mergeArgs := bson.D{}
userTargetDB := ""
if users != nil {
args = append(args, loopArg{users, "users", "tempUsersCollection", restore.OutputOptions.TempUsersColl})
}
if roles != nil {
args = append(args, loopArg{roles, "roles", "tempRolesCollection", restore.OutputOptions.TempRolesColl})
}
session, err := restore.SessionProvider.GetSession()
if err != nil {
return fmt.Errorf("error establishing connection: %v", err)
}
// For each of the users and roles intents:
// build up the mergeArgs component of the _mergeAuthzCollections command
// upload the BSONFile to a temporary collection
for _, arg := range args {
if arg.intent.Size == 0 {
// MongoDB complains if we try and remove a non-existent collection, so we should
// just skip auth collections with empty .bson files to avoid gnarly logic later on.
log.Logvf(log.Always, "%v file '%v' is empty; skipping %v restoration", arg.intentType, arg.intent.Location, arg.intentType)
}
log.Logvf(log.Always, "restoring %v from %v", arg.intentType, arg.intent.Location)
mergeArgs = append(mergeArgs, bson.E{
Key: arg.mergeParamName,
Value: "admin." + arg.tempCollectionName,
})
err := arg.intent.BSONFile.Open()
if err != nil {
return err
}
defer arg.intent.BSONFile.Close()
bsonSource := db.NewDecodedBSONSource(db.NewBSONSource(arg.intent.BSONFile))
defer bsonSource.Close()
tempCollectionNameExists, err := restore.CollectionExists(&intents.Intent{DB: "admin", C: arg.tempCollectionName})
if err != nil {
return err
}
if tempCollectionNameExists {
log.Logvf(log.Info, "dropping preexisting temporary collection admin.%v", arg.tempCollectionName)
err = session.Database("admin").Collection(arg.tempCollectionName).Drop(nil)
if err != nil {
return fmt.Errorf("error dropping preexisting temporary collection %v: %v", arg.tempCollectionName, err)
}
}
log.Logvf(log.DebugLow, "restoring %v to temporary collection", arg.intentType)
result := restore.RestoreCollectionToDB("admin", arg.tempCollectionName, bsonSource, arg.intent.BSONFile, 0)
if result.Err != nil {
return fmt.Errorf("error restoring %v: %v", arg.intentType, result.Err)
}
// make sure we always drop the temporary collection
defer func() {
session, e := restore.SessionProvider.GetSession()
if e != nil {
// logging errors here because this has no way of returning that doesn't mask other errors
log.Logvf(log.Info, "error establishing connection to drop temporary collection admin.%v: %v", arg.tempCollectionName, e)
return
}
log.Logvf(log.DebugHigh, "dropping temporary collection admin.%v", arg.tempCollectionName)
e = session.Database("admin").Collection(arg.tempCollectionName).Drop(nil)
if e != nil {
log.Logvf(log.Info, "error dropping temporary collection admin.%v: %v", arg.tempCollectionName, e)
}
}()
userTargetDB = arg.intent.DB
}
if userTargetDB == "admin" {
// _mergeAuthzCollections uses an empty db string as a sentinel for "all databases"
userTargetDB = ""
}
adminDB := session.Database("admin")
command := bson.D{}
command = append(command,
bson.E{Key: "_mergeAuthzCollections", Value: 1})
command = append(command,
mergeArgs...)
command = append(command,
bson.E{Key: "drop", Value: restore.OutputOptions.Drop},
bson.E{Key: "db", Value: userTargetDB})
if restore.ToolOptions.WriteConcern != nil {
_, wcBson, err := restore.ToolOptions.WriteConcern.MarshalBSONValue()
if err != nil {
return fmt.Errorf("error parsing write concern: %v", err)
}
writeConcern := bson.M{}
err = bson.Unmarshal(wcBson, &writeConcern)
if err != nil {
return fmt.Errorf("error parsing write concern: %v", err)
}
command = append(command, bson.E{Key: "writeConcern", Value: writeConcern})
}
log.Logvf(log.DebugLow, "merging users/roles from temp collections")
resSingle := adminDB.RunCommand(nil, command)
if err = resSingle.Err(); err != nil {
return fmt.Errorf("error running merge command: %v", err)
}
res := bson.M{}
resSingle.Decode(&res)
if util.IsFalsy(res["ok"]) {
return fmt.Errorf("_mergeAuthzCollections command: %v", res["errmsg"])
}
return nil
}
// GetDumpAuthVersion reads the admin.system.version collection in the dump directory
// to determine the authentication version of the files in the dump. If that collection is not
// present in the dump, we try to infer the authentication version based on its absence.
// Returns the authentication version number and any errors that occur.
func (restore *MongoRestore) GetDumpAuthVersion() (int, error) {
// first handle the case where we have no auth version
intent := restore.manager.AuthVersion()
if intent == nil {
if restore.InputOptions.RestoreDBUsersAndRoles {
// If we are using --restoreDbUsersAndRoles, we cannot guarantee an
// $admin.system.version collection from a 2.6 server,
// so we can assume up to version 3.
log.Logvf(log.Always, "no system.version bson file found in '%v' database dump", restore.NSOptions.DB)
log.Logv(log.Always, "warning: assuming users and roles collections are of auth version 3")
log.Logv(log.Always, "if users are from an earlier version of MongoDB, they may not restore properly")
return 3, nil
}
log.Logv(log.Info, "no system.version bson file found in dump")
log.Logv(log.Always, "assuming users in the dump directory are from <= 2.4 (auth version 1)")
return 1, nil
}
err := intent.BSONFile.Open()
if err != nil {
return 0, err
}
defer intent.BSONFile.Close()
bsonSource := db.NewDecodedBSONSource(db.NewBSONSource(intent.BSONFile))
defer bsonSource.Close()
for {
versionDoc := bson.M{}
if !bsonSource.Next(&versionDoc) {
break
}
id, ok := versionDoc["_id"].(string)
if ok && id == "authSchema" {
switch authVersion := versionDoc["currentVersion"].(type) {
case int:
return authVersion, nil
case int32:
return int(authVersion), nil
case int64:
return int(authVersion), nil
default:
return 0, fmt.Errorf("can't unmarshal system.version curentVersion as an int: %v", versionDoc["currentVersion"])
}
}
log.Logvf(log.DebugLow, "system.version document is not an authSchema %v", versionDoc["_id"])
}
err = bsonSource.Err()
if err != nil {
log.Logvf(log.Info, "can't unmarshal system.version document: %v", err)
}
return 0, fmt.Errorf("system.version bson file does not have authSchema document")
}
// ValidateAuthVersions compares the authentication version of the dump files and the
// authentication version of the target server, and returns an error if the versions
// are incompatible.
func (restore *MongoRestore) ValidateAuthVersions() error {
if restore.authVersions.Dump == 2 || restore.authVersions.Dump == 4 {
return fmt.Errorf(
"cannot restore users and roles from a dump file with auth version %v; "+
"finish the upgrade or roll it back", restore.authVersions.Dump)
}
if restore.authVersions.Server == 2 || restore.authVersions.Server == 4 {
return fmt.Errorf(
"cannot restore users and roles to a server with auth version %v; "+
"finish the upgrade or roll it back", restore.authVersions.Server)
}
switch restore.authVersions {
case authVersionPair{3, 5}:
log.Logv(log.Info,
"restoring users and roles of auth version 3 to a server of auth version 5")
case authVersionPair{5, 5}:
log.Logv(log.Info,
"restoring users and roles of auth version 5 to a server of auth version 5")
case authVersionPair{3, 3}:
log.Logv(log.Info,
"restoring users and roles of auth version 3 to a server of auth version 3")
case authVersionPair{1, 1}:
log.Logv(log.Info,
"restoring users and roles of auth version 1 to a server of auth version 1")
case authVersionPair{1, 5}:
return fmt.Errorf("cannot restore users of auth version 1 to a server of auth version 5")
case authVersionPair{5, 3}:
return fmt.Errorf("cannot restore users of auth version 5 to a server of auth version 3")
case authVersionPair{1, 3}:
log.Logv(log.Info,
"restoring users and roles of auth version 1 to a server of auth version 3")
log.Logv(log.Always,
"users and roles will have to be updated with the authSchemaUpgrade command")
case authVersionPair{5, 1}:
fallthrough
case authVersionPair{3, 1}:
return fmt.Errorf(
"cannot restore users and roles dump file >= auth version 3 to a server of auth version 1")
default:
return fmt.Errorf("invalid auth pair: dump=%v, server=%v",
restore.authVersions.Dump, restore.authVersions.Server)
}
return nil
}
// ShouldRestoreUsersAndRoles returns true if mongorestore should go through
// through the process of restoring collections pertaining to authentication.
func (restore *MongoRestore) ShouldRestoreUsersAndRoles() bool {
if restore.SkipUsersAndRoles {
return false
}
// If the user has done anything that would indicate the restoration
// of users and roles (i.e. used --restoreDbUsersAndRoles, -d admin, or
// is doing a full restore), then we check if users or roles BSON files
// actually exist in the dump dir. If they do, return true.
if restore.InputOptions.RestoreDBUsersAndRoles ||
restore.NSOptions.DB == "" ||
restore.NSOptions.DB == "admin" {
if restore.manager.Users() != nil || restore.manager.Roles() != nil {
return true
}
}
return false
}
// DropCollection drops the intent's collection.
func (restore *MongoRestore) DropCollection(intent *intents.Intent) error {
session, err := restore.SessionProvider.GetSession()
if err != nil {
return fmt.Errorf("error establishing connection: %v", err)
}
err = session.Database(intent.DB).Collection(intent.C).Drop(nil)
if err != nil {
return fmt.Errorf("error dropping collection: %v", err)
}
return nil
}
|