diff --git a/pkg/api/api.go b/pkg/api/api.go index 05d293ab6..6d895817e 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -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, }, }) } diff --git a/pkg/command/options.go b/pkg/command/options.go index 0e9fce23d..4e17b01e5 100644 --- a/pkg/command/options.go +++ b/pkg/command/options.go @@ -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) diff --git a/pkg/command/options_test.go b/pkg/command/options_test.go index 565002d12..07b866fbe 100644 --- a/pkg/command/options_test.go +++ b/pkg/command/options_test.go @@ -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") + }) } diff --git a/static/index.html b/static/index.html index 40b9bf2b8..3f1a57339 100644 --- a/static/index.html +++ b/static/index.html @@ -189,14 +189,16 @@