From 1fdbfa47197d52aac3eed5e92269ca2f30186028 Mon Sep 17 00:00:00 2001
From: vodolaz095 <ostroumov095@gmail.com>
Date: Tue, 27 Jun 2017 00:29:48 +0300
Subject: [PATCH] Add support for RethinkDB 1.0 handshake protocol (#2963)

Allow rethinkdb input plugin to work with RethinkDB 2.3.5+ databases that requires username,password authorization and Handshake protocol v1.0

* remove top level header not required in sample config

* remove top level header not required in sample config
---
 Godeps                                       |  2 +-
 etc/telegraf.conf                            | 11 +++++++
 plugins/inputs/rethinkdb/rethinkdb.go        | 20 ++++++++++++-
 plugins/inputs/rethinkdb/rethinkdb_server.go |  2 +-
 plugins/inputs/rethinkdb/rethinkdb_test.go   | 30 ++++++++++++++------
 5 files changed, 54 insertions(+), 11 deletions(-)

diff --git a/Godeps b/Godeps
index 7d00bb78..a9199e3e 100644
--- a/Godeps
+++ b/Godeps
@@ -59,8 +59,8 @@ github.com/zensqlmonitor/go-mssqldb ffe5510c6fa5e15e6d983210ab501c815b56b363
 golang.org/x/crypto dc137beb6cce2043eb6b5f223ab8bf51c32459f4
 golang.org/x/net f2499483f923065a842d38eb4c7f1927e6fc6e6d
 golang.org/x/text 506f9d5c962f284575e88337e7d9296d27e729d3
-gopkg.in/dancannon/gorethink.v1 edc7a6a68e2d8015f5ffe1b2560eed989f8a45be
 gopkg.in/fatih/pool.v2 6e328e67893eb46323ad06f0e92cb9536babbabc
+gopkg.in/gorethink/gorethink.v3 7ab832f7b65573104a555d84a27992ae9ea1f659
 gopkg.in/mgo.v2 3f83fa5005286a7fe593b055f0d7771a7dce4655
 gopkg.in/olivere/elastic.v5 ee3ebceab960cf68ab9a89ee6d78c031ef5b4a4e
 gopkg.in/yaml.v2 4c78c975fe7c825c6d1466c42be594d1d6f3aba6
diff --git a/etc/telegraf.conf b/etc/telegraf.conf
index c7691666..a02c6a12 100644
--- a/etc/telegraf.conf
+++ b/etc/telegraf.conf
@@ -1851,6 +1851,17 @@
 #   ##   rethinkdb://10.10.3.33:18832,
 #   ##   10.0.0.1:10000, etc.
 #   servers = ["127.0.0.1:28015"]
+#   ##
+#   ## If you use actual rethinkdb of > 2.3.0 with username/password authorization,
+#   ## protocol have to be named "rethinkdb2" - it will use 1_0 H.
+#   servers = ["rethinkdb2://username:password@127.0.0.1:28015"]
+#   ##
+#   ## If you use older versions of rethinkdb (<2.2) with auth_key, protocol
+#   ## have to be named "rethinkdb".
+#   servers = ["rethinkdb://username:auth_key@127.0.0.1:28015"]
+
+
+
 
 
 # # Read metrics one or many Riak servers
diff --git a/plugins/inputs/rethinkdb/rethinkdb.go b/plugins/inputs/rethinkdb/rethinkdb.go
index e32f7973..dc6b0362 100644
--- a/plugins/inputs/rethinkdb/rethinkdb.go
+++ b/plugins/inputs/rethinkdb/rethinkdb.go
@@ -8,7 +8,7 @@ import (
 	"github.com/influxdata/telegraf"
 	"github.com/influxdata/telegraf/plugins/inputs"
 
-	"gopkg.in/dancannon/gorethink.v1"
+	"gopkg.in/gorethink/gorethink.v3"
 )
 
 type RethinkDB struct {
@@ -22,6 +22,14 @@ var sampleConfig = `
   ##   rethinkdb://10.10.3.33:18832,
   ##   10.0.0.1:10000, etc.
   servers = ["127.0.0.1:28015"]
+  ##
+  ## If you use actual rethinkdb of > 2.3.0 with username/password authorization,
+  ## protocol have to be named "rethinkdb2" - it will use 1_0 H.
+  # servers = ["rethinkdb2://username:password@127.0.0.1:28015"]
+  ##
+  ## If you use older versions of rethinkdb (<2.2) with auth_key, protocol
+  ## have to be named "rethinkdb".
+  # servers = ["rethinkdb://username:auth_key@127.0.0.1:28015"]
 `
 
 func (r *RethinkDB) SampleConfig() string {
@@ -75,8 +83,18 @@ func (r *RethinkDB) gatherServer(server *Server, acc telegraf.Accumulator) error
 		pwd, set := server.Url.User.Password()
 		if set && pwd != "" {
 			connectOpts.AuthKey = pwd
+			connectOpts.HandshakeVersion = gorethink.HandshakeV0_4
 		}
 	}
+	if server.Url.Scheme == "rethinkdb2" && server.Url.User != nil {
+		pwd, set := server.Url.User.Password()
+		if set && pwd != "" {
+			connectOpts.Username = server.Url.User.Username()
+			connectOpts.Password = pwd
+			connectOpts.HandshakeVersion = gorethink.HandshakeV1_0
+		}
+	}
+
 	server.session, err = gorethink.Connect(connectOpts)
 	if err != nil {
 		return fmt.Errorf("Unable to connect to RethinkDB, %s\n", err.Error())
diff --git a/plugins/inputs/rethinkdb/rethinkdb_server.go b/plugins/inputs/rethinkdb/rethinkdb_server.go
index f172717d..a04206d3 100644
--- a/plugins/inputs/rethinkdb/rethinkdb_server.go
+++ b/plugins/inputs/rethinkdb/rethinkdb_server.go
@@ -11,7 +11,7 @@ import (
 
 	"github.com/influxdata/telegraf"
 
-	"gopkg.in/dancannon/gorethink.v1"
+	"gopkg.in/gorethink/gorethink.v3"
 )
 
 type Server struct {
diff --git a/plugins/inputs/rethinkdb/rethinkdb_test.go b/plugins/inputs/rethinkdb/rethinkdb_test.go
index 85c747f4..fa2cc92f 100644
--- a/plugins/inputs/rethinkdb/rethinkdb_test.go
+++ b/plugins/inputs/rethinkdb/rethinkdb_test.go
@@ -10,10 +10,10 @@ import (
 	"testing"
 	"time"
 
-	"gopkg.in/dancannon/gorethink.v1"
+	"gopkg.in/gorethink/gorethink.v3"
 )
 
-var connect_url, authKey string
+var connect_url, authKey, username, password string
 var server *Server
 
 func init() {
@@ -22,17 +22,31 @@ func init() {
 		connect_url = "127.0.0.1:28015"
 	}
 	authKey = os.Getenv("RETHINKDB_AUTHKEY")
-
+	username = os.Getenv("RETHINKDB_USERNAME")
+	password = os.Getenv("RETHINKDB_PASSWORD")
 }
 
 func testSetup(m *testing.M) {
 	var err error
 	server = &Server{Url: &url.URL{Host: connect_url}}
-	server.session, _ = gorethink.Connect(gorethink.ConnectOpts{
-		Address:       server.Url.Host,
-		AuthKey:       authKey,
-		DiscoverHosts: false,
-	})
+
+	if authKey {
+		server.session, _ = gorethink.Connect(gorethink.ConnectOpts{
+			Address:          server.Url.Host,
+			AuthKey:          authKey,
+			HandshakeVersion: gorethink.HandshakeV0_4,
+			DiscoverHosts:    false,
+		})
+	} else {
+		server.session, _ = gorethink.Connect(gorethink.ConnectOpts{
+			Address:          server.Url.Host,
+			Username:         username,
+			Password:         password,
+			HandshakeVersion: gorethink.HandshakeV1_0,
+			DiscoverHosts:    false,
+		})
+	}
+
 	if err != nil {
 		log.Fatalln(err.Error())
 	}
-- 
GitLab