-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
291 lines (241 loc) · 11.2 KB
/
config.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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package identity
/*
Copyright NetFoundry Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import (
"fmt"
"strings"
)
const (
ConfigFieldCert = "cert"
ConfigFieldKey = "key"
ConfigFieldServerCert = "server_cert"
ConfigFieldServerKey = "server_key"
ConfigFieldAltServerCerts = "alt_server_certs"
ConfigFieldCa = "ca"
)
// Config represents the basic data structure for and identity configuration. A Config provides details on where the
// x509 certificates and private keys are located/stored for the identity. These values are interpreted by the
// LoadIdentity function to produce an Identity that can be used to create crypto configurations (i.e. tls.Config).
// Storage locations include files, in-memory PEM, and hardware tokens.
//
// Key, Cert, ServerCert, ServerKey, and CA are URLs with the following schemes: `file`, `pem`. Additionally,
// Key supports `engine`. If the value is not in URL format it is assumed to be `file`.
//
// Example: `file://path/to/my/cert.pem` or `path/to/my/cert.pem'
// Example: `pem://-----BEGIN CERTIFICATE-----\nMIIB/TCCAYCgAwIBAgIBATAMBggqhk...`
//
// Cert must point to a source with a single leaf-first certificate chain and have a corresponding private key in the
// Key field. ServerCert may point to a source with multiple certificate chains for the same server
// (i.e. SNI/ECH support) from different trust roots. If ServerKey is not defined, it is assumed that Key will
// be the private key for all leaf certificates found in ServerCert's source. If ServerKey is defined it is assumed
// that all leaf certificates in ServerCert's source use it as a private key.
//
// The AltServerCerts property allows an array of additional server certificate chains to be loaded
// with different private keys. It is useful for scenarios where the Cert and ServerCert fields are used for
// automated certificates from one system (i.e. Ziti) and alternate server certs are automated through another
// (i.e. Lets Encrypt)
type Config struct {
Key string `json:"key" yaml:"key" mapstructure:"key"`
Cert string `json:"cert" yaml:"cert" mapstructure:"cert"`
ServerCert string `json:"server_cert,omitempty" yaml:"server_cert,omitempty" mapstructure:"server_cert,omitempty"`
ServerKey string `json:"server_key,omitempty" yaml:"server_key,omitempty" mapstructure:"server_key,omitempty"`
AltServerCerts []ServerPair `json:"alt_server_certs,omitempty" yaml:"alt_server_certs,omitempty" mapstructure:"alt_server_certs,omitempty"`
CA string `json:"ca,omitempty" yaml:"ca,omitempty" mapstructure:"ca"`
}
type ServerPair struct {
ServerCert string `json:"server_cert,omitempty" yaml:"server_cert,omitempty" mapstructure:"server_cert,omitempty"`
ServerKey string `json:"server_key,omitempty" yaml:"server_key,omitempty" mapstructure:"server_key,omitempty"`
}
// Validate validates the current IdentityConfiguration to have non-empty values all fields except
// ServerKey which assumes that Key is a suitable default.
func (config *Config) Validate() error {
return config.ValidateWithPathContext("")
}
// ValidateWithPathContext performs the same checks as Validate but also allows a path context to be
// provided for error messages when parsing deep or complex configuration.
//
// Example:
//
// `ValidateWithPathContext("my.path")` errors would be formatted as "required configuration value [my.path.cert]..."`
func (config *Config) ValidateWithPathContext(pathContext string) error {
pathContext = strings.TrimSpace(pathContext)
if pathContext != "" {
if !strings.HasSuffix(pathContext, ".") {
pathContext = pathContext + "."
}
}
if config.Cert == "" {
return fmt.Errorf("required configuration value [%s%s] is missing or is blank", pathContext, ConfigFieldCert)
}
if config.ServerCert == "" {
return fmt.Errorf("required configuration value [%s%s] is missing or is blank", pathContext, ConfigFieldServerCert)
}
if config.Key == "" {
return fmt.Errorf("required configuration value [%s%s] is missing or is blank", pathContext, ConfigFieldKey)
}
if config.CA == "" {
return fmt.Errorf("required configuration value [%s%s] is missing or is blank", pathContext, ConfigFieldCa)
}
for i, altServerCerts := range config.AltServerCerts {
if altServerCerts.ServerCert == "" {
return fmt.Errorf("required configuration value [%s%s[%d].%s] is missing or is blank", pathContext, ConfigFieldAltServerCerts, i, ConfigFieldServerCert)
}
}
return nil
}
// ValidateForClient validates the current IdentityConfiguration has enough values to initiate a client connection.
// For example: a tls.Config for a client in mTLS
func (config *Config) ValidateForClient() error {
return config.ValidateForClientWithPathContext("")
}
// ValidateForClientWithPathContext performs the same checks as ValidateForClient but also allows a path context to be
// provided for error messages when parsing deep or complex configuration.
//
// Example:
//
// `ValidateForClientWithPathContext("my.path")` errors would be formatted as "required configuration value [my.path.cert]..."`
func (config *Config) ValidateForClientWithPathContext(pathContext string) error {
pathContext = strings.TrimSpace(pathContext)
if pathContext != "" {
if !strings.HasSuffix(pathContext, ".") {
pathContext = pathContext + "."
}
}
if config.Cert == "" {
return fmt.Errorf("required configuration value [%s%s] is missing or is blank", pathContext, ConfigFieldCert)
}
if config.Key == "" {
return fmt.Errorf("required configuration value [%s%s] is missing or is blank", pathContext, ConfigFieldKey)
}
return nil
}
// ValidateForServer validates the current IdentityConfiguration has enough values to a client connection.
// For example: a tls.Config for a server in mTLS
func (config *Config) ValidateForServer() error {
return config.ValidateForServerWithPathContext("")
}
// ValidateForServerWithPathContext performs the same checks as ValidateForServer but also allows a path context to be
// provided for error messages when parsing deep or complex configuration.
//
// Example:
//
// `ValidateWithPathContext("my.path")` errors would be formatted as "required configuration value [my.path.cert]..."`
func (config *Config) ValidateForServerWithPathContext(pathContext string) error {
pathContext = strings.TrimSpace(pathContext)
if pathContext != "" {
if !strings.HasSuffix(pathContext, ".") {
pathContext = pathContext + "."
}
}
if config.ServerCert == "" {
return fmt.Errorf("required configuration value [%s%s] is missing or is blank", pathContext, ConfigFieldServerCert)
}
if config.Key == "" && config.ServerKey == "" {
return fmt.Errorf("required configuration values [%s%s], [%s%s] are both missing or are blank", pathContext, ConfigFieldKey, pathContext, ConfigFieldServerKey)
}
if config.CA == "" {
return fmt.Errorf("required configuration value [%s%s] is missing or is blank", pathContext, ConfigFieldCa)
}
for i, altServerCerts := range config.AltServerCerts {
if altServerCerts.ServerCert == "" {
return fmt.Errorf("required configuration value [%s%s[%d].%s] is missing or is blank", pathContext, ConfigFieldAltServerCerts, i, ConfigFieldServerCert)
}
}
return nil
}
// NewConfigFromMap will parse a standard identity configuration section that has been loaded from JSON/YAML/etc.
// parse functions that return interface{} maps. It expects the following fields to be defined as strings if present.
// If any fields are missing they are left as empty string in the resulting Config.
func NewConfigFromMap(identityMap map[interface{}]interface{}) (*Config, error) {
return NewConfigFromMapWithPathContext(identityMap, "")
}
// NewConfigFromMapWithPathContext performs the same checks as NewConfigFromMap but also allows a path context to be
// provided for error messages when parsing deep or complex configuration.
//
// Example:
//
// `NewConfigFromMapWithPathContext(myMap, "my.path")` errors would be formatted as "value [my.path.cert] must be a string"`
func NewConfigFromMapWithPathContext(identityMap map[interface{}]interface{}, pathContext string) (*Config, error) {
pathContext = strings.TrimSpace(pathContext)
if pathContext != "" {
if !strings.HasSuffix(pathContext, ".") {
pathContext = pathContext + "."
}
}
idConfig := &Config{}
if certInterface, ok := identityMap[ConfigFieldCert]; ok {
if cert, ok := certInterface.(string); ok {
idConfig.Cert = cert
} else {
return nil, fmt.Errorf("value [%s%s] must be a string", pathContext, ConfigFieldCert)
}
}
if serverCertInterface, ok := identityMap[ConfigFieldServerCert]; ok {
if serverCert, ok := serverCertInterface.(string); ok {
idConfig.ServerCert = serverCert
} else {
return nil, fmt.Errorf("value [%s%s] must be a string", pathContext, ConfigFieldServerCert)
}
}
if keyInterface, ok := identityMap[ConfigFieldKey]; ok {
if key, ok := keyInterface.(string); ok {
idConfig.Key = key
} else {
return nil, fmt.Errorf("value [%s%s] must be a string", pathContext, ConfigFieldKey)
}
}
if keyInterface, ok := identityMap[ConfigFieldServerKey]; ok {
if serverKey, ok := keyInterface.(string); ok {
idConfig.ServerKey = serverKey
} else {
return nil, fmt.Errorf("value [%s%s] must be a string", pathContext, ConfigFieldServerKey)
}
}
if altServerCertsInterface, ok := identityMap[ConfigFieldAltServerCerts]; ok {
if altServerCertsSliceInterface, ok := altServerCertsInterface.([]any); ok {
for i, altServerCertInterface := range altServerCertsSliceInterface {
if altServerCertMap, ok := altServerCertInterface.(map[any]any); ok {
serverCertVal := altServerCertMap[ConfigFieldServerCert]
serverKeyVal := altServerCertMap[ConfigFieldServerKey]
serverCert, ok := serverCertVal.(string)
if !ok {
return nil, fmt.Errorf("value [%s%s[%d].%s] must be a string", pathContext, ConfigFieldAltServerCerts, i, ConfigFieldServerCert)
}
serverKey := ""
if serverKeyVal != nil {
serverKey, ok = serverKeyVal.(string)
if !ok {
return nil, fmt.Errorf("value [%s%s[%d].%s] must be a string", pathContext, ConfigFieldAltServerCerts, i, ConfigFieldServerKey)
}
}
idConfig.AltServerCerts = append(idConfig.AltServerCerts, ServerPair{
ServerCert: serverCert,
ServerKey: serverKey,
})
} else {
return nil, fmt.Errorf("value [%s%s[%d]] must be an obbject", pathContext, ConfigFieldAltServerCerts, i)
}
}
} else {
return nil, fmt.Errorf("value [%s%s] must be an array", pathContext, ConfigFieldAltServerCerts)
}
} //not required
if caInterface, ok := identityMap[ConfigFieldCa]; ok {
if ca, ok := caInterface.(string); ok {
idConfig.CA = ca
} else {
return nil, fmt.Errorf("value [%s%s] must be a string", pathContext, ConfigFieldCa)
}
}
return idConfig, nil
}