Skip to content

Commit ead9823

Browse files
authored
extract json before unmarshal and use 65534 USER in dockerfile (#2333)
Signed-off-by: sayedppqq <sayed@appscode.com>
1 parent fd7c4b3 commit ead9823

File tree

3 files changed

+64
-30
lines changed

3 files changed

+64
-30
lines changed

Dockerfile.in

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ LABEL org.opencontainers.image.source https://github.com/stashed/mongodb
3535
COPY --from=0 /restic /bin/restic
3636
COPY bin/{ARG_OS}_{ARG_ARCH}/{ARG_BIN} /{ARG_BIN}
3737

38-
# https://github.com/docker-library/mongo/blob/master/6.0/Dockerfile#L12
39-
USER 999
38+
USER 65534
4039

4140
ENTRYPOINT ["/{ARG_BIN}"]

pkg/backup.go

Lines changed: 58 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -848,41 +848,41 @@ func enableBalancer(mongosHost string) error {
848848
}
849849

850850
func checkRoleExists(mongoDSN string) (bool, error) {
851-
v := make(map[string]interface{})
852851
args := append([]interface{}{
853852
"admin",
854853
"--host", mongoDSN,
855854
"--quiet",
856855
"--eval", `JSON.stringify(db.getRole("` + StashRoleName + `"))`,
857856
}, mongoCreds...)
858-
if err := sh.Command(MongoCMD, args...).Command("/usr/bin/tail", "-1").UnmarshalJSON(&v); err != nil {
857+
output, err := sh.Command(MongoCMD, args...).Command("/usr/bin/tail", "-1").Output()
858+
if err != nil {
859859
return false, err
860860
}
861861

862-
if val, ok := v["role"].(string); ok && string(val) == StashRoleName {
863-
return true, nil
862+
if strings.Contains(string(output), "null") {
863+
return false, nil
864864
}
865865

866-
return false, nil
866+
return true, nil
867867
}
868868

869869
func checkUserExists(mongoDSN string) (bool, error) {
870-
v := make(map[string]interface{})
871870
args := append([]interface{}{
872871
"admin",
873872
"--host", mongoDSN,
874873
"--quiet",
875874
"--eval", `JSON.stringify(db.getUser("` + StashUserName + `"))`,
876875
}, mongoCreds...)
877-
if err := sh.Command(MongoCMD, args...).Command("/usr/bin/tail", "-1").UnmarshalJSON(&v); err != nil {
876+
output, err := sh.Command(MongoCMD, args...).Command("/usr/bin/tail", "-1").Output()
877+
if err != nil {
878878
return false, err
879879
}
880880

881-
if val, ok := v["user"].(string); ok && string(val) == StashUserName {
882-
return true, nil
881+
if strings.Contains(string(output), "null") {
882+
return false, nil
883883
}
884884

885-
return false, nil
885+
return true, nil
886886
}
887887

888888
func createStashRoleAndUser(mongoDSN string, pass string) error {
@@ -910,7 +910,18 @@ func createStashBackupRole(mongoDSN string) error {
910910
"--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: []}))`,
911911
}, mongoCreds...)
912912

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

@@ -937,7 +948,18 @@ func createStashBackupUser(mongoDSN string, pass string) error {
937948
"--quiet",
938949
"--eval", `JSON.stringify(db.runCommand({createUser: "` + StashUserName + `" ,pwd: "` + pass + `", roles:[{role:"backup", db:"admin"}, {role: "` + StashRoleName + `",db:"admin"}]}))`,
939950
}, mongoCreds...)
940-
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+
956+
output, err = extractJSON(string(output))
957+
if err != nil {
958+
return err
959+
}
960+
961+
err = json.Unmarshal(output, &v)
962+
if err != nil {
941963
return err
942964
}
943965

@@ -949,24 +971,18 @@ func createStashBackupUser(mongoDSN string, pass string) error {
949971
}
950972

951973
func handleReshard(configsvrDSN string) (bool, error) {
952-
v := make([]interface{}, 0)
953974
args := append([]interface{}{
954975
"config",
955976
"--host", configsvrDSN,
956977
"--quiet",
957978
"--eval", `JSON.stringify(db.getCollectionNames())`,
958979
}, mongoCreds...)
959-
if err := sh.Command(MongoCMD, args...).Command("/usr/bin/tail", "-1").UnmarshalJSON(&v); err != nil {
980+
output, err := sh.Command(MongoCMD, args...).Command("/usr/bin/tail", "-1").Output()
981+
if err != nil {
982+
klog.Errorf("Error while calling getCollectionNames : %s ; output : %s \n", err.Error(), output)
960983
return false, err
961984
}
962-
963-
exists := false
964-
for _, name := range v {
965-
if name.(string) == "reshardingOperations" {
966-
exists = true
967-
break
968-
}
969-
}
985+
exists := strings.Contains(string(output), "reshardingOperations")
970986
if !exists {
971987
return false, nil
972988
}
@@ -997,7 +1013,16 @@ func handleReshard(configsvrDSN string) (bool, error) {
9971013
"--quiet",
9981014
"--eval", `JSON.stringify(db.adminCommand( { renameCollection: "config.reshardingOperations", to: "config.reshardingOperations_temp", dropTarget: true}))`,
9991015
}, mongoCreds...)
1000-
if err := sh.Command(MongoCMD, args...).Command("/usr/bin/tail", "-1").UnmarshalJSON(&res); err != nil {
1016+
output, err = sh.Command(MongoCMD, args...).Command("/usr/bin/tail", "-1").Output()
1017+
if err != nil {
1018+
return false, err
1019+
}
1020+
output, err = extractJSON(string(output))
1021+
if err != nil {
1022+
return false, err
1023+
}
1024+
err = json.Unmarshal(output, &res)
1025+
if err != nil {
10011026
return false, err
10021027
}
10031028
if val, ok := res["ok"].(float64); !ok || int(val) != 1 {
@@ -1015,7 +1040,16 @@ func renameTempReshardCollection(configsvrDSN string) error {
10151040
"--quiet",
10161041
"--eval", `JSON.stringify(db.adminCommand( { renameCollection: "config.reshardingOperations_temp", to: "config.reshardingOperations" } ))`,
10171042
}, mongoCreds...)
1018-
if err := sh.Command(MongoCMD, args...).Command("/usr/bin/tail", "-1").UnmarshalJSON(&res); err != nil {
1043+
output, err := sh.Command(MongoCMD, args...).Command("/usr/bin/tail", "-1").Output()
1044+
if err != nil {
1045+
return err
1046+
}
1047+
output, err = extractJSON(string(output))
1048+
if err != nil {
1049+
return err
1050+
}
1051+
err = json.Unmarshal(output, &res)
1052+
if err != nil {
10191053
return err
10201054
}
10211055
if val, ok := res["ok"].(float64); !ok || int(val) != 1 {

pkg/lock.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,12 @@ func setupConfigServer(configSVRDSN, secondaryHost string) error {
4141
"--eval", `JSON.stringify(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
4848
}
49+
4950
output, err = extractJSON(string(output))
5051
if err != nil {
5152
return err
@@ -63,7 +64,7 @@ func setupConfigServer(configSVRDSN, secondaryHost string) error {
6364
}
6465
val2 := float64(0)
6566
timer := 0 // wait approximately 5 minutes.
66-
v2 := make([]map[string]interface{}, 0)
67+
v2 := make(map[string]interface{}, 0)
6768
for timer < 60 && (int(val2) == 0 || int(val) != int(val2)) {
6869
timer++
6970
// find backupDocument from secondary configServer
@@ -84,13 +85,13 @@ func setupConfigServer(configSVRDSN, secondaryHost string) error {
8485
return err
8586
}
8687

87-
err = json.Unmarshal(output, &v)
88+
err = json.Unmarshal(output, &v2)
8889
if err != nil {
8990
return err
9091
}
9192

9293
if len(v2) > 0 {
93-
val2, ok = v2[0]["counter"].(float64)
94+
val2, ok = v2["counter"].(float64)
9495
if !ok {
9596
return fmt.Errorf("unable to get BackupControlDocument. got response: %v", v)
9697
}

0 commit comments

Comments
 (0)