config: fix redirect routes from protobuf (#1930)

This commit is contained in:
Travis Groth 2021-02-22 18:10:50 -05:00 committed by GitHub
parent 8b42eb5ebd
commit e56fb38cb5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 13 deletions

View file

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

View file

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