Repository navigation
Expand file tree
/
Copy pathtls.go
More file actions
201 lines (173 loc) · 5.19 KB
/
Copy pathtls.go
File metadata and controls
201 lines (173 loc) · 5.19 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
* Copyright 2017 Farsight Security, Inc.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package config
import (
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"io/ioutil"
"strings"
)
// TLSClientAuth provides a convenience wrapper for tls.ClientAuthType and
// conversion to and from string format.
//
// Supported string values are:
//
// "none": tls.NoClientCert (default)
// "request": tls.RequestClientCert
// "require": tls.RequireAnyClientCert
// "verify": tls.VerifyClientCertIfGiven
// "require+verify": tls.RequireAndVerifyClientCert
type TLSClientAuth struct{ tls.ClientAuthType }
var clientAuthTypes = map[string]tls.ClientAuthType{
"none": tls.NoClientCert,
"request": tls.RequestClientCert,
"require": tls.RequireAnyClientCert,
"verify": tls.VerifyClientCertIfGiven,
"require+verify": tls.RequireAndVerifyClientCert,
}
// String satisfies the flag.Value interface
func (auth *TLSClientAuth) String() string {
for name, typ := range clientAuthTypes {
if typ == auth.ClientAuthType {
return name
}
}
return ""
}
type invalidClientAuthType string
func (i invalidClientAuthType) Error() string {
return fmt.Sprintf(`Invalid ClientAuthType "%s".`, string(i))
}
type invalidClientAuthTypeValue tls.ClientAuthType
func (i invalidClientAuthTypeValue) Error() string {
return fmt.Sprintf("Invalid ClientAuthType value %v", tls.ClientAuthType(i))
}
// Set satisfies the flag.Value interface.
func (auth *TLSClientAuth) Set(s string) error {
if a, ok := clientAuthTypes[s]; ok {
auth.ClientAuthType = a
return nil
}
return invalidClientAuthType(s)
}
// MarshalJSON satisfies the json.Marshaler interface
func (auth TLSClientAuth) MarshalJSON() ([]byte, error) {
s := auth.String()
if s == "" {
return nil, invalidClientAuthTypeValue(auth.ClientAuthType)
}
return json.Marshal(s)
}
// MarshalYAML satisfies the yaml.Marshaler interface
func (auth TLSClientAuth) MarshalYAML() (interface{}, error) {
s := auth.String()
if s == "" {
return nil, invalidClientAuthTypeValue(auth.ClientAuthType)
}
return s, nil
}
// UnmarshalJSON satisfies the json.Unmarshaler interface
func (auth *TLSClientAuth) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
return auth.Set(strings.ToLower(s))
}
// UnmarshalYAML satisfies the yaml.Unmarshaler interface
func (auth *TLSClientAuth) UnmarshalYAML(u func(interface{}) error) error {
var s string
if err := u(&s); err != nil {
return err
}
return auth.Set(strings.ToLower(s))
}
// TLSConfig contains the configuration for TLS as it appears on the JSON
// or YAML config. Values parsed from the config are translated and loaded
// into corresponding fields in tls.Config.
type TLSConfig struct {
RootCAFiles []string `json:"rootCAFiles,omitempty" yaml:"rootCAFiles,omitempty"`
ClientCAFiles []string `json:"clientCAFiles,omitempty" yaml:"clientCAFiles,omitempty"`
ClientAuth TLSClientAuth `json:"clientAuth,omitempty" yaml:"clientAuth,omitempty"`
Certificates []struct {
CertFile string `json:"certFile" yaml:"certFile"`
KeyFile string `json:"keyFile" yaml:"keyFile"`
} `json:"certificates,omitempty" yaml:"certificates,omitempty"`
}
// TLS provides JSON and YAML Marshalers and Unmarshalers for loading
// values into tls.Config.
//
// The JSON and YAML configuration format is provided by the embedded
// type TLSConfig.
type TLS struct {
TLSConfig
*tls.Config
}
// MarshalJSON satisfies the json.Marshaler interface
func (t TLS) MarshalJSON() ([]byte, error) {
return json.Marshal(t.TLSConfig)
}
// MarshalYAML satisfies the yaml.Marshaler interface
func (t TLS) MarshalYAML() (interface{}, error) {
return t.TLSConfig, nil
}
// UnmarshalJSON satisfies the json.Unmarshaler interface
func (t *TLS) UnmarshalJSON(b []byte) (err error) {
if err = json.Unmarshal(b, &t.TLSConfig); err != nil {
return
}
t.Config, err = loadTLSConfig(t.TLSConfig)
return
}
// UnmarshalYAML satisfies the yaml.Unmarshaler interface
func (t *TLS) UnmarshalYAML(u func(interface{}) error) (err error) {
if err = u(&t.TLSConfig); err != nil {
return
}
t.Config, err = loadTLSConfig(t.TLSConfig)
return
}
func loadTLSConfig(jc TLSConfig) (*tls.Config, error) {
var err error
tc := new(tls.Config)
tc.ClientAuth = jc.ClientAuth.ClientAuthType
if len(jc.RootCAFiles) > 0 {
tc.RootCAs, err = loadCertPool(jc.RootCAFiles)
if err != nil {
return nil, err
}
}
if len(jc.ClientCAFiles) > 0 {
tc.ClientCAs, err = loadCertPool(jc.ClientCAFiles)
if err != nil {
return nil, err
}
}
for _, kp := range jc.Certificates {
cert, err := tls.LoadX509KeyPair(kp.CertFile, kp.KeyFile)
if err != nil {
return nil, err
}
tc.Certificates = append(tc.Certificates, cert)
}
tc.BuildNameToCertificate()
return tc, nil
}
func loadCertPool(files []string) (*x509.CertPool, error) {
pool := x509.NewCertPool()
for _, f := range files {
if pem, err := ioutil.ReadFile(f); err == nil {
pool.AppendCertsFromPEM(pem)
} else {
return nil, err
}
}
return pool, nil
}