From 0d9cb3295e4fbcb50e226123435bd373d1c32312 Mon Sep 17 00:00:00 2001 From: Rodrigo Villablanca Date: Sun, 9 Feb 2020 17:26:41 -0300 Subject: [PATCH 01/16] Using specific pass for each host in redis output --- libbeat/outputs/redis/redis.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/libbeat/outputs/redis/redis.go b/libbeat/outputs/redis/redis.go index 97a6b9fdafa..deff72cd394 100644 --- a/libbeat/outputs/redis/redis.go +++ b/libbeat/outputs/redis/redis.go @@ -19,6 +19,9 @@ package redis import ( "errors" + "fmt" + "net/url" + "strings" "time" "github.com/elastic/beats/libbeat/beat" @@ -129,8 +132,23 @@ func makeRedis( return outputs.Fail(err) } + pass := config.Password + if parts := strings.SplitN(host, "://", 2); len(parts) != 2 { + // Add scheme. + host = fmt.Sprintf("redis://%s", host) + } + url, err := url.Parse(host) + if err != nil { + return outputs.Fail(err) + } + + hostPass, passSet := url.User.Password() + if passSet { + pass = hostPass + } + client := newClient(conn, observer, config.Timeout, - config.Password, config.Db, key, dataType, config.Index, enc) + pass, config.Db, key, dataType, config.Index, enc) clients[i] = newBackoffClient(client, config.Backoff.Init, config.Backoff.Max) } From 02fb2b1c8bb9a851bbcfaff505a2c11c22676a48 Mon Sep 17 00:00:00 2001 From: Rodrigo Villablanca Date: Wed, 12 Feb 2020 23:47:36 -0300 Subject: [PATCH 02/16] Partials advances parsing redis scheme --- libbeat/outputs/redis/redis.go | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/libbeat/outputs/redis/redis.go b/libbeat/outputs/redis/redis.go index deff72cd394..bec4319cd3e 100644 --- a/libbeat/outputs/redis/redis.go +++ b/libbeat/outputs/redis/redis.go @@ -45,6 +45,8 @@ const ( defaultWaitRetry = 1 * time.Second defaultMaxWaitRetry = 60 * time.Second defaultPort = 6379 + redisScheme = "redis://" + tlsRedisScheme = "rediss://" ) func init() { @@ -122,31 +124,39 @@ func makeRedis( clients := make([]outputs.NetworkClient, len(hosts)) for i, host := range hosts { - enc, err := codec.CreateEncoder(beat, config.Codec) - if err != nil { - return outputs.Fail(err) + if parts := strings.SplitN(host, "://", 2); len(parts) != 2 { + host = redisScheme + host } - conn, err := transport.NewClient(transp, "tcp", host, defaultPort) + url, err := url.Parse(host) if err != nil { return outputs.Fail(err) } - pass := config.Password - if parts := strings.SplitN(host, "://", 2); len(parts) != 2 { - // Add scheme. - host = fmt.Sprintf("redis://%s", host) + if url.Scheme != redisScheme && url.Scheme != tlsRedisScheme { + return outputs.Fail(fmt.Errorf("invalid redis url scheme %s", url.Scheme)) } - url, err := url.Parse(host) + + if url.Scheme == tlsRedisScheme { + // @todo how can I force tls if there is no tls config :thinking: + } + + conn, err := transport.NewClient(transp, "tcp", host, defaultPort) if err != nil { return outputs.Fail(err) } + pass := config.Password hostPass, passSet := url.User.Password() if passSet { pass = hostPass } + enc, err := codec.CreateEncoder(beat, config.Codec) + if err != nil { + return outputs.Fail(err) + } + client := newClient(conn, observer, config.Timeout, pass, config.Db, key, dataType, config.Index, enc) clients[i] = newBackoffClient(client, config.Backoff.Init, config.Backoff.Max) From abb0df46ae100a5b971a90ca797b8d5241814098 Mon Sep 17 00:00:00 2001 From: Rodrigo Villablanca Date: Wed, 12 Feb 2020 23:55:53 -0300 Subject: [PATCH 03/16] Validate tls config when schema is 'rediss' --- libbeat/outputs/redis/redis.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libbeat/outputs/redis/redis.go b/libbeat/outputs/redis/redis.go index bec4319cd3e..6a728ec39a4 100644 --- a/libbeat/outputs/redis/redis.go +++ b/libbeat/outputs/redis/redis.go @@ -137,8 +137,8 @@ func makeRedis( return outputs.Fail(fmt.Errorf("invalid redis url scheme %s", url.Scheme)) } - if url.Scheme == tlsRedisScheme { - // @todo how can I force tls if there is no tls config :thinking: + if url.Scheme == tlsRedisScheme && transp.TLS == nil { + return outputs.Fail(errors.New("using 'rediss' scheme requires a tls config")) } conn, err := transport.NewClient(transp, "tcp", host, defaultPort) From 3f23756cd728efc1448d235f11b022025041fce3 Mon Sep 17 00:00:00 2001 From: Rodrigo Villablanca Date: Thu, 13 Feb 2020 00:10:54 -0300 Subject: [PATCH 04/16] Fix collition name with package url --- libbeat/outputs/redis/redis.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/libbeat/outputs/redis/redis.go b/libbeat/outputs/redis/redis.go index 6a728ec39a4..ca838f1603c 100644 --- a/libbeat/outputs/redis/redis.go +++ b/libbeat/outputs/redis/redis.go @@ -128,16 +128,16 @@ func makeRedis( host = redisScheme + host } - url, err := url.Parse(host) + hostUrl, err := url.Parse(host) if err != nil { return outputs.Fail(err) } - if url.Scheme != redisScheme && url.Scheme != tlsRedisScheme { - return outputs.Fail(fmt.Errorf("invalid redis url scheme %s", url.Scheme)) + if hostUrl.Scheme != redisScheme && hostUrl.Scheme != tlsRedisScheme { + return outputs.Fail(fmt.Errorf("invalid redis url scheme %s", hostUrl.Scheme)) } - if url.Scheme == tlsRedisScheme && transp.TLS == nil { + if hostUrl.Scheme == tlsRedisScheme && transp.TLS == nil { return outputs.Fail(errors.New("using 'rediss' scheme requires a tls config")) } @@ -147,7 +147,7 @@ func makeRedis( } pass := config.Password - hostPass, passSet := url.User.Password() + hostPass, passSet := hostUrl.User.Password() if passSet { pass = hostPass } From 9ce4c41c27ba9117e647b9ccb6656cdbafd14348 Mon Sep 17 00:00:00 2001 From: Rodrigo Villablanca Date: Thu, 13 Feb 2020 17:53:04 -0300 Subject: [PATCH 05/16] * fix bug with host * using specific port instead defaultPort when apply * default tls config when ussing 'rediss' scheme --- libbeat/outputs/redis/redis.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/libbeat/outputs/redis/redis.go b/libbeat/outputs/redis/redis.go index ca838f1603c..30e8c9af893 100644 --- a/libbeat/outputs/redis/redis.go +++ b/libbeat/outputs/redis/redis.go @@ -21,6 +21,7 @@ import ( "errors" "fmt" "net/url" + "strconv" "strings" "time" @@ -45,8 +46,8 @@ const ( defaultWaitRetry = 1 * time.Second defaultMaxWaitRetry = 60 * time.Second defaultPort = 6379 - redisScheme = "redis://" - tlsRedisScheme = "rediss://" + redisScheme = "redis" + tlsRedisScheme = "rediss" ) func init() { @@ -125,7 +126,7 @@ func makeRedis( clients := make([]outputs.NetworkClient, len(hosts)) for i, host := range hosts { if parts := strings.SplitN(host, "://", 2); len(parts) != 2 { - host = redisScheme + host + host = fmt.Sprintf("%s://%s", redisScheme, host) } hostUrl, err := url.Parse(host) @@ -138,10 +139,15 @@ func makeRedis( } if hostUrl.Scheme == tlsRedisScheme && transp.TLS == nil { - return outputs.Fail(errors.New("using 'rediss' scheme requires a tls config")) + transp.TLS = &transport.TLSConfig{} } - conn, err := transport.NewClient(transp, "tcp", host, defaultPort) + port := defaultPort + if hostUrl.Port() != "" { + port, _ = strconv.Atoi(hostUrl.Port()) + } + + conn, err := transport.NewClient(transp, "tcp", hostUrl.Host, port) if err != nil { return outputs.Fail(err) } From 79ad5a2b73098ff8b71d13c04fefb3eb58c374ec Mon Sep 17 00:00:00 2001 From: Rodrigo Villablanca Date: Fri, 14 Feb 2020 00:57:34 -0300 Subject: [PATCH 06/16] More validations in url --- libbeat/outputs/redis/redis.go | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/libbeat/outputs/redis/redis.go b/libbeat/outputs/redis/redis.go index 30e8c9af893..27b68051b31 100644 --- a/libbeat/outputs/redis/redis.go +++ b/libbeat/outputs/redis/redis.go @@ -124,16 +124,20 @@ func makeRedis( } clients := make([]outputs.NetworkClient, len(hosts)) - for i, host := range hosts { - if parts := strings.SplitN(host, "://", 2); len(parts) != 2 { - host = fmt.Sprintf("%s://%s", redisScheme, host) + for i, h := range hosts { + if parts := strings.SplitN(h, "://", 2); len(parts) != 2 { + h = fmt.Sprintf("%s://%s", redisScheme, h) } - hostUrl, err := url.Parse(host) + hostUrl, err := url.Parse(h) if err != nil { return outputs.Fail(err) } + if hostUrl.Host == "" { + return outputs.Fail(fmt.Errorf("invalid redis url host %s", hostUrl.Host)) + } + if hostUrl.Scheme != redisScheme && hostUrl.Scheme != tlsRedisScheme { return outputs.Fail(fmt.Errorf("invalid redis url scheme %s", hostUrl.Scheme)) } @@ -142,12 +146,18 @@ func makeRedis( transp.TLS = &transport.TLSConfig{} } - port := defaultPort + var host string + var port int if hostUrl.Port() != "" { - port, _ = strconv.Atoi(hostUrl.Port()) + parts := strings.SplitN(hostUrl.Host, ":", 2) + host = parts[0] + port, _ = strconv.Atoi(parts[1]) + } else { + host = strings.ReplaceAll(hostUrl.Host, ":", "") + port = defaultPort } - conn, err := transport.NewClient(transp, "tcp", hostUrl.Host, port) + conn, err := transport.NewClient(transp, "tcp", host, port) if err != nil { return outputs.Fail(err) } From 56cfc0e3003c8adac2b1cee945be30e8d362e44b Mon Sep 17 00:00:00 2001 From: Rodrigo Villablanca Date: Sun, 16 Feb 2020 13:07:10 -0300 Subject: [PATCH 07/16] Add some corrections --- libbeat/outputs/redis/redis.go | 37 ++++++++++++++++------------------ 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/libbeat/outputs/redis/redis.go b/libbeat/outputs/redis/redis.go index 27b68051b31..fe090dd150c 100644 --- a/libbeat/outputs/redis/redis.go +++ b/libbeat/outputs/redis/redis.go @@ -21,7 +21,6 @@ import ( "errors" "fmt" "net/url" - "strconv" "strings" "time" @@ -116,17 +115,12 @@ func makeRedis( return outputs.Fail(err) } - transp := &transport.Config{ - Timeout: config.Timeout, - Proxy: &config.Proxy, - TLS: tls, - Stats: observer, - } - clients := make([]outputs.NetworkClient, len(hosts)) for i, h := range hosts { + hasScheme := true if parts := strings.SplitN(h, "://", 2); len(parts) != 2 { h = fmt.Sprintf("%s://%s", redisScheme, h) + hasScheme = false } hostUrl, err := url.Parse(h) @@ -142,22 +136,25 @@ func makeRedis( return outputs.Fail(fmt.Errorf("invalid redis url scheme %s", hostUrl.Scheme)) } - if hostUrl.Scheme == tlsRedisScheme && transp.TLS == nil { - transp.TLS = &transport.TLSConfig{} + transp := &transport.Config{ + Timeout: config.Timeout, + Proxy: &config.Proxy, + TLS: tls, + Stats: observer, } - var host string - var port int - if hostUrl.Port() != "" { - parts := strings.SplitN(hostUrl.Host, ":", 2) - host = parts[0] - port, _ = strconv.Atoi(parts[1]) - } else { - host = strings.ReplaceAll(hostUrl.Host, ":", "") - port = defaultPort + switch hostUrl.Scheme { + case redisScheme: + if hasScheme { + transp.TLS = nil // disable TLS if user explicitely set `redis` scheme + } + case tlsRedisScheme: + if transp.TLS == nil { + transp.TLS = &transport.TLSConfig{} // enable with system default if TLS was not configured + } } - conn, err := transport.NewClient(transp, "tcp", host, port) + conn, err := transport.NewClient(transp, "tcp", hostUrl.Host, defaultPort) if err != nil { return outputs.Fail(err) } From 2d9e68b327fde8ab3ce6a255d1ba5ee807b897e5 Mon Sep 17 00:00:00 2001 From: Rodrigo Villablanca Date: Thu, 20 Feb 2020 15:49:40 -0300 Subject: [PATCH 08/16] Typo --- libbeat/outputs/redis/redis_integration_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libbeat/outputs/redis/redis_integration_test.go b/libbeat/outputs/redis/redis_integration_test.go index 5d5a5df9f0c..0ff57470acb 100644 --- a/libbeat/outputs/redis/redis_integration_test.go +++ b/libbeat/outputs/redis/redis_integration_test.go @@ -53,7 +53,7 @@ const ( ) func TestPublishListTCP(t *testing.T) { - key := "test_publist_tcp" + key := "test_publish_tcp" db := 0 redisConfig := map[string]interface{}{ "hosts": []string{getRedisAddr()}, @@ -67,7 +67,7 @@ func TestPublishListTCP(t *testing.T) { } func TestPublishListTLS(t *testing.T) { - key := "test_publist_tls" + key := "test_publish_tls" db := 0 redisConfig := map[string]interface{}{ "hosts": []string{getSRedisAddr()}, From 7e53c61706d1ff0fdae47652ac8dd7cc753da078 Mon Sep 17 00:00:00 2001 From: Rodrigo Villablanca Date: Fri, 21 Feb 2020 12:28:09 -0300 Subject: [PATCH 09/16] unit test and integration tests --- .../outputs/redis/redis_integration_test.go | 19 ++++ libbeat/outputs/redis/redis_test.go | 96 +++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 libbeat/outputs/redis/redis_test.go diff --git a/libbeat/outputs/redis/redis_integration_test.go b/libbeat/outputs/redis/redis_integration_test.go index 0ff57470acb..0e3a96143fb 100644 --- a/libbeat/outputs/redis/redis_integration_test.go +++ b/libbeat/outputs/redis/redis_integration_test.go @@ -85,6 +85,25 @@ func TestPublishListTLS(t *testing.T) { testPublishList(t, redisConfig) } +func TestPublishListMultipleHosts(t *testing.T) { + key := "test_publish_tls" + db := 0 + redisConfig := map[string]interface{}{ + "hosts": []string{"rediss://" + getSRedisAddr(), "redis://" + getRedisAddr()}, + "key": key, + "db": db, + "datatype": "list", + "timeout": "5s", + + "ssl.verification_mode": "full", + "ssl.certificate_authorities": []string{ + "../../../testing/environments/docker/sredis/pki/tls/certs/sredis.crt", + }, + } + + testPublishList(t, redisConfig) +} + func testPublishList(t *testing.T, cfg map[string]interface{}) { batches := 100 batchSize := 1000 diff --git a/libbeat/outputs/redis/redis_test.go b/libbeat/outputs/redis/redis_test.go new file mode 100644 index 00000000000..402a89b4d8f --- /dev/null +++ b/libbeat/outputs/redis/redis_test.go @@ -0,0 +1,96 @@ +package redis + +import ( + "testing" + + "github.com/elastic/beats/libbeat/beat" + "github.com/elastic/beats/libbeat/common" + "github.com/elastic/beats/libbeat/outputs" + _ "github.com/elastic/beats/libbeat/outputs/codec/json" + "github.com/stretchr/testify/assert" +) + +func TestMakeRedis(t *testing.T) { + tests := []struct { + Name string + Config map[string]interface{} + Valid bool + AdditionalValidation func(*testing.T, outputs.Group) + }{ + { + "No host", + map[string]interface{}{ + "hosts": []string{}, + }, false, + nil, + }, + { + "Invalid scheme", + map[string]interface{}{ + "hosts": []string{"redisss://localhost:6379"}, + }, false, + nil, + }, + { + "Single host", + map[string]interface{}{ + "hosts": []string{"localhost:6379"}, + }, true, + func(t2 *testing.T, groups outputs.Group) { + assert.Len(t2, groups.Clients, 1) + redisClient := groups.Clients[0].(*backoffClient) + assert.Empty(t2, redisClient.client.password) + }, + }, + { + "Multiple hosts", + map[string]interface{}{ + "hosts": []string{"redis://localhost:6379", "rediss://localhost:6380"}, + }, true, + func(t2 *testing.T, groups outputs.Group) { + assert.Len(t2, groups.Clients, 2) + }, + }, + { + "Default password", + map[string]interface{}{ + "hosts": []string{"redis://localhost:6379"}, + "password": "defaultPassword", + }, true, + func(t2 *testing.T, groups outputs.Group) { + assert.Len(t2, groups.Clients, 1) + redisClient := groups.Clients[0].(*backoffClient) + assert.Equal(t2, "defaultPassword", redisClient.client.password) + }, + }, + { + "Specific and default password", + map[string]interface{}{ + "hosts": []string{"redis://localhost:6379", "rediss://:mypassword@localhost:6380"}, + "password": "defaultPassword", + }, true, + func(t2 *testing.T, groups outputs.Group) { + assert.Len(t2, groups.Clients, 2) + redisClient := groups.Clients[0].(*backoffClient) + assert.Equal(t2, "defaultPassword", redisClient.client.password) + redisClient = groups.Clients[1].(*backoffClient) + assert.Equal(t2, "mypassword", redisClient.client.password) + }, + }, + } + beatInfo := beat.Info{Beat: "libbeat", Version: "1.2.3"} + for _, test := range tests { + t.Run(test.Name, func(t *testing.T) { + cfg, err := common.NewConfigFrom(test.Config) + assert.NoError(t, err) + groups, err := makeRedis(nil, beatInfo, outputs.NewNilObserver(), cfg) + assert.Equal(t, err == nil, test.Valid) + if err != nil && test.Valid { + t.Log(err) + } + if test.AdditionalValidation != nil { + test.AdditionalValidation(t, groups) + } + }) + } +} From ed763da45b17776e9a335e481cd15747a1a2adb7 Mon Sep 17 00:00:00 2001 From: Rodrigo Villablanca Date: Fri, 21 Feb 2020 14:08:25 -0300 Subject: [PATCH 10/16] Fix redis output integration tests --- .../outputs/redis/redis_integration_test.go | 47 +++++++++++++------ 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/libbeat/outputs/redis/redis_integration_test.go b/libbeat/outputs/redis/redis_integration_test.go index 0e3a96143fb..ef691f25d44 100644 --- a/libbeat/outputs/redis/redis_integration_test.go +++ b/libbeat/outputs/redis/redis_integration_test.go @@ -85,23 +85,42 @@ func TestPublishListTLS(t *testing.T) { testPublishList(t, redisConfig) } -func TestPublishListMultipleHosts(t *testing.T) { - key := "test_publish_tls" - db := 0 - redisConfig := map[string]interface{}{ - "hosts": []string{"rediss://" + getSRedisAddr(), "redis://" + getRedisAddr()}, - "key": key, - "db": db, - "datatype": "list", - "timeout": "5s", - - "ssl.verification_mode": "full", - "ssl.certificate_authorities": []string{ - "../../../testing/environments/docker/sredis/pki/tls/certs/sredis.crt", +func TestWithSchema(t *testing.T) { + redisURL := "redis://" + getRedisAddr() + sredisURL := "rediss://" + getSRedisAddr() + + cases := map[string]struct { + host string + }{ + "redis ignores ssl settings": { + host: redisURL, + }, + "sredis schema sends via tls": { + host: sredisURL, }, } - testPublishList(t, redisConfig) + for name, test := range cases { + t.Run(name, func(t *testing.T) { + key := "test_publish_tls" + db := 0 + redisConfig := map[string]interface{}{ + "hosts": []string{test.host}, + "key": key, + "db": db, + "datatype": "list", + "timeout": "5s", + + "ssl.verification_mode": "full", + "ssl.certificate_authorities": []string{ + "../../../testing/environments/docker/sredis/pki/tls/certs/sredis.crt", + }, + } + + testPublishList(t, redisConfig) + }) + } + } func testPublishList(t *testing.T, cfg map[string]interface{}) { From 229662134410fa3c66caf2fd55b4362106139743 Mon Sep 17 00:00:00 2001 From: Rodrigo Villablanca Date: Fri, 21 Feb 2020 15:05:51 -0300 Subject: [PATCH 11/16] Minor improvements when testing redis output --- libbeat/outputs/redis/redis_test.go | 114 +++++++++++++++------------- 1 file changed, 60 insertions(+), 54 deletions(-) diff --git a/libbeat/outputs/redis/redis_test.go b/libbeat/outputs/redis/redis_test.go index 402a89b4d8f..2e0ff77bfea 100644 --- a/libbeat/outputs/redis/redis_test.go +++ b/libbeat/outputs/redis/redis_test.go @@ -10,86 +10,92 @@ import ( "github.com/stretchr/testify/assert" ) +type checker func(*testing.T, outputs.Group) + +func checks(cs ...checker) checker { + return func(t *testing.T, g outputs.Group) { + for _, c := range cs { + c(t, g) + } + } +} + +func clientsLen(required int) checker { + return func(t *testing.T, group outputs.Group) { + assert.Len(t, group.Clients, required) + } +} + +func clientPassword(index int, pass string) checker { + return func(t *testing.T, group outputs.Group) { + redisClient := group.Clients[index].(*backoffClient) + assert.Equal(t, redisClient.client.password, pass) + } +} + func TestMakeRedis(t *testing.T) { - tests := []struct { - Name string - Config map[string]interface{} - Valid bool - AdditionalValidation func(*testing.T, outputs.Group) + tests := map[string]struct { + config map[string]interface{} + valid bool + checks checker }{ - { - "No host", - map[string]interface{}{ + "no host": { + config: map[string]interface{}{ "hosts": []string{}, - }, false, - nil, + }, }, - { - "Invalid scheme", - map[string]interface{}{ + "invald scheme": { + config: map[string]interface{}{ "hosts": []string{"redisss://localhost:6379"}, - }, false, - nil, + }, }, - { - "Single host", - map[string]interface{}{ + "Single host": { + config: map[string]interface{}{ "hosts": []string{"localhost:6379"}, - }, true, - func(t2 *testing.T, groups outputs.Group) { - assert.Len(t2, groups.Clients, 1) - redisClient := groups.Clients[0].(*backoffClient) - assert.Empty(t2, redisClient.client.password) }, + valid: true, + checks: checks(clientsLen(1), clientPassword(0, "")), }, - { - "Multiple hosts", - map[string]interface{}{ + "Multiple hosts": { + config: map[string]interface{}{ "hosts": []string{"redis://localhost:6379", "rediss://localhost:6380"}, - }, true, - func(t2 *testing.T, groups outputs.Group) { - assert.Len(t2, groups.Clients, 2) }, + valid: true, + checks: clientsLen(2), }, - { - "Default password", - map[string]interface{}{ + "Default password": { + config: map[string]interface{}{ "hosts": []string{"redis://localhost:6379"}, "password": "defaultPassword", - }, true, - func(t2 *testing.T, groups outputs.Group) { - assert.Len(t2, groups.Clients, 1) - redisClient := groups.Clients[0].(*backoffClient) - assert.Equal(t2, "defaultPassword", redisClient.client.password) }, + valid: true, + checks: checks(clientsLen(1), clientPassword(0, "defaultPassword")), }, - { - "Specific and default password", - map[string]interface{}{ + "Specific and default password": { + config: map[string]interface{}{ "hosts": []string{"redis://localhost:6379", "rediss://:mypassword@localhost:6380"}, "password": "defaultPassword", - }, true, - func(t2 *testing.T, groups outputs.Group) { - assert.Len(t2, groups.Clients, 2) - redisClient := groups.Clients[0].(*backoffClient) - assert.Equal(t2, "defaultPassword", redisClient.client.password) - redisClient = groups.Clients[1].(*backoffClient) - assert.Equal(t2, "mypassword", redisClient.client.password) }, + valid: true, + checks: checks( + clientsLen(2), + clientPassword(0, "defaultPassword"), + clientPassword(1, "mypassword"), + ), }, } beatInfo := beat.Info{Beat: "libbeat", Version: "1.2.3"} - for _, test := range tests { - t.Run(test.Name, func(t *testing.T) { - cfg, err := common.NewConfigFrom(test.Config) + for name, test := range tests { + t.Run(name, func(t *testing.T) { + cfg, err := common.NewConfigFrom(test.config) assert.NoError(t, err) groups, err := makeRedis(nil, beatInfo, outputs.NewNilObserver(), cfg) - assert.Equal(t, err == nil, test.Valid) - if err != nil && test.Valid { + assert.Equal(t, err == nil, test.valid) + if err != nil && test.valid { t.Log(err) } - if test.AdditionalValidation != nil { - test.AdditionalValidation(t, groups) + if test.checks != nil { + test.checks(t, groups) } }) } From 30a2bf00e47f5bca2835a858a4e17eee110026d2 Mon Sep 17 00:00:00 2001 From: Rodrigo Villablanca Date: Sat, 22 Feb 2020 11:35:08 -0300 Subject: [PATCH 12/16] Add license header --- libbeat/outputs/redis/redis_test.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/libbeat/outputs/redis/redis_test.go b/libbeat/outputs/redis/redis_test.go index 2e0ff77bfea..c46c824f582 100644 --- a/libbeat/outputs/redis/redis_test.go +++ b/libbeat/outputs/redis/redis_test.go @@ -1,3 +1,20 @@ +// Licensed to Elasticsearch B.V. under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Elasticsearch B.V. licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License.package redis + package redis import ( From df66485ae408c97855f08900a5ff55c776406288 Mon Sep 17 00:00:00 2001 From: Rodrigo Villablanca Date: Sat, 22 Feb 2020 11:38:00 -0300 Subject: [PATCH 13/16] Add changes to changelog --- CHANGELOG.next.asciidoc | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.next.asciidoc b/CHANGELOG.next.asciidoc index 2d986597364..76f8d0eb448 100644 --- a/CHANGELOG.next.asciidoc +++ b/CHANGELOG.next.asciidoc @@ -110,6 +110,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d - Add document_id setting to decode_json_fields processor. {pull}15859[15859] - Include network information by default on add_host_metadata and add_observer_metadata. {issue}15347[15347] {pull}16077[16077] - Add `aws_ec2` provider for autodiscover. {issue}12518[12518] {pull}14823[14823] +- Add support for multiple password in redis output. {issue}16058[16058] {pull}16206[16206] *Auditbeat* From 87106f98ca4234d201ab15c8987e89399c8aa15a Mon Sep 17 00:00:00 2001 From: Rodrigo Villablanca Date: Sat, 22 Feb 2020 11:46:35 -0300 Subject: [PATCH 14/16] Fix license header --- libbeat/outputs/redis/redis_test.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/libbeat/outputs/redis/redis_test.go b/libbeat/outputs/redis/redis_test.go index c46c824f582..387c6b363c4 100644 --- a/libbeat/outputs/redis/redis_test.go +++ b/libbeat/outputs/redis/redis_test.go @@ -13,18 +13,19 @@ // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY // KIND, either express or implied. See the License for the // specific language governing permissions and limitations -// under the License.package redis +// under the License. package redis import ( "testing" + "github.com/stretchr/testify/assert" + "github.com/elastic/beats/libbeat/beat" "github.com/elastic/beats/libbeat/common" "github.com/elastic/beats/libbeat/outputs" _ "github.com/elastic/beats/libbeat/outputs/codec/json" - "github.com/stretchr/testify/assert" ) type checker func(*testing.T, outputs.Group) From 0bd9c96b061381bdae9d8a3f8ce67c46b2147c75 Mon Sep 17 00:00:00 2001 From: Rodrigo Villablanca Date: Sat, 22 Feb 2020 11:47:32 -0300 Subject: [PATCH 15/16] Fix imports --- libbeat/outputs/redis/redis_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/libbeat/outputs/redis/redis_test.go b/libbeat/outputs/redis/redis_test.go index 387c6b363c4..3e99bf27b1d 100644 --- a/libbeat/outputs/redis/redis_test.go +++ b/libbeat/outputs/redis/redis_test.go @@ -20,12 +20,11 @@ package redis import ( "testing" - "github.com/stretchr/testify/assert" - "github.com/elastic/beats/libbeat/beat" "github.com/elastic/beats/libbeat/common" "github.com/elastic/beats/libbeat/outputs" _ "github.com/elastic/beats/libbeat/outputs/codec/json" + "github.com/stretchr/testify/assert" ) type checker func(*testing.T, outputs.Group) From 831348cf798d142dfd93bc7ee887532a62d15385 Mon Sep 17 00:00:00 2001 From: Rodrigo Villablanca Date: Sat, 22 Feb 2020 12:13:13 -0300 Subject: [PATCH 16/16] Formatting using make fmt update --- libbeat/outputs/redis/redis_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libbeat/outputs/redis/redis_test.go b/libbeat/outputs/redis/redis_test.go index 3e99bf27b1d..387c6b363c4 100644 --- a/libbeat/outputs/redis/redis_test.go +++ b/libbeat/outputs/redis/redis_test.go @@ -20,11 +20,12 @@ package redis import ( "testing" + "github.com/stretchr/testify/assert" + "github.com/elastic/beats/libbeat/beat" "github.com/elastic/beats/libbeat/common" "github.com/elastic/beats/libbeat/outputs" _ "github.com/elastic/beats/libbeat/outputs/codec/json" - "github.com/stretchr/testify/assert" ) type checker func(*testing.T, outputs.Group)