Skip to content

Commit

Permalink
rule: Allow empty upstream in rules
Browse files Browse the repository at this point in the history
Signed-off-by: arekkas <[email protected]>
  • Loading branch information
arekkas authored and arekkas committed Aug 1, 2018
1 parent c6d17c5 commit e46065a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
4 changes: 4 additions & 0 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion rule/rule_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,19 @@ 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) {
return errors.WithStack(helper.ErrBadRequest.WithReason(fmt.Sprintf("Value \"%s\" from match.methods is not a valid HTTP method, valid methods are: %v", m, methods)))
}
}

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

Expand Down
12 changes: 6 additions & 6 deletions rule/rule_validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}},
Expand Down

0 comments on commit e46065a

Please sign in to comment.