From ed54d150fea6897854e76af87951c8ff44a5c961 Mon Sep 17 00:00:00 2001 From: Igor Postelnik Date: Tue, 20 Oct 2020 12:00:34 -0500 Subject: [PATCH] Move raw ingress rules to config package --- cmd/cloudflared/config/configuration.go | 13 +++++++--- cmd/cloudflared/tunnel/configuration.go | 2 +- cmd/cloudflared/tunnel/ingress_subcommands.go | 4 +-- ingress/ingress.go | 26 +++++++------------ ingress/ingress_test.go | 10 ++++--- 5 files changed, 27 insertions(+), 28 deletions(-) diff --git a/cmd/cloudflared/config/configuration.go b/cmd/cloudflared/config/configuration.go index 9d0cdd53..035f556d 100644 --- a/cmd/cloudflared/config/configuration.go +++ b/cmd/cloudflared/config/configuration.go @@ -12,7 +12,6 @@ import ( "github.com/urfave/cli/v2" "gopkg.in/yaml.v2" - "github.com/cloudflare/cloudflared/ingress" "github.com/cloudflare/cloudflared/logger" "github.com/cloudflare/cloudflared/validation" ) @@ -197,15 +196,21 @@ func ValidateUrl(c *cli.Context, allowFromArgs bool) (string, error) { return validUrl, err } +type UnvalidatedIngressRule struct { + Hostname string + Path string + Service string +} + type Configuration struct { - TunnelID string `yaml:"tunnel"` - Ingress ingress.UnvalidatedIngress `yaml:",inline"` + TunnelID string `yaml:"tunnel"` + Ingress []UnvalidatedIngressRule sourceFile string } type configFileSettings struct { Configuration `yaml:",inline"` - // Existing settings will be aggregated in the generic map, should be read via cli.Context + // older settings will be aggregated into the generic map, should be read via cli.Context Settings map[string]interface{} `yaml:",inline"` } diff --git a/cmd/cloudflared/tunnel/configuration.go b/cmd/cloudflared/tunnel/configuration.go index fa8c4711..e5a55e4b 100644 --- a/cmd/cloudflared/tunnel/configuration.go +++ b/cmd/cloudflared/tunnel/configuration.go @@ -231,7 +231,7 @@ func prepareTunnelConfig( Version: version, Arch: fmt.Sprintf("%s_%s", buildInfo.GoOS, buildInfo.GoArch), } - ingressRules, err = ingress.ParseIngress(config.GetConfiguration().Ingress) + ingressRules, err = ingress.ParseIngress(config.GetConfiguration()) if err != nil && err != ingress.ErrNoIngressRules { return nil, err } diff --git a/cmd/cloudflared/tunnel/ingress_subcommands.go b/cmd/cloudflared/tunnel/ingress_subcommands.go index 10bde06d..20b97a29 100644 --- a/cmd/cloudflared/tunnel/ingress_subcommands.go +++ b/cmd/cloudflared/tunnel/ingress_subcommands.go @@ -71,7 +71,7 @@ func buildTestURLCommand() *cli.Command { func validateIngressCommand(c *cli.Context) error { conf := config.GetConfiguration() fmt.Println("Validating rules from", conf.Source()) - if _, err := ingress.ParseIngress(conf.Ingress); err != nil { + if _, err := ingress.ParseIngress(conf); err != nil { return errors.Wrap(err, "Validation failed") } if c.IsSet("url") { @@ -98,7 +98,7 @@ func testURLCommand(c *cli.Context) error { conf := config.GetConfiguration() fmt.Println("Using rules from", conf.Source()) - ing, err := ingress.ParseIngress(conf.Ingress) + ing, err := ingress.ParseIngress(conf) if err != nil { return errors.Wrap(err, "Validation failed") } diff --git a/ingress/ingress.go b/ingress/ingress.go index f5ad1452..30e8b079 100644 --- a/ingress/ingress.go +++ b/ingress/ingress.go @@ -7,6 +7,8 @@ import ( "strings" "github.com/pkg/errors" + + "github.com/cloudflare/cloudflared/cmd/cloudflared/config" ) var ( @@ -79,16 +81,6 @@ func matchHost(ruleHost, reqHost string) bool { return false } -type unvalidatedRule struct { - Hostname string - Path string - Service string -} - -type UnvalidatedIngress struct { - Ingress []unvalidatedRule -} - // Ingress maps eyeball requests to origins. type Ingress struct { Rules []Rule @@ -99,9 +91,9 @@ func (ing Ingress) IsEmpty() bool { return len(ing.Rules) == 0 } -func (ing UnvalidatedIngress) validate() (Ingress, error) { - rules := make([]Rule, len(ing.Ingress)) - for i, r := range ing.Ingress { +func validate(ingress []config.UnvalidatedIngressRule) (Ingress, error) { + rules := make([]Rule, len(ingress)) + for i, r := range ingress { service, err := url.Parse(r.Service) if err != nil { return Ingress{}, err @@ -122,7 +114,7 @@ func (ing UnvalidatedIngress) validate() (Ingress, error) { // The last rule should catch all hostnames. isCatchAllRule := (r.Hostname == "" || r.Hostname == "*") && r.Path == "" - isLastRule := i == len(ing.Ingress)-1 + isLastRule := i == len(ingress)-1 if isLastRule && !isCatchAllRule { return Ingress{}, errLastRuleNotCatchAll } @@ -159,9 +151,9 @@ func (e errRuleShouldNotBeCatchAll) Error() string { "will never be triggered.", e.i+1, e.hostname) } -func ParseIngress(ing UnvalidatedIngress) (Ingress, error) { - if len(ing.Ingress) == 0 { +func ParseIngress(conf *config.Configuration) (Ingress, error) { + if len(conf.Ingress) == 0 { return Ingress{}, ErrNoIngressRules } - return ing.validate() + return validate(conf.Ingress) } diff --git a/ingress/ingress_test.go b/ingress/ingress_test.go index c388cd64..aa3482a2 100644 --- a/ingress/ingress_test.go +++ b/ingress/ingress_test.go @@ -8,6 +8,8 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "gopkg.in/yaml.v2" + + "github.com/cloudflare/cloudflared/cmd/cloudflared/config" ) func Test_parseIngress(t *testing.T) { @@ -300,11 +302,11 @@ ingress: } } -func MustReadIngress(s string) UnvalidatedIngress { - var ing UnvalidatedIngress - err := yaml.Unmarshal([]byte(s), &ing) +func MustReadIngress(s string) *config.Configuration { + var conf config.Configuration + err := yaml.Unmarshal([]byte(s), &conf) if err != nil { panic(err) } - return ing + return &conf }