Skip to content

fixed password endline bug #5500

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions pkg/configs/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"net/url"
"os"
"strings"

"github.com/cortexproject/cortex/pkg/configs/db/memory"
"github.com/cortexproject/cortex/pkg/configs/db/postgres"
Expand Down Expand Up @@ -58,6 +59,21 @@ type DB interface {
Close() error
}

func SetUserPassword(u *url.URL, passwordFilePath string) (*url.URL, error) {
if u.User == nil {
return nil, fmt.Errorf("--database.password-file requires username in --database.uri")
}

passwordBytes, err := os.ReadFile(passwordFilePath)
if err != nil {
return nil, fmt.Errorf("Could not read database password file: %v", err)
}

password := strings.TrimSpace(string(passwordBytes))
u.User = url.UserPassword(u.User.Username(), password)
return u, nil
}

// New creates a new database.
func New(cfg Config) (DB, error) {
if cfg.Mock != nil {
Expand All @@ -70,14 +86,11 @@ func New(cfg Config) (DB, error) {
}

if len(cfg.PasswordFile) != 0 {
if u.User == nil {
return nil, fmt.Errorf("--database.password-file requires username in --database.uri")
}
passwordBytes, err := os.ReadFile(cfg.PasswordFile)
updatedURL, err := SetUserPassword(u, cfg.PasswordFile)
if err != nil {
return nil, fmt.Errorf("Could not read database password file: %v", err)
return nil, err
}
u.User = url.UserPassword(u.User.Username(), string(passwordBytes))
u = updatedURL
}

var d DB
Expand Down
52 changes: 52 additions & 0 deletions pkg/configs/db/db_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package db

import (
"net/url"
"os"
"testing"
)

func TestUserPasswordFromPasswordFile(t *testing.T) {

tempFile, _ := os.CreateTemp("", "testpassword")
defer os.Remove(tempFile.Name())
defer tempFile.Close()

_, writeErr := tempFile.WriteString(" testpassword ")
if writeErr != nil {
t.Fatalf("Error writing to temporary file: %v", writeErr)
}

testCases := []struct {
testName string
url string
expectedUsername string
expectedPassword string
}{
{
testName: "set username and password correctly from url and password file",
url: "testscheme://[email protected]",
expectedUsername: "testuser",
expectedPassword: "testpassword",
},
}

for _, tc := range testCases {
t.Run(tc.testName, func(t *testing.T) {
u, _ := url.Parse(tc.url)

updatedURL, err := SetUserPassword(u, tempFile.Name())
if err != nil {
t.Errorf("Unexpected error: %v", err)
}

if u.User == nil || updatedURL.User.Username() != tc.expectedUsername {
t.Errorf("Username not set as expected. Got: %v, Expected: %v", updatedURL.User.Username(), tc.expectedUsername)
}

if trimmedPassword, _ := updatedURL.User.Password(); trimmedPassword != tc.expectedPassword {
t.Errorf("Password not set as expected. Got: %v, Expected: %v", trimmedPassword, tc.expectedPassword)
}
})
}
}
1 change: 1 addition & 0 deletions pkg/configs/db/dbtest/unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ func Setup(t *testing.T) db.DB {
func Cleanup(t *testing.T, database db.DB) {
require.NoError(t, database.Close())
}