Skip to content

Commit e4b17e7

Browse files
authored
Extract json before unmarshal (#2334)
Signed-off-by: sayedppqq <sayed@appscode.com>
1 parent 3f2ad4c commit e4b17e7

File tree

2 files changed

+55
-25
lines changed

2 files changed

+55
-25
lines changed

pkg/backup.go

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -850,41 +850,41 @@ func enableBalancer(mongosHost string) error {
850850
}
851851

852852
func checkRoleExists(mongoDSN string) (bool, error) {
853-
v := make(map[string]interface{})
854853
args := append([]interface{}{
855854
"admin",
856855
"--host", mongoDSN,
857856
"--quiet",
858857
"--eval", `JSON.stringify(db.getRole("` + StashRoleName + `"))`,
859858
}, mongoCreds...)
860-
if err := sh.Command(MongoCMD, args...).Command("/usr/bin/tail", "-1").UnmarshalJSON(&v); err != nil {
859+
output, err := sh.Command(MongoCMD, args...).Command("/usr/bin/tail", "-1").Output()
860+
if err != nil {
861861
return false, err
862862
}
863863

864-
if val, ok := v["role"].(string); ok && string(val) == StashRoleName {
865-
return true, nil
864+
if strings.Contains(string(output), "null") {
865+
return false, nil
866866
}
867867

868-
return false, nil
868+
return true, nil
869869
}
870870

871871
func checkUserExists(mongoDSN string) (bool, error) {
872-
v := make(map[string]interface{})
873872
args := append([]interface{}{
874873
"admin",
875874
"--host", mongoDSN,
876875
"--quiet",
877876
"--eval", `JSON.stringify(db.getUser("` + StashUserName + `"))`,
878877
}, mongoCreds...)
879-
if err := sh.Command(MongoCMD, args...).Command("/usr/bin/tail", "-1").UnmarshalJSON(&v); err != nil {
878+
output, err := sh.Command(MongoCMD, args...).Command("/usr/bin/tail", "-1").Output()
879+
if err != nil {
880880
return false, err
881881
}
882882

883-
if val, ok := v["user"].(string); ok && string(val) == StashUserName {
884-
return true, nil
883+
if strings.Contains(string(output), "null") {
884+
return false, nil
885885
}
886886

887-
return false, nil
887+
return true, nil
888888
}
889889

890890
func createStashRoleAndUser(mongoDSN string, pass string) error {
@@ -912,7 +912,16 @@ func createStashBackupRole(mongoDSN string) error {
912912
"--eval", `JSON.stringify(db.runCommand({createRole: "` + StashRoleName + `",privileges:[{resource:{db:"config",collection:"system.preimages"},actions:["find"]},{resource:{db:"config",collection:"system.sharding_ddl_coordinators"},actions:["find"]},{resource:{db:"config",collection:"system.*"},actions:["find"]}],roles: []}))`,
913913
}, mongoCreds...)
914914

915-
if err := sh.Command(MongoCMD, args...).Command("/usr/bin/tail", "-1").UnmarshalJSON(&v); err != nil {
915+
output, err := sh.Command(MongoCMD, args...).Command("/usr/bin/tail", "-1").Output()
916+
if err != nil {
917+
return err
918+
}
919+
output, err = extractJSON(string(output))
920+
if err != nil {
921+
return err
922+
}
923+
err = json.Unmarshal(output, &v)
924+
if err != nil {
916925
return err
917926
}
918927

@@ -939,7 +948,16 @@ func createStashBackupUser(mongoDSN string, pass string) error {
939948
"--quiet",
940949
"--eval", `JSON.stringify(db.runCommand({createUser: "` + StashUserName + `" ,pwd: "` + pass + `", roles:[{role:"backup", db:"admin"}, {role: "` + StashRoleName + `",db:"admin"}]}))`,
941950
}, mongoCreds...)
942-
if err := sh.Command(MongoCMD, args...).Command("/usr/bin/tail", "-1").UnmarshalJSON(&v); err != nil {
951+
output, err := sh.Command(MongoCMD, args...).Command("/usr/bin/tail", "-1").Output()
952+
if err != nil {
953+
return err
954+
}
955+
output, err = extractJSON(string(output))
956+
if err != nil {
957+
return err
958+
}
959+
err = json.Unmarshal(output, &v)
960+
if err != nil {
943961
return err
944962
}
945963

@@ -951,24 +969,18 @@ func createStashBackupUser(mongoDSN string, pass string) error {
951969
}
952970

953971
func handleReshard(configsvrDSN string) (bool, error) {
954-
v := make([]interface{}, 0)
955972
args := append([]interface{}{
956973
"config",
957974
"--host", configsvrDSN,
958975
"--quiet",
959976
"--eval", `JSON.stringify(db.getCollectionNames())`,
960977
}, mongoCreds...)
961-
if err := sh.Command(MongoCMD, args...).Command("/usr/bin/tail", "-1").UnmarshalJSON(&v); err != nil {
978+
output, err := sh.Command(MongoCMD, args...).Command("/usr/bin/tail", "-1").Output()
979+
if err != nil {
980+
klog.Errorf("Error while calling getCollectionNames : %s ; output : %s \n", err.Error(), output)
962981
return false, err
963982
}
964-
965-
exists := false
966-
for _, name := range v {
967-
if name.(string) == "reshardingOperations" {
968-
exists = true
969-
break
970-
}
971-
}
983+
exists := strings.Contains(string(output), "reshardingOperations")
972984
if !exists {
973985
return false, nil
974986
}
@@ -999,7 +1011,16 @@ func handleReshard(configsvrDSN string) (bool, error) {
9991011
"--quiet",
10001012
"--eval", `JSON.stringify(db.adminCommand( { renameCollection: "config.reshardingOperations", to: "config.reshardingOperations_temp", dropTarget: true}))`,
10011013
}, mongoCreds...)
1002-
if err := sh.Command(MongoCMD, args...).Command("/usr/bin/tail", "-1").UnmarshalJSON(&res); err != nil {
1014+
output, err = sh.Command(MongoCMD, args...).Command("/usr/bin/tail", "-1").Output()
1015+
if err != nil {
1016+
return false, err
1017+
}
1018+
output, err = extractJSON(string(output))
1019+
if err != nil {
1020+
return false, err
1021+
}
1022+
err = json.Unmarshal(output, &res)
1023+
if err != nil {
10031024
return false, err
10041025
}
10051026
if val, ok := res["ok"].(float64); !ok || int(val) != 1 {
@@ -1017,7 +1038,16 @@ func renameTempReshardCollection(configsvrDSN string) error {
10171038
"--quiet",
10181039
"--eval", `JSON.stringify(db.adminCommand( { renameCollection: "config.reshardingOperations_temp", to: "config.reshardingOperations" } ))`,
10191040
}, mongoCreds...)
1020-
if err := sh.Command(MongoCMD, args...).Command("/usr/bin/tail", "-1").UnmarshalJSON(&res); err != nil {
1041+
output, err := sh.Command(MongoCMD, args...).Command("/usr/bin/tail", "-1").Output()
1042+
if err != nil {
1043+
return err
1044+
}
1045+
output, err = extractJSON(string(output))
1046+
if err != nil {
1047+
return err
1048+
}
1049+
err = json.Unmarshal(output, &res)
1050+
if err != nil {
10211051
return err
10221052
}
10231053
if val, ok := res["ok"].(float64); !ok || int(val) != 1 {

pkg/lock.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func setupConfigServer(configSVRDSN, secondaryHost string) error {
4141
"--eval", "db.BackupControl.findAndModify({query: { _id: 'BackupControlDocument' }, update: { $inc: { counter : 1 } }, new: true, upsert: true, writeConcern: { w: 'majority', wtimeout: 15000 }});",
4242
}, mongoCreds...)
4343

44-
output, err := sh.Command(MongoCMD, args...).Output()
44+
output, err := sh.Command(MongoCMD, args...).Command("/usr/bin/tail", "-1").Output()
4545
if err != nil {
4646
klog.Errorf("Error while running findAndModify to setup configServer : %s ; output : %s \n", err.Error(), output)
4747
return err

0 commit comments

Comments
 (0)