Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add more cookie options. #195

Merged
merged 3 commits into from
Mar 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
language: go

go:
- 1.11.x
- 1.12.x
- 1.13.x
- 1.x

go_import_path: github.com/vulcand/oxy
Expand Down
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module github.com/vulcand/oxy

go 1.12
go 1.13

require (
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd
github.com/gorilla/websocket v1.4.0
github.com/gorilla/websocket v1.4.2
github.com/gravitational/trace v0.0.0-20190726142706-a535a178675f // indirect
github.com/jonboulle/clockwork v0.1.0 // indirect
github.com/kr/pretty v0.1.0 // indirect
Expand All @@ -13,8 +13,9 @@ require (
github.com/mailgun/timetools v0.0.0-20141028012446-7e6055773c51
github.com/mailgun/ttlmap v0.0.0-20170619185759-c1c17f74874f
github.com/sirupsen/logrus v1.4.2
github.com/stretchr/testify v1.3.0
github.com/stretchr/testify v1.5.1
github.com/vulcand/predicate v1.1.0
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
launchpad.net/gocheck v0.0.0-20140225173054-000000000087 // indirect
)
13 changes: 9 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd/go.mod h1:sE
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gravitational/trace v0.0.0-20190726142706-a535a178675f h1:68WxnfBzJRYktZ30fmIjGQ74RsXYLoeH2/NITPktTMY=
github.com/gravitational/trace v0.0.0-20190726142706-a535a178675f/go.mod h1:RvdOUHE4SHqR3oXlFFKnGzms8a5dugHygGw1bqDstYI=
github.com/jonboulle/clockwork v0.1.0 h1:VKV+ZcuP6l3yW9doeqz6ziZGgcynBVQO+obU0+0hcPo=
Expand All @@ -31,8 +31,8 @@ github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6Mwd
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/vulcand/predicate v1.1.0 h1:Gq/uWopa4rx/tnZu2opOSBqHK63Yqlou/SzrbwdJiNg=
github.com/vulcand/predicate v1.1.0/go.mod h1:mlccC5IRBoc2cIFmCB8ZM62I3VDb6p2GXESMHa3CnZg=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
Expand All @@ -43,5 +43,10 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
launchpad.net/gocheck v0.0.0-20140225173054-000000000087 h1:Izowp2XBH6Ya6rv+hqbceQyw/gSGoXfH/UPoTGduL54=
launchpad.net/gocheck v0.0.0-20140225173054-000000000087/go.mod h1:hj7XX3B/0A+80Vse0e+BUHsHMTEhd0O4cpUHr/e/BUM=
38 changes: 31 additions & 7 deletions roundrobin/stickysessions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,26 @@ package roundrobin
import (
"net/http"
"net/url"
"time"
)

// StickySession is a mixin for load balancers that implements layer 7 (http cookie) session affinity
type StickySession struct {
cookieName string
options CookieOptions
}

// CookieOptions has all the options one would like to set on the affinity cookie
type CookieOptions struct {
HTTPOnly bool
Secure bool

Path string
Domain string
Expires time.Time

MaxAge int
SameSite http.SameSite
}

// StickySession is a mixin for load balancers that implements layer 7 (http cookie) session affinity
type StickySession struct {
cookieName string
options CookieOptions
}

// NewStickySession creates a new StickySession
Expand Down Expand Up @@ -53,7 +61,23 @@ func (s *StickySession) GetBackend(req *http.Request, servers []*url.URL) (*url.
// StickBackend creates and sets the cookie
func (s *StickySession) StickBackend(backend *url.URL, w *http.ResponseWriter) {
opt := s.options
cookie := &http.Cookie{Name: s.cookieName, Value: backend.String(), Path: "/", HttpOnly: opt.HTTPOnly, Secure: opt.Secure}

cp := "/"
if opt.Path != "" {
cp = opt.Path
}

cookie := &http.Cookie{
Name: s.cookieName,
Value: backend.String(),
Path: cp,
Domain: opt.Domain,
Expires: opt.Expires,
MaxAge: opt.MaxAge,
Secure: opt.Secure,
HttpOnly: opt.HTTPOnly,
SameSite: opt.SameSite,
}
http.SetCookie(*w, cookie)
}

Expand Down
165 changes: 143 additions & 22 deletions roundrobin/stickysessions_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package roundrobin

import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -54,7 +56,7 @@ func TestBasic(t *testing.T) {
}
}

func TestStickCookie(t *testing.T) {
func TestStickyCookie(t *testing.T) {
a := testutils.NewResponder("a")
b := testutils.NewResponder("b")

Expand Down Expand Up @@ -86,39 +88,158 @@ func TestStickCookie(t *testing.T) {
assert.Equal(t, a.URL, cookie.Value)
}

func TestStickCookieWithOptions(t *testing.T) {
func TestStickyCookieWithOptions(t *testing.T) {
a := testutils.NewResponder("a")
b := testutils.NewResponder("b")

defer a.Close()
defer b.Close()

fwd, err := forward.New()
require.NoError(t, err)
testCases := []struct {
desc string
name string
options CookieOptions
expected *http.Cookie
}{
{
desc: "no options",
name: "test",
options: CookieOptions{},
expected: &http.Cookie{
Name: "test",
Value: a.URL,
Path: "/",
Raw: fmt.Sprintf("test=%s; Path=/", a.URL),
},
},
{
desc: "HTTPOnly",
name: "test",
options: CookieOptions{
HTTPOnly: true,
},
expected: &http.Cookie{
Name: "test",
Value: a.URL,
Path: "/",
HttpOnly: true,
Raw: fmt.Sprintf("test=%s; Path=/; HttpOnly", a.URL),
Unparsed: nil,
},
},
{
desc: "Secure",
name: "test",
options: CookieOptions{
Secure: true,
},
expected: &http.Cookie{
Name: "test",
Value: a.URL,
Path: "/",
Secure: true,
Raw: fmt.Sprintf("test=%s; Path=/; Secure", a.URL),
},
},
{
desc: "Path",
name: "test",
options: CookieOptions{
Path: "/foo",
},
expected: &http.Cookie{
Name: "test",
Value: a.URL,
Path: "/foo",
Raw: fmt.Sprintf("test=%s; Path=/foo", a.URL),
},
},
{
desc: "Domain",
name: "test",
options: CookieOptions{
Domain: "example.org",
},
expected: &http.Cookie{
Name: "test",
Value: a.URL,
Path: "/",
Domain: "example.org",
Raw: fmt.Sprintf("test=%s; Path=/; Domain=example.org", a.URL),
},
},
{
desc: "Expires",
name: "test",
options: CookieOptions{
Expires: time.Date(1955, 11, 12, 1, 22, 0, 0, time.UTC),
},
expected: &http.Cookie{
Name: "test",
Value: a.URL,
Path: "/",
Expires: time.Date(1955, 11, 12, 1, 22, 0, 0, time.UTC),
RawExpires: "Sat, 12 Nov 1955 01:22:00 GMT",
Raw: fmt.Sprintf("test=%s; Path=/; Expires=Sat, 12 Nov 1955 01:22:00 GMT", a.URL),
},
},
{
desc: "MaxAge",
name: "test",
options: CookieOptions{
MaxAge: -20,
},
expected: &http.Cookie{
Name: "test",
Value: a.URL,
Path: "/",
MaxAge: -1,
Raw: fmt.Sprintf("test=%s; Path=/; Max-Age=0", a.URL),
},
},
{
desc: "SameSite",
name: "test",
options: CookieOptions{
SameSite: http.SameSiteNoneMode,
},
expected: &http.Cookie{
Name: "test",
Value: a.URL,
Path: "/",
SameSite: http.SameSiteNoneMode,
Raw: fmt.Sprintf("test=%s; Path=/; SameSite=None", a.URL),
},
},
}

options := CookieOptions{HTTPOnly: true, Secure: true}
sticky := NewStickySessionWithOptions("test", options)
require.NotNil(t, sticky)
for _, test := range testCases {
t.Run(test.desc, func(t *testing.T) {

lb, err := New(fwd, EnableStickySession(sticky))
require.NoError(t, err)
fwd, err := forward.New()
require.NoError(t, err)

err = lb.UpsertServer(testutils.ParseURI(a.URL))
require.NoError(t, err)
err = lb.UpsertServer(testutils.ParseURI(b.URL))
require.NoError(t, err)
sticky := NewStickySessionWithOptions(test.name, test.options)
require.NotNil(t, sticky)

proxy := httptest.NewServer(lb)
defer proxy.Close()
lb, err := New(fwd, EnableStickySession(sticky))
require.NoError(t, err)

resp, err := http.Get(proxy.URL)
require.NoError(t, err)
err = lb.UpsertServer(testutils.ParseURI(a.URL))
require.NoError(t, err)
err = lb.UpsertServer(testutils.ParseURI(b.URL))
require.NoError(t, err)

cookie := resp.Cookies()[0]
assert.Equal(t, "test", cookie.Name)
assert.Equal(t, a.URL, cookie.Value)
assert.True(t, cookie.Secure)
assert.True(t, cookie.HttpOnly)
proxy := httptest.NewServer(lb)
defer proxy.Close()

resp, err := http.Get(proxy.URL)
require.NoError(t, err)

require.Len(t, resp.Cookies(), 1)
assert.Equal(t, test.expected, resp.Cookies()[0])
})
}
}

func TestRemoveRespondingServer(t *testing.T) {
Expand Down