Skip to content

Commit a2bbd3e

Browse files
committed
Add support for TLS logging
Add support for TLS logging, including test suite. By far the most tricky part here is getting certificates that golang works with, and getting the TLS config correct. Signed-off-by: Alex Bligh <[email protected]>
1 parent 6cba2bf commit a2bbd3e

File tree

2 files changed

+224
-3
lines changed

2 files changed

+224
-3
lines changed

server.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package syslog
22

33
import (
44
"bufio"
5+
"crypto/tls"
56
"errors"
67
"net"
78
"sync"
@@ -23,7 +24,7 @@ const (
2324
)
2425

2526
type Server struct {
26-
listeners []*net.TCPListener
27+
listeners []net.Listener
2728
connections []net.Conn
2829
wait sync.WaitGroup
2930
doneTcp chan bool
@@ -105,6 +106,18 @@ func (s *Server) ListenTCP(addr string) error {
105106
return nil
106107
}
107108

109+
//Configure the server for listen on a TCP addr for TLS
110+
func (s *Server) ListenTCPTLS(addr string, config *tls.Config) error {
111+
listener, err := tls.Listen("tcp", addr, config)
112+
if err != nil {
113+
return err
114+
}
115+
116+
s.doneTcp = make(chan bool)
117+
s.listeners = append(s.listeners, listener)
118+
return nil
119+
}
120+
108121
//Starts the server, all the go routines goes to live
109122
func (s *Server) Boot() error {
110123
if s.format == nil {
@@ -130,9 +143,9 @@ func (s *Server) Boot() error {
130143
return nil
131144
}
132145

133-
func (s *Server) goAcceptConnection(listener *net.TCPListener) {
146+
func (s *Server) goAcceptConnection(listener net.Listener) {
134147
s.wait.Add(1)
135-
go func(listener *net.TCPListener) {
148+
go func(listener net.Listener) {
136149
loop:
137150
for {
138151
select {

servertls_test.go

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
package syslog
2+
3+
import (
4+
"crypto/rand"
5+
"crypto/tls"
6+
"crypto/x509"
7+
"fmt"
8+
"io"
9+
"time"
10+
11+
. "launchpad.net/gocheck"
12+
)
13+
14+
func getServerConfig() *tls.Config {
15+
capool := x509.NewCertPool()
16+
if ok := capool.AppendCertsFromPEM([]byte(ca_s)); !ok {
17+
panic("Cannot add cert")
18+
}
19+
20+
cert, err := tls.X509KeyPair([]byte(cert1_s), []byte(priv1_s))
21+
if err != nil {
22+
panic(err)
23+
}
24+
25+
config := tls.Config{
26+
ClientAuth: tls.RequireAndVerifyClientCert,
27+
Certificates: []tls.Certificate{cert},
28+
ClientCAs: capool,
29+
ServerName: "dummycert1",
30+
}
31+
config.Rand = rand.Reader
32+
33+
return &config
34+
}
35+
36+
func getClientConfig() *tls.Config {
37+
capool := x509.NewCertPool()
38+
if ok := capool.AppendCertsFromPEM([]byte(ca_s)); !ok {
39+
panic("Cannot add cert")
40+
}
41+
42+
cert, err := tls.X509KeyPair([]byte(cert1_s), []byte(priv1_s))
43+
if err != nil {
44+
panic(err)
45+
}
46+
47+
config := tls.Config{
48+
Certificates: []tls.Certificate{cert},
49+
InsecureSkipVerify: true,
50+
ServerName: "dummycert1",
51+
RootCAs: capool,
52+
}
53+
config.Rand = rand.Reader
54+
55+
return &config
56+
}
57+
58+
func (s *ServerSuite) TestTLS(c *C) {
59+
handler := new(HandlerMock)
60+
server := NewServer()
61+
server.SetFormat(RFC3164)
62+
server.SetHandler(handler)
63+
server.ListenTCPTLS("0.0.0.0:5143", getServerConfig())
64+
65+
server.Boot()
66+
go func(server *Server) {
67+
time.Sleep(100 * time.Millisecond)
68+
conn, err := tls.Dial("tcp", "127.0.0.1:5143", getClientConfig())
69+
if err != nil {
70+
panic(err)
71+
}
72+
defer conn.Close()
73+
74+
if _, err := io.WriteString(conn, fmt.Sprintf("%s\n", exampleSyslog)); err != nil {
75+
panic(err)
76+
}
77+
server.Kill()
78+
}(server)
79+
server.Wait()
80+
81+
c.Check(handler.LastLogParts["hostname"], Equals, "hostname")
82+
c.Check(handler.LastLogParts["tag"], Equals, "tag")
83+
c.Check(handler.LastLogParts["content"], Equals, "content")
84+
c.Check(handler.LastMessageLength, Equals, int64(len(exampleSyslog)))
85+
c.Check(handler.LastError, IsNil)
86+
}
87+
88+
const (
89+
priv1_s = `-----BEGIN RSA PRIVATE KEY-----
90+
MIIJKAIBAAKCAgEAxUfRcXt1/H6dWtHseq70x+VyrIj+4g+zjCa0UrdEUR8QQavO
91+
DTDUBuQmeASU40AnCO24Cnx0y7Kt6ZHrf3K9xI17aJj9qvE+9qQpfg+YMHFOFFuA
92+
AANKDcl3rmifwwo+hWB6DQRqD/CNACAFCez4W4O0+sETl/LbUkMw5I7ImKli1mlL
93+
PMfrId9ezOvyfWHZEQHRyDYBCkYsZDLW2mMySOJy1r1l4azIhshUcrDT+gBZHiyi
94+
81g2BS6n60O0xBHwiHSGvTpBTwXLpvJ44HeG4rJjRz9TMD2c+XrIeZWXsM7xAqMg
95+
F4uK2lUDSHM+1RBgQyJTMDodspSJQOz1Fc83Sze1Nyq9hprZo9/U5J+ML75Cumd9
96+
kDr1NF2hBk+49uDJtaU3czxexGN1p24hmTmJpnd6fvJ1hOZadX34DaluF7NXGXEO
97+
odMB6ggGqNNcHfws1Q5Xuyk6skwXtgWHLWdlygGYJ2qfj9l0F/gVDknjBubMIdrp
98+
JakkMvCXcGlqw+paIXQZMBQquwlrsesD+/YGEmVHvREGJsnXa8XHiTje1xKBPw5L
99+
sn/eY3f787NCy5atNlGPGtPY5IL2oiNMtHCOH9fufBTswR0ch0ZUoR109NjDw2EU
100+
ye0YOQ8B0uJyrva/aM0l/DH+ieXCAN4sxnyYN3yfo3J8nh4Hq/n22K8gZO8CAwEA
101+
AQKCAgAmz4svtScwBkS0oknQlOzJCqW1tbnXBVnAP7kH8M/62Y6cLM17oNiFhore
102+
35/e2TcUtZeYUIW1sTAvnCplR1B4A5F8sWRuJcnKQd970luRZCkFLj8PQZZnAfSO
103+
ljyf5TsJiEJanzyyaBOFK8dx/XGap12KW0OciAWHuHo87K4gAmrUXaCUk4v5fPUs
104+
gVqSOha3FtGLfrxTphyDldDY49z3o70N6/LII/LLOUwLyCfbrgfaPNPN5dOyz0vv
105+
p8E/NXxJjAsZ3QUO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vfC2OgU3FQ
171+
-----END CERTIFICATE-----
172+
`
173+
ca_s = `-----BEGIN CERTIFICATE-----
174+
MIIF/DCCA+SgAwIBAgIJAPu2wMXkvlz1MA0GCSqGSIb3DQEBCwUAMFsxCzAJBgNV
175+
BAYTAlhYMQ0wCwYDVQQIEwROb25lMQ0wCwYDVQQHEwROb25lMQ0wCwYDVQQKEwRO
176+
b25lMQ0wCwYDVQQLEwROb25lMRAwDgYDVQQDEwdkdW1teWNhMCAXDTE1MDcxNjE3
177+
NDMyN1oYDzIxMTUwNjIyMTc0MzI3WjBbMQswCQYDVQQGEwJYWDENMAsGA1UECBME
178+
Tm9uZTENMAsGA1UEBxMETm9uZTENMAsGA1UEChMETm9uZTENMAsGA1UECxMETm9u
179+
ZTEQMA4GA1UEAxMHZHVtbXljYTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoC
180+
ggIBAOBYd9DycwftWY+ixY3ppC8jKlJkgH+gTRzMfnDABSH0Kx83sdzhfZ2/jPCy
181+
si7v+m62Zr64ovxIQhzebZX4L3ioQOMwm2Ri+9Y+OIShaIgtsZtJNFOAG1kjTVNV
182+
Wvh8rVMk1LgfOfSWyaHBW9TzJSkHuv9zdAqbo2MngfrFzNaUZDryeYH3+xqOXMIB
183+
JusMfR5PdBtXmOCh9H7++IvFLmakA48QCV/VvRntdufPX2dN2N1+oP+136ch5Izx
184+
JS41oQJ90MWYEgBXZJne14KW1+V15IRjGYubFJfDAiVTe3h0rgNySfbTI09/3Es5
185+
THZeZ3nfv6ZfGFqoaL4Dy1sj/HT9FMVvt8Af5Gsh4dfpoGZLNugr5xBGJbqe2QtU
186+
JK4zyiG0msVVrUWDhpNcYhookFk1vYJ1Ajj3drlTc2lJlZbxwYu507A3p+pJ0d0D
187+
8NrFyuOR9Jb347UCd87c6aYARqlIpqQb2yIvvtKLuEpM42iSApN23oes7PcMxtXv
188+
yCJVvqHzctRYHxhjc/Vls5PMLqFVBxqRuCDAemVLlAVM55//uNERZj5kovbh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TEDEXXbV54vhWK2Xl0+BY
205+
GSDhNiPbMnysmreLxrnygHJCpCn2i75NwnUtDdb1nqGn3MsVVout+pdNyuN2RGUo
206+
-----END CERTIFICATE-----
207+
`
208+
)

0 commit comments

Comments
 (0)