mirror of
https://github.com/cloudflare/cloudflared.git
synced 2025-07-27 15:49:58 +00:00
TUN-3475: Unify config file handling with typed config for new fields
This commit is contained in:
@@ -3,14 +3,13 @@ package config
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
homedir "github.com/mitchellh/go-homedir"
|
||||
"github.com/urfave/cli/v2"
|
||||
"github.com/urfave/cli/v2/altsrc"
|
||||
"gopkg.in/yaml.v2"
|
||||
|
||||
"github.com/cloudflare/cloudflared/ingress"
|
||||
@@ -198,44 +197,126 @@ func ValidateUrl(c *cli.Context, allowFromArgs bool) (string, error) {
|
||||
return validUrl, err
|
||||
}
|
||||
|
||||
func ReadRules(c *cli.Context) (ingress.Ingress, error) {
|
||||
configFilePath := c.String("config")
|
||||
if configFilePath == "" {
|
||||
return ingress.Ingress{}, ingress.ErrNoIngressRules
|
||||
}
|
||||
fmt.Printf("Reading from config file %s\n", configFilePath)
|
||||
configBytes, err := ioutil.ReadFile(configFilePath)
|
||||
if err != nil {
|
||||
return ingress.Ingress{}, err
|
||||
}
|
||||
rules, err := ingress.ParseIngress(configBytes)
|
||||
return rules, err
|
||||
func ReadIngressRules(config *ConfigFileSettings) (ingress.Ingress, error) {
|
||||
return ingress.ParseIngress(config.Ingress)
|
||||
}
|
||||
|
||||
var configFileInputSource struct {
|
||||
lastLoadedFile string
|
||||
context altsrc.InputSourceContext
|
||||
type ConfigFileSettings struct {
|
||||
TunnelID string `yaml:"tunnel"`
|
||||
Ingress ingress.UnvalidatedIngress `yaml:",inline"`
|
||||
Settings map[string]interface{} `yaml:",inline"`
|
||||
sourceFile string
|
||||
}
|
||||
|
||||
// GetConfigFileSource returns InputSourceContext initialized from the configuration file.
|
||||
func (c *ConfigFileSettings) Source() string {
|
||||
return c.sourceFile
|
||||
}
|
||||
|
||||
func (c *ConfigFileSettings) Int(name string) (int, error) {
|
||||
if raw, ok := c.Settings[name]; ok {
|
||||
if v, ok := raw.(int); ok {
|
||||
return v, nil
|
||||
}
|
||||
return 0, fmt.Errorf("expected int found %T for %s", raw, name)
|
||||
}
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func (c *ConfigFileSettings) Duration(name string) (time.Duration, error) {
|
||||
if raw, ok := c.Settings[name]; ok {
|
||||
switch v := raw.(type) {
|
||||
case time.Duration:
|
||||
return v, nil
|
||||
case string:
|
||||
return time.ParseDuration(v)
|
||||
}
|
||||
return 0, fmt.Errorf("expected duration found %T for %s", raw, name)
|
||||
}
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func (c *ConfigFileSettings) Float64(name string) (float64, error) {
|
||||
if raw, ok := c.Settings[name]; ok {
|
||||
if v, ok := raw.(float64); ok {
|
||||
return v, nil
|
||||
}
|
||||
return 0, fmt.Errorf("expected int found %T for %s", raw, name)
|
||||
}
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func (c *ConfigFileSettings) String(name string) (string, error) {
|
||||
if raw, ok := c.Settings[name]; ok {
|
||||
if v, ok := raw.(string); ok {
|
||||
return v, nil
|
||||
}
|
||||
return "", fmt.Errorf("expected int found %T for %s", raw, name)
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func (c *ConfigFileSettings) StringSlice(name string) ([]string, error) {
|
||||
if raw, ok := c.Settings[name]; ok {
|
||||
if v, ok := raw.([]string); ok {
|
||||
return v, nil
|
||||
}
|
||||
return nil, fmt.Errorf("expected int found %T for %s", raw, name)
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (c *ConfigFileSettings) IntSlice(name string) ([]int, error) {
|
||||
if raw, ok := c.Settings[name]; ok {
|
||||
if v, ok := raw.([]int); ok {
|
||||
return v, nil
|
||||
}
|
||||
return nil, fmt.Errorf("expected int found %T for %s", raw, name)
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (c *ConfigFileSettings) Generic(name string) (cli.Generic, error) {
|
||||
return nil, errors.New("option type Generic not supported")
|
||||
}
|
||||
|
||||
func (c *ConfigFileSettings) Bool(name string) (bool, error) {
|
||||
if raw, ok := c.Settings[name]; ok {
|
||||
if v, ok := raw.(bool); ok {
|
||||
return v, nil
|
||||
}
|
||||
return false, fmt.Errorf("expected int found %T for %s", raw, name)
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
var configuration ConfigFileSettings
|
||||
|
||||
func GetConfiguration() *ConfigFileSettings {
|
||||
return &configuration
|
||||
}
|
||||
|
||||
// ReadConfigFile returns InputSourceContext initialized from the configuration file.
|
||||
// On repeat calls returns with the same file, returns without reading the file again; however,
|
||||
// if value of "config" flag changes, will read the new config file
|
||||
func GetConfigFileSource(c *cli.Context, log logger.Service) (altsrc.InputSourceContext, error) {
|
||||
func ReadConfigFile(c *cli.Context, log logger.Service) (*ConfigFileSettings, error) {
|
||||
configFile := c.String("config")
|
||||
if configFileInputSource.lastLoadedFile == configFile {
|
||||
if configFileInputSource.context == nil {
|
||||
return nil, ErrNoConfigFile
|
||||
}
|
||||
return configFileInputSource.context, nil
|
||||
if configuration.Source() == configFile || configFile == "" {
|
||||
return &configuration, nil
|
||||
}
|
||||
|
||||
configFileInputSource.lastLoadedFile = configFile
|
||||
log.Debugf("Loading configuration from %s", configFile)
|
||||
src, err := altsrc.NewYamlSourceFromFile(configFile)
|
||||
file, err := os.Open(configFile)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
err = ErrNoConfigFile
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
defer file.Close()
|
||||
if err := yaml.NewDecoder(file).Decode(&configuration); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
configFileInputSource.context = src
|
||||
return src, nil
|
||||
configuration.sourceFile = configFile
|
||||
return &configuration, nil
|
||||
}
|
||||
|
@@ -4,9 +4,10 @@ import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/cloudflare/cloudflared/logger"
|
||||
"github.com/cloudflare/cloudflared/watcher"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type mockNotifier struct {
|
||||
|
@@ -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
|
||||
}
|
||||
|
@@ -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
|
||||
}
|
||||
|
@@ -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 {
|
||||
|
@@ -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.`)
|
||||
}
|
||||
|
Reference in New Issue
Block a user