mirror of
https://github.com/pomerium/pomerium.git
synced 2025-08-06 10:21:05 +02:00
config: only validate redirect response code when non-nil (#5358)
* config: only validate redirect response code when non-nil * update unit tests --------- Co-authored-by: Kenneth Jenkins <51246568+kenjenkins@users.noreply.github.com>
This commit is contained in:
parent
526e2a58d6
commit
c8b6b8f1a9
3 changed files with 30 additions and 30 deletions
|
@ -1986,20 +1986,21 @@ func Test_buildPolicyRouteRedirectAction(t *testing.T) {
|
||||||
})
|
})
|
||||||
t.Run("ResponseCode", func(t *testing.T) {
|
t.Run("ResponseCode", func(t *testing.T) {
|
||||||
codes := []struct {
|
codes := []struct {
|
||||||
Number int32
|
Number *int32
|
||||||
Enum envoy_config_route_v3.RedirectAction_RedirectResponseCode
|
Enum envoy_config_route_v3.RedirectAction_RedirectResponseCode
|
||||||
}{
|
}{
|
||||||
{301, envoy_config_route_v3.RedirectAction_MOVED_PERMANENTLY},
|
{nil, envoy_config_route_v3.RedirectAction_MOVED_PERMANENTLY},
|
||||||
{302, envoy_config_route_v3.RedirectAction_FOUND},
|
{proto.Int32(301), envoy_config_route_v3.RedirectAction_MOVED_PERMANENTLY},
|
||||||
{303, envoy_config_route_v3.RedirectAction_SEE_OTHER},
|
{proto.Int32(302), envoy_config_route_v3.RedirectAction_FOUND},
|
||||||
{307, envoy_config_route_v3.RedirectAction_TEMPORARY_REDIRECT},
|
{proto.Int32(303), envoy_config_route_v3.RedirectAction_SEE_OTHER},
|
||||||
{308, envoy_config_route_v3.RedirectAction_PERMANENT_REDIRECT},
|
{proto.Int32(307), envoy_config_route_v3.RedirectAction_TEMPORARY_REDIRECT},
|
||||||
|
{proto.Int32(308), envoy_config_route_v3.RedirectAction_PERMANENT_REDIRECT},
|
||||||
}
|
}
|
||||||
for i := range codes {
|
for i := range codes {
|
||||||
c := &codes[i]
|
c := &codes[i]
|
||||||
t.Run(fmt.Sprint(c.Number), func(t *testing.T) {
|
t.Run(fmt.Sprint(c.Number), func(t *testing.T) {
|
||||||
action, err := b.buildPolicyRouteRedirectAction(&config.PolicyRedirect{
|
action, err := b.buildPolicyRouteRedirectAction(&config.PolicyRedirect{
|
||||||
ResponseCode: &c.Number,
|
ResponseCode: c.Number,
|
||||||
})
|
})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, &envoy_config_route_v3.RedirectAction{
|
assert.Equal(t, &envoy_config_route_v3.RedirectAction{
|
||||||
|
|
|
@ -233,12 +233,10 @@ func (r *PolicyRedirect) validate() error {
|
||||||
|
|
||||||
// GetEnvoyResponseCode returns the ResponseCode as the corresponding Envoy enum value.
|
// GetEnvoyResponseCode returns the ResponseCode as the corresponding Envoy enum value.
|
||||||
func (r *PolicyRedirect) GetEnvoyResponseCode() (envoy_config_route_v3.RedirectAction_RedirectResponseCode, error) {
|
func (r *PolicyRedirect) GetEnvoyResponseCode() (envoy_config_route_v3.RedirectAction_RedirectResponseCode, error) {
|
||||||
var code int32
|
if r == nil || r.ResponseCode == nil {
|
||||||
if r != nil && r.ResponseCode != nil {
|
return envoy_config_route_v3.RedirectAction_RedirectResponseCode(0), nil
|
||||||
code = *r.ResponseCode
|
|
||||||
}
|
}
|
||||||
|
switch code := *r.ResponseCode; code {
|
||||||
switch code {
|
|
||||||
case http.StatusMovedPermanently:
|
case http.StatusMovedPermanently:
|
||||||
return envoy_config_route_v3.RedirectAction_MOVED_PERMANENTLY, nil
|
return envoy_config_route_v3.RedirectAction_MOVED_PERMANENTLY, nil
|
||||||
case http.StatusFound:
|
case http.StatusFound:
|
||||||
|
|
|
@ -74,31 +74,32 @@ func Test_PolicyValidate_RedirectResponseCode(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
Code int32
|
Code *int32
|
||||||
ExpectedError string
|
ExpectedError string
|
||||||
}{
|
}{
|
||||||
{0, "unsupported redirect response code 0"},
|
{nil, ""},
|
||||||
{100, "unsupported redirect response code 100"},
|
{proto.Int32(0), "unsupported redirect response code 0"},
|
||||||
{200, "unsupported redirect response code 200"},
|
{proto.Int32(100), "unsupported redirect response code 100"},
|
||||||
{300, "unsupported redirect response code 300"},
|
{proto.Int32(200), "unsupported redirect response code 200"},
|
||||||
{301, ""},
|
{proto.Int32(300), "unsupported redirect response code 300"},
|
||||||
{302, ""},
|
{proto.Int32(301), ""},
|
||||||
{303, ""},
|
{proto.Int32(302), ""},
|
||||||
{304, "unsupported redirect response code 304"},
|
{proto.Int32(303), ""},
|
||||||
{305, "unsupported redirect response code 305"},
|
{proto.Int32(304), "unsupported redirect response code 304"},
|
||||||
{306, "unsupported redirect response code 306"},
|
{proto.Int32(305), "unsupported redirect response code 305"},
|
||||||
{307, ""},
|
{proto.Int32(306), "unsupported redirect response code 306"},
|
||||||
{308, ""},
|
{proto.Int32(307), ""},
|
||||||
{309, "unsupported redirect response code 309"},
|
{proto.Int32(308), ""},
|
||||||
{400, "unsupported redirect response code 400"},
|
{proto.Int32(309), "unsupported redirect response code 309"},
|
||||||
{500, "unsupported redirect response code 500"},
|
{proto.Int32(400), "unsupported redirect response code 400"},
|
||||||
{600, "unsupported redirect response code 600"},
|
{proto.Int32(500), "unsupported redirect response code 500"},
|
||||||
|
{proto.Int32(600), "unsupported redirect response code 600"},
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := range cases {
|
for i := range cases {
|
||||||
c := &cases[i]
|
c := &cases[i]
|
||||||
t.Run(fmt.Sprint(c.Code), func(t *testing.T) {
|
t.Run(fmt.Sprint(c.Code), func(t *testing.T) {
|
||||||
r.ResponseCode = &c.Code
|
r.ResponseCode = c.Code
|
||||||
err := p.Validate()
|
err := p.Validate()
|
||||||
if c.ExpectedError != "" {
|
if c.ExpectedError != "" {
|
||||||
assert.ErrorContains(t, err, c.ExpectedError)
|
assert.ErrorContains(t, err, c.ExpectedError)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue