Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions tool/tsh/common/access_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,13 @@ func onRequestSearch(cf *CLIConf) error {
if err != nil {
return trace.Wrap(err)
}
tableColumns = []string{"Name", "Hostname", "Labels", "Resource ID"}

switch cf.ResourceKind {
case types.KindDatabase:
tableColumns = []string{"Database Name", "Labels", "Resource ID"}
default:
tableColumns = []string{"Name", "Hostname", "Labels", "Resource ID"}
}
}

var rows [][]string
Expand Down Expand Up @@ -512,11 +518,21 @@ func onRequestSearch(cf *CLIConf) error {
if r, ok := resource.(interface{ GetHostname() string }); ok {
hostName = r.GetHostname()
}
row = []string{
common.FormatResourceName(resource, cf.Verbose),
hostName,
common.FormatLabels(resource.GetAllLabels(), cf.Verbose),
resourceID,

switch cf.ResourceKind {
case types.KindDatabase:
row = []string{
common.FormatResourceName(resource, cf.Verbose),
common.FormatLabels(resource.GetAllLabels(), cf.Verbose),
resourceID,
}
default:
row = []string{
common.FormatResourceName(resource, cf.Verbose),
hostName,
common.FormatLabels(resource.GetAllLabels(), cf.Verbose),
resourceID,
}
}
}
rows = append(rows, row)
Expand Down
93 changes: 92 additions & 1 deletion tool/tsh/common/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,38 @@ func getDatabaseInfo(cf *CLIConf, tc *client.TeleportClient, routes []tlsca.Rout
}

db, err := getDatabaseByNameOrDiscoveredName(cf, tc, routes)
if err != nil {
switch {
// If the database cannot be found, try again with UseSearchAsRoles. If
// the database is then found with UseSearchAsRoles, make an access request
// for it and elevate the user with the request ID upon approval.
//
// Note that the access request must be made before the database connection
// is made to avoid mangling the request with the database client tools.
// Thus the flow for auto database access request is different from SSH.
//
// Performance considerations:
// - For common scenarios where UseSearchAsRoles is not desired, it would
// be rare that cf.DatabaseName would be not found in the first API call
// so there won't be a second call usually.
// - accessChecker.GetAllowedSearchAsRoles can be checked to avoid the
// second API call but creating the access checker requires more calls.
// - The db commands do provide "--disable-access-request" to bypass the
// second call. If needed, we can add it to `tsh login` and profile yaml
// in the future.
case shouldRetryGetDatabaseUsingSearchAsRoles(cf, tc, err):
orgErr := err
if db, err = getDatabaseByNameOrDiscoveredNameUsingSearchAsRoles(cf, tc); err != nil {
return nil, trace.Wrap(orgErr) // Returns the original not found error.
}
if err := makeDatabaseAccessRequestAndWaitForApproval(cf, tc, db); err != nil {
return nil, trace.Wrap(err)
}

// Reset routes. Once access requeset is approved, user certs are
// reissued with client.CertCacheDrop.
routes = nil

case err != nil:
return nil, trace.Wrap(err)
}

Expand All @@ -853,6 +884,56 @@ func getDatabaseInfo(cf *CLIConf, tc *client.TeleportClient, routes []tlsca.Rout
return info, nil
}

var dbCommandsWithAccessRequestSupport = []string{
"db login",
"proxy db",
"db connect",
}

func shouldRetryGetDatabaseUsingSearchAsRoles(cf *CLIConf, tc *client.TeleportClient, getDatabaseError error) bool {
// If already using SearchAsRoles, nothing to retry.
if tc.UseSearchAsRoles {
return false
}
// Only retry when the database cannot be found.
if !trace.IsNotFound(getDatabaseError) {
return false
}
// Check if auto access request is disabled.
if cf.disableAccessRequest {
return false
}
// Check if the `tsh` command supports auto access request.
return slices.Contains(dbCommandsWithAccessRequestSupport, cf.command)
}

func makeAccessRequestForDatabase(tc *client.TeleportClient, db types.Database) (types.AccessRequest, error) {
requestResourceIDs := []types.ResourceID{{
ClusterName: tc.SiteName,
Kind: types.KindDatabase,
Name: db.GetName(),
}}

req, err := services.NewAccessRequestWithResources(tc.Username, nil /* roles */, requestResourceIDs)
return req, trace.Wrap(err)
}

func makeDatabaseAccessRequestAndWaitForApproval(cf *CLIConf, tc *client.TeleportClient, db types.Database) error {
req, err := makeAccessRequestForDatabase(tc, db)
if err != nil {
return trace.Wrap(err)
}

fmt.Fprintf(cf.Stdout(), "You do not currently have access to %q, attempting to request access.\n\n", db.GetName())
if err := setAccessRequestReason(cf, req); err != nil {
return trace.Wrap(err)
}
if err := sendAccessRequestAndWaitForApproval(cf, tc, req); err != nil {
return trace.Wrap(err)
}
return nil
}

func requestedDatabaseRoles(cf *CLIConf) []string {
if cf.DatabaseRoles == "" {
return nil
Expand Down Expand Up @@ -1043,6 +1124,15 @@ func getDatabaseByNameOrDiscoveredName(cf *CLIConf, tc *client.TeleportClient, a
return chooseOneDatabase(cf, databases)
}

func getDatabaseByNameOrDiscoveredNameUsingSearchAsRoles(cf *CLIConf, tc *client.TeleportClient) (types.Database, error) {
tc.UseSearchAsRoles = true
defer func() {
tc.UseSearchAsRoles = false
}()
db, err := getDatabaseByNameOrDiscoveredName(cf, tc, nil)
return db, trace.Wrap(err)
}

func filterActiveDatabases(routes []tlsca.RouteToDatabase, databases types.Databases) types.Databases {
databasesByName := databases.ToMap()
var out types.Databases
Expand All @@ -1068,6 +1158,7 @@ func listDatabasesWithPredicate(ctx context.Context, tc *client.TeleportClient,
ResourceType: types.KindDatabaseServer,
PredicateExpression: predicate,
Labels: tc.Labels,
UseSearchAsRoles: tc.UseSearchAsRoles,
})
return trace.Wrap(err)
})
Expand Down
Loading