AUTH-2686: Added error handling to tunnel subcommand

This commit is contained in:
Michael Borkenstein 2020-05-18 13:24:17 -05:00
parent df3ad2b223
commit 6a7418e1af
3 changed files with 30 additions and 25 deletions

View File

@ -10,6 +10,7 @@ import (
"time"
"github.com/cloudflare/cloudflared/carrier"
"github.com/cloudflare/cloudflared/cmd/cloudflared/cliutil"
"github.com/cloudflare/cloudflared/cmd/cloudflared/shell"
"github.com/cloudflare/cloudflared/cmd/cloudflared/token"
"github.com/cloudflare/cloudflared/sshgen"
@ -66,20 +67,6 @@ func Flags() []cli.Flag {
return []cli.Flag{} // no flags yet.
}
// Ensures exit with error code if actionFunc returns an error
func errorHandler(actionFunc cli.ActionFunc) cli.ActionFunc {
return func(ctx *cli.Context) error {
err := actionFunc(ctx)
if err != nil {
// os.Exits with error code if err is cli.ExitCoder or cli.MultiError
cli.HandleExitCoder(err)
err = cli.Exit(err.Error(), 1)
}
return err
}
}
// Commands returns all the Access related subcommands
func Commands() []*cli.Command {
return []*cli.Command{
@ -95,7 +82,7 @@ func Commands() []*cli.Command {
Subcommands: []*cli.Command{
{
Name: "login",
Action: errorHandler(login),
Action: cliutil.ErrorHandler(login),
Usage: "login <url of access application>",
Description: `The login subcommand initiates an authentication flow with your identity provider.
The subcommand will launch a browser. For headless systems, a url is provided.
@ -111,7 +98,7 @@ func Commands() []*cli.Command {
},
{
Name: "curl",
Action: errorHandler(curl),
Action: cliutil.ErrorHandler(curl),
Usage: "curl [--allow-request, -ar] <url> [<curl args>...]",
Description: `The curl subcommand wraps curl and automatically injects the JWT into a cf-access-token
header when using curl to reach an application behind Access.`,
@ -120,7 +107,7 @@ func Commands() []*cli.Command {
},
{
Name: "token",
Action: errorHandler(generateToken),
Action: cliutil.ErrorHandler(generateToken),
Usage: "token -app=<url of access application>",
ArgsUsage: "url of Access application",
Description: `The token subcommand produces a JWT which can be used to authenticate requests.`,
@ -132,7 +119,7 @@ func Commands() []*cli.Command {
},
{
Name: "tcp",
Action: errorHandler(ssh),
Action: cliutil.ErrorHandler(ssh),
Aliases: []string{"rdp", "ssh", "smb"},
Usage: "",
ArgsUsage: "",
@ -171,7 +158,7 @@ func Commands() []*cli.Command {
},
{
Name: "ssh-config",
Action: errorHandler(sshConfig),
Action: cliutil.ErrorHandler(sshConfig),
Usage: "",
Description: `Prints an example configuration ~/.ssh/config`,
Flags: []cli.Flag{
@ -187,7 +174,7 @@ func Commands() []*cli.Command {
},
{
Name: "ssh-gen",
Action: errorHandler(sshGen),
Action: cliutil.ErrorHandler(sshGen),
Usage: "",
Description: `Generates a short lived certificate for given hostname`,
Flags: []cli.Flag{

View File

@ -0,0 +1,17 @@
package cliutil
import "gopkg.in/urfave/cli.v2"
// Ensures exit with error code if actionFunc returns an error
func ErrorHandler(actionFunc cli.ActionFunc) cli.ActionFunc {
return func(ctx *cli.Context) error {
err := actionFunc(ctx)
if err != nil {
// os.Exits with error code if err is cli.ExitCoder or cli.MultiError
cli.HandleExitCoder(err)
err = cli.Exit(err.Error(), 1)
}
return err
}
}

View File

@ -17,6 +17,7 @@ import (
"github.com/cloudflare/cloudflared/awsuploader"
"github.com/cloudflare/cloudflared/cmd/cloudflared/buildinfo"
"github.com/cloudflare/cloudflared/cmd/cloudflared/cliutil"
"github.com/cloudflare/cloudflared/cmd/cloudflared/config"
"github.com/cloudflare/cloudflared/cmd/cloudflared/updater"
"github.com/cloudflare/cloudflared/connection"
@ -100,7 +101,7 @@ func Commands() []*cli.Command {
cmds := []*cli.Command{
{
Name: "login",
Action: login,
Action: cliutil.ErrorHandler(login),
Usage: "Generate a configuration file with your login details",
ArgsUsage: " ",
Flags: []cli.Flag{
@ -113,7 +114,7 @@ func Commands() []*cli.Command {
},
{
Name: "proxy-dns",
Action: tunneldns.Run,
Action: cliutil.ErrorHandler(tunneldns.Run),
Usage: "Run a DNS over HTTPS proxy server.",
Flags: []cli.Flag{
&cli.StringFlag{
@ -162,7 +163,7 @@ func Commands() []*cli.Command {
cmds = append(cmds, &cli.Command{
Name: "tunnel",
Action: tunnel,
Action: cliutil.ErrorHandler(tunnel),
Before: Before,
Category: "Tunnel",
Usage: "Make a locally-running web service accessible over the internet using Argo Tunnel.",
@ -662,13 +663,13 @@ func dbConnectCmd() *cli.Command {
}
// Override action to setup the Proxy, then if successful, start the tunnel daemon.
cmd.Action = func(c *cli.Context) error {
cmd.Action = cliutil.ErrorHandler(func(c *cli.Context) error {
err := dbconnect.CmdAction(c)
if err == nil {
err = tunnel(c)
}
return err
}
})
return cmd
}