Skip to content

Commit

Permalink
Add testdata and TLS listener test
Browse files Browse the repository at this point in the history
  • Loading branch information
Bob Shannon committed Apr 17, 2018
1 parent 2559fff commit 474e9f3
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 1 deletion.
25 changes: 25 additions & 0 deletions plugins/inputs/socket_listener/socket_listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package socket_listener

import (
"bytes"
"crypto/tls"
"log"
"net"
"os"
"testing"
"time"

"github.com/influxdata/telegraf/internal"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand All @@ -25,6 +27,29 @@ func testEmptyLog(t *testing.T) func() {
}
}

func TestSocketListener_tls(t *testing.T) {
defer testEmptyLog(t)()

sl := newSocketListener()
sl.ServiceAddress = "tcp://127.0.0.1:0"
sl.TLSCert = "testdata/server.pem"
sl.TLSKey = "testdata/server.key"
sl.TLSAllowedCACerts = []string{"testdata/ca.pem"}

acc := &testutil.Accumulator{}
err := sl.Start(acc)
require.NoError(t, err)
defer sl.Stop()

tlsCfg, err := internal.GetTLSConfig("testdata/client.pem", "testdata/client.key", "testdata/ca.pem", false)
require.NoError(t, err)

secureClient, err := tls.Dial("tcp", sl.Closer.(net.Listener).Addr().String(), tlsCfg)
require.NoError(t, err)

testSocketListener(t, sl, secureClient)
}

func TestSocketListener_tcp(t *testing.T) {
defer testEmptyLog(t)()

Expand Down
11 changes: 11 additions & 0 deletions plugins/inputs/socket_listener/testdata/ca.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-----BEGIN CERTIFICATE-----
MIIBfjCCASSgAwIBAgIQOcVQOVdiYAlrATkSZ00c2zAKBggqhkjOPQQDAjAkMRAw
DgYDVQQKEwdBY21lIENvMRAwDgYDVQQDEwdSb290IENBMB4XDTE4MDQxNzAyNDAw
NloXDTE5MDQxNzAyNDAwNlowJDEQMA4GA1UEChMHQWNtZSBDbzEQMA4GA1UEAxMH
Um9vdCBDQTBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABGhlpfpBmoCtiwBYeW+6
1zR4d8cmsKXWza5blMJIKY9gugDHTbDbW1kbNv+IotXmF94aQKY8SsYyF8SqQstr
MUGjODA2MA4GA1UdDwEB/wQEAwICBDATBgNVHSUEDDAKBggrBgEFBQcDATAPBgNV
HRMBAf8EBTADAQH/MAoGCCqGSM49BAMCA0gAMEUCIQCLhyfDywGQR+NhR68uh+Ps
OohSa5VGAe1nlCWwxjW4ugIgYRHKQ0qyf9kmE/+OlKGi+fsiZRJt57lO+r97YeML
R6o=
-----END CERTIFICATE-----
5 changes: 5 additions & 0 deletions plugins/inputs/socket_listener/testdata/client.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIAi2HTH3731OXAPNyQB2pyJz3CsyJQMzM6ypofD8q9proAoGCCqGSM49
AwEHoUQDQgAEfp9yWlCj8cf4pmwAyophsdqLCVSWFQQ41VlKnsnqfeCL3ALXV84W
vYMCpQKTk5ajIxMwaRz5V/RkGs4Y3VJLFg==
-----END EC PRIVATE KEY-----
10 changes: 10 additions & 0 deletions plugins/inputs/socket_listener/testdata/client.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-----BEGIN CERTIFICATE-----
MIIBezCCASCgAwIBAgIBBDAKBggqhkjOPQQDAjAkMRAwDgYDVQQKEwdBY21lIENv
MRAwDgYDVQQDEwdSb290IENBMB4XDTE4MDQxNzAyNDAwNloXDTE5MDQxNzAyNDAw
NlowMjEQMA4GA1UEChMHQWNtZSBDbzEeMBwGA1UEAwwVY2xpZW50X2F1dGhfdGVz
dF9jZXJ0MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEfp9yWlCj8cf4pmwAyoph
sdqLCVSWFQQ41VlKnsnqfeCL3ALXV84WvYMCpQKTk5ajIxMwaRz5V/RkGs4Y3VJL
FqM1MDMwDgYDVR0PAQH/BAQDAgeAMBMGA1UdJQQMMAoGCCsGAQUFBwMCMAwGA1Ud
EwEB/wQCMAAwCgYIKoZIzj0EAwIDSQAwRgIhAMzENYnCp9vbi6WEKKs5Ah5kf5Uq
IQZrsF6rbvX8bUq6AiEAl5pB2DivzzlSO1ME4TUnkH8PuRXoWF4WEhY7EsY7cCA=
-----END CERTIFICATE-----
5 changes: 5 additions & 0 deletions plugins/inputs/socket_listener/testdata/server.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIM6RnKmPvlS8pDPsohwQVFLyHDQyaNKckB3LW97EkDPJoAoGCCqGSM49
AwEHoUQDQgAEPAtSzaOBtFL6BFEqXLK9B4agy0qpTA/sMUTm9g92hz5Ic+1bVpi7
33sLjmjjqrYLFust+j+FqpeDRGovUDU0gA==
-----END EC PRIVATE KEY-----
11 changes: 11 additions & 0 deletions plugins/inputs/socket_listener/testdata/server.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-----BEGIN CERTIFICATE-----
MIIBnTCCAUKgAwIBAgIRAMjiXxeTNq5WuymjNs/SMMQwCgYIKoZIzj0EAwIwJDEQ
MA4GA1UEChMHQWNtZSBDbzEQMA4GA1UEAxMHUm9vdCBDQTAeFw0xODA0MTcwMjQw
MDZaFw0xOTA0MTcwMjQwMDZaMCgxEDAOBgNVBAoTB0FjbWUgQ28xFDASBgNVBAMM
C3Rlc3RfY2VydF8xMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEPAtSzaOBtFL6
BFEqXLK9B4agy0qpTA/sMUTm9g92hz5Ic+1bVpi733sLjmjjqrYLFust+j+FqpeD
RGovUDU0gKNRME8wDgYDVR0PAQH/BAQDAgWgMBMGA1UdJQQMMAoGCCsGAQUFBwMB
MAwGA1UdEwEB/wQCMAAwGgYDVR0RBBMwEYIJbG9jYWxob3N0hwR/AAABMAoGCCqG
SM49BAMCA0kAMEYCIQC5LOQXoGLQ1q8vuZhg0CBoBtGhJzq1NQgQrvbWOG1XMQIh
AMXysM4Ppawj9uvhYkdgwhIvM8/Ul5AUtb/GOCxqYhL4
-----END CERTIFICATE-----
2 changes: 1 addition & 1 deletion plugins/outputs/socket_writer/socket_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (sw *SocketWriter) Connect() error {
return fmt.Errorf("invalid address: %s", sw.Address)
}

tlsCfg, err := internal.GetClientTLSConfig(sw.SSLCert, sw.SSLKey, sw.SSLCA, sw.InsecureSkipVerify)
tlsCfg, err := internal.GetTLSConfig(sw.SSLCert, sw.SSLKey, sw.SSLCA, sw.InsecureSkipVerify)
if err != nil {
return err
}
Expand Down

0 comments on commit 474e9f3

Please sign in to comment.