forked from spiffe/spire
/
plugintypes.go
135 lines (120 loc) · 3.38 KB
/
plugintypes.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
package x509certificate
import (
"crypto/x509"
plugintypes "github.com/accuknox/spire-plugin-sdk/proto/spire/plugin/types"
"github.com/accuknox/spire/proto/spire/common"
apitypes "github.com/spiffe/spire-api-sdk/proto/spire/api/types"
)
func FromPluginProto(pb *plugintypes.X509Certificate) (*x509.Certificate, error) {
return fromProtoFields(pb.Asn1)
}
func FromPluginProtos(pbs []*plugintypes.X509Certificate) ([]*x509.Certificate, error) {
if pbs == nil {
return nil, nil
}
x509Certificates := make([]*x509.Certificate, 0, len(pbs))
for _, pb := range pbs {
x509Certificate, err := FromPluginProto(pb)
if err != nil {
return nil, err
}
x509Certificates = append(x509Certificates, x509Certificate)
}
return x509Certificates, nil
}
func ToPluginProto(x509Certificate *x509.Certificate) (*plugintypes.X509Certificate, error) {
asn1, err := toProtoFields(x509Certificate)
if err != nil {
return nil, err
}
return &plugintypes.X509Certificate{
Asn1: asn1,
}, nil
}
func ToPluginProtos(x509Certificates []*x509.Certificate) ([]*plugintypes.X509Certificate, error) {
if x509Certificates == nil {
return nil, nil
}
pbs := make([]*plugintypes.X509Certificate, 0, len(x509Certificates))
for _, x509Certificate := range x509Certificates {
pb, err := ToPluginProto(x509Certificate)
if err != nil {
return nil, err
}
pbs = append(pbs, pb)
}
return pbs, nil
}
func ToPluginFromCommonProtos(pbs []*common.Certificate) ([]*plugintypes.X509Certificate, error) {
certs, err := FromCommonProtos(pbs)
if err != nil {
return nil, err
}
return ToPluginProtos(certs)
}
func RawFromPluginProto(pb *plugintypes.X509Certificate) ([]byte, error) {
return rawFromProtoFields(pb.Asn1)
}
func RawFromPluginProtos(pbs []*plugintypes.X509Certificate) ([][]byte, error) {
if pbs == nil {
return nil, nil
}
rawX509Certificates := make([][]byte, 0, len(pbs))
for _, pb := range pbs {
rawX509Certificate, err := RawFromPluginProto(pb)
if err != nil {
return nil, err
}
rawX509Certificates = append(rawX509Certificates, rawX509Certificate)
}
return rawX509Certificates, nil
}
func RawToPluginProto(rawX509Certificate []byte) (*plugintypes.X509Certificate, error) {
asn1, err := rawToProtoFields(rawX509Certificate)
if err != nil {
return nil, err
}
return &plugintypes.X509Certificate{
Asn1: asn1,
}, nil
}
func RawToPluginProtos(rawX509Certificates [][]byte) ([]*plugintypes.X509Certificate, error) {
if rawX509Certificates == nil {
return nil, nil
}
pbs := make([]*plugintypes.X509Certificate, 0, len(rawX509Certificates))
for _, rawX509Certificate := range rawX509Certificates {
pb, err := RawToPluginProto(rawX509Certificate)
if err != nil {
return nil, err
}
pbs = append(pbs, pb)
}
return pbs, nil
}
func ToPluginFromAPIProto(pb *apitypes.X509Certificate) (*plugintypes.X509Certificate, error) {
if pb == nil {
return nil, nil
}
asn1, err := rawFromProtoFields(pb.Asn1)
if err != nil {
return nil, err
}
return &plugintypes.X509Certificate{
Asn1: asn1,
}, nil
}
func ToPluginFromAPIProtos(pbs []*apitypes.X509Certificate) ([]*plugintypes.X509Certificate, error) {
if pbs == nil {
return nil, nil
}
var x509Authorities []*plugintypes.X509Certificate
for _, pb := range pbs {
authority, err := ToPluginFromAPIProto(pb)
if err != nil {
return nil, err
}
x509Authorities = append(x509Authorities, authority)
}
return x509Authorities, nil
}