Merge pull request #309 from lleszczu/customCaForRedis
Add support for Redis with custom CA.
This commit is contained in:
		
						commit
						9f920b0fc1
					
				|  | @ -12,7 +12,7 @@ | ||||||
| - [#302](https://github.com/pusher/oauth2_proxy/pull/302) Rewrite dist script (@syscll) | - [#302](https://github.com/pusher/oauth2_proxy/pull/302) Rewrite dist script (@syscll) | ||||||
| - [#304](https://github.com/pusher/oauth2_proxy/pull/304) Add new Logo! :tada: (@JoelSpeed) | - [#304](https://github.com/pusher/oauth2_proxy/pull/304) Add new Logo! :tada: (@JoelSpeed) | ||||||
| - [#300](https://github.com/pusher/oauth2_proxy/pull/300) Added userinfo endpoint (@kbabuadze) | - [#300](https://github.com/pusher/oauth2_proxy/pull/300) Added userinfo endpoint (@kbabuadze) | ||||||
| 
 | - [#309](https://github.com/pusher/oauth2_proxy/pull/309) Added support for custom CA when connecting to Redis cache | ||||||
| # v4.0.0 | # v4.0.0 | ||||||
| 
 | 
 | ||||||
| - [#248](https://github.com/pusher/oauth2_proxy/pull/248) Fix issue with X-Auth-Request-Redirect header being ignored | - [#248](https://github.com/pusher/oauth2_proxy/pull/248) Fix issue with X-Auth-Request-Redirect header being ignored | ||||||
|  |  | ||||||
							
								
								
									
										2
									
								
								main.go
								
								
								
								
							
							
						
						
									
										2
									
								
								main.go
								
								
								
								
							|  | @ -91,6 +91,8 @@ func main() { | ||||||
| 	flagSet.String("redis-connection-url", "", "URL of redis server for redis session storage (eg: redis://HOST[:PORT])") | 	flagSet.String("redis-connection-url", "", "URL of redis server for redis session storage (eg: redis://HOST[:PORT])") | ||||||
| 	flagSet.Bool("redis-use-sentinel", false, "Connect to redis via sentinels. Must set --redis-sentinel-master-name and --redis-sentinel-connection-urls to use this feature") | 	flagSet.Bool("redis-use-sentinel", false, "Connect to redis via sentinels. Must set --redis-sentinel-master-name and --redis-sentinel-connection-urls to use this feature") | ||||||
| 	flagSet.String("redis-sentinel-master-name", "", "Redis sentinel master name. Used in conjunction with --redis-use-sentinel") | 	flagSet.String("redis-sentinel-master-name", "", "Redis sentinel master name. Used in conjunction with --redis-use-sentinel") | ||||||
|  | 	flagSet.String("redis-ca-path", "", "Redis custom CA path") | ||||||
|  | 	flagSet.Bool("redis-insecure-skip-tls-verify", false, "Use insecure TLS connection to redis") | ||||||
| 	flagSet.Var(&redisSentinelConnectionURLs, "redis-sentinel-connection-urls", "List of Redis sentinel connection URLs (eg redis://HOST[:PORT]). Used in conjunction with --redis-use-sentinel") | 	flagSet.Var(&redisSentinelConnectionURLs, "redis-sentinel-connection-urls", "List of Redis sentinel connection URLs (eg redis://HOST[:PORT]). Used in conjunction with --redis-use-sentinel") | ||||||
| 
 | 
 | ||||||
| 	flagSet.String("logging-filename", "", "File to log requests to, empty for stdout") | 	flagSet.String("logging-filename", "", "File to log requests to, empty for stdout") | ||||||
|  |  | ||||||
|  | @ -27,4 +27,6 @@ type RedisStoreOptions struct { | ||||||
| 	UseSentinel            bool     `flag:"redis-use-sentinel" cfg:"redis_use_sentinel" env:"OAUTH2_PROXY_REDIS_USE_SENTINEL"` | 	UseSentinel            bool     `flag:"redis-use-sentinel" cfg:"redis_use_sentinel" env:"OAUTH2_PROXY_REDIS_USE_SENTINEL"` | ||||||
| 	SentinelMasterName     string   `flag:"redis-sentinel-master-name" cfg:"redis_sentinel_master_name" env:"OAUTH2_PROXY_REDIS_SENTINEL_MASTER_NAME"` | 	SentinelMasterName     string   `flag:"redis-sentinel-master-name" cfg:"redis_sentinel_master_name" env:"OAUTH2_PROXY_REDIS_SENTINEL_MASTER_NAME"` | ||||||
| 	SentinelConnectionURLs []string `flag:"redis-sentinel-connection-urls" cfg:"redis_sentinel_connection_urls" env:"OAUTH2_PROXY_REDIS_SENTINEL_CONNECTION_URLS"` | 	SentinelConnectionURLs []string `flag:"redis-sentinel-connection-urls" cfg:"redis_sentinel_connection_urls" env:"OAUTH2_PROXY_REDIS_SENTINEL_CONNECTION_URLS"` | ||||||
|  | 	RedisCAPath            string   `flag:"redis-ca-path" cfg:"redis_ca_path" env:"OAUTH2_PROXY_REDIS_CA_PATH"` | ||||||
|  | 	RedisInsecureTLS       bool     `flag:"redis-insecure-skip-tls-verify" cfg:"redis_insecure_skip_tls_verify" env:"OAUTH2_PROXY_REDIS_INSECURE_SKIP_TLS_VERIFY"` | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -4,10 +4,12 @@ import ( | ||||||
| 	"crypto/aes" | 	"crypto/aes" | ||||||
| 	"crypto/cipher" | 	"crypto/cipher" | ||||||
| 	"crypto/rand" | 	"crypto/rand" | ||||||
|  | 	"crypto/x509" | ||||||
| 	"encoding/base64" | 	"encoding/base64" | ||||||
| 	"encoding/hex" | 	"encoding/hex" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"io" | 	"io" | ||||||
|  | 	"io/ioutil" | ||||||
| 	"net/http" | 	"net/http" | ||||||
| 	"strings" | 	"strings" | ||||||
| 	"time" | 	"time" | ||||||
|  | @ -17,6 +19,7 @@ import ( | ||||||
| 	"github.com/pusher/oauth2_proxy/pkg/apis/sessions" | 	"github.com/pusher/oauth2_proxy/pkg/apis/sessions" | ||||||
| 	"github.com/pusher/oauth2_proxy/pkg/cookies" | 	"github.com/pusher/oauth2_proxy/pkg/cookies" | ||||||
| 	"github.com/pusher/oauth2_proxy/pkg/encryption" | 	"github.com/pusher/oauth2_proxy/pkg/encryption" | ||||||
|  | 	"github.com/pusher/oauth2_proxy/pkg/logger" | ||||||
| ) | ) | ||||||
| 
 | 
 | ||||||
| // TicketData is a structure representing the ticket used in server session storage
 | // TicketData is a structure representing the ticket used in server session storage
 | ||||||
|  | @ -64,6 +67,31 @@ func newRedisClient(opts options.RedisStoreOptions) (*redis.Client, error) { | ||||||
| 		return nil, fmt.Errorf("unable to parse redis url: %s", err) | 		return nil, fmt.Errorf("unable to parse redis url: %s", err) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 	if opts.RedisInsecureTLS != false { | ||||||
|  | 		opt.TLSConfig.InsecureSkipVerify = true | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	if opts.RedisCAPath != "" { | ||||||
|  | 		rootCAs, err := x509.SystemCertPool() | ||||||
|  | 		if err != nil { | ||||||
|  | 			logger.Printf("failed to load system cert pool for redis connection, falling back to empty cert pool") | ||||||
|  | 		} | ||||||
|  | 		if rootCAs == nil { | ||||||
|  | 			rootCAs = x509.NewCertPool() | ||||||
|  | 		} | ||||||
|  | 		certs, err := ioutil.ReadFile(opts.RedisCAPath) | ||||||
|  | 		if err != nil { | ||||||
|  | 			return nil, fmt.Errorf("failed to load %q, %v", opts.RedisCAPath, err) | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 		// Append our cert to the system pool
 | ||||||
|  | 		if ok := rootCAs.AppendCertsFromPEM(certs); !ok { | ||||||
|  | 			logger.Printf("no certs appended, using system certs only") | ||||||
|  | 		} | ||||||
|  | 
 | ||||||
|  | 		opt.TLSConfig.RootCAs = rootCAs | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	client := redis.NewClient(opt) | 	client := redis.NewClient(opt) | ||||||
| 	return client, nil | 	return client, nil | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue