-
Notifications
You must be signed in to change notification settings - Fork 35
/
config.go
364 lines (312 loc) · 8.32 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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
* Copyright contributors to the Hyperledger Fabric Operator project
*
* SPDX-License-Identifier: Apache-2.0
*
* 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:
*
* http://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.
*/
package config
import (
"crypto/x509"
"encoding/pem"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/pkg/errors"
v1 "github.com/IBM-Blockchain/fabric-operator/pkg/apis/ca/v1"
"github.com/IBM-Blockchain/fabric-operator/pkg/util"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/yaml"
)
type Type string
const (
EnrollmentCA Type = "enrollment"
TLSCA Type = "tls"
)
func (t Type) Is(typ Type) bool {
return t == typ
}
type InputType string
var (
File InputType = "File"
Pem InputType = "Pem"
Base64 InputType = "Base64"
Bccsp InputType = "Bccsp"
)
var log = logf.Log.WithName("initializer_config")
type Config struct {
ServerConfig *v1.ServerConfig
HomeDir string
MountPath string
Update bool
SqlitePath string
tlsCrypto map[string][]byte
dbCrypto map[string][]byte
caCrypto map[string][]byte
operationsCrypto map[string][]byte
intermediateCrypto map[string][]byte
}
func (c *Config) GetServerConfig() *v1.ServerConfig {
return c.ServerConfig
}
func (c *Config) GetHomeDir() string {
return c.HomeDir
}
func (c *Config) GetTLSCrypto() map[string][]byte {
return c.tlsCrypto
}
func (c *Config) HandleCertInput(input, location string, store map[string][]byte) error {
var err error
inputType := GetInputType(input)
log.Info(fmt.Sprintf("Handling input of cert type '%s', to be stored at '%s'", inputType, location))
data := []byte{}
switch inputType {
case Pem:
data = util.PemStringToBytes(input)
err = c.StoreInMap(data, location, store)
if err != nil {
return err
}
case File:
// On an update of config overrides, file is not a valid override value as the operator
// won't have access to it. Cert can only be passed as base64.
if !c.Update {
data, err = util.FileToBytes(input)
if err != nil {
return err
}
err = c.StoreInMap(data, location, store)
if err != nil {
return err
}
}
case Base64:
data, err = util.Base64ToBytes(input)
if err != nil {
return err
}
err = c.StoreInMap(data, location, store)
if err != nil {
return err
}
case Bccsp:
return nil
default:
return errors.Errorf("invalid input type: %s", input)
}
if len(data) != 0 {
err := c.EnsureDirAndWriteFile(location, data)
if err != nil {
return err
}
}
return nil
}
func (c *Config) EnsureDirAndWriteFile(location string, data []byte) error {
path := filepath.Join(c.HomeDir, location)
err := util.EnsureDir(filepath.Dir(path))
if err != nil {
return err
}
err = ioutil.WriteFile(filepath.Clean(path), data, 0600)
if err != nil {
return err
}
return nil
}
func (c *Config) HandleKeyInput(input, location string, store map[string][]byte) error {
var err error
inputType := GetInputType(input)
log.Info(fmt.Sprintf("Handling input of key type '%s', to be stored at '%s'", inputType, location))
data := []byte{}
switch inputType {
case Pem:
data = util.PemStringToBytes(input)
err = c.StoreInMap(data, location, store)
if err != nil {
return err
}
case File:
// On an update of config overrides, file is not a valid override value as the operator
// won't have access to it. Key can only be passed as base64.
if !c.Update {
data, err = util.FileToBytes(input)
if err != nil {
return err
}
err = c.StoreInMap(data, location, store)
if err != nil {
return err
}
}
case Base64:
data, err = util.Base64ToBytes(input)
if err != nil {
return err
}
err = c.StoreInMap(data, location, store)
if err != nil {
return err
}
case Bccsp:
// If HSM enabled, don't try to read key from file system
if c.UsingPKCS11() {
return nil
}
// On an update of config overrides, reading from keystore is not valid. After init create
// the key stored in a kubernetes secret and operator won't have access to it.
if !c.Update {
data, err = c.GetSigningKey(c.HomeDir)
if err != nil {
return err
}
err = c.StoreInMap(data, location, store)
if err != nil {
return err
}
}
default:
return errors.Errorf("invalid input type: %s", input)
}
if len(data) != 0 {
err := c.EnsureDirAndWriteFile(location, data)
if err != nil {
return err
}
}
return nil
}
func (c *Config) StoreInMap(data []byte, location string, store map[string][]byte) error {
if len(data) == 0 {
return nil
}
key := ConvertStringForSecrets(location, true)
store[key] = data
return nil
}
// GetSigningKey applies to non-hsm use cases where the key exists on the filesystem.
// The filesystem is read and then key is then stored in a kubernetes secret.
func (c *Config) GetSigningKey(path string) ([]byte, error) {
keystoreDir := filepath.Join(path, "msp", "keystore")
files, err := ioutil.ReadDir(keystoreDir)
if err != nil {
return nil, err
}
if len(files) == 0 {
return nil, fmt.Errorf("no keys found in keystore directory: %s", keystoreDir)
}
// Need this loop to find appropriate key. Three files are generated
// by default by the CA: IssuerRevocationPrivateKey, IssuerSecretKey, and *_sk
// We are only interested in file ending with 'sk' which the is Private Key
// associated with the x509 certificate
for _, file := range files {
fileBytes, err := ioutil.ReadFile(filepath.Clean(filepath.Join(keystoreDir, file.Name())))
if err != nil {
return nil, err
}
block, _ := pem.Decode(fileBytes)
if block == nil {
continue
}
_, err = x509.ParsePKCS8PrivateKey(block.Bytes)
if err == nil {
return fileBytes, nil
}
}
return nil, errors.Errorf("failed to parse CA's private key")
}
func (c *Config) SetUpdate(update bool) {
c.Update = update
}
func (c *Config) SetServerConfig(cfg *v1.ServerConfig) {
c.ServerConfig = cfg
}
func (c *Config) SetMountPaths(caType Type) {
switch caType {
case EnrollmentCA:
c.CAMountPath()
c.DBMountPath()
c.IntermediateMountPath()
c.OperationsMountPath()
c.TLSMountPath()
case TLSCA:
c.CAMountPath()
c.DBMountPath()
}
}
func (c *Config) UsingPKCS11() bool {
if c.ServerConfig != nil && c.ServerConfig.CAConfig.CSP != nil {
if strings.ToLower(c.ServerConfig.CAConfig.CSP.ProviderName) == "pkcs11" {
return true
}
}
return false
}
func GetInputType(input string) InputType {
data := []byte(input)
block, _ := pem.Decode(data)
if block != nil {
return Pem
}
data, err := util.Base64ToBytes(input)
if err == nil && data != nil {
return Base64
}
// If input string is found as an already exisiting file, return CertFile type
_, err = os.Stat(input)
if err == nil {
return File
}
return Bccsp
}
func ConvertStringForSecrets(filepath string, forward bool) string {
// shared//tlsca//db/certs/certfile0.pem
if forward {
return strings.Replace(filepath, "/", "_", -1)
}
// data[shared__tlsca__db_certs_certfile0.pem
return strings.Replace(filepath, "_", "/", -1)
}
func IsValidPostgressDatasource(datasourceStr string) bool {
regexpssions := []string{`host=\S+`, `port=\d+`, `user=\S+`, `password=\S+`, `dbname=\S+`, `sslmode=\S+`}
for _, regexpression := range regexpssions {
re := regexp.MustCompile(regexpression)
matches := len(re.FindStringSubmatch(datasourceStr))
if matches == 0 {
return false
}
}
return true
}
func ValidCryptoInput(certFile, keyFile string) error {
if certFile == "" && keyFile != "" {
return errors.New("Key file specified but no corresponding certificate file specified, both must be passed")
}
if certFile != "" && keyFile == "" {
return errors.New("Certificate file specified but no corresponding key file specified, both must be passed")
}
return nil
}
func ReadFrom(from *[]byte) (*Config, error) {
config := &v1.ServerConfig{}
err := yaml.Unmarshal(*from, config)
if err != nil {
return nil, err
}
return &Config{
ServerConfig: config,
}, nil
}