From e56fb38cb585cd8012033247459a0738c0ecf4d6 Mon Sep 17 00:00:00 2001 From: Travis Groth Date: Mon, 22 Feb 2021 18:10:50 -0500 Subject: [PATCH] config: fix redirect routes from protobuf (#1930) --- config/policy.go | 29 ++++++++++++++++------------- config/policy_test.go | 17 +++++++++++++++++ 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/config/policy.go b/config/policy.go index 324d5a6c5..b47c0d2cd 100644 --- a/config/policy.go +++ b/config/policy.go @@ -177,14 +177,8 @@ type PolicyRedirect struct { func NewPolicyFromProto(pb *configpb.Route) (*Policy, error) { timeout, _ := ptypes.Duration(pb.GetTimeout()) - to, err := ParseWeightedUrls(pb.GetTo()...) - if err != nil { - return nil, err - } - p := &Policy{ From: pb.GetFrom(), - To: to, AllowedUsers: pb.GetAllowedUsers(), AllowedGroups: pb.GetAllowedGroups(), AllowedDomains: pb.GetAllowedDomains(), @@ -216,6 +210,7 @@ func NewPolicyFromProto(pb *configpb.Route) (*Policy, error) { PassIdentityHeaders: pb.GetPassIdentityHeaders(), KubernetesServiceAccountToken: pb.GetKubernetesServiceAccountToken(), } + if pb.Redirect.IsSet() { p.Redirect = &PolicyRedirect{ HTTPSRedirect: pb.Redirect.HttpsRedirect, @@ -227,6 +222,13 @@ func NewPolicyFromProto(pb *configpb.Route) (*Policy, error) { ResponseCode: pb.Redirect.ResponseCode, StripQuery: pb.Redirect.StripQuery, } + } else { + to, err := ParseWeightedUrls(pb.GetTo()...) + if err != nil { + return nil, err + } + + p.To = to } p.EnvoyOpts = pb.EnvoyOpts @@ -267,16 +269,9 @@ func (p *Policy) ToProto() (*configpb.Route, error) { }) } - to, weights, err := p.To.Flatten() - if err != nil { - return nil, err - } - pb := &configpb.Route{ Name: fmt.Sprint(p.RouteID()), From: p.From, - To: to, - LoadBalancingWeights: weights, AllowedUsers: p.AllowedUsers, AllowedGroups: p.AllowedGroups, AllowedDomains: p.AllowedDomains, @@ -320,6 +315,14 @@ func (p *Policy) ToProto() (*configpb.Route, error) { ResponseCode: p.Redirect.ResponseCode, StripQuery: p.Redirect.StripQuery, } + } else { + to, weights, err := p.To.Flatten() + if err != nil { + return nil, err + } + + pb.To = to + pb.LoadBalancingWeights = weights } return pb, nil diff --git a/config/policy_test.go b/config/policy_test.go index 58de325cf..dfa940a6a 100644 --- a/config/policy_test.go +++ b/config/policy_test.go @@ -6,6 +6,7 @@ import ( "testing" envoy_config_cluster_v3 "github.com/envoyproxy/go-control-plane/envoy/config/cluster/v3" + "github.com/golang/protobuf/proto" "github.com/google/go-cmp/cmp" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -214,4 +215,20 @@ func TestPolicy_FromToPb(t *testing.T) { assert.Equal(t, tc.expectedPolicyName, policyFromPb.EnvoyOpts.Name) } }) + + t.Run("redirect route", func(t *testing.T) { + p := &Policy{ + From: "https://pomerium.io", + Redirect: &PolicyRedirect{ + HTTPSRedirect: proto.Bool(true), + }, + } + + pbPolicy, err := p.ToProto() + require.NoError(t, err) + + policyFromProto, err := NewPolicyFromProto(pbPolicy) + assert.NoError(t, err) + assert.Equal(t, p.Redirect.HTTPSRedirect, policyFromProto.Redirect.HTTPSRedirect) + }) }