forked from SmartBFT-Go/fabric-sdk-go
/
check_cert_attributes.go
52 lines (45 loc) · 1.47 KB
/
check_cert_attributes.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
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package msp
import (
"crypto/x509"
"encoding/pem"
"testing"
"github.com/hyperledger/fabric-sdk-go/internal/github.com/hyperledger/fabric/common/attrmgr"
"github.com/hyperledger/fabric-sdk-go/pkg/client/msp"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
)
// nolint: deadcode
func checkCertAttributes(t *testing.T, certBytes []byte, expected []msp.Attribute) {
attrs, err := getCertAttributes(certBytes)
require.NoError(t, err)
for _, a := range expected {
v, ok, err := attrs.Value(a.Name)
require.NoError(t, err)
require.True(t, attrs.Contains(a.Name), "does not contain attribute '%s'", a.Name)
require.True(t, ok, "attribute '%s' was not found", a.Name)
require.True(t, v == a.Value, "incorrect value for '%s'; expected '%s' but found '%s'", a.Name, a.Value, v)
}
}
func getCertAttributes(certBytes []byte) (*attrmgr.Attributes, error) {
decoded, _ := pem.Decode(certBytes)
if decoded == nil {
return nil, errors.New("Failed cert decoding")
}
cert, err := x509.ParseCertificate(decoded.Bytes)
if err != nil {
return nil, errors.Errorf("failed to parse certificate: %s", err)
}
if cert == nil {
return nil, errors.Errorf("failed to parse certificate: %s", err)
}
mgr := attrmgr.New()
attrs, err := mgr.GetAttributesFromCert(cert)
if err != nil {
return nil, errors.Errorf("Failed to GetAttributesFromCert: %s", err)
}
return attrs, nil
}