1
0
Fork 0

Add a mechanism to format the sticky cookie value

Co-authored-by: Jean-Baptiste Doumenjou <925513+jbdoumenjou@users.noreply.github.com>
This commit is contained in:
Tom Moulard 2021-04-29 17:56:03 +02:00 committed by GitHub
parent 70a02158e5
commit dc8d5ef744
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 49 additions and 6 deletions

View file

@ -26,6 +26,7 @@ import (
"github.com/traefik/traefik/v2/pkg/server/service/loadbalancer/mirror"
"github.com/traefik/traefik/v2/pkg/server/service/loadbalancer/wrr"
"github.com/vulcand/oxy/roundrobin"
"github.com/vulcand/oxy/roundrobin/stickycookie"
)
const (
@ -310,7 +311,13 @@ func (m *Manager) getLoadBalancer(ctx context.Context, serviceName string, servi
SameSite: convertSameSite(service.Sticky.Cookie.SameSite),
}
options = append(options, roundrobin.EnableStickySession(roundrobin.NewStickySessionWithOptions(cookieName, opts)))
// Sticky Cookie Value
cv, err := stickycookie.NewFallbackValue(&stickycookie.RawValue{}, &stickycookie.HashValue{})
if err != nil {
return nil, err
}
options = append(options, roundrobin.EnableStickySession(roundrobin.NewStickySessionWithOptions(cookieName, opts).SetCookieValue(cv)))
logger.Debugf("Sticky session cookie name: %v", cookieName)
}

View file

@ -120,6 +120,7 @@ func TestGetLoadBalancerServiceHandler(t *testing.T) {
serviceName string
service *dynamic.ServersLoadBalancer
responseModifier func(*http.Response) error
cookieRawValue string
expected []ExpectedResult
}{
@ -258,6 +259,34 @@ func TestGetLoadBalancerServiceHandler(t *testing.T) {
},
},
},
{
desc: "Cookie value is backward compatible",
serviceName: "test",
service: &dynamic.ServersLoadBalancer{
Sticky: &dynamic.Sticky{
Cookie: &dynamic.Cookie{},
},
Servers: []dynamic.Server{
{
URL: server1.URL,
},
{
URL: server2.URL,
},
},
},
cookieRawValue: "_6f743=" + server1.URL,
expected: []ExpectedResult{
{
StatusCode: http.StatusOK,
XFrom: "first",
},
{
StatusCode: http.StatusOK,
XFrom: "first",
},
},
},
}
for _, test := range testCases {
@ -269,6 +298,10 @@ func TestGetLoadBalancerServiceHandler(t *testing.T) {
assert.NotNil(t, handler)
req := testhelpers.MustNewRequest(http.MethodGet, "http://callme", nil)
if test.cookieRawValue != "" {
req.Header.Set("Cookie", test.cookieRawValue)
}
for _, expected := range test.expected {
recorder := httptest.NewRecorder()
@ -282,6 +315,7 @@ func TestGetLoadBalancerServiceHandler(t *testing.T) {
req.Header.Set("Cookie", cookieHeader)
assert.Equal(t, expected.SecureCookie, strings.Contains(cookieHeader, "Secure"))
assert.Equal(t, expected.HTTPOnlyCookie, strings.Contains(cookieHeader, "HttpOnly"))
assert.NotContains(t, cookieHeader, "://")
}
}
})