简体   繁体   中英

How to provide a password to the redigo client in redisearch-go (Golang)

I'm trying to use Go to connect to my RediSearch (Redis module) instance.

Combing through the documentation and Go code that generates the client, I don't see how to pass authentication options to the Redigo client within... Is this an oversight on my part or are we just expected to use RediSearch without securing the database?

I've also tried the following when generating a RediSearch client. Each gives errors regarding the format of the URL:

redisearch.NewClient("mypasswordhere@10.10.10.50:6379", index)
redisearch.NewClient("redis://mypasswordhere@10.10.10.50:6379", index)

Within pool.go , in the NewSingleHostPool function that returns a client, it seems like it should pass DialOptions into the Dial Redigo function instead of nil , and have those options be passed into the RediSearch NewClient function...

Call NewClientWithPool with a pool dial function of your choice.

p := &redis.Pool{
    Dial: func() (*Conn, error) { 
       return redis.DialURL("redis://mypasswordhere@10.10.10.50:6379") 
    },
    MaxIdle: 3,                     // adjust to taste 
    IdleTimeout: 240 * time.Second, // adjust to taste

}
c := redissearch.NewClientFromPool(p, index)

A nice way to do this is like so, with built-in constructs for password auth by redigo:

func newPool(server string, password string) *redis.Pool {
    return &redis.Pool{
        MaxIdle:     3,
        IdleTimeout: 240 * time.Second,
        Dial: func() (redis.Conn, error) {
            return redis.Dial("tcp",
                server,
                redis.DialPassword(password))
        },

        TestOnBorrow: func(c redis.Conn, t time.Time) error {
            _, err := c.Do("PING")
            return err
        },
    }
}

Your server should look like this: redis-1344.333.us-east-1-mz.ec2.com:17885 and the password is plain text.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM