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
|
|
|
|
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-05-04 20:15:17 +00:00
|
|
|
"github.com/cloudflare/cloudflared/h2mux"
|
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
|
|
|
|
2020-05-07 19:58:33 +00:00
|
|
|
"github.com/getsentry/raven-go"
|
2020-11-25 06:55:13 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/rs/zerolog"
|
2020-08-05 10:49:53 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2020-11-25 06:55:13 +00:00
|
|
|
"golang.org/x/net/idna"
|
2018-10-08 19:20:28 +00:00
|
|
|
)
|
|
|
|
|
2019-01-23 21:42:10 +00:00
|
|
|
const (
|
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"
|
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
|
|
|
|
|
|
|
Host {{.Hostname}}
|
|
|
|
{{- if .ShortLivedCerts}}
|
2019-06-03 15:35:07 +00:00
|
|
|
ProxyCommand bash -c '{{.Cloudflared}} access ssh-gen --hostname %h; ssh -tt %r@cfpipe-{{.Hostname}} >&2 <&1'
|
2019-05-22 20:41:21 +00:00
|
|
|
|
|
|
|
Host cfpipe-{{.Hostname}}
|
2019-06-03 15:35:07 +00:00
|
|
|
HostName {{.Hostname}}
|
|
|
|
ProxyCommand {{.Cloudflared}} access ssh --hostname %h
|
|
|
|
IdentityFile ~/.cloudflared/{{.Hostname}}-cf_key
|
|
|
|
CertificateFile ~/.cloudflared/{{.Hostname}}-cf_key-cert.pub
|
2019-05-22 20:41:21 +00:00
|
|
|
{{- else}}
|
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{}
|
2018-10-19 20:44:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Init will initialize and store vars from the main program
|
2021-01-25 21:51:58 +00:00
|
|
|
func Init(shutdown chan struct{}) {
|
|
|
|
shutdownC = shutdown
|
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>",
|
2020-05-13 18:53:31 +00:00
|
|
|
Description: `Cloudflare Access protects internal resources by securing, authenticating and monitoring access
|
2018-10-08 19:20:28 +00:00
|
|
|
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{
|
|
|
|
{
|
|
|
|
Name: "login",
|
2020-05-18 18:24:17 +00:00
|
|
|
Action: cliutil.ErrorHandler(login),
|
2018-10-08 19:20:28 +00:00
|
|
|
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.
|
|
|
|
Once authenticated with your identity provider, the login command will generate a JSON Web Token (JWT)
|
|
|
|
scoped to your identity, the application you intend to reach, and valid for a session duration set by your
|
|
|
|
administrator. cloudflared stores the token in local storage.`,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "url",
|
|
|
|
Hidden: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "curl",
|
2020-05-18 18:24:17 +00:00
|
|
|
Action: cliutil.ErrorHandler(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",
|
2020-05-18 18:24:17 +00:00
|
|
|
Action: cliutil.ErrorHandler(generateToken),
|
2018-10-08 19:20:28 +00:00
|
|
|
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.`,
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "app",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2018-10-19 20:44:35 +00:00
|
|
|
{
|
2020-05-14 15:26:09 +00:00
|
|
|
Name: "tcp",
|
2020-05-18 18:24:17 +00:00
|
|
|
Action: cliutil.ErrorHandler(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.",
|
2018-10-19 20:44:35 +00:00
|
|
|
},
|
2019-10-02 20:56:28 +00:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: sshDestinationFlag,
|
|
|
|
Usage: "specify the destination address of your SSH server.",
|
|
|
|
},
|
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.",
|
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.",
|
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.",
|
|
|
|
},
|
2020-06-05 15:18:40 +00:00
|
|
|
&cli.StringFlag{
|
2020-11-15 01:49:44 +00:00
|
|
|
Name: logger.LogSSHDirectoryFlag,
|
2020-06-05 15:18:40 +00:00
|
|
|
Aliases: []string{"logfile"}, //added to match the tunnel side
|
|
|
|
Usage: "Save application log to this directory for reporting issues.",
|
|
|
|
},
|
|
|
|
&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
|
|
|
|
Usage: "Application logging level {fatal, error, info, debug}. ",
|
|
|
|
},
|
2021-02-03 19:00:55 +00:00
|
|
|
&cli.StringFlag{
|
2021-03-10 19:39:33 +00:00
|
|
|
Name: sshConnectTo,
|
2021-02-03 19:00:55 +00:00
|
|
|
Hidden: true,
|
2021-03-10 19:39:33 +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
|
|
|
},
|
2019-05-22 20:41:21 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "ssh-config",
|
2020-05-18 18:24:17 +00:00
|
|
|
Action: cliutil.ErrorHandler(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",
|
2020-05-18 18:24:17 +00:00
|
|
|
Action: cliutil.ErrorHandler(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 {
|
2020-05-07 19:58:33 +00:00
|
|
|
if err := raven.SetDSN(sentryDSN); err != nil {
|
|
|
|
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
|
|
|
|
2018-10-08 19:20:28 +00:00
|
|
|
args := c.Args()
|
2019-10-22 15:41:44 +00:00
|
|
|
rawURL := ensureURLScheme(args.First())
|
|
|
|
appURL, err := url.Parse(rawURL)
|
2018-10-08 19:20:28 +00:00
|
|
|
if args.Len() < 1 || 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 19:39:33 +00:00
|
|
|
if err := verifyTokenAtEdge(appURL, 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 19:39:33 +00:00
|
|
|
cfdToken, err := token.GetAppTokenIfExists(appURL)
|
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
|
|
|
}
|
2020-05-07 19:58:33 +00:00
|
|
|
fmt.Fprintf(os.Stdout, "Successfully fetched your token:\n\n%s\n\n", cfdToken)
|
2018-10-08 19:20:28 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-22 15:41:44 +00:00
|
|
|
// ensureURLScheme prepends a URL with https:// if it doesnt have a scheme. http:// URLs will not be converted.
|
|
|
|
func ensureURLScheme(url string) string {
|
|
|
|
url = strings.Replace(strings.ToLower(url), "http://", "https://", 1)
|
|
|
|
if !strings.HasPrefix(url, "https://") {
|
|
|
|
url = fmt.Sprintf("https://%s", url)
|
|
|
|
|
|
|
|
}
|
|
|
|
return url
|
|
|
|
}
|
|
|
|
|
2018-10-08 19:20:28 +00:00
|
|
|
// curl provides a wrapper around curl, passing Access JWT along in request
|
|
|
|
func curl(c *cli.Context) error {
|
2020-05-07 19:58:33 +00:00
|
|
|
if err := raven.SetDSN(sentryDSN); err != nil {
|
|
|
|
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 19:39:33 +00:00
|
|
|
tok, err := token.GetAppTokenIfExists(appURL)
|
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 19:39:33 +00:00
|
|
|
tok, err = token.FetchToken(appURL, 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")
|
2020-05-04 20:15:17 +00:00
|
|
|
cmdArgs = append(cmdArgs, fmt.Sprintf("%s: %s", h2mux.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...)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// token dumps provided token to stdout
|
2018-10-19 20:44:35 +00:00
|
|
|
func generateToken(c *cli.Context) error {
|
2020-05-07 19:58:33 +00:00
|
|
|
if err := raven.SetDSN(sentryDSN); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-08 19:20:28 +00:00
|
|
|
appURL, err := url.Parse(c.String("app"))
|
|
|
|
if err != nil || c.NumFlags() < 1 {
|
|
|
|
fmt.Fprintln(os.Stderr, "Please provide a url.")
|
|
|
|
return err
|
|
|
|
}
|
2021-03-10 19:39:33 +00:00
|
|
|
tok, err := token.GetAppTokenIfExists(appURL)
|
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")
|
|
|
|
}
|
|
|
|
|
2019-10-22 15:41:44 +00:00
|
|
|
originURL, err := url.Parse(ensureURLScheme(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 19:39:33 +00:00
|
|
|
cfdToken, err := token.FetchTokenWithRedirect(fetchTokenURL, log)
|
2019-05-22 20:41:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-03-10 19:39:33 +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 19:39:33 +00:00
|
|
|
// getAppURL will pull the appURL 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 {
|
|
|
|
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 19:39:33 +00:00
|
|
|
func verifyTokenAtEdge(appUrl *url.URL, c *cli.Context, log *zerolog.Logger) error {
|
2019-09-19 18:47:08 +00:00
|
|
|
headers := buildRequestHeaders(c.StringSlice(sshHeaderFlag))
|
|
|
|
if c.IsSet(sshTokenIDFlag) {
|
2020-05-04 20:15:17 +00:00
|
|
|
headers.Add(h2mux.CFAccessClientIDHeader, c.String(sshTokenIDFlag))
|
2019-09-19 18:47:08 +00:00
|
|
|
}
|
|
|
|
if c.IsSet(sshTokenSecretFlag) {
|
2020-05-04 20:15:17 +00:00
|
|
|
headers.Add(h2mux.CFAccessClientSecretHeader, c.String(sshTokenSecretFlag))
|
2019-09-19 18:47:08 +00:00
|
|
|
}
|
2021-03-10 19:39:33 +00:00
|
|
|
options := &carrier.StartOptions{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 19:39:33 +00:00
|
|
|
if err := token.RemoveTokenIfExists(appUrl); 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")
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|