-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathextractor.go
91 lines (77 loc) · 2.22 KB
/
extractor.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
package endpoint
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
import (
"reflect"
"sync"
"github.com/hyperledger-labs/fabric-smart-client/platform/view/driver"
"github.com/pkg/errors"
"go.uber.org/zap/zapcore"
)
func NewPKIExtractor() *PKIExtractor {
return &PKIExtractor{
publicKeyExtractors: []driver.PublicKeyExtractor{},
publicKeyIDSynthesizer: DefaultPublicKeyIDSynthesizer{},
}
}
type PKIExtractor struct {
pkiExtractorsLock sync.RWMutex
publicKeyExtractors []driver.PublicKeyExtractor
publicKeyIDSynthesizer driver.PublicKeyIDSynthesizer
}
func (r *PKIExtractor) AddPublicKeyExtractor(publicKeyExtractor driver.PublicKeyExtractor) error {
r.pkiExtractorsLock.Lock()
defer r.pkiExtractorsLock.Unlock()
if publicKeyExtractor == nil {
return errors.New("pki resolver should not be nil")
}
r.publicKeyExtractors = append(r.publicKeyExtractors, publicKeyExtractor)
return nil
}
func (r *PKIExtractor) SetPublicKeyIDSynthesizer(publicKeyIDSynthesizer driver.PublicKeyIDSynthesizer) {
r.publicKeyIDSynthesizer = publicKeyIDSynthesizer
}
func (r *PKIExtractor) PkiResolve(resolver *Resolver) []byte {
resolver.PKILock.RLock()
if len(resolver.PKI) != 0 {
resolver.PKILock.RUnlock()
return resolver.PKI
}
resolver.PKILock.RUnlock()
resolver.PKILock.Lock()
defer resolver.PKILock.Unlock()
if len(resolver.PKI) == 0 {
resolver.PKI = r.ExtractPKI(resolver.Id)
}
return resolver.PKI
}
func (r *PKIExtractor) ExtractPKI(id []byte) []byte {
r.pkiExtractorsLock.RLock()
defer r.pkiExtractorsLock.RUnlock()
for _, extractor := range r.publicKeyExtractors {
if pk, err := extractor.ExtractPublicKey(id); pk != nil {
if logger.IsEnabledFor(zapcore.DebugLevel) {
logger.Debugf("pki resolved for [%s]", id)
}
return r.publicKeyIDSynthesizer.PublicKeyID(pk)
} else {
if logger.IsEnabledFor(zapcore.DebugLevel) {
logger.Debugf("pki not resolved by [%s] for [%s]: [%s]", getIdentifier(extractor), id, err)
}
}
}
logger.Warnf("cannot resolve pki for [%s]", id)
return nil
}
func getIdentifier(f any) string {
if f == nil {
return "<nil view>"
}
t := reflect.TypeOf(f)
for t.Kind() == reflect.Ptr {
t = t.Elem()
}
return t.PkgPath() + "/" + t.Name()
}