-
Notifications
You must be signed in to change notification settings - Fork 27
/
tls.go
144 lines (124 loc) · 3.21 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package certs
/*
Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
*/
import (
"crypto/tls"
"crypto/x509"
"encoding/pem"
"fmt"
"runtime"
"github.com/facebookincubator/go2chef"
"github.com/mitchellh/mapstructure"
)
type TLSConfiguration struct {
TrustedCACerts []*x509.Certificate
ClientCerts []tls.Certificate
DisableCertVerification bool
}
func NewTLSConfiguration() *TLSConfiguration {
return &TLSConfiguration{
TrustedCACerts: nil,
ClientCerts: nil,
DisableCertVerification: false,
}
}
var TLS = NewTLSConfiguration()
func (t *TLSConfiguration) GetTLSClientConf() (*tls.Config, error) {
var rcaPool *x509.CertPool
// pull the system cert pool as a base...unless we're on windows
if runtime.GOOS != "windows" {
if p, err := x509.SystemCertPool(); err != nil {
return nil, err
} else {
rcaPool = p
}
} else {
// only set the cert pool if we have CAs to trust
// because windows will work
if len(t.TrustedCACerts) > 0 {
rcaPool = x509.NewCertPool()
}
}
if len(t.TrustedCACerts) > 0 && rcaPool != nil {
for _, rca := range t.TrustedCACerts {
rcaPool.AddCert(rca)
}
}
return &tls.Config{
Certificates: t.ClientCerts,
RootCAs: rcaPool,
}, nil
}
type tlsParse struct {
TrustedCACerts []string `mapstructure:"trusted_ca_certs"`
ClientCerts []tlsParseClientCert `mapstructure:"client_certs"`
DisableCertVerification bool `mapstructure:"disable_cert_verification"`
}
type tlsParseClientCert struct {
Certificate string `mapstructure:"certificate"`
Key string `mapstructure:"key"`
}
func LoadTLSConfigurationFromMap(data interface{}) (*TLSConfiguration, error) {
parse := tlsParse{}
if err := mapstructure.Decode(data, &parse); err != nil {
return nil, err
}
out := NewTLSConfiguration()
tcc, err := loadCertStringArray(parse.TrustedCACerts)
if err != nil {
return nil, err
}
cc, err := loadClientCerts(parse.ClientCerts)
if err != nil {
return nil, err
}
out.TrustedCACerts = tcc
out.ClientCerts = cc
out.DisableCertVerification = parse.DisableCertVerification
return out, nil
}
func loadCertStringArray(certSA []string) ([]*x509.Certificate, error) {
var dest []*x509.Certificate
for i, certPEM := range certSA {
block, _ := pem.Decode([]byte(certPEM))
if block == nil {
return nil, fmt.Errorf("error decoding trusted_ca_certs element %d", i)
}
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return nil, err
}
dest = append(dest, cert)
}
return dest, nil
}
func loadClientCerts(in []tlsParseClientCert) ([]tls.Certificate, error) {
var out []tls.Certificate
for _, kp := range in {
crt, err := tls.X509KeyPair([]byte(kp.Certificate), []byte(kp.Key))
if err != nil {
return nil, err
}
out = append(out, crt)
}
return out, nil
}
func tlsProcessor(f string, data interface{}) error {
t, err := LoadTLSConfigurationFromMap(data)
if err != nil {
return err
}
TLS = t
return nil
}
func toFullCerts(in []*x509.Certificate) []x509.Certificate {
var fc []x509.Certificate
for _, c := range in {
fc = append(fc, *c)
}
return fc
}
func init() {
go2chef.GlobalConfiguration.MustRegister("tls", tlsProcessor)
}