From ea3940bbfe23f2ef72e7c3c744bce789a09b59d6 Mon Sep 17 00:00:00 2001 From: Paul Lorenz Date: Mon, 14 Nov 2022 11:26:51 -0500 Subject: [PATCH] Add `ziti tunnel` command --- CHANGELOG.md | 4 +++ ziti-tunnel/cmd/ziti-tunnel/main.go | 4 +-- ziti/cmd/cmd.go | 3 +++ .../subcmd => ziti/tunnel}/host.go | 25 +++++++++-------- .../subcmd => ziti/tunnel}/proxy.go | 25 +++++++++-------- .../subcmd => ziti/tunnel}/root.go | 27 ++++++++++++------- .../ziti-tunnel/subcmd => ziti/tunnel}/run.go | 23 ++++++++-------- .../subcmd => ziti/tunnel}/tproxy.go | 22 +++++++-------- .../subcmd => ziti/tunnel}/version.go | 22 +++++++-------- 9 files changed, 82 insertions(+), 73 deletions(-) rename {ziti-tunnel/cmd/ziti-tunnel/subcmd => ziti/tunnel}/host.go (70%) rename {ziti-tunnel/cmd/ziti-tunnel/subcmd => ziti/tunnel}/proxy.go (73%) rename {ziti-tunnel/cmd/ziti-tunnel/subcmd => ziti/tunnel}/root.go (92%) rename {ziti-tunnel/cmd/ziti-tunnel/subcmd => ziti/tunnel}/run.go (78%) rename {ziti-tunnel/cmd/ziti-tunnel/subcmd => ziti/tunnel}/tproxy.go (76%) rename {ziti-tunnel/cmd/ziti-tunnel/subcmd => ziti/tunnel}/version.go (73%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9a41247ba..315c83f40 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,10 @@ * Add `ziti controller` and `ziti router` commands * They should work exactly the same as `ziti-controller` and `ziti router` * The standalone binaries for `ziti-controller` and `ziti-router` are deprecated and will be removed in a future release +* Add hidden `ziti tunnel` command + * Should work exactly the same as `ziti-tunnel` + * Is hidden as `ziti-edge-tunnel` is the preferred tunnelling application + * The standalone binary `ziti-tunnel` is deprecated and will be removed in a future release # Release 0.26.11 diff --git a/ziti-tunnel/cmd/ziti-tunnel/main.go b/ziti-tunnel/cmd/ziti-tunnel/main.go index f70c3f392..9b8d8c7de 100644 --- a/ziti-tunnel/cmd/ziti-tunnel/main.go +++ b/ziti-tunnel/cmd/ziti-tunnel/main.go @@ -19,7 +19,7 @@ package main import ( "github.com/michaelquigley/pfxlog" "github.com/openziti/foundation/v2/debugz" - "github.com/openziti/ziti/ziti-tunnel/cmd/ziti-tunnel/subcmd" + "github.com/openziti/ziti/ziti/tunnel" "github.com/sirupsen/logrus" ) @@ -29,5 +29,5 @@ func init() { func main() { debugz.AddStackDumpHandler() - subcmd.Execute() + tunnel.Execute() } diff --git a/ziti/cmd/cmd.go b/ziti/cmd/cmd.go index 774f5d836..e4166eace 100644 --- a/ziti/cmd/cmd.go +++ b/ziti/cmd/cmd.go @@ -31,6 +31,7 @@ import ( "github.com/openziti/ziti/ziti/controller" "github.com/openziti/ziti/ziti/internal/log" "github.com/openziti/ziti/ziti/router" + "github.com/openziti/ziti/ziti/tunnel" "github.com/openziti/ziti/ziti/util" "io" "os" @@ -124,6 +125,7 @@ func NewCmdRoot(in io.Reader, out, err io.Writer, cmd *cobra.Command) *cobra.Com initCommands := NewCmdInit(out, err) createCommands := NewCmdCreate(out, err) controllerCmd := controller.NewRunCmd() + tunnelCmd := tunnel.NewTunnelCmd() routerCmd := router.NewRunCmd() agentCommands := agentcli.NewAgentCmd(p) pkiCommands := NewCmdPKI(out, err) @@ -159,6 +161,7 @@ func NewCmdRoot(in io.Reader, out, err io.Writer, cmd *cobra.Command) *cobra.Com agentCommands, controllerCmd, routerCmd, + tunnelCmd, pkiCommands, unwrapIdentityFileCommand, }, diff --git a/ziti-tunnel/cmd/ziti-tunnel/subcmd/host.go b/ziti/tunnel/host.go similarity index 70% rename from ziti-tunnel/cmd/ziti-tunnel/subcmd/host.go rename to ziti/tunnel/host.go index dea77cf03..9adf17130 100644 --- a/ziti-tunnel/cmd/ziti-tunnel/subcmd/host.go +++ b/ziti/tunnel/host.go @@ -14,27 +14,26 @@ limitations under the License. */ -package subcmd +package tunnel import ( "github.com/openziti/edge/tunnel/intercept/host" "github.com/spf13/cobra" ) -var runHostCmd = &cobra.Command{ - Use: "host", - Short: "Run in 'host' mode", - Long: "The 'host' mode will only host services", - Args: cobra.ExactArgs(0), - RunE: runHost, - PostRun: rootPostRun, +func NewHostCmd() *cobra.Command { + return &cobra.Command{ + Use: "host", + Short: "Run in 'host' mode", + Long: "The 'host' mode will only host services", + Args: cobra.ExactArgs(0), + RunE: runHost, + PostRun: rootPostRun, + } } -func init() { - root.AddCommand(runHostCmd) -} - -func runHost(_ *cobra.Command, args []string) error { +func runHost(cmd *cobra.Command, args []string) error { + root := cmd.Root() if !root.Flag(resolverCfgFlag).Changed { _ = root.PersistentFlags().Set(resolverCfgFlag, "") } diff --git a/ziti-tunnel/cmd/ziti-tunnel/subcmd/proxy.go b/ziti/tunnel/proxy.go similarity index 73% rename from ziti-tunnel/cmd/ziti-tunnel/subcmd/proxy.go rename to ziti/tunnel/proxy.go index f2f465c7d..33eb8a6ea 100644 --- a/ziti-tunnel/cmd/ziti-tunnel/subcmd/proxy.go +++ b/ziti/tunnel/proxy.go @@ -14,7 +14,7 @@ limitations under the License. */ -package subcmd +package tunnel import ( "github.com/openziti/edge/tunnel/intercept/proxy" @@ -25,20 +25,19 @@ import ( "strconv" ) -var runProxyCmd = &cobra.Command{ - Use: "proxy [sevice-name:port]", - Short: "Run in 'proxy' mode", - Long: "The 'proxy' intercept mode creates a network listener for each service that is intercepted.", - Args: cobra.MinimumNArgs(1), - RunE: runProxy, - PostRun: rootPostRun, +func NewProxyCmd() *cobra.Command { + return &cobra.Command{ + Use: "proxy [sevice-name:port]", + Short: "Run in 'proxy' mode", + Long: "The 'proxy' intercept mode creates a network listener for each service that is intercepted.", + Args: cobra.MinimumNArgs(1), + RunE: runProxy, + PostRun: rootPostRun, + } } -func init() { - root.AddCommand(runProxyCmd) -} - -func runProxy(_ *cobra.Command, args []string) error { +func runProxy(cmd *cobra.Command, args []string) error { + root := cmd.Root() // Fiddle with the poll rate and resolver settings if the user didn't wan't anything special. if !root.Flag(svcPollRateFlag).Changed { _ = root.PersistentFlags().Set(svcPollRateFlag, strconv.FormatUint(math.MaxUint32, 10)) diff --git a/ziti-tunnel/cmd/ziti-tunnel/subcmd/root.go b/ziti/tunnel/root.go similarity index 92% rename from ziti-tunnel/cmd/ziti-tunnel/subcmd/root.go rename to ziti/tunnel/root.go index f3c737526..0bef0a567 100644 --- a/ziti-tunnel/cmd/ziti-tunnel/subcmd/root.go +++ b/ziti/tunnel/root.go @@ -14,13 +14,12 @@ limitations under the License. */ -package subcmd +package tunnel import ( "github.com/openziti/ziti/ziti/cmd/common" "github.com/openziti/ziti/ziti/constants" "github.com/openziti/ziti/ziti/util" - "io/ioutil" "os" "path/filepath" "time" @@ -45,7 +44,14 @@ const ( dnsSvcIpRangeFlag = "dnsSvcIpRange" ) -func init() { +func NewTunnelCmd() *cobra.Command { + var root = &cobra.Command{ + Use: filepath.Base(os.Args[0]), + Short: "Ziti Tunnel", + PersistentPreRun: rootPreRun, + Hidden: true, + } + root.PersistentFlags().BoolP("verbose", "v", false, "Enable verbose mode") root.PersistentFlags().StringP("identity", "i", "", "Path to JSON file that contains an enrolled identity") root.PersistentFlags().String("identity-dir", "", "Path to directory file that contains one or more enrolled identities") @@ -58,12 +64,13 @@ func init() { p := common.NewOptionsProvider(os.Stdout, os.Stderr) root.AddCommand(enrollment.NewEnrollCommand(p)) -} + root.AddCommand(NewHostCmd()) + root.AddCommand(NewProxyCmd()) + root.AddCommand(NewRunCmd()) + root.AddCommand(NewTProxyCmd()) + root.AddCommand(NewVersionCmd()) -var root = &cobra.Command{ - Use: filepath.Base(os.Args[0]), - Short: "Ziti Tunnel", - PersistentPreRun: rootPreRun, + return root } var interceptor intercept.Interceptor @@ -72,7 +79,7 @@ var cliAgentEnabled bool var cliAgentAddr string func Execute() { - if err := root.Execute(); err != nil { + if err := NewTunnelCmd().Execute(); err != nil { pfxlog.Logger().Errorf("error: %s", err) os.Exit(1) } @@ -125,7 +132,7 @@ func rootPostRun(cmd *cobra.Command, _ []string) { } if idDir := cmd.Flag("identity-dir").Value.String(); idDir != "" { - files, err := ioutil.ReadDir(idDir) + files, err := os.ReadDir(idDir) if err != nil { log.Fatalf("failed to scan directory %s: %v", idDir, err) } diff --git a/ziti-tunnel/cmd/ziti-tunnel/subcmd/run.go b/ziti/tunnel/run.go similarity index 78% rename from ziti-tunnel/cmd/ziti-tunnel/subcmd/run.go rename to ziti/tunnel/run.go index cc833ec10..d776936b1 100644 --- a/ziti-tunnel/cmd/ziti-tunnel/subcmd/run.go +++ b/ziti/tunnel/run.go @@ -1,3 +1,4 @@ +//go:build linux // +build linux /* @@ -16,7 +17,7 @@ limitations under the License. */ -package subcmd +package tunnel import ( "github.com/michaelquigley/pfxlog" @@ -25,17 +26,15 @@ import ( "github.com/spf13/cobra" ) -var runCmd = &cobra.Command{ - Use: "run ", - Short: "Auto-select interceptor", - Long: "Provided for backwards compatibility with scripts that were coded around older ziti-tunnel versions.", - Args: cobra.MaximumNArgs(1), - Run: run, - PostRun: rootPostRun, -} - -func init() { - root.AddCommand(runCmd) +func NewRunCmd() *cobra.Command { + return &cobra.Command{ + Use: "run ", + Short: "Auto-select interceptor", + Long: "Provided for backwards compatibility with scripts that were coded around older ziti-tunnel versions.", + Args: cobra.MaximumNArgs(1), + Run: run, + PostRun: rootPostRun, + } } func run(cmd *cobra.Command, args []string) { diff --git a/ziti-tunnel/cmd/ziti-tunnel/subcmd/tproxy.go b/ziti/tunnel/tproxy.go similarity index 76% rename from ziti-tunnel/cmd/ziti-tunnel/subcmd/tproxy.go rename to ziti/tunnel/tproxy.go index 36e4a5f58..2b58102d7 100644 --- a/ziti-tunnel/cmd/ziti-tunnel/subcmd/tproxy.go +++ b/ziti/tunnel/tproxy.go @@ -1,3 +1,4 @@ +//go:build linux // +build linux /* @@ -16,7 +17,7 @@ limitations under the License. */ -package subcmd +package tunnel import ( "fmt" @@ -24,17 +25,16 @@ import ( "github.com/spf13/cobra" ) -var runTProxyCmd = &cobra.Command{ - Use: "tproxy", - Short: "Use the 'tproxy' interceptor", - Long: "The 'tproxy' interceptor captures packets by using the TPROXY iptables target.", - RunE: runTProxy, - PostRun: rootPostRun, -} - -func init() { +func NewTProxyCmd() *cobra.Command { + var runTProxyCmd = &cobra.Command{ + Use: "tproxy", + Short: "Use the 'tproxy' interceptor", + Long: "The 'tproxy' interceptor captures packets by using the TPROXY iptables target.", + RunE: runTProxy, + PostRun: rootPostRun, + } runTProxyCmd.PersistentFlags().String("lanIf", "", "if specified, INPUT rules for intercepted service addresses are assigned to this interface ") - root.AddCommand(runTProxyCmd) + return runTProxyCmd } func runTProxy(cmd *cobra.Command, args []string) error { diff --git a/ziti-tunnel/cmd/ziti-tunnel/subcmd/version.go b/ziti/tunnel/version.go similarity index 73% rename from ziti-tunnel/cmd/ziti-tunnel/subcmd/version.go rename to ziti/tunnel/version.go index b490914ee..947ac932a 100644 --- a/ziti-tunnel/cmd/ziti-tunnel/subcmd/version.go +++ b/ziti/tunnel/version.go @@ -14,22 +14,20 @@ limitations under the License. */ -package subcmd +package tunnel import ( - "github.com/openziti/ziti/common/version" "fmt" + "github.com/openziti/ziti/common/version" "github.com/spf13/cobra" ) -func init() { - root.AddCommand(versionCmd) -} - -var versionCmd = &cobra.Command{ - Use: "version", - Short: "Show component version", - Run: func(cmd *cobra.Command, args []string) { - fmt.Println(version.GetVersion()) - }, +func NewVersionCmd() *cobra.Command { + return &cobra.Command{ + Use: "version", + Short: "Show component version", + Run: func(cmd *cobra.Command, args []string) { + fmt.Println(version.GetVersion()) + }, + } }