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

Bookmarks fall back to sslmode=disable if mode is not set or invalid #244

Merged
merged 1 commit into from Jun 6, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions data/bookmark_invalid_ssl.toml
@@ -0,0 +1,5 @@
host = "localhost"
port = 5432
user = "postgres"
database = "mydatabase"
ssl = "disabled"
17 changes: 17 additions & 0 deletions pkg/bookmarks/bookmarks.go
Expand Up @@ -54,6 +54,23 @@ func readServerConfig(path string) (Bookmark, error) {
bookmark.Port = 5432
}

// List of all supported by portgres modes
modes := []string{"disable", "allow", "prefer", "required", "verify-ca", "verify-full"}
valid := false

for _, mode := range modes {
if bookmark.Ssl == mode {
valid = true
break
}
}

// Fall back to a default mode if mode is not set or invalid
// Typical typo: ssl mode set to "disabled"
if bookmark.Ssl == "" || !valid {
bookmark.Ssl = "disable"
}

return bookmark, err
}

Expand Down
9 changes: 6 additions & 3 deletions pkg/bookmarks/bookmarks_test.go
Expand Up @@ -19,7 +19,6 @@ func Test_Invalid_Bookmark_Files(t *testing.T) {

func Test_Bookmark(t *testing.T) {
bookmark, err := readServerConfig("../../data/bookmark.toml")

assert.Equal(t, nil, err)
assert.Equal(t, "localhost", bookmark.Host)
assert.Equal(t, 5432, bookmark.Port)
Expand All @@ -28,6 +27,10 @@ func Test_Bookmark(t *testing.T) {
assert.Equal(t, "disable", bookmark.Ssl)
assert.Equal(t, "", bookmark.Password)
assert.Equal(t, "", bookmark.Url)

bookmark, err = readServerConfig("../../data/bookmark_invalid_ssl.toml")
assert.Equal(t, nil, err)
assert.Equal(t, "disable", bookmark.Ssl)
}

func Test_Bookmark_URL(t *testing.T) {
Expand All @@ -39,7 +42,7 @@ func Test_Bookmark_URL(t *testing.T) {
assert.Equal(t, 5432, bookmark.Port)
assert.Equal(t, "", bookmark.User)
assert.Equal(t, "", bookmark.Database)
assert.Equal(t, "", bookmark.Ssl)
assert.Equal(t, "disable", bookmark.Ssl)
assert.Equal(t, "", bookmark.Password)
}

Expand All @@ -65,7 +68,7 @@ func Test_ReadBookmarks(t *testing.T) {
bookmarks, err := ReadAll("../../data")

assert.Equal(t, nil, err)
assert.Equal(t, 2, len(bookmarks))
assert.Equal(t, 3, len(bookmarks))
}

func Test_GetBookmark(t *testing.T) {
Expand Down