teleport/tool/tbot/db.go
Noah Stride 8b346ef06b
Refactor tbot (#12855)
* start refactoring tbot to have a core struct

* refactor tbot into lib/

* move `tbot` subpackages to `lib/tbot`

* remove mutex pointer

* move `tshwrap` to `lib/` from `/tool/tbot/`

* move new template ssh client render test to lib/

* address pr feedback

* add request changed
2022-06-01 17:15:26 +00:00

83 lines
2.3 KiB
Go

/*
Copyright 2022 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"path/filepath"
"github.com/gravitational/teleport/lib/tbot/config"
"github.com/gravitational/teleport/lib/tbot/tshwrap"
"github.com/gravitational/teleport/lib/utils"
"github.com/gravitational/trace"
)
func onDBCommand(botConfig *config.BotConfig, cf *config.CLIConf) error {
wrapper, err := tshwrap.New()
if err != nil {
return trace.Wrap(err)
}
if err := tshwrap.CheckTSHSupported(wrapper); err != nil {
return trace.Wrap(err)
}
destination, err := tshwrap.GetDestination(botConfig, cf)
if err != nil {
return trace.Wrap(err)
}
destinationPath, err := tshwrap.GetDestinationPath(destination)
if err != nil {
return trace.Wrap(err)
}
identityTemplate, err := tshwrap.GetIdentityTemplate(destination)
if err != nil {
return trace.Wrap(err)
}
env, err := tshwrap.GetEnvForTSH(destination)
if err != nil {
return trace.Wrap(err)
}
identityPath := filepath.Join(destinationPath, identityTemplate.FileName)
identity, err := tshwrap.LoadIdentity(identityPath)
if err != nil {
return trace.Wrap(err)
}
args := []string{"-i", identityPath, "db", "--proxy=" + cf.Proxy}
if cf.Cluster != "" {
// If we caught --cluster in our args, pass it through.
args = append(args, "--cluster="+cf.Cluster)
} else if !utils.HasPrefixAny("--cluster", cf.RemainingArgs) {
// If no `--cluster` was provided after a `--`, pass along the cluster
// name in the identity.
args = append(args, "--cluster="+identity.RouteToCluster)
}
args = append(args, cf.RemainingArgs...)
// Pass through the debug flag, and prepend to satisfy argument ordering
// needs (`-d` must precede `db`).
if botConfig.Debug {
args = append([]string{"-d"}, args...)
}
return trace.Wrap(wrapper.Exec(env, args...), "executing `tsh db`")
}