-
Notifications
You must be signed in to change notification settings - Fork 0
/
syslogtls.go
95 lines (84 loc) · 2.25 KB
/
syslogtls.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package syslogtls
import (
"crypto/x509"
"encoding/base64"
"errors"
"github.com/akkeris/logtrain/internal/debug"
syslog "github.com/trevorlinton/remote_syslog2/syslog"
"net/url"
"strings"
"time"
)
// Syslog tls output structure
type Syslog struct {
url url.URL
endpoint string
logger *syslog.Logger
roots *x509.CertPool
errors chan<- error
}
var syslogSchemas = []string{"syslog+tls://"}
const syslogNetwork = "tls"
const maxLogSize int = 99990
// Test for a syslog tls schema
func Test(endpoint string) bool {
for _, schema := range syslogSchemas {
if strings.HasPrefix(strings.ToLower(endpoint), schema) == true {
return true
}
}
return false
}
// Create a syslog tls output
func Create(endpoint string, errorsCh chan<- error) (*Syslog, error) {
if Test(endpoint) == false {
return nil, errors.New("Invalid endpoint")
}
u, err := url.Parse(endpoint)
if err != nil {
return nil, err
}
roots, err := x509.SystemCertPool()
if err != nil {
roots = x509.NewCertPool()
}
if u.Query().Get("ca") != "" {
decoded, err := base64.StdEncoding.DecodeString(u.Query().Get("ca"))
if err != nil {
return nil, err
}
if ok := roots.AppendCertsFromPEM([]byte(decoded)); ok == false {
return nil, errors.New("the ca provided was invalid")
}
}
return &Syslog{
endpoint: endpoint,
url: *u,
roots: roots,
errors: errorsCh,
}, nil
}
// Dial connects to the syslog output
func (log *Syslog) Dial() error {
debug.Debugf("[syslog+tls/output]: Dial called for %s\n", log.endpoint)
dest, err := syslog.Dial("logtrain.akkeris-system.svc.cluster.local", syslogNetwork, log.url.Host, log.roots, time.Second*4, time.Second*4, maxLogSize)
if err != nil {
debug.Debugf("[syslog+tls/output]: Dial encountered an error on %s: %s\n", log.endpoint, err.Error())
return err
}
log.logger = dest
return nil
}
// Close the syslog output
func (log *Syslog) Close() error {
debug.Debugf("[syslog+tls/output]: Close called for %s\n", log.endpoint)
return log.logger.Close()
}
// Pools returns whether the syslog endpoint pools connections
func (log *Syslog) Pools() bool {
return false
}
// Packets returns a channel to send packets to the syslog endpoint
func (log *Syslog) Packets() chan syslog.Packet {
return log.logger.Packets
}