@@ -57,7 +57,8 @@ import (
57
57
var (
58
58
MongoCMD = "/usr/bin/mongo"
59
59
OpenSSLCMD = "/usr/bin/openssl"
60
- adminCreds []interface {}
60
+ mongoCreds []interface {}
61
+ dumpCreds []interface {}
61
62
cleanupFuncs []func () error
62
63
)
63
64
@@ -246,16 +247,24 @@ func (opt *mongoOptions) backupMongoDB(targetRef api_v1beta1.TargetRef) (*restic
246
247
return nil , err
247
248
}
248
249
249
- appBindingSecret , err := opt .kubeClient .CoreV1 ().Secrets (opt .appBindingNamespace ).Get (context .TODO (), appBinding .Spec .Secret .Name , metav1.GetOptions {})
250
+ authSecret , err := opt .kubeClient .CoreV1 ().Secrets (opt .appBindingNamespace ).Get (context .TODO (), appBinding .Spec .Secret .Name , metav1.GetOptions {})
250
251
if err != nil {
251
252
return nil , err
252
253
}
253
254
254
- err = appBinding .TransformSecret (opt .kubeClient , appBindingSecret .Data )
255
+ err = appBinding .TransformSecret (opt .kubeClient , authSecret .Data )
255
256
if err != nil {
256
257
return nil , err
257
258
}
258
259
260
+ var tlsSecret * core.Secret
261
+ if appBinding .Spec .TLSSecret != nil {
262
+ tlsSecret , err = opt .kubeClient .CoreV1 ().Secrets (opt .appBindingNamespace ).Get (context .TODO (), appBinding .Spec .TLSSecret .Name , metav1.GetOptions {})
263
+ if err != nil {
264
+ return nil , err
265
+ }
266
+ }
267
+
259
268
hostname , err := appBinding .Hostname ()
260
269
if err != nil {
261
270
return nil , err
@@ -313,24 +322,34 @@ func (opt *mongoOptions) backupMongoDB(targetRef api_v1beta1.TargetRef) (*restic
313
322
}
314
323
315
324
if appBinding .Spec .ClientConfig .CABundle != nil {
325
+ if tlsSecret == nil {
326
+ return nil , errors .Wrap (err , "spec.tlsSecret needs to be set in appbinding for TLS secured database." )
327
+ }
328
+
316
329
if err := os .WriteFile (filepath .Join (opt .setupOptions .ScratchDir , MongoTLSCertFileName ), appBinding .Spec .ClientConfig .CABundle , os .ModePerm ); err != nil {
317
330
return nil , err
318
331
}
319
- adminCreds = []interface {}{
332
+ mongoCreds = []interface {}{
333
+ "--tls" ,
334
+ "--tlsCAFile" , filepath .Join (opt .setupOptions .ScratchDir , MongoTLSCertFileName ),
335
+ "--tlsCertificateKeyFile" , filepath .Join (opt .setupOptions .ScratchDir , MongoClientPemFileName ),
336
+ }
337
+ dumpCreds = []interface {}{
320
338
"--ssl" ,
321
339
"--sslCAFile" , filepath .Join (opt .setupOptions .ScratchDir , MongoTLSCertFileName ),
340
+ "--sslPEMKeyFile" , filepath .Join (opt .setupOptions .ScratchDir , MongoClientPemFileName ),
322
341
}
323
342
324
343
// get certificate secret to get client certificate
325
344
var pemBytes []byte
326
345
var ok bool
327
- pemBytes , ok = appBindingSecret .Data [MongoClientPemFileName ]
346
+ pemBytes , ok = tlsSecret .Data [MongoClientPemFileName ]
328
347
if ! ok {
329
- crt , ok := appBindingSecret .Data [core .TLSCertKey ]
348
+ crt , ok := tlsSecret .Data [core .TLSCertKey ]
330
349
if ! ok {
331
350
return nil , errors .Wrap (err , "unable to retrieve tls.crt from secret." )
332
351
}
333
- key , ok := appBindingSecret .Data [core .TLSPrivateKeyKey ]
352
+ key , ok := tlsSecret .Data [core .TLSPrivateKeyKey ]
334
353
if ! ok {
335
354
return nil , errors .Wrap (err , "unable to retrieve tls.key from secret." )
336
355
}
@@ -344,18 +363,22 @@ func (opt *mongoOptions) backupMongoDB(targetRef api_v1beta1.TargetRef) (*restic
344
363
if err != nil {
345
364
return nil , errors .Wrap (err , "unable to get user from ssl." )
346
365
}
347
- adminCreds = append (adminCreds , []interface {}{
348
- "--sslPEMKeyFile" , filepath .Join (opt .setupOptions .ScratchDir , MongoClientPemFileName ),
366
+ userAuth := []interface {}{
349
367
"-u" , user ,
350
368
"--authenticationMechanism" , "MONGODB-X509" ,
351
369
"--authenticationDatabase" , "$external" ,
352
- }... )
370
+ }
371
+ mongoCreds = append (mongoCreds , userAuth ... )
372
+ dumpCreds = append (dumpCreds , userAuth ... )
373
+
353
374
} else {
354
- adminCreds = []interface {}{
355
- fmt .Sprintf ("--username=%s" , appBindingSecret .Data [MongoUserKey ]),
356
- fmt .Sprintf ("--password=%s" , appBindingSecret .Data [MongoPasswordKey ]),
375
+ userAuth : = []interface {}{
376
+ fmt .Sprintf ("--username=%s" , authSecret .Data [MongoUserKey ]),
377
+ fmt .Sprintf ("--password=%s" , authSecret .Data [MongoPasswordKey ]),
357
378
"--authenticationDatabase" , opt .authenticationDatabase ,
358
379
}
380
+ mongoCreds = append (mongoCreds , userAuth ... )
381
+ dumpCreds = append (dumpCreds , userAuth ... )
359
382
}
360
383
361
384
getBackupOpt := func (mongoDSN , hostKey string , isStandalone bool ) restic.BackupOptions {
@@ -373,7 +396,7 @@ func (opt *mongoOptions) backupMongoDB(targetRef api_v1beta1.TargetRef) (*restic
373
396
Args : append ([]interface {}{
374
397
"--host" , mongoDSN ,
375
398
"--archive" ,
376
- }, adminCreds ... ),
399
+ }, dumpCreds ... ),
377
400
}
378
401
userArgs := strings .Fields (opt .mongoArgs )
379
402
@@ -533,7 +556,7 @@ func getPrimaryNSecondaryMember(mongoDSN string) (primary, secondary string, err
533
556
"--host" , mongoDSN ,
534
557
"--quiet" ,
535
558
"--eval" , "JSON.stringify(rs.isMaster())" ,
536
- }, adminCreds ... )
559
+ }, mongoCreds ... )
537
560
// even --quiet doesn't skip replicaset PrimaryConnection log. so take tha last line. issue tracker: https://jira.mongodb.org/browse/SERVER-27159
538
561
if err := sh .Command (MongoCMD , args ... ).Command ("/usr/bin/tail" , "-1" ).UnmarshalJSON (& v ); err != nil {
539
562
return "" , "" , err
@@ -574,9 +597,9 @@ func disabelBalancer(mongosHost string) error {
574
597
"--host" , mongosHost ,
575
598
"--quiet" ,
576
599
"--eval" , "JSON.stringify(sh.stopBalancer())" ,
577
- }, adminCreds ... )
600
+ }, mongoCreds ... )
578
601
// disable balancer
579
- if err := sh .Command (MongoCMD , args ... ).UnmarshalJSON (& v ); err != nil {
602
+ if err := sh .Command (MongoCMD , args ... ).Command ( "/usr/bin/tail" , "-1" ). UnmarshalJSON (& v ); err != nil {
580
603
return err
581
604
}
582
605
@@ -590,8 +613,8 @@ func disabelBalancer(mongosHost string) error {
590
613
"--host" , mongosHost ,
591
614
"--quiet" ,
592
615
"--eval" , "while(sh.isBalancerRunning()){ print('waiting for balancer to stop...'); sleep(1000);}" ,
593
- }, adminCreds ... )
594
- if err := sh .Command (MongoCMD , args ... ).Run (); err != nil {
616
+ }, mongoCreds ... )
617
+ if err := sh .Command (MongoCMD , args ... ).Command ( "/usr/bin/tail" , "-1" ). Run (); err != nil {
595
618
return err
596
619
}
597
620
return nil
@@ -608,8 +631,8 @@ func enableBalancer(mongosHost string) error {
608
631
"--host" , mongosHost ,
609
632
"--quiet" ,
610
633
"--eval" , "JSON.stringify(sh.setBalancerState(true))" ,
611
- }, adminCreds ... )
612
- if err := sh .Command (MongoCMD , args ... ).UnmarshalJSON (& v ); err != nil {
634
+ }, mongoCreds ... )
635
+ if err := sh .Command (MongoCMD , args ... ).Command ( "/usr/bin/tail" , "-1" ). UnmarshalJSON (& v ); err != nil {
613
636
return err
614
637
}
615
638
@@ -634,8 +657,8 @@ func lockConfigServer(configSVRDSN, secondaryHost string) error {
634
657
"--host" , configSVRDSN ,
635
658
"--quiet" ,
636
659
"--eval" , "db.BackupControl.findAndModify({query: { _id: 'BackupControlDocument' }, update: { $inc: { counter : 1 } }, new: true, upsert: true, writeConcern: { w: 'majority', wtimeout: 15000 }});" ,
637
- }, adminCreds ... )
638
- if err := sh .Command (MongoCMD , args ... ).Command ("tail" , "-1" ).UnmarshalJSON (& v ); err != nil {
660
+ }, mongoCreds ... )
661
+ if err := sh .Command (MongoCMD , args ... ).Command ("/usr/bin/ tail" , "-1" ).UnmarshalJSON (& v ); err != nil {
639
662
return err
640
663
}
641
664
val , ok := v ["counter" ].(float64 )
@@ -651,8 +674,8 @@ func lockConfigServer(configSVRDSN, secondaryHost string) error {
651
674
"config" ,
652
675
"--host" , secondaryHost ,
653
676
"--quiet" ,
654
- "--eval" , "rs.slaveOk (); db.BackupControl.find({ '_id' : 'BackupControlDocument' }).readConcern('majority');" ,
655
- }, adminCreds ... )
677
+ "--eval" , "rs.secondaryOk (); db.BackupControl.find({ '_id' : 'BackupControlDocument' }).readConcern('majority');" ,
678
+ }, mongoCreds ... )
656
679
657
680
if err := sh .Command (MongoCMD , args ... ).UnmarshalJSON (& v ); err != nil {
658
681
return err
@@ -688,8 +711,8 @@ func lockSecondaryMember(mongohost string) error {
688
711
"--host" , mongohost ,
689
712
"--quiet" ,
690
713
"--eval" , "JSON.stringify(db.fsyncLock())" ,
691
- }, adminCreds ... )
692
- if err := sh .Command (MongoCMD , args ... ).UnmarshalJSON (& v ); err != nil {
714
+ }, mongoCreds ... )
715
+ if err := sh .Command (MongoCMD , args ... ).Command ( "/usr/bin/tail" , "-1" ). UnmarshalJSON (& v ); err != nil {
693
716
return err
694
717
}
695
718
@@ -714,8 +737,8 @@ func unlockSecondaryMember(mongohost string) error {
714
737
"--host" , mongohost ,
715
738
"--quiet" ,
716
739
"--eval" , "JSON.stringify(db.fsyncUnlock())" ,
717
- }, adminCreds ... )
718
- if err := sh .Command (MongoCMD , args ... ).UnmarshalJSON (& v ); err != nil {
740
+ }, mongoCreds ... )
741
+ if err := sh .Command (MongoCMD , args ... ).Command ( "/usr/bin/tail" , "-1" ). UnmarshalJSON (& v ); err != nil {
719
742
return err
720
743
}
721
744
0 commit comments