mirror of
https://github.com/gravitational/teleport.git
synced 2026-11-02 23:31:08 +08:00
* 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>
76 lines
2.7 KiB
Go
76 lines
2.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 (
|
|
"log/slog"
|
|
|
|
"github.com/gravitational/trace"
|
|
|
|
"github.com/gravitational/teleport/lib/tbot/config"
|
|
)
|
|
|
|
// InitCommand implements a command for `tbot init`
|
|
type InitCommand struct {
|
|
*AuthProxyArgs
|
|
*LegacyDestinationDirArgs
|
|
*genericExecutorHandler[InitCommand]
|
|
|
|
Owner string
|
|
BotUser string
|
|
ReaderUser string
|
|
InitDir string
|
|
Clean bool
|
|
}
|
|
|
|
// NewInitCommand constructs an InitCommand at the top level of the given
|
|
// application. It will execute `action` when selected by the user.
|
|
func NewInitCommand(app KingpinClause, action func(*InitCommand) error) *InitCommand {
|
|
cmd := app.Command("init", "Initialize a certificate destination directory for writes from a separate bot user.")
|
|
|
|
c := &InitCommand{}
|
|
c.AuthProxyArgs = newAuthProxyArgs(cmd)
|
|
c.LegacyDestinationDirArgs = newLegacyDestinationDirArgs(cmd)
|
|
c.genericExecutorHandler = newGenericExecutorHandler(cmd, c, action)
|
|
|
|
cmd.Flag("owner", "Defines Linux \"user:group\" owner of \"--destination-dir\". Defaults to the Linux user running tbot if unspecified.").StringVar(&c.Owner)
|
|
cmd.Flag("bot-user", "Enables POSIX ACLs and defines Linux user that can read/write short-lived certificates to \"--destination-dir\".").StringVar(&c.BotUser)
|
|
cmd.Flag("reader-user", "Enables POSIX ACLs and defines Linux user that will read short-lived certificates from \"--destination-dir\".").StringVar(&c.ReaderUser)
|
|
cmd.Flag("init-dir", "If using a config file and multiple destinations are configured, controls which destination dir to configure.").StringVar(&c.InitDir)
|
|
cmd.Flag("clean", "If set, remove unexpected files and directories from the destination.").BoolVar(&c.Clean)
|
|
|
|
return c
|
|
}
|
|
|
|
func (c *InitCommand) ApplyConfig(cfg *config.BotConfig, l *slog.Logger) error {
|
|
if c.AuthProxyArgs != nil {
|
|
if err := c.AuthProxyArgs.ApplyConfig(cfg, l); err != nil {
|
|
return trace.Wrap(err)
|
|
}
|
|
}
|
|
|
|
if c.LegacyDestinationDirArgs != nil {
|
|
if err := c.LegacyDestinationDirArgs.ApplyConfig(cfg, l); err != nil {
|
|
return trace.Wrap(err)
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|