Add support for a bookmarks-only mode (#716)
* Add support for bookmarks-only mode * Add error for missing bookmarks in bookmarks-only mode * Error when settings url or connect backend together with bookmarks-only * Add tests for parsing options
This commit is contained in:
committed by
GitHub
parent
605c483d5b
commit
f4e7643e22
@@ -154,6 +154,8 @@ func Connect(c *gin.Context) {
|
||||
|
||||
if bookmarkID := c.Request.FormValue("bookmark_id"); bookmarkID != "" {
|
||||
cl, err = ConnectWithBookmark(bookmarkID)
|
||||
} else if command.Opts.BookmarksOnly {
|
||||
err = errNotPermitted
|
||||
} else {
|
||||
cl, err = ConnectWithURL(c)
|
||||
}
|
||||
@@ -558,9 +560,10 @@ func GetInfo(c *gin.Context) {
|
||||
successResponse(c, gin.H{
|
||||
"app": command.Info,
|
||||
"features": gin.H{
|
||||
"session_lock": command.Opts.LockSession,
|
||||
"query_timeout": command.Opts.QueryTimeout,
|
||||
"local_queries": QueryStore != nil,
|
||||
"session_lock": command.Opts.LockSession,
|
||||
"query_timeout": command.Opts.QueryTimeout,
|
||||
"local_queries": QueryStore != nil,
|
||||
"bookmarks_only": command.Opts.BookmarksOnly,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -50,6 +50,7 @@ type Options struct {
|
||||
LockSession bool `long:"lock-session" description:"Lock session to a single database connection"`
|
||||
Bookmark string `short:"b" long:"bookmark" description:"Bookmark to use for connection. Bookmark files are stored under $HOME/.pgweb/bookmarks/*.toml" default:""`
|
||||
BookmarksDir string `long:"bookmarks-dir" description:"Overrides default directory for bookmark files to search" default:""`
|
||||
BookmarksOnly bool `long:"bookmarks-only" description:"Allow only connections from bookmarks"`
|
||||
QueriesDir string `long:"queries-dir" description:"Overrides default directory for local queries"`
|
||||
DisablePrettyJSON bool `long:"no-pretty-json" description:"Disable JSON formatting feature for result export"`
|
||||
DisableSSH bool `long:"no-ssh" description:"Disable database connections via SSH"`
|
||||
@@ -118,6 +119,10 @@ func ParseOptions(args []string) (Options, error) {
|
||||
}
|
||||
}
|
||||
|
||||
if getPrefixedEnvVar("BOOKMARKS_ONLY") != "" {
|
||||
opts.BookmarksOnly = true
|
||||
}
|
||||
|
||||
if getPrefixedEnvVar("SESSIONS") != "" {
|
||||
opts.Sessions = true
|
||||
}
|
||||
@@ -162,6 +167,18 @@ func ParseOptions(args []string) (Options, error) {
|
||||
}
|
||||
}
|
||||
|
||||
if opts.BookmarksOnly {
|
||||
if opts.URL != "" {
|
||||
return opts, errors.New("--url not supported in bookmarks-only mode")
|
||||
}
|
||||
if opts.Host != "" && opts.Host != "localhost" {
|
||||
return opts, errors.New("--host not supported in bookmarks-only mode")
|
||||
}
|
||||
if opts.ConnectBackend != "" {
|
||||
return opts, errors.New("--connect-backend not supported in bookmarks-only mode")
|
||||
}
|
||||
}
|
||||
|
||||
homePath, err := homedir.Dir()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "[WARN] cant detect home dir: %v", err)
|
||||
|
||||
@@ -80,4 +80,18 @@ func TestParseOptions(t *testing.T) {
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "../../data/passfile", opts.Passfile)
|
||||
})
|
||||
|
||||
t.Run("bookmarks only mode", func(t *testing.T) {
|
||||
_, err := ParseOptions([]string{"--bookmarks-only"})
|
||||
assert.NoError(t, err)
|
||||
|
||||
_, err = ParseOptions([]string{"--bookmarks-only", "--url", "test"})
|
||||
assert.EqualError(t, err, "--url not supported in bookmarks-only mode")
|
||||
|
||||
_, err = ParseOptions([]string{"--bookmarks-only", "--host", "test", "--port", "5432"})
|
||||
assert.EqualError(t, err, "--host not supported in bookmarks-only mode")
|
||||
|
||||
_, err = ParseOptions([]string{"--bookmarks-only", "--connect-backend", "test", "--sessions", "--connect-token", "token", "--url", "127.0.0.2"})
|
||||
assert.EqualError(t, err, "--connect-backend not supported in bookmarks-only mode")
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user