Skip to content

Commit 19cae99

Browse files
feat: Add support for DB_PASS_FILE
This commit introduces support for reading the database password from a file specified by the `DB_PASS_FILE` environment variable or the `--pass-file` CLI flag. - Added `--pass-file` flag and `DB_PASS_FILE` environment variable. - Updated logic in `cmd/root.go` to read the password from the specified file. - Ensured that an explicitly set password via `DB_PASS` or `--pass` takes precedence. - Updated documentation in `docs/configuration.md`. - Added tests in `cmd/dump_test.go` to cover the new functionality and precedence logic.
1 parent d70d361 commit 19cae99

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

cmd/dump_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,20 @@ func TestDumpCmd(t *testing.T) {
4040
DBConn: database.Connection{Host: "abc", Port: defaultPort},
4141
FilenamePattern: "db_backup_{{ .now }}.{{ .compression }}",
4242
}, core.TimerOptions{Frequency: defaultFrequency, Begin: defaultBegin}, nil},
43+
{"file URL with pass-file", []string{"--server", "abc", "--target", "file:///foo/bar", "--pass-file", "testdata/password.txt"}, "", false, core.DumpOptions{
44+
Targets: []storage.Storage{file.New(*fileTargetURL)},
45+
MaxAllowedPacket: defaultMaxAllowedPacket,
46+
Compressor: &compression.GzipCompressor{},
47+
DBConn: database.Connection{Host: "abc", Port: defaultPort, Pass: "testpassword"},
48+
FilenamePattern: "db_backup_{{ .now }}.{{ .compression }}",
49+
}, core.TimerOptions{Frequency: defaultFrequency, Begin: defaultBegin}, nil},
50+
{"file URL with pass and pass-file (pass takes precedence)", []string{"--server", "abc", "--target", "file:///foo/bar", "--pass", "explicitpass", "--pass-file", "testdata/password.txt"}, "", false, core.DumpOptions{
51+
Targets: []storage.Storage{file.New(*fileTargetURL)},
52+
MaxAllowedPacket: defaultMaxAllowedPacket,
53+
Compressor: &compression.GzipCompressor{},
54+
DBConn: database.Connection{Host: "abc", Port: defaultPort, Pass: "explicitpass"},
55+
FilenamePattern: "db_backup_{{ .now }}.{{ .compression }}",
56+
}, core.TimerOptions{Frequency: defaultFrequency, Begin: defaultBegin}, nil},
4357
{"file URL with prune", []string{"--server", "abc", "--target", "file:///foo/bar", "--retention", "1h"}, "", false, core.DumpOptions{
4458
Targets: []storage.Storage{file.New(*fileTargetURL)},
4559
MaxAllowedPacket: defaultMaxAllowedPacket,

cmd/root.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,18 @@ func rootCmd(execs execs) (*cobra.Command, error) {
167167
cmdConfig.dbconn.Pass = dbPass
168168
}
169169

170+
// read password from file if pass is not set and pass-file is set
171+
if cmdConfig.dbconn.Pass == "" {
172+
dbPassFile := v.GetString("pass-file")
173+
if dbPassFile != "" {
174+
passBytes, err := os.ReadFile(dbPassFile)
175+
if err != nil {
176+
return fmt.Errorf("failed to read password from file %s: %w", dbPassFile, err)
177+
}
178+
cmdConfig.dbconn.Pass = strings.TrimSpace(string(passBytes))
179+
}
180+
}
181+
170182
// these are not from the config file, as they are generic credentials, used across all targets.
171183
// the config file uses specific ones per target
172184
cmdConfig.creds = credentials.Creds{
@@ -222,6 +234,9 @@ func rootCmd(execs execs) (*cobra.Command, error) {
222234
// pass via CLI or env var
223235
pflags.String("pass", "", "password for database server")
224236

237+
// pass-file via CLI or env var
238+
pflags.String("pass-file", "", "path to file containing password for database server")
239+
225240
// debug via CLI or env var or default
226241
pflags.IntP("verbose", "v", 0, "set log level, 1 is debug, 2 is trace")
227242
pflags.Bool("debug", false, "set log level to debug, equivalent of --verbose=1; if both set, --version always overrides")

cmd/testdata/password.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
testpassword

docs/configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ The following are the environment variables, CLI flags and configuration file op
6868
| port to use to connect to database. Optional. | BR | `port` | `DB_PORT` | `database.port` | 3306 |
6969
| username for the database | BR | `user` | `DB_USER` | `database.credentials.username` | |
7070
| password for the database | BR | `pass` | `DB_PASS` | `database.credentials.password` | |
71+
| path to file containing password for the database. `pass` takes precedence if both are set. | BR | `pass-file` | `DB_PASS_FILE` | | |
7172
| names of databases to dump, comma-separated | B | `include` | `DB_DUMP_INCLUDE` | `dump.include` | all databases in the server |
7273
| names of databases to exclude from the dump | B | `exclude` | `DB_DUMP_EXCLUDE` | `dump.exclude` | |
7374
| do not include `USE <database>;` statement in the dump | B | `no-database-name` | `NO_DATABASE_NAME` | `dump.noDatabaseName` | `false` |

0 commit comments

Comments
 (0)