Add redis-url and redis-insecure-tls command line options

A URL can contain all of the information in the individual flags as a
single string rather than as separate pieces. I've mostly run into URLs
pointing to Redis and this make those existing URLs easier to work with.

This also introduces the -redis-insecure-tls flag which turns off TLS
certificate hostname verification. We've chosen Heroku Redis for a
recent project which requires TLS but, for reasons I don't know, they
don't provide a certificate that is valid for the hostname. I also
wasn't able to get the existing -redis-tls flag to work.
This commit is contained in:
Chris Gaffney 2021-04-23 22:58:32 -04:00 committed by Ken Hibino
parent 034026507b
commit 5cd001daee
2 changed files with 53 additions and 23 deletions

64
main.go
View File

@ -26,14 +26,18 @@ var (
flagRedisDB int
flagRedisPassword string
flagRedisTLS string
flagRedisURL string
flagRedisInsecureTLS bool
)
func init() {
flag.IntVar(&flagPort, "port", 8080, "port number to use for web ui server")
flag.StringVar(&flagRedisAddr, "redis_addr", "127.0.0.1:6379", "address of redis server to connect to")
flag.IntVar(&flagRedisDB, "redis_db", 0, "redis database number")
flag.StringVar(&flagRedisPassword, "redis_password", "", "password to use when connecting to redis server")
flag.StringVar(&flagRedisTLS, "redis_tls", "", "server name for TLS validation used when connecting to redis server")
flag.StringVar(&flagRedisAddr, "redis-addr", "127.0.0.1:6379", "address of redis server to connect to")
flag.IntVar(&flagRedisDB, "redis-db", 0, "redis database number")
flag.StringVar(&flagRedisPassword, "redis-password", "", "password to use when connecting to redis server")
flag.StringVar(&flagRedisTLS, "redis-tls", "", "server name for TLS validation used when connecting to redis server")
flag.StringVar(&flagRedisURL, "redis-url", "", "URL to redis server")
flag.BoolVar(&flagRedisInsecureTLS, "redis-insecure-tls", false, "Disable TLS certificate host checks")
}
// staticFileServer implements the http.Handler interface, so we can use it
@ -85,31 +89,57 @@ func (srv *staticFileServer) indexFilePath() string {
return filepath.Join(srv.staticDirPath, srv.indexFileName)
}
func getRedisOptionsFromFlags() (*redis.Options, error) {
var err error
var opts *redis.Options
if flagRedisURL != "" {
opts, err = redis.ParseURL(flagRedisURL)
if err != nil {
return nil, err
}
} else {
opts = &redis.Options{
Addr: flagRedisAddr,
DB: flagRedisDB,
Password: flagRedisPassword,
TLSConfig: &tls.Config{},
}
}
if tls := opts.TLSConfig; tls != nil {
if tlsHost := flagRedisTLS; tlsHost != "" {
tls.ServerName = tlsHost
}
if flagRedisInsecureTLS {
tls.InsecureSkipVerify = true
}
}
return opts, nil
}
//go:embed ui/build/*
var staticContents embed.FS
func main() {
flag.Parse()
var tlsConfig *tls.Config
if flagRedisTLS != "" {
tlsConfig = &tls.Config{ServerName: flagRedisTLS}
opts, err := getRedisOptionsFromFlags()
if err != nil {
log.Fatal(err)
}
inspector := inspeq.New(asynq.RedisClientOpt{
Addr: flagRedisAddr,
DB: flagRedisDB,
Password: flagRedisPassword,
TLSConfig: tlsConfig,
Addr: opts.Addr,
DB: opts.DB,
Password: opts.Password,
TLSConfig: opts.TLSConfig,
})
defer inspector.Close()
rdb := redis.NewClient(&redis.Options{
Addr: flagRedisAddr,
DB: flagRedisDB,
Password: flagRedisPassword,
TLSConfig: tlsConfig,
})
rdb := redis.NewClient(opts)
defer rdb.Close()
router := mux.NewRouter()

View File

@ -30,7 +30,7 @@ func newRedisInfoHandlerFunc(rdb *redis.Client) http.HandlerFunc {
}
info := parseRedisInfo(res)
resp := RedisInfoResponse{
Addr: flagRedisAddr,
Addr: rdb.Options().Addr,
Info: info,
RawInfo: res,
}