Skip to content

Commit 8f94f54

Browse files
authored
feat: Adds a new command line for tunnel run for token file
Adds a new command line flag for `tunnel run` which allows a file to be read for the token. I've left the token command line argument with priority.
1 parent 2827b2f commit 8f94f54

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

cmd/cloudflared/tunnel/subcommands.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const (
4141
CredFileFlag = "credentials-file"
4242
CredContentsFlag = "credentials-contents"
4343
TunnelTokenFlag = "token"
44+
TunnelTokenFileFlag = "token-file"
4445
overwriteDNSFlagName = "overwrite-dns"
4546
noDiagLogsFlagName = "no-diag-logs"
4647
noDiagMetricsFlagName = "no-diag-metrics"
@@ -126,9 +127,14 @@ var (
126127
})
127128
tunnelTokenFlag = altsrc.NewStringFlag(&cli.StringFlag{
128129
Name: TunnelTokenFlag,
129-
Usage: "The Tunnel token. When provided along with credentials, this will take precedence.",
130+
Usage: "The Tunnel token. When provided along with credentials, this will take precedence. Also takes precedence over token-file",
130131
EnvVars: []string{"TUNNEL_TOKEN"},
131132
})
133+
tunnelTokenFileFlag = altsrc.NewStringFlag(&cli.StringFlag{
134+
Name: TunnelTokenFileFlag,
135+
Usage: "Filepath at which to read the tunnel token. When provided along with credentials, this will take precedence.",
136+
EnvVars: []string{"TUNNEL_TOKEN_FILE"},
137+
})
132138
forceDeleteFlag = &cli.BoolFlag{
133139
Name: flags.Force,
134140
Aliases: []string{"f"},
@@ -708,6 +714,7 @@ func buildRunCommand() *cli.Command {
708714
selectProtocolFlag,
709715
featuresFlag,
710716
tunnelTokenFlag,
717+
tunnelTokenFileFlag,
711718
icmpv4SrcFlag,
712719
icmpv6SrcFlag,
713720
maxActiveFlowsFlag,
@@ -748,12 +755,22 @@ func runCommand(c *cli.Context) error {
748755
"your origin will not be reachable. You should remove the `hostname` property to avoid this warning.")
749756
}
750757

758+
tokenStr := c.String(TunnelTokenFlag)
759+
// Check if tokenStr is blank before checking for tokenFile
760+
if tokenStr == "" {
761+
if tokenFile := c.String(TunnelTokenFileFlag); tokenFile != "" {
762+
data, err := os.ReadFile(tokenFile)
763+
if err != nil {
764+
return cliutil.UsageError("Failed to read token file: " + err.Error())
765+
}
766+
tokenStr = strings.TrimSpace(string(data))
767+
}
768+
}
751769
// Check if token is provided and if not use default tunnelID flag method
752-
if tokenStr := c.String(TunnelTokenFlag); tokenStr != "" {
770+
if tokenStr != "" {
753771
if token, err := ParseToken(tokenStr); err == nil {
754772
return sc.runWithCredentials(token.Credentials())
755773
}
756-
757774
return cliutil.UsageError("Provided Tunnel token is not valid.")
758775
} else {
759776
tunnelRef := c.Args().First()

0 commit comments

Comments
 (0)