Skip to content

Commit e4a3529

Browse files
committed
keeper: remove trailing new lines from passwords
Remove trailing new lines (trailing line feed and carriage return chars) like already done by initdb and libpq when reading a password file.
1 parent cfa6f95 commit e4a3529

File tree

2 files changed

+117
-11
lines changed

2 files changed

+117
-11
lines changed

cmd/keeper/cmd/keeper.go

+30-11
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func readPasswordFromFile(filepath string) (string, error) {
165165
if err != nil {
166166
return "", fmt.Errorf("unable to read password from file %s: %v", filepath, err)
167167
}
168-
return strings.TrimSpace(string(pwBytes)), nil
168+
return string(pwBytes), nil
169169
}
170170

171171
// walLevel returns the wal_level value to use.
@@ -1798,16 +1798,6 @@ func keeper(c *cobra.Command, args []string) {
17981798
log.Fatalf("only one of --pg-su-password or --pg-su-passwordfile must be provided")
17991799
}
18001800

1801-
if cfg.pgSUUsername == cfg.pgReplUsername {
1802-
log.Warn("superuser name and replication user name are the same. Different users are suggested.")
1803-
if cfg.pgReplAuthMethod != cfg.pgSUAuthMethod {
1804-
log.Fatalf("do not support different auth methods when utilizing superuser for replication.")
1805-
}
1806-
if cfg.pgSUPassword != cfg.pgReplPassword && cfg.pgSUAuthMethod != "trust" && cfg.pgReplAuthMethod != "trust" {
1807-
log.Fatalf("provided superuser name and replication user name are the same but provided passwords are different")
1808-
}
1809-
}
1810-
18111801
if cfg.pgReplPasswordFile != "" {
18121802
cfg.pgReplPassword, err = readPasswordFromFile(cfg.pgReplPasswordFile)
18131803
if err != nil {
@@ -1821,6 +1811,35 @@ func keeper(c *cobra.Command, args []string) {
18211811
}
18221812
}
18231813

1814+
// Trim trailing new lines from passwords
1815+
tp := strings.TrimRight(cfg.pgSUPassword, "\r\n")
1816+
if cfg.pgSUPassword != tp {
1817+
log.Warn("superuser password contain trailing new line, removing")
1818+
if tp == "" {
1819+
log.Fatalf("superuser password is empty after removing trailing new line")
1820+
}
1821+
cfg.pgSUPassword = tp
1822+
}
1823+
1824+
tp = strings.TrimRight(cfg.pgReplPassword, "\r\n")
1825+
if cfg.pgReplPassword != tp {
1826+
log.Warn("replication user password contain trailing new line, removing")
1827+
if tp == "" {
1828+
log.Fatalf("replication user password is empty after removing trailing new line")
1829+
}
1830+
cfg.pgReplPassword = tp
1831+
}
1832+
1833+
if cfg.pgSUUsername == cfg.pgReplUsername {
1834+
log.Warn("superuser name and replication user name are the same. Different users are suggested.")
1835+
if cfg.pgReplAuthMethod != cfg.pgSUAuthMethod {
1836+
log.Fatalf("do not support different auth methods when utilizing superuser for replication.")
1837+
}
1838+
if cfg.pgSUPassword != cfg.pgReplPassword && cfg.pgSUAuthMethod != "trust" && cfg.pgReplAuthMethod != "trust" {
1839+
log.Fatalf("provided superuser name and replication user name are the same but provided passwords are different")
1840+
}
1841+
}
1842+
18241843
// Open (and create if needed) the lock file.
18251844
// There is no need to clean up this file since we don't use the file as an actual lock. We get a lock
18261845
// on the file. So the lock get released when our process stops (or log.Fatalfs).

tests/integration/init_test.go

+87
Original file line numberDiff line numberDiff line change
@@ -512,3 +512,90 @@ func TestExclusiveLock(t *testing.T) {
512512
t.Fatalf("expecting keeper reporting that failed to take an exclusive lock on data dir")
513513
}
514514
}
515+
516+
func TestPasswordTrailingNewLine(t *testing.T) {
517+
t.Parallel()
518+
519+
dir, err := ioutil.TempDir("", "")
520+
if err != nil {
521+
t.Fatalf("unexpected err: %v", err)
522+
}
523+
defer os.RemoveAll(dir)
524+
525+
tstore, err := NewTestStore(t, dir)
526+
if err != nil {
527+
t.Fatalf("unexpected err: %v", err)
528+
}
529+
if err := tstore.Start(); err != nil {
530+
t.Fatalf("unexpected err: %v", err)
531+
}
532+
if err := tstore.WaitUp(10 * time.Second); err != nil {
533+
t.Fatalf("error waiting on store up: %v", err)
534+
}
535+
storeEndpoints := fmt.Sprintf("%s:%s", tstore.listenAddress, tstore.port)
536+
defer tstore.Stop()
537+
538+
clusterName := uuid.NewV4().String()
539+
540+
u := uuid.NewV4()
541+
id := fmt.Sprintf("%x", u[:4])
542+
543+
pgSUPassword := "stolon_superuserpassword\n"
544+
pgReplPassword := "stolon_replpassword\n"
545+
546+
tk, err := NewTestKeeperWithID(t, dir, id, clusterName, pgSUUsername, pgSUPassword, pgReplUsername, pgReplPassword, tstore.storeBackend, storeEndpoints)
547+
if err != nil {
548+
t.Fatalf("unexpected err: %v", err)
549+
}
550+
if err := tk.StartExpect(); err != nil {
551+
t.Fatalf("unexpected err: %v", err)
552+
}
553+
554+
if err := tk.cmd.ExpectTimeout("superuser password contain trailing new line, removing", 30*time.Second); err != nil {
555+
t.Fatalf(`expecting keeper reporting "superuser password contain trailing new line, removing"`)
556+
}
557+
if err := tk.cmd.ExpectTimeout("replication user password contain trailing new line, removing", 30*time.Second); err != nil {
558+
t.Fatalf(`expecting keeper reporting "replication user password contain trailing new line, removing"`)
559+
}
560+
tk.Stop()
561+
562+
pgSUPassword = "stolon_superuserpassword\n"
563+
pgReplPassword = "\n"
564+
565+
tk, err = NewTestKeeperWithID(t, dir, id, clusterName, pgSUUsername, pgSUPassword, pgReplUsername, pgReplPassword, tstore.storeBackend, storeEndpoints)
566+
if err != nil {
567+
t.Fatalf("unexpected err: %v", err)
568+
}
569+
if err := tk.StartExpect(); err != nil {
570+
t.Fatalf("unexpected err: %v", err)
571+
}
572+
573+
if err := tk.cmd.ExpectTimeout("replication user password contain trailing new line, removing", 30*time.Second); err != nil {
574+
t.Fatalf(`expecting keeper reporting "replication user password contain trailing new line, removing"`)
575+
}
576+
if err := tk.cmd.ExpectTimeout("replication user password is empty after removing trailing new line", 30*time.Second); err != nil {
577+
t.Fatalf(`expecting keeper reporting "replication user password is empty after removing trailing new line"`)
578+
}
579+
580+
tk.Stop()
581+
582+
pgSUPassword = "\n"
583+
pgReplPassword = "stolon_replpassword\n"
584+
585+
tk, err = NewTestKeeperWithID(t, dir, id, clusterName, pgSUUsername, pgSUPassword, pgReplUsername, pgReplPassword, tstore.storeBackend, storeEndpoints)
586+
if err != nil {
587+
t.Fatalf("unexpected err: %v", err)
588+
}
589+
if err := tk.StartExpect(); err != nil {
590+
t.Fatalf("unexpected err: %v", err)
591+
}
592+
593+
if err := tk.cmd.ExpectTimeout("superuser password contain trailing new line, removing", 30*time.Second); err != nil {
594+
t.Fatalf(`expecting keeper reporting "superuser password contain trailing new line, removing"`)
595+
}
596+
if err := tk.cmd.ExpectTimeout("superuser password is empty after removing trailing new line", 30*time.Second); err != nil {
597+
t.Fatalf(`expecting keeper reporting "superuser password is empty after removing trailing new line"`)
598+
}
599+
600+
tk.Stop()
601+
}

0 commit comments

Comments
 (0)