RediSearch / redisearch-go

Go client for RediSearch

Home Page:https://redisearch.io

Repository from Github https://github.comRediSearch/redisearch-goRepository from Github https://github.comRediSearch/redisearch-go

Support for passing authentication options to redigo

joshuaray opened this issue · comments

Based on what I'm seeing in the documentation and the go code that generates the client, I'm not seeing any way to pass authentication options to the redigo library...is this an oversight on my part or are we just expected to use redisearch without securing the database?

I've tried all of the following when generating a redisearch client and each one gives errors regarding the format of the URL:

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

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

hi there @joshuaray, thank you for bringing up this subject since I believe severall users share the same doubt. As of version 0.9.1 we support all secure connections, pool sharing, etc, via the NewClientFromPool. You can dial with TLS, password, or any specific config of the vanilla go client, and then use that pool when create one or more search clients.
We will add examples of different types of secured connection setup to the documentation =)

Here is a quick example on how to test with docker and go:

docker run redisearch with password

docker run -p 6379:6379 redislabs/redisearch:edge --requirepass supersecret --loadmodule /usr/lib/redis/modules/redisearch.so

example of creating a secured client connection

package main

import (
"fmt"
redisearch "github.com/RediSearch/redisearch-go/redisearch"
"github.com/gomodule/redigo/redis"
"log"
"time"
)

// exemplifies the NewClientFromPool function
func main() {
	host := "localhost:6379"
	password := "supersecret"
	pool := &redis.Pool{Dial: func() (redis.Conn, error) {
		return redis.Dial("tcp", host, redis.DialPassword(password))
	}}
	c := redisearch.NewClientFromPool(pool, "search-client-1")

	// Create a schema
	sc := redisearch.NewSchema(redisearch.DefaultOptions).
		AddField(redisearch.NewTextField("body")).
		AddField(redisearch.NewTextFieldOptions("title", redisearch.TextFieldOptions{Weight: 5.0, Sortable: true})).
		AddField(redisearch.NewNumericField("date"))

	// Drop an existing index. If the index does not exist an error is returned
	c.Drop()

	// Create the index with the given schema
	if err := c.CreateIndex(sc); err != nil {
		log.Fatal(err)
	}

	// Create a document with an id and given score
	doc := redisearch.NewDocument("doc1", 1.0)
	doc.Set("title", "Hello world").
		Set("body", "foo bar").
		Set("date", time.Now().Unix())

	// Index the document. The API accepts multiple documents at a time
	if err := c.Index([]redisearch.Document{doc}...); err != nil {
		log.Fatal(err)
	}

	// Searching with limit and sorting
	docs, total, err := c.Search(redisearch.NewQuery("hello world").
		Limit(0, 2).
		SetReturnFields("title"))

	fmt.Println(docs[0].Id, docs[0].Properties["title"], total, err)
	// Output: doc1 Hello world 1 <nil>
}