This repository has been archived by the owner on Mar 6, 2024. It is now read-only.
forked from s7techlab/hlf-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
tls_certs_mapper.go
138 lines (111 loc) · 3.44 KB
/
tls_certs_mapper.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
package discovery
import (
"sync"
"github.com/atomyze-ru/hlf-sdk-go/api"
"github.com/atomyze-ru/hlf-sdk-go/api/config"
)
// implementation of tlsConfigMapper interface
var _ tlsConfigMapper = (*TLSCertsMapper)(nil)
// TLSCertsMapper - if tls is enabled with gossip maps provided from cfg TLS certs to discovered peers
type TLSCertsMapper struct {
addrCfgMap map[string]*config.TlsConfig
lock sync.RWMutex
}
func NewTLSCertsMapper(certsCfg []config.TLSCertsMapperConfig) *TLSCertsMapper {
addrCfgMap := map[string]*config.TlsConfig{}
for i := range certsCfg {
addrCfgMap[certsCfg[i].Address] = &certsCfg[i].TlsConfig
}
return &TLSCertsMapper{addrCfgMap: addrCfgMap, lock: sync.RWMutex{}}
}
// TlsConfigForAddress - get tls config for provided address
// if config wasn't provided on startup time return disabled tls
func (m *TLSCertsMapper) TlsConfigForAddress(address string) *config.TlsConfig {
m.lock.RLock()
defer m.lock.RUnlock()
v, ok := m.addrCfgMap[address]
if ok {
return v
}
return &config.TlsConfig{
Enabled: false,
}
}
/*
decorators over api.ChaincodeDiscoverer/ChannelDiscoverer
adds TLS settings(if they were provided in cfg) for discovered peers
*/
type chaincodeDiscovererTLSDecorator struct {
target api.ChaincodeDiscoverer
tlsMapper tlsConfigMapper
}
func newChaincodeDiscovererTLSDecorator(
target api.ChaincodeDiscoverer,
tlsMapper tlsConfigMapper,
) *chaincodeDiscovererTLSDecorator {
return &chaincodeDiscovererTLSDecorator{
target: target,
tlsMapper: tlsMapper,
}
}
func (d *chaincodeDiscovererTLSDecorator) Endorsers() []*api.HostEndpoint {
return addTLSSettings(d.target.Endorsers(), d.tlsMapper)
}
func (d *chaincodeDiscovererTLSDecorator) Orderers() []*api.HostEndpoint {
return addTLSSettings(d.target.Orderers(), d.tlsMapper)
}
func (d *chaincodeDiscovererTLSDecorator) ChaincodeVersion() string {
return d.target.ChaincodeVersion()
}
func (d *chaincodeDiscovererTLSDecorator) ChaincodeName() string {
return d.target.ChaincodeName()
}
func (d *chaincodeDiscovererTLSDecorator) ChannelName() string {
return d.target.ChannelName()
}
/* */
type channelDiscovererTLSDecorator struct {
target api.ChannelDiscoverer
tlsMapper tlsConfigMapper
}
func newChannelDiscovererTLSDecorator(
target api.ChannelDiscoverer,
tlsMapper tlsConfigMapper,
) *channelDiscovererTLSDecorator {
return &channelDiscovererTLSDecorator{
target: target,
tlsMapper: tlsMapper,
}
}
func (d *channelDiscovererTLSDecorator) Orderers() []*api.HostEndpoint {
return addTLSSettings(d.target.Orderers(), d.tlsMapper)
}
func (d *channelDiscovererTLSDecorator) ChannelName() string {
return d.target.ChannelName()
}
func addTLSSettings(endpoints []*api.HostEndpoint, tlsMapper tlsConfigMapper) []*api.HostEndpoint {
for i := range endpoints {
for j := range endpoints[i].HostAddresses {
tlsCfg := tlsMapper.TlsConfigForAddress(endpoints[i].HostAddresses[j].Address)
endpoints[i].HostAddresses[j].TLSSettings = *tlsCfg
}
}
return endpoints
}
/* */
type localPeersDiscovererTLSDecorator struct {
target api.LocalPeersDiscoverer
tlsMapper tlsConfigMapper
}
func newLocalPeersDiscovererTLSDecorator(
target api.LocalPeersDiscoverer,
tlsMapper tlsConfigMapper,
) *localPeersDiscovererTLSDecorator {
return &localPeersDiscovererTLSDecorator{
target: target,
tlsMapper: tlsMapper,
}
}
func (d *localPeersDiscovererTLSDecorator) Peers() []*api.HostEndpoint {
return addTLSSettings(d.target.Peers(), d.tlsMapper)
}