Move raw ingress rules to config package

This commit is contained in:
Igor Postelnik 2020-10-20 12:00:34 -05:00
parent ca4887fb19
commit ed54d150fe
5 changed files with 27 additions and 28 deletions

View File

@ -12,7 +12,6 @@ import (
"github.com/urfave/cli/v2" "github.com/urfave/cli/v2"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"github.com/cloudflare/cloudflared/ingress"
"github.com/cloudflare/cloudflared/logger" "github.com/cloudflare/cloudflared/logger"
"github.com/cloudflare/cloudflared/validation" "github.com/cloudflare/cloudflared/validation"
) )
@ -197,15 +196,21 @@ func ValidateUrl(c *cli.Context, allowFromArgs bool) (string, error) {
return validUrl, err return validUrl, err
} }
type UnvalidatedIngressRule struct {
Hostname string
Path string
Service string
}
type Configuration struct { type Configuration struct {
TunnelID string `yaml:"tunnel"` TunnelID string `yaml:"tunnel"`
Ingress ingress.UnvalidatedIngress `yaml:",inline"` Ingress []UnvalidatedIngressRule
sourceFile string sourceFile string
} }
type configFileSettings struct { type configFileSettings struct {
Configuration `yaml:",inline"` 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"` Settings map[string]interface{} `yaml:",inline"`
} }

View File

@ -231,7 +231,7 @@ func prepareTunnelConfig(
Version: version, Version: version,
Arch: fmt.Sprintf("%s_%s", buildInfo.GoOS, buildInfo.GoArch), 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 { if err != nil && err != ingress.ErrNoIngressRules {
return nil, err return nil, err
} }

View File

@ -71,7 +71,7 @@ func buildTestURLCommand() *cli.Command {
func validateIngressCommand(c *cli.Context) error { func validateIngressCommand(c *cli.Context) error {
conf := config.GetConfiguration() conf := config.GetConfiguration()
fmt.Println("Validating rules from", conf.Source()) 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") return errors.Wrap(err, "Validation failed")
} }
if c.IsSet("url") { if c.IsSet("url") {
@ -98,7 +98,7 @@ func testURLCommand(c *cli.Context) error {
conf := config.GetConfiguration() conf := config.GetConfiguration()
fmt.Println("Using rules from", conf.Source()) fmt.Println("Using rules from", conf.Source())
ing, err := ingress.ParseIngress(conf.Ingress) ing, err := ingress.ParseIngress(conf)
if err != nil { if err != nil {
return errors.Wrap(err, "Validation failed") return errors.Wrap(err, "Validation failed")
} }

View File

@ -7,6 +7,8 @@ import (
"strings" "strings"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/cloudflare/cloudflared/cmd/cloudflared/config"
) )
var ( var (
@ -79,16 +81,6 @@ func matchHost(ruleHost, reqHost string) bool {
return false return false
} }
type unvalidatedRule struct {
Hostname string
Path string
Service string
}
type UnvalidatedIngress struct {
Ingress []unvalidatedRule
}
// Ingress maps eyeball requests to origins. // Ingress maps eyeball requests to origins.
type Ingress struct { type Ingress struct {
Rules []Rule Rules []Rule
@ -99,9 +91,9 @@ func (ing Ingress) IsEmpty() bool {
return len(ing.Rules) == 0 return len(ing.Rules) == 0
} }
func (ing UnvalidatedIngress) validate() (Ingress, error) { func validate(ingress []config.UnvalidatedIngressRule) (Ingress, error) {
rules := make([]Rule, len(ing.Ingress)) rules := make([]Rule, len(ingress))
for i, r := range ing.Ingress { for i, r := range ingress {
service, err := url.Parse(r.Service) service, err := url.Parse(r.Service)
if err != nil { if err != nil {
return Ingress{}, err return Ingress{}, err
@ -122,7 +114,7 @@ func (ing UnvalidatedIngress) validate() (Ingress, error) {
// The last rule should catch all hostnames. // The last rule should catch all hostnames.
isCatchAllRule := (r.Hostname == "" || r.Hostname == "*") && r.Path == "" isCatchAllRule := (r.Hostname == "" || r.Hostname == "*") && r.Path == ""
isLastRule := i == len(ing.Ingress)-1 isLastRule := i == len(ingress)-1
if isLastRule && !isCatchAllRule { if isLastRule && !isCatchAllRule {
return Ingress{}, errLastRuleNotCatchAll return Ingress{}, errLastRuleNotCatchAll
} }
@ -159,9 +151,9 @@ func (e errRuleShouldNotBeCatchAll) Error() string {
"will never be triggered.", e.i+1, e.hostname) "will never be triggered.", e.i+1, e.hostname)
} }
func ParseIngress(ing UnvalidatedIngress) (Ingress, error) { func ParseIngress(conf *config.Configuration) (Ingress, error) {
if len(ing.Ingress) == 0 { if len(conf.Ingress) == 0 {
return Ingress{}, ErrNoIngressRules return Ingress{}, ErrNoIngressRules
} }
return ing.validate() return validate(conf.Ingress)
} }

View File

@ -8,6 +8,8 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"github.com/cloudflare/cloudflared/cmd/cloudflared/config"
) )
func Test_parseIngress(t *testing.T) { func Test_parseIngress(t *testing.T) {
@ -300,11 +302,11 @@ ingress:
} }
} }
func MustReadIngress(s string) UnvalidatedIngress { func MustReadIngress(s string) *config.Configuration {
var ing UnvalidatedIngress var conf config.Configuration
err := yaml.Unmarshal([]byte(s), &ing) err := yaml.Unmarshal([]byte(s), &conf)
if err != nil { if err != nil {
panic(err) panic(err)
} }
return ing return &conf
} }