Fall back to sslmode=disable if ssl mode is not set or invalid in bookmarks

This commit is contained in:
Dan Sosedoff 2017-06-05 21:29:09 -05:00
parent 943963f760
commit 75dc1c1548
3 changed files with 28 additions and 3 deletions

View File

@ -0,0 +1,5 @@
host = "localhost"
port = 5432
user = "postgres"
database = "mydatabase"
ssl = "disabled"

View File

@ -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
}

View File

@ -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)
@ -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) {
@ -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)
}
@ -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) {