Skip to content

Commit 250a6b1

Browse files
committed
fix: Continue connecting even if password is not detected
1 parent 36dcb18 commit 250a6b1

File tree

12 files changed

+121
-118
lines changed

12 files changed

+121
-118
lines changed

internal/actions/dump/dump_test.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ import (
1313
)
1414

1515
func Test_buildCommand(t *testing.T) {
16-
pgpassword := command.NewEnv("PGPASSWORD", "")
17-
mysqlPwd := command.NewEnv("MYSQL_PWD", "")
18-
1916
type args struct {
2017
conf Dump
2118
}
@@ -28,31 +25,31 @@ func Test_buildCommand(t *testing.T) {
2825
{
2926
"postgres-gzip",
3027
args{Dump{Dump: conftypes.Dump{Global: &conftypes.Global{Dialect: postgres.Postgres{}, Host: "1.1.1.1", Database: "d", Username: "u", RemoteGzip: true}}}},
31-
command.NewBuilder(command.Raw("{"), pgpassword, "pg_dump", "--host=1.1.1.1", "--username=u", "--dbname=d", "--verbose", command.Raw("|| kill $$; }"), command.Pipe, "gzip", "--force"),
28+
command.NewBuilder(command.Raw("{"), "pg_dump", "--host=1.1.1.1", "--username=u", "--dbname=d", "--verbose", command.Raw("|| kill $$; }"), command.Pipe, "gzip", "--force"),
3229
require.NoError,
3330
},
3431
{
3532
"postgres-gzip-no-compression",
3633
args{Dump{Dump: conftypes.Dump{Global: &conftypes.Global{Dialect: postgres.Postgres{}, Host: "1.1.1.1", Database: "d", Username: "u"}}}},
37-
command.NewBuilder(command.Raw("{"), pgpassword, "pg_dump", "--host=1.1.1.1", "--username=u", "--dbname=d", "--verbose", command.Raw("|| kill $$; }")),
34+
command.NewBuilder(command.Raw("{"), "pg_dump", "--host=1.1.1.1", "--username=u", "--dbname=d", "--verbose", command.Raw("|| kill $$; }")),
3835
require.NoError,
3936
},
4037
{
4138
"postgres-plain",
4239
args{Dump{Dump: conftypes.Dump{Files: conftypes.Files{Format: sqlformat.Plain}, Global: &conftypes.Global{Dialect: postgres.Postgres{}, Host: "1.1.1.1", Database: "d", Username: "u", RemoteGzip: true}}}},
43-
command.NewBuilder(command.Raw("{"), pgpassword, "pg_dump", "--host=1.1.1.1", "--username=u", "--dbname=d", "--verbose", command.Raw("|| kill $$; }"), command.Pipe, "gzip", "--force"),
40+
command.NewBuilder(command.Raw("{"), "pg_dump", "--host=1.1.1.1", "--username=u", "--dbname=d", "--verbose", command.Raw("|| kill $$; }"), command.Pipe, "gzip", "--force"),
4441
require.NoError,
4542
},
4643
{
4744
"postgres-custom",
4845
args{Dump{Dump: conftypes.Dump{Files: conftypes.Files{Format: sqlformat.Custom}, Global: &conftypes.Global{Dialect: postgres.Postgres{}, Host: "1.1.1.1", Database: "d", Username: "u", RemoteGzip: true}}}},
49-
command.NewBuilder(command.Raw("{"), pgpassword, "pg_dump", "--host=1.1.1.1", "--username=u", "--dbname=d", "--format=custom", "--verbose", command.Raw("|| kill $$; }")),
46+
command.NewBuilder(command.Raw("{"), "pg_dump", "--host=1.1.1.1", "--username=u", "--dbname=d", "--format=custom", "--verbose", command.Raw("|| kill $$; }")),
5047
require.NoError,
5148
},
5249
{
5350
"mariadb-gzip",
5451
args{Dump{Dump: conftypes.Dump{Files: conftypes.Files{Format: sqlformat.Gzip}, Global: &conftypes.Global{Dialect: mariadb.MariaDB{}, Host: "1.1.1.1", Database: "d", Username: "u", RemoteGzip: true}}}},
55-
command.NewBuilder(command.Raw("{"), mysqlPwd, command.Raw(`"$(which mariadb-dump || which mysqldump)"`), "--host=1.1.1.1", "--user=u", "d", "--verbose", command.Raw("|| kill $$; }"), command.Pipe, "gzip", "--force"),
52+
command.NewBuilder(command.Raw("{"), command.Raw(`"$(which mariadb-dump || which mysqldump)"`), "--host=1.1.1.1", "--user=u", "d", "--verbose", command.Raw("|| kill $$; }"), command.Pipe, "gzip", "--force"),
5653
require.NoError,
5754
},
5855
}

internal/actions/restore/restore_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ import (
1616
)
1717

1818
func TestRestore_buildCommand(t *testing.T) {
19-
pgpassword := command.NewEnv("PGPASSWORD", "")
20-
2119
type fields struct {
2220
Restore conftypes.Restore
2321
Analyze bool
@@ -36,28 +34,28 @@ func TestRestore_buildCommand(t *testing.T) {
3634
"postgres-gzip",
3735
fields{Restore: conftypes.Restore{Global: &conftypes.Global{Dialect: postgres.Postgres{}, Host: "1.1.1.1", Database: "d", Username: "u", RemoteGzip: true}}},
3836
args{sqlformat.Gzip},
39-
command.NewBuilder("gunzip", "--force", command.Pipe, command.Raw("{"), pgpassword, "psql", "--host=1.1.1.1", "--username=u", "--dbname=d", command.Raw("|| { cat >/dev/null; kill $$; }; }")),
37+
command.NewBuilder("gunzip", "--force", command.Pipe, command.Raw("{"), "psql", "--host=1.1.1.1", "--username=u", "--dbname=d", command.Raw("|| { cat >/dev/null; kill $$; }; }")),
4038
require.NoError,
4139
},
4240
{
4341
"postgres-plain",
4442
fields{Restore: conftypes.Restore{Global: &conftypes.Global{Dialect: postgres.Postgres{}, Host: "1.1.1.1", Database: "d", Username: "u", RemoteGzip: true}}},
4543
args{sqlformat.Gzip},
46-
command.NewBuilder("gunzip", "--force", command.Pipe, command.Raw("{"), pgpassword, "psql", "--host=1.1.1.1", "--username=u", "--dbname=d", command.Raw("|| { cat >/dev/null; kill $$; }; }")),
44+
command.NewBuilder("gunzip", "--force", command.Pipe, command.Raw("{"), "psql", "--host=1.1.1.1", "--username=u", "--dbname=d", command.Raw("|| { cat >/dev/null; kill $$; }; }")),
4745
require.NoError,
4846
},
4947
{
5048
"postgres-custom",
5149
fields{Restore: conftypes.Restore{Global: &conftypes.Global{Dialect: postgres.Postgres{}, Host: "1.1.1.1", Database: "d", Username: "u", RemoteGzip: true}}},
5250
args{sqlformat.Gzip},
53-
command.NewBuilder("gunzip", "--force", command.Pipe, command.Raw("{"), pgpassword, "psql", "--host=1.1.1.1", "--username=u", "--dbname=d", command.Raw("|| { cat >/dev/null; kill $$; }; }")),
51+
command.NewBuilder("gunzip", "--force", command.Pipe, command.Raw("{"), "psql", "--host=1.1.1.1", "--username=u", "--dbname=d", command.Raw("|| { cat >/dev/null; kill $$; }; }")),
5452
require.NoError,
5553
},
5654
{
5755
"postgres-remote-gzip-disabled",
5856
fields{Restore: conftypes.Restore{Global: &conftypes.Global{Dialect: postgres.Postgres{}, Host: "1.1.1.1", Database: "d", Username: "u"}}},
5957
args{sqlformat.Gzip},
60-
command.NewBuilder(command.Raw("{"), command.Env{Key: "PGPASSWORD", Value: ""}, "psql", "--host=1.1.1.1", "--username=u", "--dbname=d", command.Raw("|| { cat >/dev/null; kill $$; }; }")),
58+
command.NewBuilder(command.Raw("{"), "psql", "--host=1.1.1.1", "--username=u", "--dbname=d", command.Raw("|| { cat >/dev/null; kill $$; }; }")),
6159
require.NoError,
6260
},
6361
}

internal/database/mariadb/mariadb.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,11 @@ func (db MariaDB) PasswordEnvs(c *conftypes.Global) kubernetes.ConfigLookups {
9696

9797
func (MariaDB) ExecCommand(conf *conftypes.Exec) *command.Builder {
9898
cmd := command.NewBuilder(
99-
command.NewEnv("MYSQL_PWD", conf.Password),
10099
"exec", command.Raw(`"$(which mariadb || which mysql)"`), "--host="+conf.Host, "--user="+conf.Username,
101100
)
101+
if conf.Password != "" {
102+
cmd.Unshift(command.NewEnv("MYSQL_PWD", conf.Password))
103+
}
102104
if conf.Port != 0 {
103105
cmd.Push("--port=" + strconv.Itoa(int(conf.Port)))
104106
}
@@ -116,9 +118,11 @@ func (MariaDB) ExecCommand(conf *conftypes.Exec) *command.Builder {
116118

117119
func (MariaDB) DumpCommand(conf *conftypes.Dump) *command.Builder {
118120
cmd := command.NewBuilder(
119-
command.NewEnv("MYSQL_PWD", conf.Password),
120121
command.Raw(`"$(which mariadb-dump || which mysqldump)"`), "--host="+conf.Host, "--user="+conf.Username, conf.Database,
121122
)
123+
if conf.Password != "" {
124+
cmd.Unshift(command.NewEnv("MYSQL_PWD", conf.Password))
125+
}
122126
if conf.Port != 0 {
123127
cmd.Push("--port=" + strconv.Itoa(int(conf.Port)))
124128
}
@@ -139,9 +143,11 @@ func (MariaDB) DumpCommand(conf *conftypes.Dump) *command.Builder {
139143

140144
func (MariaDB) RestoreCommand(conf *conftypes.Restore, _ sqlformat.Format) *command.Builder {
141145
cmd := command.NewBuilder(
142-
command.NewEnv("MYSQL_PWD", conf.Password),
143146
command.Raw(`"$(which mariadb || which mysql)"`), "--host="+conf.Host, "--user="+conf.Username, "--database="+conf.Database,
144147
)
148+
if conf.Password != "" {
149+
cmd.Unshift(command.NewEnv("MYSQL_PWD", conf.Password))
150+
}
145151
if conf.Port != 0 {
146152
cmd.Push("--port=" + strconv.Itoa(int(conf.Port)))
147153
}

internal/database/mariadb/mariadb_test.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,28 +41,28 @@ func TestMariaDB_DumpCommand(t *testing.T) {
4141
}{
4242
{
4343
"default",
44-
args{&conftypes.Dump{Global: &conftypes.Global{Host: "1.1.1.1", Database: "d", Username: "u"}}},
45-
command.NewBuilder(command.NewEnv("MYSQL_PWD", ""), command.Raw(`"$(which mariadb-dump || which mysqldump)"`), "--host=1.1.1.1", "--user=u", "d", "--verbose"),
44+
args{&conftypes.Dump{Global: &conftypes.Global{Host: "1.1.1.1", Database: "d", Username: "u", Password: "p"}}},
45+
command.NewBuilder(command.NewEnv("MYSQL_PWD", "p"), command.Raw(`"$(which mariadb-dump || which mysqldump)"`), "--host=1.1.1.1", "--user=u", "d", "--verbose"),
4646
},
4747
{
4848
"clean",
49-
args{&conftypes.Dump{Clean: true, Global: &conftypes.Global{Host: "1.1.1.1", Database: "d", Username: "u"}}},
50-
command.NewBuilder(command.NewEnv("MYSQL_PWD", ""), command.Raw(`"$(which mariadb-dump || which mysqldump)"`), "--host=1.1.1.1", "--user=u", "d", "--add-drop-table", "--verbose"),
49+
args{&conftypes.Dump{Clean: true, Global: &conftypes.Global{Host: "1.1.1.1", Database: "d", Username: "u", Password: "p"}}},
50+
command.NewBuilder(command.NewEnv("MYSQL_PWD", "p"), command.Raw(`"$(which mariadb-dump || which mysqldump)"`), "--host=1.1.1.1", "--user=u", "d", "--add-drop-table", "--verbose"),
5151
},
5252
{
5353
"tables",
54-
args{&conftypes.Dump{Table: []string{"table1", "table2"}, Global: &conftypes.Global{Host: "1.1.1.1", Database: "d", Username: "u"}}},
55-
command.NewBuilder(command.NewEnv("MYSQL_PWD", ""), command.Raw(`"$(which mariadb-dump || which mysqldump)"`), "--host=1.1.1.1", "--user=u", "d", "table1", "table2", "--verbose"),
54+
args{&conftypes.Dump{Table: []string{"table1", "table2"}, Global: &conftypes.Global{Host: "1.1.1.1", Database: "d", Username: "u", Password: "p"}}},
55+
command.NewBuilder(command.NewEnv("MYSQL_PWD", "p"), command.Raw(`"$(which mariadb-dump || which mysqldump)"`), "--host=1.1.1.1", "--user=u", "d", "table1", "table2", "--verbose"),
5656
},
5757
{
5858
"exclude-table",
59-
args{&conftypes.Dump{ExcludeTable: []string{"table1", "table2"}, Global: &conftypes.Global{Host: "1.1.1.1", Database: "d", Username: "u"}}},
60-
command.NewBuilder(command.NewEnv("MYSQL_PWD", ""), command.Raw(`"$(which mariadb-dump || which mysqldump)"`), "--host=1.1.1.1", "--user=u", "d", "--ignore-table=table1", "--ignore-table=table2", "--verbose"),
59+
args{&conftypes.Dump{ExcludeTable: []string{"table1", "table2"}, Global: &conftypes.Global{Host: "1.1.1.1", Database: "d", Username: "u", Password: "p"}}},
60+
command.NewBuilder(command.NewEnv("MYSQL_PWD", "p"), command.Raw(`"$(which mariadb-dump || which mysqldump)"`), "--host=1.1.1.1", "--user=u", "d", "--ignore-table=table1", "--ignore-table=table2", "--verbose"),
6161
},
6262
{
6363
"port",
6464
args{&conftypes.Dump{Global: &conftypes.Global{Port: 1234}}},
65-
command.NewBuilder(command.NewEnv("MYSQL_PWD", ""), command.Raw(`"$(which mariadb-dump || which mysqldump)"`), "--host=", "--user=", "", "--port=1234", "--verbose"),
65+
command.NewBuilder(command.Raw(`"$(which mariadb-dump || which mysqldump)"`), "--host=", "--user=", "", "--port=1234", "--verbose"),
6666
},
6767
}
6868
for _, tt := range tests {
@@ -85,23 +85,23 @@ func TestMariaDB_ExecCommand(t *testing.T) {
8585
}{
8686
{
8787
"default",
88-
args{&conftypes.Exec{Global: &conftypes.Global{Host: "1.1.1.1", Database: "d", Username: "u"}}},
89-
command.NewBuilder(command.Env{Key: "MYSQL_PWD"}, "exec", command.Raw(`"$(which mariadb || which mysql)"`), "--host=1.1.1.1", "--user=u", "--database=d"),
88+
args{&conftypes.Exec{Global: &conftypes.Global{Host: "1.1.1.1", Database: "d", Username: "u", Password: "p"}}},
89+
command.NewBuilder(command.NewEnv("MYSQL_PWD", "p"), "exec", command.Raw(`"$(which mariadb || which mysql)"`), "--host=1.1.1.1", "--user=u", "--database=d"),
9090
},
9191
{
9292
"disable-headers",
93-
args{&conftypes.Exec{DisableHeaders: true, Global: &conftypes.Global{Host: "1.1.1.1", Database: "d", Username: "u"}}},
94-
command.NewBuilder(command.Env{Key: "MYSQL_PWD"}, "exec", command.Raw(`"$(which mariadb || which mysql)"`), "--host=1.1.1.1", "--user=u", "--database=d", "--skip-column-names"),
93+
args{&conftypes.Exec{DisableHeaders: true, Global: &conftypes.Global{Host: "1.1.1.1", Database: "d", Username: "u", Password: "p"}}},
94+
command.NewBuilder(command.NewEnv("MYSQL_PWD", "p"), "exec", command.Raw(`"$(which mariadb || which mysql)"`), "--host=1.1.1.1", "--user=u", "--database=d", "--skip-column-names"),
9595
},
9696
{
9797
"command",
98-
args{&conftypes.Exec{Command: "show databases", Global: &conftypes.Global{Host: "1.1.1.1", Database: "d", Username: "u"}}},
99-
command.NewBuilder(command.Env{Key: "MYSQL_PWD"}, "exec", command.Raw(`"$(which mariadb || which mysql)"`), "--host=1.1.1.1", "--user=u", "--database=d", "--execute=show databases"),
98+
args{&conftypes.Exec{Command: "show databases", Global: &conftypes.Global{Host: "1.1.1.1", Database: "d", Username: "u", Password: "p"}}},
99+
command.NewBuilder(command.NewEnv("MYSQL_PWD", "p"), "exec", command.Raw(`"$(which mariadb || which mysql)"`), "--host=1.1.1.1", "--user=u", "--database=d", "--execute=show databases"),
100100
},
101101
{
102102
"port",
103103
args{&conftypes.Exec{Global: &conftypes.Global{Port: 1234}}},
104-
command.NewBuilder(command.NewEnv("MYSQL_PWD", ""), "exec", command.Raw(`"$(which mariadb || which mysql)"`), "--host=", "--user=", "--port=1234"),
104+
command.NewBuilder("exec", command.Raw(`"$(which mariadb || which mysql)"`), "--host=", "--user=", "--port=1234"),
105105
},
106106
}
107107
for _, tt := range tests {
@@ -162,23 +162,23 @@ func TestMariaDB_RestoreCommand(t *testing.T) {
162162
}{
163163
{
164164
"gzip",
165-
args{&conftypes.Restore{Global: &conftypes.Global{Host: "1.1.1.1", Database: "d", Username: "u"}}, sqlformat.Gzip},
166-
command.NewBuilder(command.Env{Key: "MYSQL_PWD"}, command.Raw(`"$(which mariadb || which mysql)"`), "--host=1.1.1.1", "--user=u", "--database=d"),
165+
args{&conftypes.Restore{Global: &conftypes.Global{Host: "1.1.1.1", Database: "d", Username: "u", Password: "p"}}, sqlformat.Gzip},
166+
command.NewBuilder(command.NewEnv("MYSQL_PWD", "p"), command.Raw(`"$(which mariadb || which mysql)"`), "--host=1.1.1.1", "--user=u", "--database=d"),
167167
},
168168
{
169169
"plain",
170-
args{&conftypes.Restore{Global: &conftypes.Global{Host: "1.1.1.1", Database: "d", Username: "u"}}, sqlformat.Plain},
171-
command.NewBuilder(command.Env{Key: "MYSQL_PWD"}, command.Raw(`"$(which mariadb || which mysql)"`), "--host=1.1.1.1", "--user=u", "--database=d"),
170+
args{&conftypes.Restore{Global: &conftypes.Global{Host: "1.1.1.1", Database: "d", Username: "u", Password: "p"}}, sqlformat.Plain},
171+
command.NewBuilder(command.NewEnv("MYSQL_PWD", "p"), command.Raw(`"$(which mariadb || which mysql)"`), "--host=1.1.1.1", "--user=u", "--database=d"),
172172
},
173173
{
174174
"custom",
175-
args{&conftypes.Restore{Global: &conftypes.Global{Host: "1.1.1.1", Database: "d", Username: "u"}}, sqlformat.Custom},
176-
command.NewBuilder(command.Env{Key: "MYSQL_PWD"}, command.Raw(`"$(which mariadb || which mysql)"`), "--host=1.1.1.1", "--user=u", "--database=d"),
175+
args{&conftypes.Restore{Global: &conftypes.Global{Host: "1.1.1.1", Database: "d", Username: "u", Password: "p"}}, sqlformat.Custom},
176+
command.NewBuilder(command.NewEnv("MYSQL_PWD", "p"), command.Raw(`"$(which mariadb || which mysql)"`), "--host=1.1.1.1", "--user=u", "--database=d"),
177177
},
178178
{
179179
"port",
180180
args{&conftypes.Restore{Global: &conftypes.Global{Port: 1234}}, sqlformat.Plain},
181-
command.NewBuilder(command.NewEnv("MYSQL_PWD", ""), command.Raw(`"$(which mariadb || which mysql)"`), "--host=", "--user=", "--database=", "--port=1234"),
181+
command.NewBuilder(command.Raw(`"$(which mariadb || which mysql)"`), "--host=", "--user=", "--database=", "--port=1234"),
182182
},
183183
}
184184
for _, tt := range tests {

internal/database/meilisearch/meilisearch.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ func (Meilisearch) PodFilters() filter.Filter {
3838
func (Meilisearch) PasswordEnvs(_ *conftypes.Global) kubernetes.ConfigLookups {
3939
return kubernetes.ConfigLookups{
4040
kubernetes.LookupEnv{"MEILI_MASTER_KEY"},
41-
kubernetes.LookupNop{},
4241
}
4342
}
4443

internal/database/mongodb/mongodb.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,11 @@ func (db MongoDB) ExecCommand(conf *conftypes.Exec) *command.Builder {
9494
"exec", command.Raw(`"$(which mongosh || which mongo)"`),
9595
"--host="+conf.Host,
9696
"--username="+conf.Username,
97-
"--password="+conf.Password,
9897
"--authenticationDatabase="+db.AuthenticationDatabase(conf.Global),
9998
)
99+
if conf.Password != "" {
100+
cmd.Push("--password=" + conf.Password)
101+
}
100102
if conf.Port != 0 {
101103
cmd.Push("--port=" + strconv.Itoa(int(conf.Port)))
102104
}
@@ -118,9 +120,11 @@ func (db MongoDB) DumpCommand(conf *conftypes.Dump) *command.Builder {
118120
"--archive",
119121
"--host="+conf.Host,
120122
"--username="+conf.Username,
121-
"--password="+conf.Password,
122123
"--authenticationDatabase="+db.AuthenticationDatabase(conf.Global),
123124
)
125+
if conf.Password != "" {
126+
cmd.Push("--password=" + conf.Password)
127+
}
124128
if conf.Port != 0 {
125129
cmd.Push("--port=" + strconv.Itoa(int(conf.Port)))
126130
}
@@ -145,9 +149,11 @@ func (db MongoDB) RestoreCommand(conf *conftypes.Restore, _ sqlformat.Format) *c
145149
"--archive",
146150
"--host="+conf.Host,
147151
"--username="+conf.Username,
148-
"--password="+conf.Password,
149152
"--authenticationDatabase="+db.AuthenticationDatabase(conf.Global),
150153
)
154+
if conf.Password != "" {
155+
cmd.Push("--password=" + conf.Password)
156+
}
151157
if conf.Port != 0 {
152158
cmd.Push("--port=" + strconv.Itoa(int(conf.Port)))
153159
}

0 commit comments

Comments
 (0)