2020-11-23 21:36:16 +00:00
|
|
|
package tunnel
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/cloudflare/cloudflared/cmd/cloudflared/config"
|
2020-11-25 06:55:13 +00:00
|
|
|
|
2020-11-23 21:36:16 +00:00
|
|
|
"github.com/google/uuid"
|
2020-11-25 06:55:13 +00:00
|
|
|
"github.com/rs/zerolog"
|
2020-11-23 21:36:16 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CredFinder can find the tunnel credentials file.
|
|
|
|
type CredFinder interface {
|
|
|
|
Path() (string, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements CredFinder and looks for the credentials file at the given
|
|
|
|
// filepath.
|
|
|
|
type staticPath struct {
|
|
|
|
filePath string
|
|
|
|
fs fileSystem
|
|
|
|
}
|
|
|
|
|
|
|
|
func newStaticPath(filePath string, fs fileSystem) CredFinder {
|
|
|
|
return staticPath{
|
|
|
|
filePath: filePath,
|
|
|
|
fs: fs,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a staticPath) Path() (string, error) {
|
|
|
|
if a.filePath != "" && a.fs.validFilePath(a.filePath) {
|
|
|
|
return a.filePath, nil
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("Tunnel credentials file '%s' doesn't exist or is not a file", a.filePath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements CredFinder and looks for the credentials file in several directories
|
|
|
|
// searching for a file named <id>.json
|
|
|
|
type searchByID struct {
|
2020-11-25 06:55:13 +00:00
|
|
|
id uuid.UUID
|
|
|
|
c *cli.Context
|
|
|
|
log *zerolog.Logger
|
|
|
|
fs fileSystem
|
2020-11-23 21:36:16 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 06:55:13 +00:00
|
|
|
func newSearchByID(id uuid.UUID, c *cli.Context, log *zerolog.Logger, fs fileSystem) CredFinder {
|
2020-11-23 21:36:16 +00:00
|
|
|
return searchByID{
|
2020-11-25 06:55:13 +00:00
|
|
|
id: id,
|
|
|
|
c: c,
|
|
|
|
log: log,
|
|
|
|
fs: fs,
|
2020-11-23 21:36:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s searchByID) Path() (string, error) {
|
|
|
|
|
|
|
|
// Fallback to look for tunnel credentials in the origin cert directory
|
2020-11-25 06:55:13 +00:00
|
|
|
if originCertPath, err := findOriginCert(s.c, s.log); err == nil {
|
2020-11-23 21:36:16 +00:00
|
|
|
originCertDir := filepath.Dir(originCertPath)
|
|
|
|
if filePath, err := tunnelFilePath(s.id, originCertDir); err == nil {
|
|
|
|
if s.fs.validFilePath(filePath) {
|
|
|
|
return filePath, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Last resort look under default config directories
|
|
|
|
for _, configDir := range config.DefaultConfigSearchDirectories() {
|
|
|
|
if filePath, err := tunnelFilePath(s.id, configDir); err == nil {
|
|
|
|
if s.fs.validFilePath(filePath) {
|
|
|
|
return filePath, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("Tunnel credentials file not found")
|
|
|
|
}
|