mirror of
https://github.com/pomerium/pomerium.git
synced 2025-05-31 18:07:17 +02:00
config: fix redirect routes from protobuf (#1930)
This commit is contained in:
parent
8b42eb5ebd
commit
e56fb38cb5
2 changed files with 33 additions and 13 deletions
|
@ -177,14 +177,8 @@ type PolicyRedirect struct {
|
||||||
func NewPolicyFromProto(pb *configpb.Route) (*Policy, error) {
|
func NewPolicyFromProto(pb *configpb.Route) (*Policy, error) {
|
||||||
timeout, _ := ptypes.Duration(pb.GetTimeout())
|
timeout, _ := ptypes.Duration(pb.GetTimeout())
|
||||||
|
|
||||||
to, err := ParseWeightedUrls(pb.GetTo()...)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
p := &Policy{
|
p := &Policy{
|
||||||
From: pb.GetFrom(),
|
From: pb.GetFrom(),
|
||||||
To: to,
|
|
||||||
AllowedUsers: pb.GetAllowedUsers(),
|
AllowedUsers: pb.GetAllowedUsers(),
|
||||||
AllowedGroups: pb.GetAllowedGroups(),
|
AllowedGroups: pb.GetAllowedGroups(),
|
||||||
AllowedDomains: pb.GetAllowedDomains(),
|
AllowedDomains: pb.GetAllowedDomains(),
|
||||||
|
@ -216,6 +210,7 @@ func NewPolicyFromProto(pb *configpb.Route) (*Policy, error) {
|
||||||
PassIdentityHeaders: pb.GetPassIdentityHeaders(),
|
PassIdentityHeaders: pb.GetPassIdentityHeaders(),
|
||||||
KubernetesServiceAccountToken: pb.GetKubernetesServiceAccountToken(),
|
KubernetesServiceAccountToken: pb.GetKubernetesServiceAccountToken(),
|
||||||
}
|
}
|
||||||
|
|
||||||
if pb.Redirect.IsSet() {
|
if pb.Redirect.IsSet() {
|
||||||
p.Redirect = &PolicyRedirect{
|
p.Redirect = &PolicyRedirect{
|
||||||
HTTPSRedirect: pb.Redirect.HttpsRedirect,
|
HTTPSRedirect: pb.Redirect.HttpsRedirect,
|
||||||
|
@ -227,6 +222,13 @@ func NewPolicyFromProto(pb *configpb.Route) (*Policy, error) {
|
||||||
ResponseCode: pb.Redirect.ResponseCode,
|
ResponseCode: pb.Redirect.ResponseCode,
|
||||||
StripQuery: pb.Redirect.StripQuery,
|
StripQuery: pb.Redirect.StripQuery,
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
to, err := ParseWeightedUrls(pb.GetTo()...)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
p.To = to
|
||||||
}
|
}
|
||||||
|
|
||||||
p.EnvoyOpts = pb.EnvoyOpts
|
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{
|
pb := &configpb.Route{
|
||||||
Name: fmt.Sprint(p.RouteID()),
|
Name: fmt.Sprint(p.RouteID()),
|
||||||
From: p.From,
|
From: p.From,
|
||||||
To: to,
|
|
||||||
LoadBalancingWeights: weights,
|
|
||||||
AllowedUsers: p.AllowedUsers,
|
AllowedUsers: p.AllowedUsers,
|
||||||
AllowedGroups: p.AllowedGroups,
|
AllowedGroups: p.AllowedGroups,
|
||||||
AllowedDomains: p.AllowedDomains,
|
AllowedDomains: p.AllowedDomains,
|
||||||
|
@ -320,6 +315,14 @@ func (p *Policy) ToProto() (*configpb.Route, error) {
|
||||||
ResponseCode: p.Redirect.ResponseCode,
|
ResponseCode: p.Redirect.ResponseCode,
|
||||||
StripQuery: p.Redirect.StripQuery,
|
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
|
return pb, nil
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
envoy_config_cluster_v3 "github.com/envoyproxy/go-control-plane/envoy/config/cluster/v3"
|
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/google/go-cmp/cmp"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
@ -214,4 +215,20 @@ func TestPolicy_FromToPb(t *testing.T) {
|
||||||
assert.Equal(t, tc.expectedPolicyName, policyFromPb.EnvoyOpts.Name)
|
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)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue