Skip to content

Commit

Permalink
Add access to global config for the commands
Browse files Browse the repository at this point in the history
  • Loading branch information
vapopov committed Dec 17, 2024
1 parent ecbbbf5 commit 8047189
Show file tree
Hide file tree
Showing 41 changed files with 86 additions and 48 deletions.
4 changes: 2 additions & 2 deletions integration/tctl_terraform_env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func TestTCTLTerraformCommand_ProxyJoin(t *testing.T) {
tctlCommand := common.TerraformCommand{}

app := kingpin.New("test", "test")
tctlCommand.Initialize(app, tctlCfg)
tctlCommand.Initialize(app, nil, tctlCfg)
_, err = app.Parse([]string{"terraform", "env"})
require.NoError(t, err)
// Create io buffer writer
Expand Down Expand Up @@ -179,7 +179,7 @@ func TestTCTLTerraformCommand_AuthJoin(t *testing.T) {
tctlCommand := common.TerraformCommand{}

app := kingpin.New("test", "test")
tctlCommand.Initialize(app, tctlCfg)
tctlCommand.Initialize(app, nil, tctlCfg)
_, err = app.Parse([]string{"terraform", "env"})
require.NoError(t, err)
// Create io buffer writer
Expand Down
3 changes: 2 additions & 1 deletion tool/tctl/common/access_request_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
"github.com/gravitational/teleport/lib/services"
"github.com/gravitational/teleport/lib/tlsca"
commonclient "github.com/gravitational/teleport/tool/tctl/common/client"
tctlcfg "github.com/gravitational/teleport/tool/tctl/common/config"
)

// AccessRequestCommand implements `tctl users` set of commands
Expand Down Expand Up @@ -77,7 +78,7 @@ type AccessRequestCommand struct {
}

// Initialize allows AccessRequestCommand to plug itself into the CLI parser
func (c *AccessRequestCommand) Initialize(app *kingpin.Application, config *servicecfg.Config) {
func (c *AccessRequestCommand) Initialize(app *kingpin.Application, _ *tctlcfg.GlobalCLIFlags, config *servicecfg.Config) {
c.config = config
requests := app.Command("requests", "Manage access requests.").Alias("request")

Expand Down
3 changes: 2 additions & 1 deletion tool/tctl/common/accessmonitoring/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/gravitational/teleport/lib/service/servicecfg"
"github.com/gravitational/teleport/lib/utils"
commonclient "github.com/gravitational/teleport/tool/tctl/common/client"
tctlcfg "github.com/gravitational/teleport/tool/tctl/common/config"
)

// Command implements `tctl audit` group of commands.
Expand All @@ -45,7 +46,7 @@ type Command struct {
}

// Initialize allows to implement Command interface.
func (c *Command) Initialize(app *kingpin.Application, cfg *servicecfg.Config) {
func (c *Command) Initialize(app *kingpin.Application, _ *tctlcfg.GlobalCLIFlags, cfg *servicecfg.Config) {
c.innerCmdMap = map[string]runFunc{}

auditCmd := app.Command("audit", "Audit command.")
Expand Down
3 changes: 2 additions & 1 deletion tool/tctl/common/acl_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/gravitational/teleport/lib/service/servicecfg"
"github.com/gravitational/teleport/lib/utils"
commonclient "github.com/gravitational/teleport/tool/tctl/common/client"
tctlcfg "github.com/gravitational/teleport/tool/tctl/common/config"
)

// ACLCommand implements the `tctl acl` family of commands.
Expand Down Expand Up @@ -65,7 +66,7 @@ const (
)

// Initialize allows ACLCommand to plug itself into the CLI parser
func (c *ACLCommand) Initialize(app *kingpin.Application, _ *servicecfg.Config) {
func (c *ACLCommand) Initialize(app *kingpin.Application, _ *tctlcfg.GlobalCLIFlags, _ *servicecfg.Config) {
acl := app.Command("acl", "Manage access lists.").Alias("access-lists")

c.ls = acl.Command("ls", "List cluster access lists.")
Expand Down
3 changes: 2 additions & 1 deletion tool/tctl/common/admin_action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import (
"github.com/gravitational/teleport/lib/utils"
"github.com/gravitational/teleport/lib/utils/hostid"
tctl "github.com/gravitational/teleport/tool/tctl/common"
tctlcfg "github.com/gravitational/teleport/tool/tctl/common/config"
testserver "github.com/gravitational/teleport/tool/teleport/testenv"
tsh "github.com/gravitational/teleport/tool/tsh/common"
)
Expand Down Expand Up @@ -1156,7 +1157,7 @@ func runTestCase(t *testing.T, ctx context.Context, client *authclient.Client, t

app := utils.InitCLIParser("tctl", tctl.GlobalHelpString)
cfg := servicecfg.MakeDefaultConfig()
tc.cliCommand.Initialize(app, cfg)
tc.cliCommand.Initialize(app, &tctlcfg.GlobalCLIFlags{}, cfg)

args := strings.Split(tc.command, " ")
commandName, err := app.Parse(args)
Expand Down
3 changes: 2 additions & 1 deletion tool/tctl/common/alert_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
libclient "github.com/gravitational/teleport/lib/client"
"github.com/gravitational/teleport/lib/service/servicecfg"
commonclient "github.com/gravitational/teleport/tool/tctl/common/client"
tctlcfg "github.com/gravitational/teleport/tool/tctl/common/config"
)

// AlertCommand implements the `tctl alerts` family of commands.
Expand All @@ -63,7 +64,7 @@ type AlertCommand struct {
}

// Initialize allows AlertCommand to plug itself into the CLI parser
func (c *AlertCommand) Initialize(app *kingpin.Application, config *servicecfg.Config) {
func (c *AlertCommand) Initialize(app *kingpin.Application, _ *tctlcfg.GlobalCLIFlags, config *servicecfg.Config) {
c.config = config
alert := app.Command("alerts", "Manage cluster alerts.").Alias("alert")

Expand Down
3 changes: 2 additions & 1 deletion tool/tctl/common/app_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/gravitational/teleport/lib/service/servicecfg"
"github.com/gravitational/teleport/lib/utils"
commonclient "github.com/gravitational/teleport/tool/tctl/common/client"
tctlcfg "github.com/gravitational/teleport/tool/tctl/common/config"
)

// AppsCommand implements "tctl apps" group of commands.
Expand All @@ -56,7 +57,7 @@ type AppsCommand struct {
}

// Initialize allows AppsCommand to plug itself into the CLI parser
func (c *AppsCommand) Initialize(app *kingpin.Application, config *servicecfg.Config) {
func (c *AppsCommand) Initialize(app *kingpin.Application, _ *tctlcfg.GlobalCLIFlags, config *servicecfg.Config) {
c.config = config

apps := app.Command("apps", "Operate on applications registered with the cluster.")
Expand Down
3 changes: 2 additions & 1 deletion tool/tctl/common/auth_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import (
"github.com/gravitational/teleport/lib/services"
"github.com/gravitational/teleport/lib/utils"
commonclient "github.com/gravitational/teleport/tool/tctl/common/client"
tctlcfg "github.com/gravitational/teleport/tool/tctl/common/config"
)

// authCommandClient is aggregated client interface for auth command.
Expand Down Expand Up @@ -110,7 +111,7 @@ type AuthCommand struct {
}

// Initialize allows TokenCommand to plug itself into the CLI parser
func (a *AuthCommand) Initialize(app *kingpin.Application, config *servicecfg.Config) {
func (a *AuthCommand) Initialize(app *kingpin.Application, _ *tctlcfg.GlobalCLIFlags, config *servicecfg.Config) {
a.config = config
// operations with authorities
auth := app.Command("auth", "Operations with user and host certificate authorities (CAs).").Hidden()
Expand Down
3 changes: 2 additions & 1 deletion tool/tctl/common/bots_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import (
"github.com/gravitational/teleport/lib/service/servicecfg"
"github.com/gravitational/teleport/lib/utils"
commonclient "github.com/gravitational/teleport/tool/tctl/common/client"
tctlcfg "github.com/gravitational/teleport/tool/tctl/common/config"
)

type BotsCommand struct {
Expand Down Expand Up @@ -83,7 +84,7 @@ type BotsCommand struct {
}

// Initialize sets up the "tctl bots" command.
func (c *BotsCommand) Initialize(app *kingpin.Application, config *servicecfg.Config) {
func (c *BotsCommand) Initialize(app *kingpin.Application, _ *tctlcfg.GlobalCLIFlags, config *servicecfg.Config) {
bots := app.Command("bots", "Manage Machine ID bots on the cluster.").Alias("bot")

c.botsList = bots.Command("ls", "List all certificate renewal bots registered with the cluster.")
Expand Down
3 changes: 2 additions & 1 deletion tool/tctl/common/db_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/gravitational/teleport/lib/service/servicecfg"
"github.com/gravitational/teleport/lib/utils"
commonclient "github.com/gravitational/teleport/tool/tctl/common/client"
tctlcfg "github.com/gravitational/teleport/tool/tctl/common/config"
)

// DBCommand implements "tctl db" group of commands.
Expand All @@ -56,7 +57,7 @@ type DBCommand struct {
}

// Initialize allows DBCommand to plug itself into the CLI parser.
func (c *DBCommand) Initialize(app *kingpin.Application, config *servicecfg.Config) {
func (c *DBCommand) Initialize(app *kingpin.Application, _ *tctlcfg.GlobalCLIFlags, config *servicecfg.Config) {
c.config = config

db := app.Command("db", "Operate on databases registered with the cluster.")
Expand Down
3 changes: 2 additions & 1 deletion tool/tctl/common/desktop_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/gravitational/teleport/lib/auth/authclient"
"github.com/gravitational/teleport/lib/service/servicecfg"
commonclient "github.com/gravitational/teleport/tool/tctl/common/client"
tctlcfg "github.com/gravitational/teleport/tool/tctl/common/config"
)

// DesktopCommand implements "tctl desktop" group of commands.
Expand All @@ -51,7 +52,7 @@ type DesktopCommand struct {
}

// Initialize allows DesktopCommand to plug itself into the CLI parser
func (c *DesktopCommand) Initialize(app *kingpin.Application, config *servicecfg.Config) {
func (c *DesktopCommand) Initialize(app *kingpin.Application, _ *tctlcfg.GlobalCLIFlags, config *servicecfg.Config) {
c.config = config

desktop := app.Command("desktop", "Operate on registered desktops.").Alias("desktops").Alias("windows_desktop").Alias("windows_desktops")
Expand Down
3 changes: 2 additions & 1 deletion tool/tctl/common/devices.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
dtnative "github.com/gravitational/teleport/lib/devicetrust/native"
"github.com/gravitational/teleport/lib/service/servicecfg"
commonclient "github.com/gravitational/teleport/tool/tctl/common/client"
tctlcfg "github.com/gravitational/teleport/tool/tctl/common/config"
)

// DevicesCommand implements the `tctl devices` command.
Expand Down Expand Up @@ -68,7 +69,7 @@ var osTypeToEnum = map[osType]devicepb.OSType{
windowsType: devicepb.OSType_OS_TYPE_WINDOWS,
}

func (c *DevicesCommand) Initialize(app *kingpin.Application, cfg *servicecfg.Config) {
func (c *DevicesCommand) Initialize(app *kingpin.Application, _ *tctlcfg.GlobalCLIFlags, cfg *servicecfg.Config) {
devicesCmd := app.Command("devices", "Register and manage trusted devices").Hidden()

addCmd := devicesCmd.Command("add", "Register managed devices.")
Expand Down
5 changes: 3 additions & 2 deletions tool/tctl/common/edit_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
"github.com/gravitational/teleport/lib/services"
"github.com/gravitational/teleport/lib/utils"
commonclient "github.com/gravitational/teleport/tool/tctl/common/client"
tctlcfg "github.com/gravitational/teleport/tool/tctl/common/config"
)

// EditCommand implements the `tctl edit` command for modifying
Expand All @@ -56,7 +57,7 @@ type EditCommand struct {
Editor func(filename string) error
}

func (e *EditCommand) Initialize(app *kingpin.Application, config *servicecfg.Config) {
func (e *EditCommand) Initialize(app *kingpin.Application, _ *tctlcfg.GlobalCLIFlags, config *servicecfg.Config) {
e.app = app
e.config = config
e.cmd = app.Command("edit", "Edit a Teleport resource.")
Expand Down Expand Up @@ -124,7 +125,7 @@ func (e *EditCommand) editResource(ctx context.Context, client *authclient.Clien
withSecrets: true,
confirm: e.confirm,
}
rc.Initialize(e.app, e.config)
rc.Initialize(e.app, nil, e.config)

err = rc.Get(ctx, client)
if closeErr := f.Close(); closeErr != nil {
Expand Down
3 changes: 2 additions & 1 deletion tool/tctl/common/externalauditstorage_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/gravitational/teleport/lib/auth/authclient"
"github.com/gravitational/teleport/lib/service/servicecfg"
commonclient "github.com/gravitational/teleport/tool/tctl/common/client"
tctlcfg "github.com/gravitational/teleport/tool/tctl/common/config"
)

// ExternalAuditStorageCommand implements "tctl externalauditstorage" group of commands.
Expand All @@ -43,7 +44,7 @@ type ExternalAuditStorageCommand struct {
}

// Initialize allows ExternalAuditStorageCommand to plug itself into the CLI parser.
func (c *ExternalAuditStorageCommand) Initialize(app *kingpin.Application, config *servicecfg.Config) {
func (c *ExternalAuditStorageCommand) Initialize(app *kingpin.Application, _ *tctlcfg.GlobalCLIFlags, config *servicecfg.Config) {
c.config = config

externalAuditStorage := app.Command("externalauditstorage", "Operate on External Audit Storage configuration.").Hidden()
Expand Down
3 changes: 2 additions & 1 deletion tool/tctl/common/fido2.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ import (
"github.com/gravitational/teleport/lib/service/servicecfg"
"github.com/gravitational/teleport/tool/common/fido2"
commonclient "github.com/gravitational/teleport/tool/tctl/common/client"
tctlcfg "github.com/gravitational/teleport/tool/tctl/common/config"
)

// fido2Command adapts fido2.Command for tctl.
type fido2Command struct {
impl *fido2.Command
}

func (c *fido2Command) Initialize(app *kingpin.Application, _ *servicecfg.Config) {
func (c *fido2Command) Initialize(app *kingpin.Application, _ *tctlcfg.GlobalCLIFlags, _ *servicecfg.Config) {
c.impl = fido2.NewCommand(app)
}

Expand Down
5 changes: 3 additions & 2 deletions tool/tctl/common/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import (
"github.com/gravitational/teleport/lib/service/servicecfg"
"github.com/gravitational/teleport/lib/utils"
commonclient "github.com/gravitational/teleport/tool/tctl/common/client"
tctlcfg "github.com/gravitational/teleport/tool/tctl/common/config"
)

type options struct {
Expand All @@ -60,7 +61,7 @@ func withEditor(editor func(string) error) optionsFunc {
}

type cliCommand interface {
Initialize(app *kingpin.Application, cfg *servicecfg.Config)
Initialize(app *kingpin.Application, _ *tctlcfg.GlobalCLIFlags, cfg *servicecfg.Config)
TryRun(ctx context.Context, cmd string, clientFunc commonclient.InitFunc) (bool, error)
}

Expand All @@ -69,7 +70,7 @@ func runCommand(t *testing.T, client *authclient.Client, cmd cliCommand, args []
cfg.CircuitBreakerConfig = breaker.NoopBreakerConfig()

app := utils.InitCLIParser("tctl", GlobalHelpString)
cmd.Initialize(app, cfg)
cmd.Initialize(app, &tctlcfg.GlobalCLIFlags{}, cfg)

selectedCmd, err := app.Parse(args)
require.NoError(t, err)
Expand Down
3 changes: 2 additions & 1 deletion tool/tctl/common/idp_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"github.com/gravitational/teleport/lib/service/servicecfg"
"github.com/gravitational/teleport/lib/utils"
commonclient "github.com/gravitational/teleport/tool/tctl/common/client"
tctlcfg "github.com/gravitational/teleport/tool/tctl/common/config"
)

// subcommandRunner is used to create pluggable subcommand under
Expand All @@ -63,7 +64,7 @@ type samlIdPCommand struct {
}

// Initialize installs the base "idp" command and all subcommands.
func (t *IdPCommand) Initialize(app *kingpin.Application, cfg *servicecfg.Config) {
func (t *IdPCommand) Initialize(app *kingpin.Application, _ *tctlcfg.GlobalCLIFlags, cfg *servicecfg.Config) {
idp := app.Command("idp", "Teleport Identity Provider")

idp.Alias(`
Expand Down
3 changes: 2 additions & 1 deletion tool/tctl/common/inventory_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/gravitational/teleport/lib/utils"
vc "github.com/gravitational/teleport/lib/versioncontrol"
commonclient "github.com/gravitational/teleport/tool/tctl/common/client"
tctlcfg "github.com/gravitational/teleport/tool/tctl/common/config"
)

// InventoryCommand implements the `tctl inventory` family of commands.
Expand Down Expand Up @@ -65,7 +66,7 @@ type InventoryCommand struct {
}

// Initialize allows AccessRequestCommand to plug itself into the CLI parser
func (c *InventoryCommand) Initialize(app *kingpin.Application, config *servicecfg.Config) {
func (c *InventoryCommand) Initialize(app *kingpin.Application, _ *tctlcfg.GlobalCLIFlags, config *servicecfg.Config) {
c.config = config
inventory := app.Command("inventory", "Manage Teleport instance inventory.").Hidden()

Expand Down
3 changes: 2 additions & 1 deletion tool/tctl/common/kube_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/gravitational/teleport/lib/service/servicecfg"
"github.com/gravitational/teleport/lib/utils"
commonclient "github.com/gravitational/teleport/tool/tctl/common/client"
tctlcfg "github.com/gravitational/teleport/tool/tctl/common/config"
)

// KubeCommand implements "tctl kube" group of commands.
Expand All @@ -56,7 +57,7 @@ type KubeCommand struct {
}

// Initialize allows KubeCommand to plug itself into the CLI parser
func (c *KubeCommand) Initialize(app *kingpin.Application, config *servicecfg.Config) {
func (c *KubeCommand) Initialize(app *kingpin.Application, _ *tctlcfg.GlobalCLIFlags, config *servicecfg.Config) {
c.config = config

kube := app.Command("kube", "Operate on registered Kubernetes clusters.")
Expand Down
3 changes: 2 additions & 1 deletion tool/tctl/common/loadtest_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import (
"github.com/gravitational/teleport/lib/services"
"github.com/gravitational/teleport/lib/utils"
commonclient "github.com/gravitational/teleport/tool/tctl/common/client"
tctlcfg "github.com/gravitational/teleport/tool/tctl/common/config"
)

// LoadtestCommand implements the `tctl loadtest` family of commands.
Expand Down Expand Up @@ -72,7 +73,7 @@ type LoadtestCommand struct {
}

// Initialize allows LoadtestCommand to plug itself into the CLI parser
func (c *LoadtestCommand) Initialize(app *kingpin.Application, config *servicecfg.Config) {
func (c *LoadtestCommand) Initialize(app *kingpin.Application, _ *tctlcfg.GlobalCLIFlags, config *servicecfg.Config) {
c.config = config
loadtest := app.Command("loadtest", "Tools for generating artificial load").Hidden()

Expand Down
3 changes: 2 additions & 1 deletion tool/tctl/common/lock_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/gravitational/teleport/lib/auth/authclient"
"github.com/gravitational/teleport/lib/service/servicecfg"
commonclient "github.com/gravitational/teleport/tool/tctl/common/client"
tctlcfg "github.com/gravitational/teleport/tool/tctl/common/config"
)

// LockCommand implements `tctl lock` group of commands.
Expand All @@ -43,7 +44,7 @@ type LockCommand struct {
}

// Initialize allows LockCommand to plug itself into the CLI parser.
func (c *LockCommand) Initialize(app *kingpin.Application, config *servicecfg.Config) {
func (c *LockCommand) Initialize(app *kingpin.Application, _ *tctlcfg.GlobalCLIFlags, config *servicecfg.Config) {
c.config = config

c.mainCmd = app.Command("lock", "Create a new lock.")
Expand Down
3 changes: 2 additions & 1 deletion tool/tctl/common/loginrule/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"github.com/gravitational/teleport/lib/services"
"github.com/gravitational/teleport/lib/utils"
commonclient "github.com/gravitational/teleport/tool/tctl/common/client"
tctlcfg "github.com/gravitational/teleport/tool/tctl/common/config"
)

type subcommand interface {
Expand All @@ -51,7 +52,7 @@ type Command struct {
}

// Initialize installs the base "login_rule" command and all subcommands.
func (t *Command) Initialize(app *kingpin.Application, cfg *servicecfg.Config) {
func (t *Command) Initialize(app *kingpin.Application, _ *tctlcfg.GlobalCLIFlags, cfg *servicecfg.Config) {
loginRuleCommand := app.Command("login_rule", "Test login rules")

t.subcommands = []subcommand{
Expand Down
Loading

0 comments on commit 8047189

Please sign in to comment.