git.iko.soy/typeable/oauth2-proxy
on master help sign in
Add Redis sentinel compatibility

(cherry picked from commit ff36b61f8cee4ecf0b91a90b5e1b651b526bb6b6)

commit518c1d3e8ed0fbebac48074518314acc5e65e6bc authorJoel Speed <Joel.speed@hotmail.co.uk> dateFri 24 May 2019, 17:32 (7 years ago) parentfc06e2d
3 files changed+29−6
main.go+5−1 pkg/apis/options/sessions.go+4−1 pkg/sessions/redis/redis_store.go+20−4
main.go+5−1
@@ -24,6 +24,7 @@ func main() {
2424 upstreams := StringArray{}
2525 skipAuthRegex := StringArray{}
2626 googleGroups := StringArray{}
27+ redisSentinelConnectionURLs := StringArray{}
2728
2829 config := flagSet.String("config", "", "path to config file")
2930 showVersion := flagSet.Bool("version", false, "print version string")
@@ -76,7 +77,10 @@ func main() {
7677 flagSet.Bool("cookie-httponly", true, "set HttpOnly cookie flag")
7778
7879 flagSet.String("session-store-type", "cookie", "the session storage provider to use")
79− flagSet.String("redis-connection-url", "", "URL of redis server for redis session storage type (eg: redis://HOST[:PORT])")
80+ flagSet.String("redis-connection-url", "", "URL of redis server for redis session storage (eg: redis://HOST[:PORT])")
81+ flagSet.Bool("redis-use-sentinel", false, "Connect to redis via sentinels. Must set --redis-sentinel-master-name and --redis-sentinel-conneciton-urls to use this feature")
82+ flagSet.String("redis-sentinel-master-name", "", "Redis sentinel master name. Used in conjuction with --redis-use-sentinel")
83+ flagSet.Var(&redisSentinelConnectionURLs, "redis-sentinel-connection-urls", "List of Redis sentinel conneciton URLs (eg redis://HOST[:PORT]). Used in conjuction with --redis-use-sentinel")
8084
8185 flagSet.String("logging-filename", "", "File to log requests to, empty for stdout")
8286 flagSet.Int("logging-max-size", 100, "Maximum size in megabytes of the log file before rotation")
pkg/apis/options/sessions.go+4−1
@@ -25,5 +25,8 @@ var RedisSessionStoreType = "redis"
2525
2626// RedisStoreOptions contains configuration options for the RedisSessionStore.
2727type RedisStoreOptions struct {
28− RedisConnectionURL string `flag:"redis-connection-url" cfg:"redis_connection_url" env:"OAUTH2_PROXY_REDIS_CONNECTION_URL"`
28+ RedisConnectionURL string `flag:"redis-connection-url" cfg:"redis_connection_url" env:"OAUTH2_PROXY_REDIS_CONNECTION_URL"`
29+ UseSentinel bool `flag:"redis-use-sentinel" cfg:"redis_use_sentinel" env:"OAUTH2_PROXY_REDIS_USE_SENTINEL"`
30+ SentinelMasterName string `flag:"redis-sentinel-master-name" cfg:"redis_sentinel_master_name" env:"OAUTH2_PROXY_REDIS_SENTINEL_MASTER_NAME"`
31+ SentinelConnectionURLs []string `flag:"redis-sentinel-connection-urls" cfg:"redis_sentinel_connection_urls" env:"OAUTH2_PROXY_REDIS_SENTINEL_CONNECTION_URLS"`
2932}
pkg/sessions/redis/redis_store.go+20−4
@@ -36,13 +36,11 @@ type SessionStore struct {
3636// NewRedisSessionStore initialises a new instance of the SessionStore from
3737// the configuration given
3838func NewRedisSessionStore(opts *options.SessionOptions, cookieOpts *options.CookieOptions) (sessions.SessionStore, error) {
39− opt, err := redis.ParseURL(opts.RedisStoreOptions.RedisConnectionURL)
39+ client, err := newRedisClient(opts.RedisStoreOptions)
4040 if err != nil {
41− return nil, fmt.Errorf("unable to parse redis url: %s", err)
41+ return nil, fmt.Errorf("error constructing redis client: %v", err)
4242 }
4343
44− client := redis.NewClient(opt)
45−
4644 rs := &SessionStore{
4745 Client: client,
4846 CookieCipher: opts.Cipher,
@@ -52,6 +50,24 @@ func NewRedisSessionStore(opts *options.SessionOptions, cookieOpts *options.Cook
5250
5351}
5452
53+func newRedisClient(opts options.RedisStoreOptions) (*redis.Client, error) {
54+ if opts.UseSentinel {
55+ client := redis.NewFailoverClient(&redis.FailoverOptions{
56+ MasterName: opts.SentinelMasterName,
57+ SentinelAddrs: opts.SentinelConnectionURLs,
58+ })
59+ return client, nil
60+ }
61+
62+ opt, err := redis.ParseURL(opts.RedisConnectionURL)
63+ if err != nil {
64+ return nil, fmt.Errorf("unable to parse redis url: %s", err)
65+ }
66+
67+ client := redis.NewClient(opt)
68+ return client, nil
69+}
70+
5571// Save takes a sessions.SessionState and stores the information from it
5672// to redies, and adds a new ticket cookie on the HTTP response writer
5773func (store *SessionStore) Save(rw http.ResponseWriter, req *http.Request, s *sessions.SessionState) error {