-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
72 lines (64 loc) · 1.79 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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package comm
import (
"io/ioutil"
"time"
"github.com/hyperledger/fabric/common/crypto/tlsgen"
"github.com/hyperledger/fabric/core/comm"
"github.com/pkg/errors"
)
type genTLSCertFunc func() (*tlsgen.CertKeyPair, error)
// Config defines configuration of a Client
type Config struct {
CertPath string
KeyPath string
PeerCACertPath string
Timeout time.Duration
}
// ToSecureOptions converts this Config to SecureOptions.
// The given function generates a self signed client TLS certificate if
// the TLS certificate and key aren't present at the config
func (conf Config) ToSecureOptions(newSelfSignedTLSCert genTLSCertFunc) (*comm.SecureOptions, error) {
if conf.PeerCACertPath == "" {
return &comm.SecureOptions{}, nil
}
caBytes, err := loadFile(conf.PeerCACertPath)
if err != nil {
return nil, errors.WithStack(err)
}
var keyBytes, certBytes []byte
// If TLS key and certificate aren't given, generate a self signed one on the fly
if conf.KeyPath == "" && conf.CertPath == "" {
tlsCert, err := newSelfSignedTLSCert()
if err != nil {
return nil, err
}
keyBytes, certBytes = tlsCert.Key, tlsCert.Cert
} else {
keyBytes, err = loadFile(conf.KeyPath)
if err != nil {
return nil, errors.WithStack(err)
}
certBytes, err = loadFile(conf.CertPath)
if err != nil {
return nil, errors.WithStack(err)
}
}
return &comm.SecureOptions{
Key: keyBytes,
Certificate: certBytes,
UseTLS: true,
ServerRootCAs: [][]byte{caBytes},
RequireClientCert: true,
}, nil
}
func loadFile(path string) ([]byte, error) {
b, err := ioutil.ReadFile(path)
if err != nil {
return nil, errors.Errorf("Failed opening file %s: %v", path, err)
}
return b, nil
}