From e46065afa1d6ad14fa62dad9c6b145e46623c7f0 Mon Sep 17 00:00:00 2001 From: arekkas Date: Wed, 1 Aug 2018 13:18:24 +0200 Subject: [PATCH] rule: Allow empty upstream in rules Signed-off-by: arekkas --- proxy/proxy.go | 4 ++++ rule/rule_validator.go | 7 ++++++- rule/rule_validator_test.go | 12 ++++++------ 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/proxy/proxy.go b/proxy/proxy.go index 6567fd0cfb..aef9218f71 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -143,6 +143,10 @@ func EnrichRequestedURL(r *http.Request) { } func configureBackendURL(r *http.Request, rl *rule.Rule) error { + if rl.Upstream.URL == "" { + return errors.Errorf("Unable to forward the request because matched rule does not define an upstream URL") + } + p, err := url.Parse(rl.Upstream.URL) if err != nil { return errors.WithStack(err) diff --git a/rule/rule_validator.go b/rule/rule_validator.go index b34faece75..f6d782e20d 100644 --- a/rule/rule_validator.go +++ b/rule/rule_validator.go @@ -41,6 +41,9 @@ func ValidateRule( // if !govalidator.IsURL(r.Match.URL) { // return errors.WithStack(helper.ErrBadRequest.WithReason(fmt.Sprintf("Value \"%s\" from match.url field is not a valid url.", r.Match.URL))) // } + if r.Match.URL == "" { + return errors.WithStack(helper.ErrBadRequest.WithReason(fmt.Sprintf("Value \"%s\" from match.url field is not a valid url.", r.Match.URL))) + } for _, m := range r.Match.Methods { if !stringslice.Has(methods, m) { @@ -48,7 +51,9 @@ func ValidateRule( } } - if !govalidator.IsURL(r.Upstream.URL) { + if r.Upstream.URL == "" { + // Having no upstream URL is fine here because the judge does not need an upstream! + } else if !govalidator.IsURL(r.Upstream.URL) { return errors.WithStack(helper.ErrBadRequest.WithReason(fmt.Sprintf("Value \"%s\" from upstream.url field is not a valid url.", r.Upstream.URL))) } diff --git a/rule/rule_validator_test.go b/rule/rule_validator_test.go index 5472b41c84..84e4ea74a0 100644 --- a/rule/rule_validator_test.go +++ b/rule/rule_validator_test.go @@ -39,17 +39,17 @@ func TestValidateRule(t *testing.T) { assertReason(t, v(&Rule{}), "from match.url field is not a valid url.") - assertReason(t, v(&Rule{ - Match: RuleMatch{URL: "asdf"}, - }), "from match.url field is not a valid url.") + // assertReason(t, v(&Rule{ + // Match: RuleMatch{URL: "asdf"}, + // }), "from match.url field is not a valid url.") assertReason(t, v(&Rule{ Match: RuleMatch{URL: "https://www.ory.sh", Methods: []string{"FOO"}}, }), "from match.methods is not a valid HTTP method") - assertReason(t, v(&Rule{ - Match: RuleMatch{URL: "https://www.ory.sh", Methods: []string{"POST"}}, - }), "from upstream.url field is not a valid url.") + // assertReason(t, v(&Rule{ + // Match: RuleMatch{URL: "https://www.ory.sh", Methods: []string{"POST"}}, + // }), "from upstream.url field is not a valid url.") assertReason(t, v(&Rule{ Match: RuleMatch{URL: "https://www.ory.sh", Methods: []string{"POST"}},