Skip to content

Commit 6dc8ed7

Browse files
authored
fix: expand home directory for credentials file
## Issue The [documentation for creating a tunnel's configuration file](https://developers.cloudflare.com/cloudflare-one/connections/connect-networks/get-started/create-local-tunnel/#4-create-a-configuration-file) does not specify that the `credentials-file` field in `config.yml` needs to be an absolute path. A user (E.G. me 🤦) might add a path like `~/.cloudflared/<uuid>.json` and wonder why the `cloudflared tunnel run` command is throwing a credentials file not found error. Although one might consider it intuitive, it's not a fair assumption as a lot of CLI tools allow file paths with `~` for specifying files. P.S. The tunnel ID in the following snippet is not a real tunnel ID, I just generated it. ``` url: http://localhost:8000 tunnel: 958a1ef6-ff8c-4455-825a-5aed91242135 credentials-file: ~/.cloudflared/958a1ef6-ff8c-4455-825a-5aed91242135.json ``` Furthermore, the error has a confusing message for the user as the file at the logged path actually exists, it is just that `os.Stat` failed because it could not expand the `~`. ## Solution This commit fixes the above issue by running a `homedir.Expand` on the `credentials-file` path in the `credentialFinder` function.
1 parent e0b1ac0 commit 6dc8ed7

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

cmd/cloudflared/tunnel/subcommand_context.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010

1111
"github.com/google/uuid"
12+
"github.com/mitchellh/go-homedir"
1213
"github.com/pkg/errors"
1314
"github.com/rs/zerolog"
1415
"github.com/urfave/cli/v2"
@@ -54,7 +55,12 @@ func newSubcommandContext(c *cli.Context) (*subcommandContext, error) {
5455
// Returns something that can find the given tunnel's credentials file.
5556
func (sc *subcommandContext) credentialFinder(tunnelID uuid.UUID) CredFinder {
5657
if path := sc.c.String(CredFileFlag); path != "" {
57-
return newStaticPath(path, sc.fs)
58+
// Expand path if CredFileFlag contains `~`
59+
absPath, err := homedir.Expand(path)
60+
if err != nil {
61+
return newStaticPath(path, sc.fs)
62+
}
63+
return newStaticPath(absPath, sc.fs)
5864
}
5965
return newSearchByID(tunnelID, sc.c, sc.log, sc.fs)
6066
}

0 commit comments

Comments
 (0)