Files
teleport/lib/tbot/cli/ssh_proxy_test.go
T
Tim BuckleyandNoah Stride 83f10035ff Machine ID: Refactor tbot CLI (#47130)
* Machine ID: Refactor `tbot` CLI

This significantly refactors the tbot CLI. In addition to a major
overhaul of our CLI config handling, it also exposes a number of new
subcommands for starting more than just an identity output via pure
CLI.

* Move new CLI handling into lib/tbot/cli; add all new subcommands

This moves the new CLI handling code into `lib/tbot/cli`, splits
`cli.go` into several better organized files, and adds all missing
subcommands for major tbot functionality.

* Add sane global handling, convert most other commands to new style

This introduces a dedicated global args struct and removes CLIConf.
As most other non-start (and configure) commands depended on that
effectively global namespace, this refactors all of them to use the
new style with properly namespaced command structs.

This also introduces a `genericExecutorHandler` helper to simplify
subcommands that run an arbitrary action without modifying the
config.

This also solves a number of longstanding CLI handling bugs resulting
from the shared arg namespace. A few args were moved to globals that
should always have been exposed, so now things like `--log-format`
will work consistently.

A few commands were left unconverted since they don't make use of
any of the global args or config loading machinery. They could be
converted easily but are technically simpler as written. We may
opt to convert them, or to do so later.

Some headway on testing, but more work is needed.

* First batch of tests

This adds the first batch of tests on the new CLI along with a bunch
of associated changes made while fixing these tests and others.

This also splits off 2 new flag helpers: LegacyDestinationDirArgs and
AuthProxyArgs. These are reusable embeddable structs to handle
legacy-style --destination-dir behavior, and modern --auth-server
and --proxy-server. `tbot init` and its tests had a subtle dependency
old-style `--destination-dir` handling so we can now reuse that.

There may be other subcommands that require something similar.

* Remove outdated TODO

* Restore TestConfigCLIOnlySample

* Fix lints

* Fix missing embedded flag init

* Code review feedback

* Add docstrings

* Add unit tests for all "start" commands and globals

Also includes a few minor fixes discovered in testing - DiagAddr
was not handled properly in shared args, and removed an unused field.

* Fix imports

* Add helper for repetitive test

* Fix command name

* Adjust behaviour of `tbot kube credentials` command

* Add tests for remaining commands

* Appease linter

* Add godocs

---------

Co-authored-by: Noah Stride <noah.stride@goteleport.com>
2024-10-11 08:22:34 +00:00

57 lines
1.7 KiB
Go

/*
* 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 cli
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestSSHProxyCommand(t *testing.T) {
testCommand(t, NewSSHProxyCommand, []testCommandCase[*SSHProxyCommand]{
{
name: "success",
args: []string{
"ssh-proxy-command",
"--destination-dir=/bar",
"--cluster=foo",
"--user=noah",
"--host=example.com",
"--proxy-server=example.com:443",
"--tls-routing",
"--connection-upgrade",
"--proxy-templates=/tmp/tsh.yaml",
"--resume",
},
assert: func(t *testing.T, got *SSHProxyCommand) {
require.Equal(t, "/bar", got.DestinationDir)
require.Equal(t, "foo", got.Cluster)
require.Equal(t, "noah", got.User)
require.Equal(t, "example.com", got.Host)
require.Equal(t, "example.com:443", got.ProxyServer)
require.True(t, got.TLSRoutingEnabled)
require.True(t, got.ConnectionUpgradeRequired)
require.Equal(t, "/tmp/tsh.yaml", got.TSHConfigPath)
require.True(t, got.EnableResumption)
},
},
})
}