2018-10-08 19:20:28 +00:00
|
|
|
package access
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-03-08 16:46:23 +00:00
|
|
|
"io"
|
2019-09-19 18:47:08 +00:00
|
|
|
"net/http"
|
2018-10-08 19:20:28 +00:00
|
|
|
"net/url"
|
|
|
|
"os"
|
2021-03-08 16:46:23 +00:00
|
|
|
"os/exec"
|
2019-02-01 22:56:33 +00:00
|
|
|
"strings"
|
2019-05-29 20:31:09 +00:00
|
|
|
"text/template"
|
2019-09-19 18:47:08 +00:00
|
|
|
"time"
|
2018-10-08 19:20:28 +00:00
|
|
|
|
2022-12-25 04:44:15 +00:00
|
|
|
"github.com/getsentry/sentry-go"
|
2021-03-23 14:30:43 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
"golang.org/x/net/idna"
|
|
|
|
|
2019-09-19 18:47:08 +00:00
|
|
|
"github.com/cloudflare/cloudflared/carrier"
|
2020-05-18 18:24:17 +00:00
|
|
|
"github.com/cloudflare/cloudflared/cmd/cloudflared/cliutil"
|
2020-04-29 20:51:32 +00:00
|
|
|
"github.com/cloudflare/cloudflared/logger"
|
2019-05-22 20:41:21 +00:00
|
|
|
"github.com/cloudflare/cloudflared/sshgen"
|
2021-03-08 16:46:23 +00:00
|
|
|
"github.com/cloudflare/cloudflared/token"
|
2019-05-22 20:41:21 +00:00
|
|
|
"github.com/cloudflare/cloudflared/validation"
|
2018-10-08 19:20:28 +00:00
|
|
|
)
|
|
|
|
|
2019-01-23 21:42:10 +00:00
|
|
|
const (
|
2024-08-15 22:04:08 +00:00
|
|
|
appURLFlag = "app"
|
2023-11-15 21:07:39 +00:00
|
|
|
loginQuietFlag = "quiet"
|
2020-11-15 01:49:44 +00:00
|
|
|
sshHostnameFlag = "hostname"
|
|
|
|
sshDestinationFlag = "destination"
|
|
|
|
sshURLFlag = "url"
|
|
|
|
sshHeaderFlag = "header"
|
|
|
|
sshTokenIDFlag = "service-token-id"
|
|
|
|
sshTokenSecretFlag = "service-token-secret"
|
|
|
|
sshGenCertFlag = "short-lived-cert"
|
2021-02-03 19:00:55 +00:00
|
|
|
sshConnectTo = "connect-to"
|
2023-06-29 17:29:15 +00:00
|
|
|
sshDebugStream = "debug-stream"
|
2020-11-15 01:49:44 +00:00
|
|
|
sshConfigTemplate = `
|
2019-06-03 15:35:07 +00:00
|
|
|
Add to your {{.Home}}/.ssh/config:
|
2019-05-22 20:41:21 +00:00
|
|
|
|
|
|
|
{{- if .ShortLivedCerts}}
|
2022-11-02 10:44:34 +00:00
|
|
|
Match host {{.Hostname}} exec "{{.Cloudflared}} access ssh-gen --hostname %h"
|
2019-06-03 15:35:07 +00:00
|
|
|
ProxyCommand {{.Cloudflared}} access ssh --hostname %h
|
2022-11-02 10:44:34 +00:00
|
|
|
IdentityFile ~/.cloudflared/%h-cf_key
|
|
|
|
CertificateFile ~/.cloudflared/%h-cf_key-cert.pub
|
2019-05-22 20:41:21 +00:00
|
|
|
{{- else}}
|
2022-11-02 10:44:34 +00:00
|
|
|
Host {{.Hostname}}
|
2019-06-03 15:35:07 +00:00
|
|
|
ProxyCommand {{.Cloudflared}} access ssh --hostname %h
|
2019-05-22 20:41:21 +00:00
|
|
|
{{end}}
|
|
|
|
`
|
2019-01-23 21:42:10 +00:00
|
|
|
)
|
|
|
|
|
2018-10-08 19:20:28 +00:00
|
|
|
const sentryDSN = "https://56a9c9fa5c364ab28f34b14f35ea0f1b@sentry.io/189878"
|
|
|
|
|
2018-10-19 20:44:35 +00:00
|
|
|
var (
|
2021-02-03 19:00:55 +00:00
|
|
|
shutdownC chan struct{}
|
2022-06-24 18:51:53 +00:00
|
|
|
userAgent = "DEV"
|
2018-10-19 20:44:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Init will initialize and store vars from the main program
|
2022-06-24 18:51:53 +00:00
|
|
|
func Init(shutdown chan struct{}, version string) {
|
2021-01-25 21:51:58 +00:00
|
|
|
shutdownC = shutdown
|
2022-06-24 18:51:53 +00:00
|
|
|
userAgent = fmt.Sprintf("cloudflared/%s", version)
|
2018-10-19 20:44:35 +00:00
|
|
|
}
|
|
|
|
|
2018-10-08 19:20:28 +00:00
|
|
|
// Flags return the global flags for Access related commands (hopefully none)
|
|
|
|
func Flags() []cli.Flag {
|
|
|
|
return []cli.Flag{} // no flags yet.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Commands returns all the Access related subcommands
|
|
|
|
func Commands() []*cli.Command {
|
|
|
|
return []*cli.Command{
|
|
|
|
{
|
|
|
|
Name: "access",
|
2020-05-13 18:53:31 +00:00
|
|
|
Aliases: []string{"forward"},
|
|
|
|
Category: "Access",
|
2018-10-08 19:20:28 +00:00
|
|
|
Usage: "access <subcommand>",
|
2021-03-12 14:29:07 +00:00
|
|
|
Description: `Cloudflare Access protects internal resources by securing, authenticating and monitoring access
|
|
|
|
per-user and by application. With Cloudflare Access, only authenticated users with the required permissions are
|
|
|
|
able to reach sensitive resources. The commands provided here allow you to interact with Access protected
|
2020-05-13 18:53:31 +00:00
|
|
|
applications from the command line.`,
|
2018-10-08 19:20:28 +00:00
|
|
|
Subcommands: []*cli.Command{
|
|
|
|
{
|
2024-08-15 22:04:08 +00:00
|
|
|
Name: "login",
|
|
|
|
Action: cliutil.Action(login),
|
|
|
|
Usage: "login <url of access application>",
|
|
|
|
ArgsUsage: "url of Access application",
|
2018-10-08 19:20:28 +00:00
|
|
|
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.
|
|
|
|
Once authenticated with your identity provider, the login command will generate a JSON Web Token (JWT)
|
2021-03-12 14:29:07 +00:00
|
|
|
scoped to your identity, the application you intend to reach, and valid for a session duration set by your
|
2018-10-08 19:20:28 +00:00
|
|
|
administrator. cloudflared stores the token in local storage.`,
|
2023-11-15 21:07:39 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: loginQuietFlag,
|
|
|
|
Aliases: []string{"q"},
|
|
|
|
Usage: "do not print the jwt to the command line",
|
|
|
|
},
|
2024-08-15 22:04:08 +00:00
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "no-verbose",
|
|
|
|
Usage: "print only the jwt to stdout",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: appURLFlag,
|
|
|
|
},
|
2023-11-15 21:07:39 +00:00
|
|
|
},
|
2018-10-08 19:20:28 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "curl",
|
2021-02-05 01:44:03 +00:00
|
|
|
Action: cliutil.Action(curl),
|
2019-01-24 21:37:00 +00:00
|
|
|
Usage: "curl [--allow-request, -ar] <url> [<curl args>...]",
|
2018-10-08 19:20:28 +00:00
|
|
|
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.`,
|
|
|
|
ArgsUsage: "allow-request will allow the curl request to continue even if the jwt is not present.",
|
|
|
|
SkipFlagParsing: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "token",
|
2021-02-05 01:44:03 +00:00
|
|
|
Action: cliutil.Action(generateToken),
|
2024-08-15 22:04:08 +00:00
|
|
|
Usage: "token <url of access application>",
|
2018-10-08 19:20:28 +00:00
|
|
|
ArgsUsage: "url of Access application",
|
|
|
|
Description: `The token subcommand produces a JWT which can be used to authenticate requests.`,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
2024-08-15 22:04:08 +00:00
|
|
|
Name: appURLFlag,
|
2018-10-08 19:20:28 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-10-19 20:44:35 +00:00
|
|
|
{
|
2020-05-14 15:26:09 +00:00
|
|
|
Name: "tcp",
|
2021-02-05 01:44:03 +00:00
|
|
|
Action: cliutil.Action(ssh),
|
2020-05-14 15:26:09 +00:00
|
|
|
Aliases: []string{"rdp", "ssh", "smb"},
|
2018-10-19 20:44:35 +00:00
|
|
|
Usage: "",
|
|
|
|
ArgsUsage: "",
|
2020-05-14 15:26:09 +00:00
|
|
|
Description: `The tcp subcommand sends data over a proxy to the Cloudflare edge.`,
|
2018-10-19 20:44:35 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
2020-05-13 18:53:31 +00:00
|
|
|
Name: sshHostnameFlag,
|
|
|
|
Aliases: []string{"tunnel-host", "T"},
|
|
|
|
Usage: "specify the hostname of your application.",
|
2023-12-16 00:02:36 +00:00
|
|
|
EnvVars: []string{"TUNNEL_SERVICE_HOSTNAME"},
|
2018-10-19 20:44:35 +00:00
|
|
|
},
|
2019-10-02 20:56:28 +00:00
|
|
|
&cli.StringFlag{
|
2024-04-02 16:14:35 +00:00
|
|
|
Name: sshDestinationFlag,
|
|
|
|
Usage: "specify the destination address of your SSH server.",
|
2023-12-16 00:02:36 +00:00
|
|
|
EnvVars: []string{"TUNNEL_SERVICE_DESTINATION"},
|
2019-10-02 20:56:28 +00:00
|
|
|
},
|
2018-10-19 20:44:35 +00:00
|
|
|
&cli.StringFlag{
|
2020-05-13 18:53:31 +00:00
|
|
|
Name: sshURLFlag,
|
|
|
|
Aliases: []string{"listener", "L"},
|
|
|
|
Usage: "specify the host:port to forward data to Cloudflare edge.",
|
2023-12-16 00:02:36 +00:00
|
|
|
EnvVars: []string{"TUNNEL_SERVICE_URL"},
|
2018-10-19 20:44:35 +00:00
|
|
|
},
|
2019-02-07 16:56:33 +00:00
|
|
|
&cli.StringSliceFlag{
|
2019-01-23 21:42:10 +00:00
|
|
|
Name: sshHeaderFlag,
|
2019-02-07 16:56:33 +00:00
|
|
|
Aliases: []string{"H"},
|
2019-01-23 21:42:10 +00:00
|
|
|
Usage: "specify additional headers you wish to send.",
|
2019-03-06 19:09:13 +00:00
|
|
|
},
|
2020-06-08 22:01:48 +00:00
|
|
|
&cli.StringFlag{
|
2019-01-23 21:42:10 +00:00
|
|
|
Name: sshTokenIDFlag,
|
2019-03-06 19:09:13 +00:00
|
|
|
Aliases: []string{"id"},
|
2019-01-23 21:42:10 +00:00
|
|
|
Usage: "specify an Access service token ID you wish to use.",
|
2021-05-31 18:38:42 +00:00
|
|
|
EnvVars: []string{"TUNNEL_SERVICE_TOKEN_ID"},
|
2019-03-06 19:09:13 +00:00
|
|
|
},
|
2020-06-08 22:01:48 +00:00
|
|
|
&cli.StringFlag{
|
2019-01-23 21:42:10 +00:00
|
|
|
Name: sshTokenSecretFlag,
|
2019-03-06 19:09:13 +00:00
|
|
|
Aliases: []string{"secret"},
|
2019-01-23 21:42:10 +00:00
|
|
|
Usage: "specify an Access service token secret you wish to use.",
|
2021-05-31 18:38:42 +00:00
|
|
|
EnvVars: []string{"TUNNEL_SERVICE_TOKEN_SECRET"},
|
2019-01-23 21:42:10 +00:00
|
|
|
},
|
2020-06-05 15:18:40 +00:00
|
|
|
&cli.StringFlag{
|
2023-06-29 17:29:15 +00:00
|
|
|
Name: logger.LogFileFlag,
|
|
|
|
Usage: "Save application log to this file for reporting issues.",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: logger.LogSSHDirectoryFlag,
|
|
|
|
Usage: "Save application log to this directory for reporting issues.",
|
2020-06-05 15:18:40 +00:00
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
2020-11-15 01:49:44 +00:00
|
|
|
Name: logger.LogSSHLevelFlag,
|
2020-06-05 15:18:40 +00:00
|
|
|
Aliases: []string{"loglevel"}, //added to match the tunnel side
|
2021-03-12 14:29:07 +00:00
|
|
|
Usage: "Application logging level {debug, info, warn, error, fatal}. ",
|
2020-06-05 15:18:40 +00:00
|
|
|
},
|
2021-02-03 19:00:55 +00:00
|
|
|
&cli.StringFlag{
|
2021-03-10 21:52:35 +00:00
|
|
|
Name: sshConnectTo,
|
2021-02-03 19:00:55 +00:00
|
|
|
Hidden: true,
|
2021-03-10 21:52:35 +00:00
|
|
|
Usage: "Connect to alternate location for testing, value is host, host:port, or sni:port:host",
|
2021-02-03 19:00:55 +00:00
|
|
|
},
|
2023-06-29 17:29:15 +00:00
|
|
|
&cli.Uint64Flag{
|
|
|
|
Name: sshDebugStream,
|
|
|
|
Hidden: true,
|
|
|
|
Usage: "Writes up-to the max provided stream payloads to the logger as debug statements.",
|
|
|
|
},
|
2019-05-22 20:41:21 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "ssh-config",
|
2021-02-05 01:44:03 +00:00
|
|
|
Action: cliutil.Action(sshConfig),
|
2019-05-22 20:41:21 +00:00
|
|
|
Usage: "",
|
|
|
|
Description: `Prints an example configuration ~/.ssh/config`,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: sshHostnameFlag,
|
|
|
|
Usage: "specify the hostname of your application.",
|
|
|
|
},
|
2019-01-23 21:42:10 +00:00
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: sshGenCertFlag,
|
|
|
|
Usage: "specify if you wish to generate short lived certs.",
|
2019-02-07 16:56:33 +00:00
|
|
|
},
|
2018-10-19 20:44:35 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2019-05-22 20:41:21 +00:00
|
|
|
Name: "ssh-gen",
|
2021-02-05 01:44:03 +00:00
|
|
|
Action: cliutil.Action(sshGen),
|
2019-05-22 20:41:21 +00:00
|
|
|
Usage: "",
|
|
|
|
Description: `Generates a short lived certificate for given hostname`,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: sshHostnameFlag,
|
|
|
|
Usage: "specify the hostname of your application.",
|
|
|
|
},
|
|
|
|
},
|
2018-10-19 20:44:35 +00:00
|
|
|
},
|
2018-10-08 19:20:28 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// login pops up the browser window to do the actual login and JWT generation
|
|
|
|
func login(c *cli.Context) error {
|
2022-12-25 04:44:15 +00:00
|
|
|
err := sentry.Init(sentry.ClientOptions{
|
|
|
|
Dsn: sentryDSN,
|
|
|
|
Release: c.App.Version,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2020-05-07 19:58:33 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-04-29 20:51:32 +00:00
|
|
|
|
2020-11-25 06:55:13 +00:00
|
|
|
log := logger.CreateLoggerFromContext(c, logger.EnableTerminalLog)
|
2020-04-29 20:51:32 +00:00
|
|
|
|
2024-08-15 22:04:08 +00:00
|
|
|
appURL, err := getAppURLFromArgs(c)
|
|
|
|
if err != nil {
|
2020-12-28 18:10:01 +00:00
|
|
|
log.Error().Msg("Please provide the url of the Access application")
|
2018-10-08 19:20:28 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-03-10 21:52:35 +00:00
|
|
|
|
|
|
|
appInfo, err := token.GetAppInfo(appURL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := verifyTokenAtEdge(appURL, appInfo, c, log); err != nil {
|
2020-12-28 18:10:01 +00:00
|
|
|
log.Err(err).Msg("Could not verify token")
|
2019-09-19 18:47:08 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-10 21:52:35 +00:00
|
|
|
cfdToken, err := token.GetAppTokenIfExists(appInfo)
|
2020-05-07 19:58:33 +00:00
|
|
|
if err != nil {
|
2019-09-19 18:47:08 +00:00
|
|
|
fmt.Fprintln(os.Stderr, "Unable to find token for provided application.")
|
2018-10-08 19:20:28 +00:00
|
|
|
return err
|
2020-05-07 19:58:33 +00:00
|
|
|
} else if cfdToken == "" {
|
|
|
|
fmt.Fprintln(os.Stderr, "token for provided application was empty.")
|
|
|
|
return errors.New("empty application token")
|
2018-10-08 19:20:28 +00:00
|
|
|
}
|
2023-11-15 21:07:39 +00:00
|
|
|
|
|
|
|
if c.Bool(loginQuietFlag) {
|
|
|
|
return nil
|
|
|
|
}
|
2024-08-15 22:04:08 +00:00
|
|
|
|
|
|
|
// Chatty by default for backward compat. The new --app flag
|
|
|
|
// is an implicit opt-out of the backwards-compatible chatty output.
|
|
|
|
if c.Bool("no-verbose") || c.IsSet(appURLFlag) {
|
|
|
|
fmt.Fprint(os.Stdout, cfdToken)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(os.Stdout, "Successfully fetched your token:\n\n%s\n\n", cfdToken)
|
|
|
|
}
|
2018-10-08 19:20:28 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// curl provides a wrapper around curl, passing Access JWT along in request
|
|
|
|
func curl(c *cli.Context) error {
|
2022-12-25 04:44:15 +00:00
|
|
|
err := sentry.Init(sentry.ClientOptions{
|
|
|
|
Dsn: sentryDSN,
|
|
|
|
Release: c.App.Version,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2020-05-07 19:58:33 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-11-25 06:55:13 +00:00
|
|
|
log := logger.CreateLoggerFromContext(c, logger.EnableTerminalLog)
|
2020-04-29 20:51:32 +00:00
|
|
|
|
2018-10-08 19:20:28 +00:00
|
|
|
args := c.Args()
|
|
|
|
if args.Len() < 1 {
|
2020-11-25 06:55:13 +00:00
|
|
|
log.Error().Msg("Please provide the access app and command you wish to run.")
|
2018-10-08 19:20:28 +00:00
|
|
|
return errors.New("incorrect args")
|
|
|
|
}
|
|
|
|
|
2019-01-24 21:37:00 +00:00
|
|
|
cmdArgs, allowRequest := parseAllowRequest(args.Slice())
|
2020-11-25 06:55:13 +00:00
|
|
|
appURL, err := getAppURL(cmdArgs, log)
|
2018-10-08 19:20:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-10 21:52:35 +00:00
|
|
|
appInfo, err := token.GetAppInfo(appURL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-17 15:42:50 +00:00
|
|
|
|
|
|
|
// Verify that the existing token is still good; if not fetch a new one
|
|
|
|
if err := verifyTokenAtEdge(appURL, appInfo, c, log); err != nil {
|
|
|
|
log.Err(err).Msg("Could not verify token")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-10 21:52:35 +00:00
|
|
|
tok, err := token.GetAppTokenIfExists(appInfo)
|
2018-10-19 20:44:35 +00:00
|
|
|
if err != nil || tok == "" {
|
2018-10-08 19:20:28 +00:00
|
|
|
if allowRequest {
|
2020-11-25 06:55:13 +00:00
|
|
|
log.Info().Msg("You don't have an Access token set. Please run access token <access application> to fetch one.")
|
2021-03-08 16:46:23 +00:00
|
|
|
return run("curl", cmdArgs...)
|
2018-10-08 19:20:28 +00:00
|
|
|
}
|
2021-03-10 21:52:35 +00:00
|
|
|
tok, err = token.FetchToken(appURL, appInfo, log)
|
2018-10-08 19:20:28 +00:00
|
|
|
if err != nil {
|
2020-12-28 18:10:01 +00:00
|
|
|
log.Err(err).Msg("Failed to refresh token")
|
2018-10-08 19:20:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cmdArgs = append(cmdArgs, "-H")
|
2021-03-26 04:04:56 +00:00
|
|
|
cmdArgs = append(cmdArgs, fmt.Sprintf("%s: %s", carrier.CFAccessTokenHeader, tok))
|
2021-03-08 16:46:23 +00:00
|
|
|
return run("curl", cmdArgs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// run kicks off a shell task and pipe the results to the respective std pipes
|
|
|
|
func run(cmd string, args ...string) error {
|
|
|
|
c := exec.Command(cmd, args...)
|
2022-07-20 14:46:41 +00:00
|
|
|
c.Stdin = os.Stdin
|
2021-03-08 16:46:23 +00:00
|
|
|
stderr, err := c.StderrPipe()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
io.Copy(os.Stderr, stderr)
|
|
|
|
}()
|
|
|
|
|
|
|
|
stdout, err := c.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
io.Copy(os.Stdout, stdout)
|
|
|
|
}()
|
|
|
|
return c.Run()
|
2018-10-08 19:20:28 +00:00
|
|
|
}
|
|
|
|
|
2024-08-15 22:04:08 +00:00
|
|
|
func getAppURLFromArgs(c *cli.Context) (*url.URL, error) {
|
|
|
|
var appURLStr string
|
|
|
|
args := c.Args()
|
|
|
|
if args.Len() < 1 {
|
|
|
|
appURLStr = c.String(appURLFlag)
|
|
|
|
} else {
|
|
|
|
appURLStr = args.First()
|
|
|
|
}
|
|
|
|
return parseURL(appURLStr)
|
|
|
|
}
|
|
|
|
|
2018-10-08 19:20:28 +00:00
|
|
|
// token dumps provided token to stdout
|
2018-10-19 20:44:35 +00:00
|
|
|
func generateToken(c *cli.Context) error {
|
2022-12-25 04:44:15 +00:00
|
|
|
err := sentry.Init(sentry.ClientOptions{
|
|
|
|
Dsn: sentryDSN,
|
|
|
|
Release: c.App.Version,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2020-05-07 19:58:33 +00:00
|
|
|
return err
|
|
|
|
}
|
2024-08-15 22:04:08 +00:00
|
|
|
appURL, err := getAppURLFromArgs(c)
|
|
|
|
if err != nil {
|
2018-10-08 19:20:28 +00:00
|
|
|
fmt.Fprintln(os.Stderr, "Please provide a url.")
|
|
|
|
return err
|
|
|
|
}
|
2021-03-10 21:52:35 +00:00
|
|
|
|
|
|
|
appInfo, err := token.GetAppInfo(appURL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tok, err := token.GetAppTokenIfExists(appInfo)
|
2018-10-19 20:44:35 +00:00
|
|
|
if err != nil || tok == "" {
|
2020-11-18 14:15:54 +00:00
|
|
|
fmt.Fprintln(os.Stderr, "Unable to find token for provided application. Please run login command to generate token.")
|
2018-10-08 19:20:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-19 20:44:35 +00:00
|
|
|
if _, err := fmt.Fprint(os.Stdout, tok); err != nil {
|
2018-10-08 19:20:28 +00:00
|
|
|
fmt.Fprintln(os.Stderr, "Failed to write token to stdout.")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-10-19 20:44:35 +00:00
|
|
|
// sshConfig prints an example SSH config to stdout
|
|
|
|
func sshConfig(c *cli.Context) error {
|
2019-05-22 20:41:21 +00:00
|
|
|
genCertBool := c.Bool(sshGenCertFlag)
|
|
|
|
hostname := c.String(sshHostnameFlag)
|
|
|
|
if hostname == "" {
|
|
|
|
hostname = "[your hostname]"
|
|
|
|
}
|
|
|
|
|
|
|
|
type config struct {
|
|
|
|
Home string
|
|
|
|
ShortLivedCerts bool
|
|
|
|
Hostname string
|
|
|
|
Cloudflared string
|
|
|
|
}
|
|
|
|
|
|
|
|
t := template.Must(template.New("sshConfig").Parse(sshConfigTemplate))
|
|
|
|
return t.Execute(os.Stdout, config{Home: os.Getenv("HOME"), ShortLivedCerts: genCertBool, Hostname: hostname, Cloudflared: cloudflaredPath()})
|
|
|
|
}
|
|
|
|
|
|
|
|
// sshGen generates a short lived certificate for provided hostname
|
|
|
|
func sshGen(c *cli.Context) error {
|
2020-11-25 06:55:13 +00:00
|
|
|
log := logger.CreateLoggerFromContext(c, logger.EnableTerminalLog)
|
2020-04-29 20:51:32 +00:00
|
|
|
|
2019-05-22 20:41:21 +00:00
|
|
|
// get the hostname from the cmdline and error out if its not provided
|
|
|
|
rawHostName := c.String(sshHostnameFlag)
|
|
|
|
hostname, err := validation.ValidateHostname(rawHostName)
|
|
|
|
if err != nil || rawHostName == "" {
|
|
|
|
return cli.ShowCommandHelp(c, "ssh-gen")
|
|
|
|
}
|
|
|
|
|
2023-07-25 16:33:11 +00:00
|
|
|
originURL, err := parseURL(hostname)
|
2019-05-22 20:41:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// this fetchToken function mutates the appURL param. We should refactor that
|
|
|
|
fetchTokenURL := &url.URL{}
|
|
|
|
*fetchTokenURL = *originURL
|
2021-03-10 21:52:35 +00:00
|
|
|
|
|
|
|
appInfo, err := token.GetAppInfo(fetchTokenURL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cfdToken, err := token.FetchTokenWithRedirect(fetchTokenURL, appInfo, log)
|
2019-05-22 20:41:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-24 21:31:02 +00:00
|
|
|
if err := sshgen.GenerateShortLivedCertificate(originURL, cfdToken); err != nil {
|
2019-05-22 20:41:21 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-02-01 22:56:33 +00:00
|
|
|
return nil
|
2018-10-19 20:44:35 +00:00
|
|
|
}
|
|
|
|
|
2021-03-10 21:52:35 +00:00
|
|
|
// getAppURL will pull the request URL needed for fetching a user's Access token
|
2020-11-25 06:55:13 +00:00
|
|
|
func getAppURL(cmdArgs []string, log *zerolog.Logger) (*url.URL, error) {
|
2019-01-24 21:37:00 +00:00
|
|
|
if len(cmdArgs) < 1 {
|
2020-11-25 06:55:13 +00:00
|
|
|
log.Error().Msg("Please provide a valid URL as the first argument to curl.")
|
2019-01-24 21:37:00 +00:00
|
|
|
return nil, errors.New("not a valid url")
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := processURL(cmdArgs[0])
|
|
|
|
if err != nil {
|
2020-11-25 06:55:13 +00:00
|
|
|
log.Error().Msg("Please provide a valid URL as the first argument to curl.")
|
2019-01-24 21:37:00 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return u, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseAllowRequest will parse cmdArgs and return a copy of the args and result
|
|
|
|
// of the allow request was present
|
|
|
|
func parseAllowRequest(cmdArgs []string) ([]string, bool) {
|
|
|
|
if len(cmdArgs) > 1 {
|
|
|
|
if cmdArgs[0] == "--allow-request" || cmdArgs[0] == "-ar" {
|
|
|
|
return cmdArgs[1:], true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cmdArgs, false
|
|
|
|
}
|
|
|
|
|
2018-10-08 19:20:28 +00:00
|
|
|
// processURL will preprocess the string (parse to a url, convert to punycode, etc).
|
|
|
|
func processURL(s string) (*url.URL, error) {
|
|
|
|
u, err := url.ParseRequestURI(s)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-01-24 21:37:00 +00:00
|
|
|
|
|
|
|
if u.Host == "" {
|
|
|
|
return nil, errors.New("not a valid host")
|
|
|
|
}
|
|
|
|
|
2018-10-08 19:20:28 +00:00
|
|
|
host, err := idna.ToASCII(u.Hostname())
|
|
|
|
if err != nil { // we fail to convert to punycode, just return the url we parsed.
|
|
|
|
return u, nil
|
|
|
|
}
|
|
|
|
if u.Port() != "" {
|
|
|
|
u.Host = fmt.Sprintf("%s:%s", host, u.Port())
|
|
|
|
} else {
|
|
|
|
u.Host = host
|
|
|
|
}
|
|
|
|
|
|
|
|
return u, nil
|
|
|
|
}
|
2019-02-01 22:56:33 +00:00
|
|
|
|
|
|
|
// cloudflaredPath pulls the full path of cloudflared on disk
|
|
|
|
func cloudflaredPath() string {
|
2023-12-14 16:32:31 +00:00
|
|
|
path, err := os.Executable()
|
|
|
|
if err == nil && isFileThere(path) {
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
2019-02-01 22:56:33 +00:00
|
|
|
for _, p := range strings.Split(os.Getenv("PATH"), ":") {
|
|
|
|
path := fmt.Sprintf("%s/%s", p, "cloudflared")
|
|
|
|
if isFileThere(path) {
|
|
|
|
return path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "cloudflared"
|
|
|
|
}
|
|
|
|
|
|
|
|
// isFileThere will check for the presence of candidate path
|
|
|
|
func isFileThere(candidate string) bool {
|
|
|
|
fi, err := os.Stat(candidate)
|
|
|
|
if err != nil || fi.IsDir() || !fi.Mode().IsRegular() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2019-09-19 18:47:08 +00:00
|
|
|
|
|
|
|
// verifyTokenAtEdge checks for a token on disk, or generates a new one.
|
|
|
|
// Then makes a request to to the origin with the token to ensure it is valid.
|
|
|
|
// Returns nil if token is valid.
|
2021-03-10 21:52:35 +00:00
|
|
|
func verifyTokenAtEdge(appUrl *url.URL, appInfo *token.AppInfo, c *cli.Context, log *zerolog.Logger) error {
|
2023-07-25 16:33:11 +00:00
|
|
|
headers := parseRequestHeaders(c.StringSlice(sshHeaderFlag))
|
2019-09-19 18:47:08 +00:00
|
|
|
if c.IsSet(sshTokenIDFlag) {
|
2021-03-26 04:04:56 +00:00
|
|
|
headers.Add(cfAccessClientIDHeader, c.String(sshTokenIDFlag))
|
2019-09-19 18:47:08 +00:00
|
|
|
}
|
|
|
|
if c.IsSet(sshTokenSecretFlag) {
|
2021-03-26 04:04:56 +00:00
|
|
|
headers.Add(cfAccessClientSecretHeader, c.String(sshTokenSecretFlag))
|
2019-09-19 18:47:08 +00:00
|
|
|
}
|
2021-03-10 21:52:35 +00:00
|
|
|
options := &carrier.StartOptions{AppInfo: appInfo, OriginURL: appUrl.String(), Headers: headers}
|
2019-09-19 18:47:08 +00:00
|
|
|
|
2020-11-25 06:55:13 +00:00
|
|
|
if valid, err := isTokenValid(options, log); err != nil {
|
2019-09-19 18:47:08 +00:00
|
|
|
return err
|
|
|
|
} else if valid {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-10 21:52:35 +00:00
|
|
|
if err := token.RemoveTokenIfExists(appInfo); err != nil {
|
2019-09-19 18:47:08 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-11-25 06:55:13 +00:00
|
|
|
if valid, err := isTokenValid(options, log); err != nil {
|
2019-09-19 18:47:08 +00:00
|
|
|
return err
|
|
|
|
} else if !valid {
|
|
|
|
return errors.New("failed to verify token")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// isTokenValid makes a request to the origin and returns true if the response was not a 302.
|
2020-11-25 06:55:13 +00:00
|
|
|
func isTokenValid(options *carrier.StartOptions, log *zerolog.Logger) (bool, error) {
|
|
|
|
req, err := carrier.BuildAccessRequest(options, log)
|
2019-09-19 18:47:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, errors.Wrap(err, "Could not create access request")
|
|
|
|
}
|
2022-06-24 18:51:53 +00:00
|
|
|
req.Header.Set("User-Agent", userAgent)
|
2023-06-20 10:48:38 +00:00
|
|
|
|
|
|
|
query := req.URL.Query()
|
|
|
|
query.Set("cloudflared_token_check", "true")
|
|
|
|
req.URL.RawQuery = query.Encode()
|
|
|
|
|
2019-09-19 18:47:08 +00:00
|
|
|
// Do not follow redirects
|
|
|
|
client := &http.Client{
|
|
|
|
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
|
|
|
return http.ErrUseLastResponse
|
|
|
|
},
|
|
|
|
Timeout: time.Second * 5,
|
|
|
|
}
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
// A redirect to login means the token was invalid.
|
|
|
|
return !carrier.IsAccessResponse(resp), nil
|
|
|
|
}
|