Skip to content
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

Fixed db password file endline issue #5920

Merged
merged 9 commits into from
May 6, 2024
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* [ENHANCEMENT] Query Frontend/Querier: Added store gateway postings touched count and touched size in Querier stats and log in Query Frontend. #5892
* [ENHANCEMENT] Query Frontend/Querier: Returns `warnings` on prometheus query responses. #5916
* [CHANGE] Upgrade Dockerfile Node version from 14x to 18x. #5906
* [BUGFIX] Configsdb: Fix endline issue in db password. #5920

## 1.17.0 2024-04-30

Expand Down
27 changes: 20 additions & 7 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 @@ -70,14 +71,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 := setPassword(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 All @@ -87,10 +85,25 @@ func New(cfg Config) (DB, error) {
case "postgres":
d, err = postgres.New(u.String(), cfg.MigrationsDir)
default:
return nil, fmt.Errorf("Unknown database type: %s", u.Scheme)
return nil, fmt.Errorf("unknown database type: %s", u.Scheme)
}
if err != nil {
return nil, err
}
return traced{timed{d}}, nil
}

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

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

passwordStr := strings.TrimSpace(string(passwordBytes))
u.User = url.UserPassword(u.User.Username(), passwordStr)
return u, nil
}
63 changes: 63 additions & 0 deletions pkg/configs/db/db_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package db

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

"github.com/stretchr/testify/assert"
)

func TestSetPassword(t *testing.T) {

testCases := []struct {
testName string
url string
passwordStr string
isError bool
expected string
}{
{
testName: "Test1",
url: "scheme://[email protected]",
passwordStr: "\n\tpassword\n\n\t",
isError: false,
expected: "scheme://user:[email protected]",
},
{
testName: "Test2",
url: "scheme://host.com",
passwordStr: "\n\tpassword\n\n\t",
isError: true,
expected: "--database.password-file requires username in --database.uri",
},
}

for _, tc := range testCases {
passwordFile, err := os.CreateTemp("", "passwordFile")
if err != nil {
t.Fatalf("error while creating the password file: %v", err)
}

defer os.Remove(passwordFile.Name())
defer passwordFile.Close()

_, err = passwordFile.WriteString(tc.passwordStr)
if err != nil {
t.Fatalf("error while writing to the password file: %v", err)
}

t.Run(tc.testName, func(t *testing.T) {
u, _ := url.Parse(tc.url)
uNew, err := setPassword(u, passwordFile.Name())
if tc.isError {
assert.Error(t, err)
assert.Equal(t, tc.expected, err.Error())
} else {
assert.NoError(t, err)
uExp, _ := url.Parse(tc.expected)
assert.Equal(t, uNew, uExp)
}
})
}
}
Loading