forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.go
136 lines (116 loc) · 3.69 KB
/
connection.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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package comm
import (
"crypto/tls"
"crypto/x509"
"sync"
"github.com/hyperledger/fabric/common/channelconfig"
"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/msp"
"google.golang.org/grpc/credentials"
)
var commLogger = flogging.MustGetLogger("comm")
// CredentialSupport type manages credentials used for gRPC client connections
type CredentialSupport struct {
mutex sync.RWMutex
appRootCAsByChain map[string][][]byte
serverRootCAs [][]byte
clientCert tls.Certificate
}
// NewCredentialSupport creates a CredentialSupport instance.
func NewCredentialSupport(rootCAs ...[]byte) *CredentialSupport {
return &CredentialSupport{
appRootCAsByChain: make(map[string][][]byte),
serverRootCAs: rootCAs,
}
}
// SetClientCertificate sets the tls.Certificate to use for gRPC client
// connections
func (cs *CredentialSupport) SetClientCertificate(cert tls.Certificate) {
cs.mutex.Lock()
cs.clientCert = cert
cs.mutex.Unlock()
}
// GetClientCertificate returns the client certificate of the CredentialSupport
func (cs *CredentialSupport) GetClientCertificate() tls.Certificate {
cs.mutex.RLock()
defer cs.mutex.RUnlock()
return cs.clientCert
}
// GetPeerCredentials returns gRPC transport credentials for use by gRPC
// clients which communicate with remote peer endpoints.
func (cs *CredentialSupport) GetPeerCredentials() credentials.TransportCredentials {
cs.mutex.RLock()
defer cs.mutex.RUnlock()
var appRootCAs [][]byte
appRootCAs = append(appRootCAs, cs.serverRootCAs...)
for _, appRootCA := range cs.appRootCAsByChain {
appRootCAs = append(appRootCAs, appRootCA...)
}
certPool := x509.NewCertPool()
for _, appRootCA := range appRootCAs {
err := AddPemToCertPool(appRootCA, certPool)
if err != nil {
commLogger.Warningf("Failed adding certificates to peer's client TLS trust pool: %s", err)
}
}
return credentials.NewTLS(&tls.Config{
Certificates: []tls.Certificate{cs.clientCert},
RootCAs: certPool,
})
}
func (cs *CredentialSupport) AppRootCAsByChain() map[string][][]byte {
cs.mutex.RLock()
defer cs.mutex.RUnlock()
return cs.appRootCAsByChain
}
// BuildTrustedRootsForChain populates the appRootCAs and orderRootCAs maps by
// getting the root and intermediate certs for all msps associated with the
// MSPManager.
func (cs *CredentialSupport) BuildTrustedRootsForChain(cm channelconfig.Resources) {
appOrgMSPs := make(map[string]struct{})
if ac, ok := cm.ApplicationConfig(); ok {
for _, appOrg := range ac.Organizations() {
appOrgMSPs[appOrg.MSPID()] = struct{}{}
}
}
ordOrgMSPs := make(map[string]struct{})
if ac, ok := cm.OrdererConfig(); ok {
for _, ordOrg := range ac.Organizations() {
ordOrgMSPs[ordOrg.MSPID()] = struct{}{}
}
}
cid := cm.ConfigtxValidator().ChannelID()
msps, err := cm.MSPManager().GetMSPs()
if err != nil {
commLogger.Errorf("Error getting root CAs for channel %s (%s)", cid, err)
return
}
var appRootCAs [][]byte
for k, v := range msps {
// we only support the fabric MSP
if v.GetType() != msp.FABRIC {
continue
}
for _, root := range v.GetTLSRootCerts() {
// check to see of this is an app org MSP
if _, ok := appOrgMSPs[k]; ok {
commLogger.Debugf("adding app root CAs for MSP [%s]", k)
appRootCAs = append(appRootCAs, root)
}
}
for _, intermediate := range v.GetTLSIntermediateCerts() {
// check to see of this is an app org MSP
if _, ok := appOrgMSPs[k]; ok {
commLogger.Debugf("adding app root CAs for MSP [%s]", k)
appRootCAs = append(appRootCAs, intermediate)
}
}
}
cs.mutex.Lock()
cs.appRootCAsByChain[cid] = appRootCAs
cs.mutex.Unlock()
}