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

74
main.go
View File

@ -21,19 +21,23 @@ import (
// Command-line flags // Command-line flags
var ( var (
flagPort int flagPort int
flagRedisAddr string flagRedisAddr string
flagRedisDB int flagRedisDB int
flagRedisPassword string flagRedisPassword string
flagRedisTLS string flagRedisTLS string
flagRedisURL string
flagRedisInsecureTLS bool
) )
func init() { func init() {
flag.IntVar(&flagPort, "port", 8080, "port number to use for web ui server") 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.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.IntVar(&flagRedisDB, "redis-db", 0, "redis database number")
flag.StringVar(&flagRedisPassword, "redis_password", "", "password to use when connecting to redis server") 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(&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 // 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) 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/* //go:embed ui/build/*
var staticContents embed.FS var staticContents embed.FS
func main() { func main() {
flag.Parse() flag.Parse()
var tlsConfig *tls.Config opts, err := getRedisOptionsFromFlags()
if flagRedisTLS != "" { if err != nil {
tlsConfig = &tls.Config{ServerName: flagRedisTLS} log.Fatal(err)
} }
inspector := inspeq.New(asynq.RedisClientOpt{ inspector := inspeq.New(asynq.RedisClientOpt{
Addr: flagRedisAddr, Addr: opts.Addr,
DB: flagRedisDB, DB: opts.DB,
Password: flagRedisPassword, Password: opts.Password,
TLSConfig: tlsConfig, TLSConfig: opts.TLSConfig,
}) })
defer inspector.Close() defer inspector.Close()
rdb := redis.NewClient(&redis.Options{ rdb := redis.NewClient(opts)
Addr: flagRedisAddr,
DB: flagRedisDB,
Password: flagRedisPassword,
TLSConfig: tlsConfig,
})
defer rdb.Close() defer rdb.Close()
router := mux.NewRouter() router := mux.NewRouter()

View File

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