1
0
mirror of https://github.com/fumiama/terasu-cloudflared.git synced 2026-06-08 12:00:36 +08:00

TUN-3475: Unify config file handling with typed config for new fields

This commit is contained in:
Igor Postelnik
2020-10-19 17:33:40 -05:00
parent 051908aaef
commit eaf03305bd
8 changed files with 333 additions and 258 deletions

View File

@@ -206,10 +206,8 @@ func TunnelCommand(c *cli.Context) error {
if name := c.String("name"); name != "" { // Start a named tunnel
return runAdhocNamedTunnel(sc, name)
}
if ref, err := sc.getConfigFileTunnelRef(); err != nil {
return err
} else if ref != "" {
return runNamedTunnel(sc, ref)
if ref := config.GetConfiguration().TunnelID; ref != "" {
return fmt.Errorf("Use `cloudflared tunnel run` to start tunnel %s", ref)
}
// Start a classic tunnel
@@ -312,7 +310,7 @@ func StartServer(
connectedSignal := signal.New(make(chan struct{}))
dnsReadySignal := make(chan struct{})
if c.String("config") == "" {
if config.GetConfiguration().Source() == "" {
log.Infof(config.ErrNoConfigFile.Error())
}
@@ -576,7 +574,7 @@ func SetFlagsFromConfigFile(c *cli.Context) error {
return cliutil.PrintLoggerSetupError("error setting up logger", err)
}
inputSource, err := config.GetConfigFileSource(c, log)
inputSource, err := config.ReadConfigFile(c, log)
if err != nil {
if err == config.ErrNoConfigFile {
return nil
@@ -1269,7 +1267,16 @@ func buildRuleCommand() *cli.Command {
// Validates the ingress rules in the cloudflared config file
func ValidateCommand(c *cli.Context) error {
_, err := config.ReadRules(c)
logger, err := createLogger(c, false, false)
if err != nil {
return err
}
configFile, err := config.ReadConfigFile(c, logger)
if err != nil {
return err
}
fmt.Println("Validating rules from", configFile.Source())
_, err = config.ReadIngressRules(configFile)
if err != nil {
return errors.Wrap(err, "Validation failed")
}
@@ -1282,7 +1289,15 @@ func ValidateCommand(c *cli.Context) error {
// Checks which ingress rule matches the given URL.
func RuleCommand(c *cli.Context) error {
rules, err := config.ReadRules(c)
logger, err := createLogger(c, false, false)
if err != nil {
return err
}
configFile, err := config.ReadConfigFile(c, logger)
if err != nil {
return err
}
rules, err := config.ReadIngressRules(configFile)
if err != nil {
return err
}

View File

@@ -231,7 +231,7 @@ func prepareTunnelConfig(
Version: version,
Arch: fmt.Sprintf("%s_%s", buildInfo.GoOS, buildInfo.GoArch),
}
ingressRules, err = config.ReadRules(c)
ingressRules, err = config.ReadIngressRules(config.GetConfiguration())
if err != nil && err != ingress.ErrNoIngressRules {
return nil, err
}

View File

@@ -121,6 +121,7 @@ func (sc *subcommandContext) tunnelCredentialsPath(tunnelID uuid.UUID) (string,
if validFilePath(filePath) {
return filePath, nil
}
return "", fmt.Errorf("Tunnel credentials file %s doesn't exist or is not a file", filePath)
}
// Fallback to look for tunnel credentials in the origin cert directory
@@ -144,18 +145,6 @@ func (sc *subcommandContext) tunnelCredentialsPath(tunnelID uuid.UUID) (string,
return "", fmt.Errorf("Tunnel credentials file not found")
}
// getConfigFileTunnelRef returns tunnel UUID or name set in the configuration file
func (sc *subcommandContext) getConfigFileTunnelRef() (string, error) {
if src, err := config.GetConfigFileSource(sc.c, sc.logger); err == nil {
if tunnelRef, err := src.String("tunnel"); err != nil {
return "", errors.Wrapf(err, "invalid tunnel ID or name")
} else {
return tunnelRef, nil
}
}
return "", nil
}
func (sc *subcommandContext) create(name string) (*tunnelstore.Tunnel, error) {
client, err := sc.client()
if err != nil {

View File

@@ -22,6 +22,7 @@ import (
"gopkg.in/yaml.v2"
"github.com/cloudflare/cloudflared/cmd/cloudflared/cliutil"
"github.com/cloudflare/cloudflared/cmd/cloudflared/config"
"github.com/cloudflare/cloudflared/logger"
"github.com/cloudflare/cloudflared/tunnelrpc/pogs"
"github.com/cloudflare/cloudflared/tunnelstore"
@@ -341,11 +342,8 @@ func runCommand(c *cli.Context) error {
}
tunnelRef := c.Args().First()
if tunnelRef == "" {
// attempt to read from the config file
if tunnelRef, err = sc.getConfigFileTunnelRef(); err != nil {
return err
}
// see if tunnel id was in the config file
tunnelRef = config.GetConfiguration().TunnelID
if tunnelRef == "" {
return cliutil.UsageError(`"cloudflared tunnel run" requires the ID or name of the tunnel to run as the last command line argument or in the configuration file.`)
}