-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Add WebSocket handler for WebUI database sessions #49749
Merged
Merged
Changes from 5 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
0e2468e
feat(web): add websocket handler for database webui sessions
gabrielcorado 1a70cc5
refactor: move common structs into a separate package
gabrielcorado add121d
refactor(web): use ALPN local proxy to dial databases
gabrielcorado 25f7e6e
Merge branch 'master' into gabrielcorado/pg-webui-handler
gabrielcorado 7a6fd3b
feat(repl): add default registry
gabrielcorado bbb6b38
refactor(web): code review suggestions
gabrielcorado 6ef2712
refactor: update repl config parameters
gabrielcorado 0db0613
refactor: move default getter implementation
gabrielcorado b76c6f7
Merge branch 'master' into gabrielcorado/pg-webui-handler
gabrielcorado 6237ed0
feat(web): add supports_interactive field on dbs
gabrielcorado 46df432
Merge branch 'master' into gabrielcorado/pg-webui-handler
gabrielcorado 010445e
refactor: code review suggestions
gabrielcorado 9cb2b71
refactor: update database REPL interfaces
gabrielcorado 4c67214
Merge branch 'master' into gabrielcorado/pg-webui-handler
gabrielcorado e773a87
chore(web): remove debug print
gabrielcorado b344080
Merge branch 'master' into gabrielcorado/pg-webui-handler
gabrielcorado df26468
feat: register postgres repl
gabrielcorado 0e094b9
refactor(web): update MakeDatabase to receive access checker and inte…
gabrielcorado c05b558
Merge branch 'master' into gabrielcorado/pg-webui-handler
gabrielcorado a53effd
chore(web): remove unused function
gabrielcorado File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Teleport | ||
// Copyright (C) 2024 Gravitational, Inc. | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU Affero General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU Affero General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU Affero General Public License | ||
// along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package repl | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"net" | ||
"sync" | ||
|
||
"github.com/gravitational/trace" | ||
|
||
"github.com/gravitational/teleport/lib/tlsca" | ||
) | ||
|
||
// NewREPLConfig represents the database REPL constructor config. | ||
type NewREPLConfig struct { | ||
// Client is the user terminal client. | ||
Client io.ReadWriter | ||
// ServerConn is the database server connection. | ||
ServerConn net.Conn | ||
// Route is the session routing information. | ||
Route tlsca.RouteToDatabase | ||
} | ||
|
||
// REPLNewFunc defines the constructor function for database REPL | ||
// sessions. | ||
type REPLNewFunc func(context.Context, *NewREPLConfig) (REPLInstance, error) | ||
|
||
// REPLInstance represents a REPL instance. | ||
type REPLInstance interface { | ||
// Run executes the REPL. This is a blocking operation. | ||
Run(context.Context) error | ||
} | ||
|
||
// REPLGetter is an interface for retrieving REPL constructor functions given | ||
// the database protocol. | ||
type REPLGetter interface { | ||
// GetREPL returns a start function for the specified protocol. | ||
GetREPL(ctx context.Context, dbProtocol string) (REPLNewFunc, error) | ||
} | ||
|
||
// registry implements a default package level registry. | ||
var registry = &REPLRegistry{repl: make(map[string]REPLNewFunc)} | ||
|
||
// REPLRegistry implements a database REPL registry. | ||
type REPLRegistry struct { | ||
mu sync.Mutex | ||
gabrielcorado marked this conversation as resolved.
Show resolved
Hide resolved
|
||
repl map[string]REPLNewFunc | ||
} | ||
|
||
// GetREPL implements REPLGetter. | ||
func (r *REPLRegistry) GetREPL(_ context.Context, dbProtocol string) (REPLNewFunc, error) { | ||
r.mu.Lock() | ||
defer r.mu.Unlock() | ||
|
||
if startFunc, ok := r.repl[dbProtocol]; ok { | ||
return startFunc, nil | ||
} | ||
|
||
return nil, trace.NotImplemented("REPL not registered for protocol %q", dbProtocol) | ||
} | ||
|
||
func (r *REPLRegistry) register(dbProtocol string, f REPLNewFunc) { | ||
r.mu.Lock() | ||
defer r.mu.Unlock() | ||
|
||
r.repl[dbProtocol] = f | ||
} | ||
|
||
// DefaultGetter implements a default instance of REPLGetter. | ||
var DefaultGetter REPLGetter = registry | ||
|
||
// RegisterREPL registers a new REPL for the database protocol. | ||
func RegisterREPL(dbProtocol string, f REPLNewFunc) { | ||
registry.register(dbProtocol, f) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -43,6 +43,7 @@ import ( | |||||
"github.com/gravitational/teleport/lib/auth/state" | ||||||
"github.com/gravitational/teleport/lib/backend" | ||||||
"github.com/gravitational/teleport/lib/backend/lite" | ||||||
dbrepl "github.com/gravitational/teleport/lib/client/db/repl" | ||||||
"github.com/gravitational/teleport/lib/cloud/imds" | ||||||
"github.com/gravitational/teleport/lib/defaults" | ||||||
"github.com/gravitational/teleport/lib/events" | ||||||
|
@@ -265,6 +266,10 @@ type Config struct { | |||||
// AccessGraph represents AccessGraph server config | ||||||
AccessGraph AccessGraphConfig | ||||||
|
||||||
// DatabaseREPLGetter is used to retrieve datatabase REPL given the | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
// protocol. | ||||||
DatabaseREPLGetter dbrepl.REPLGetter | ||||||
|
||||||
// token is either the token needed to join the auth server, or a path pointing to a file | ||||||
// that contains the token | ||||||
// | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
some interface nits:
is
REPLNewFunc
necessary? canGetREPL
return theREPLInstance
?would it be easier if
REPLGetter
is a func instead of an interface? Do we expect other functions forREPLGetter
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't initialize the REPL right away, so we can reuse this function to fill the
SupportsInteractive
field on the API. The WebUI uses this field to show the connect button on the database connect dialog.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok. if the only goal is the check
SupportsInteractive
at an earlier stage, could we have a dedicated call? something like:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated the interface, can you take a look?