Skip to content

Commit 8dc7e8d

Browse files
committed
fix: add URI parameter to MongoDB backup validation
- Add password decryption from database.Mongodb - Build MongoDB URI using BuildMongodumpURI() method - Add --uri parameter to mongorestore --dryRun command (required) - Add safe logging with password masking in URI - Fixes 'exit status 1' error when validating MongoDB backups
1 parent 7d58b6b commit 8dc7e8d

File tree

1 file changed

+37
-4
lines changed

1 file changed

+37
-4
lines changed

backend/internal/features/backups/backups/usecases/mongodb/validate_backup_uc.go

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,18 @@ func (uc *ValidateMongodbBackupUsecase) Execute(
5454
}, nil
5555
}
5656

57+
// Decrypt password for URI construction
58+
decryptedPassword, err := uc.fieldEncryptor.Decrypt(database.ID, mongo.Password)
59+
if err != nil {
60+
return &ValidationResult{
61+
IsValid: false,
62+
Error: stringPtr(fmt.Sprintf("failed to decrypt database password: %v", err)),
63+
}, nil
64+
}
65+
66+
// Build MongoDB URI (required for mongorestore --dryRun)
67+
uri := mongo.BuildMongodumpURI(decryptedPassword)
68+
5769
// Get backup data from storage
5870
fieldEncryptor := util_encryption.GetFieldEncryptor()
5971
rawReader, err := storage.GetFile(fieldEncryptor, backup.ID)
@@ -89,14 +101,35 @@ func (uc *ValidateMongodbBackupUsecase) Execute(
89101
config.GetEnv().MongodbInstallDir,
90102
)
91103

92-
// Run mongorestore --dryRun with stdin input (like restore does)
93-
cmd := exec.CommandContext(
94-
ctx,
95-
mongorestoreBin,
104+
// Build command arguments with URI (required for --dryRun)
105+
args := []string{
106+
"--uri=" + uri,
96107
"--archive",
97108
"--gzip",
98109
"--dryRun",
99110
"--quiet",
111+
}
112+
113+
// Create safe args for logging (mask password in URI)
114+
safeArgs := make([]string, len(args))
115+
for i, arg := range args {
116+
if len(arg) > 6 && arg[:6] == "--uri=" {
117+
safeArgs[i] = "--uri=mongodb://***:***@***"
118+
} else {
119+
safeArgs[i] = arg
120+
}
121+
}
122+
uc.logger.Info(
123+
"Executing MongoDB validation command",
124+
"command", mongorestoreBin,
125+
"args", safeArgs,
126+
)
127+
128+
// Run mongorestore --dryRun with stdin input (like restore does)
129+
cmd := exec.CommandContext(
130+
ctx,
131+
mongorestoreBin,
132+
args...,
100133
)
101134

102135
cmd.Stdin = backupReader

0 commit comments

Comments
 (0)