forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 1
/
tls.go
86 lines (69 loc) · 1.69 KB
/
tls.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
package testutil
import (
"fmt"
"io/ioutil"
"os"
"path"
"github.com/influxdata/telegraf/internal/tls"
)
type pki struct {
path string
}
func NewPKI(path string) *pki {
return &pki{path: path}
}
func (p *pki) TLSClientConfig() *tls.ClientConfig {
return &tls.ClientConfig{
TLSCA: p.CACertPath(),
TLSCert: p.ClientCertPath(),
TLSKey: p.ClientKeyPath(),
}
}
func (p *pki) TLSServerConfig() *tls.ServerConfig {
return &tls.ServerConfig{
TLSAllowedCACerts: []string{p.CACertPath()},
TLSCert: p.ServerCertPath(),
TLSKey: p.ServerKeyPath(),
}
}
func (p *pki) ReadCACert() string {
return readCertificate(p.CACertPath())
}
func (p *pki) CACertPath() string {
return path.Join(p.path, "cacert.pem")
}
func (p *pki) ReadClientCert() string {
return readCertificate(p.ClientCertPath())
}
func (p *pki) ClientCertPath() string {
return path.Join(p.path, "clientcert.pem")
}
func (p *pki) ReadClientKey() string {
return readCertificate(p.ClientKeyPath())
}
func (p *pki) ClientKeyPath() string {
return path.Join(p.path, "clientkey.pem")
}
func (p *pki) ReadServerCert() string {
return readCertificate(p.ServerCertPath())
}
func (p *pki) ServerCertPath() string {
return path.Join(p.path, "servercert.pem")
}
func (p *pki) ReadServerKey() string {
return readCertificate(p.ServerKeyPath())
}
func (p *pki) ServerKeyPath() string {
return path.Join(p.path, "serverkey.pem")
}
func readCertificate(filename string) string {
file, err := os.Open(filename)
if err != nil {
panic(fmt.Sprintf("opening %q: %v", filename, err))
}
octets, err := ioutil.ReadAll(file)
if err != nil {
panic(fmt.Sprintf("reading %q: %v", filename, err))
}
return string(octets)
}