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"
"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"`
}

View File

@ -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
}

View File

@ -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")
}

View File

@ -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)
}

View File

@ -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
}