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

Adds an Obfuscator interface, and implements a default GIGO obfuscator #156

Closed
wants to merge 6 commits into from
Closed
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
23 changes: 23 additions & 0 deletions roundrobin/obfuscator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package roundrobin

// Obfuscator is an interface you can pass to NewStickySessionWithObfuscator,
// to encode/encrypt/jumble/whatever your StickySession values
type Obfuscator interface {
// Obfuscate takes a raw string and returns the obfuscated value
Obfuscate(string) string
// Normalize takes an obfuscated string and returns the raw value
Normalize(string) string
}

// DefaultObfuscator is a no-op that returns the raw/obfuscated strings as-is
type DefaultObfuscator struct{}

// Obfuscate takes a raw string and returns the obfuscated value
func (o *DefaultObfuscator) Obfuscate(raw string) string {
return raw
}

// Normalize takes an obfuscated string and returns the raw value
func (o *DefaultObfuscator) Normalize(obfuscated string) string {
return obfuscated
}
22 changes: 18 additions & 4 deletions roundrobin/stickysessions.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ type StickySession struct {

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

// NewStickySession creates a new StickySession
Expand All @@ -39,7 +40,13 @@ func (s *StickySession) GetBackend(req *http.Request, servers []*url.URL) (*url.
return nil, false, err
}

serverURL, err := url.Parse(cookie.Value)
cookieValue := cookie.Value
if s.options.Obfuscator != nil {
// We have an Obfuscator, let's use it
cookieValue = s.options.Obfuscator.Normalize(cookieValue)
}

serverURL, err := url.Parse(cookieValue)
if err != nil {
return nil, false, err
}
Expand All @@ -53,7 +60,14 @@ 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}

cookieValue := backend.String()
if opt.Obfuscator != nil {
// We have an Obfuscator, let's use it
opt.Obfuscator.Obfuscate(cookieValue)
}

cookie := &http.Cookie{Name: s.cookieName, Value: cookieValue, Path: "/", HttpOnly: opt.HTTPOnly, Secure: opt.Secure}
http.SetCookie(*w, cookie)
}

Expand Down