forked from spiffe/spire
/
entry.go
195 lines (171 loc) · 5.15 KB
/
entry.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package api
import (
"context"
"errors"
"fmt"
"github.com/accuknox/go-spiffe/v2/spiffeid"
"github.com/accuknox/spire/pkg/common/protoutil"
"github.com/accuknox/spire/pkg/common/x509util"
"github.com/accuknox/spire/proto/spire/common"
"github.com/spiffe/spire-api-sdk/proto/spire/api/types"
)
// RegistrationEntriesToProto converts RegistrationEntry's into Entry's
func RegistrationEntriesToProto(es []*common.RegistrationEntry) ([]*types.Entry, error) {
if es == nil {
return nil, nil
}
pbs := make([]*types.Entry, 0, len(es))
for _, e := range es {
pb, err := RegistrationEntryToProto(e)
if err != nil {
return nil, err
}
pbs = append(pbs, pb)
}
return pbs, nil
}
// RegistrationEntryToProto converts RegistrationEntry into types Entry
func RegistrationEntryToProto(e *common.RegistrationEntry) (*types.Entry, error) {
if e == nil {
return nil, errors.New("missing registration entry")
}
spiffeID, err := spiffeid.FromString(e.SpiffeId)
if err != nil {
return nil, fmt.Errorf("invalid SPIFFE ID: %w", err)
}
parentID, err := spiffeid.FromString(e.ParentId)
if err != nil {
return nil, fmt.Errorf("invalid parent ID: %w", err)
}
var federatesWith []string
if len(e.FederatesWith) > 0 {
federatesWith = make([]string, 0, len(e.FederatesWith))
for _, trustDomainID := range e.FederatesWith {
td, err := spiffeid.TrustDomainFromString(trustDomainID)
if err != nil {
return nil, fmt.Errorf("invalid federated trust domain: %w", err)
}
federatesWith = append(federatesWith, td.String())
}
}
return &types.Entry{
Id: e.EntryId,
SpiffeId: ProtoFromID(spiffeID),
ParentId: ProtoFromID(parentID),
Selectors: ProtoFromSelectors(e.Selectors),
X509SvidTtl: e.X509SvidTtl,
FederatesWith: federatesWith,
Admin: e.Admin,
Downstream: e.Downstream,
ExpiresAt: e.EntryExpiry,
DnsNames: append([]string(nil), e.DnsNames...),
RevisionNumber: e.RevisionNumber,
StoreSvid: e.StoreSvid,
JwtSvidTtl: e.JwtSvidTtl,
}, nil
}
// ProtoToRegistrationEntry converts and validate entry into common registration entry
func ProtoToRegistrationEntry(ctx context.Context, td spiffeid.TrustDomain, e *types.Entry) (*common.RegistrationEntry, error) {
return ProtoToRegistrationEntryWithMask(ctx, td, e, nil)
}
// ProtoToRegistrationEntryWithMask converts and validate entry into common registration entry,
// while allowing empty values for SpiffeId, ParentId, and Selectors IF their corresponding values
// in the mask are false.
// This allows the user to not specify these fields while updating using a mask.
// All other fields are allowed to be empty (with or without a mask).
func ProtoToRegistrationEntryWithMask(ctx context.Context, td spiffeid.TrustDomain, e *types.Entry, mask *types.EntryMask) (_ *common.RegistrationEntry, err error) {
if e == nil {
return nil, errors.New("missing entry")
}
if mask == nil {
mask = protoutil.AllTrueEntryMask
}
var parentID spiffeid.ID
if mask.ParentId {
parentID, err = TrustDomainMemberIDFromProto(ctx, td, e.ParentId)
if err != nil {
return nil, fmt.Errorf("invalid parent ID: %w", err)
}
}
var spiffeID spiffeid.ID
if mask.SpiffeId {
spiffeID, err = TrustDomainWorkloadIDFromProto(ctx, td, e.SpiffeId)
if err != nil {
return nil, fmt.Errorf("invalid spiffe ID: %w", err)
}
}
var admin bool
if mask.Admin {
admin = e.Admin
}
var dnsNames []string
if mask.DnsNames {
dnsNames = make([]string, 0, len(e.DnsNames))
for _, dnsName := range e.DnsNames {
if err := x509util.ValidateDNS(dnsName); err != nil {
return nil, fmt.Errorf("invalid DNS name: %w", err)
}
dnsNames = append(dnsNames, dnsName)
}
}
var downstream bool
if mask.Downstream {
downstream = e.Downstream
}
var expiresAt int64
if mask.ExpiresAt {
expiresAt = e.ExpiresAt
}
var federatesWith []string
if mask.FederatesWith {
federatesWith = make([]string, 0, len(e.FederatesWith))
for _, trustDomainName := range e.FederatesWith {
td, err := spiffeid.TrustDomainFromString(trustDomainName)
if err != nil {
return nil, fmt.Errorf("invalid federated trust domain: %w", err)
}
federatesWith = append(federatesWith, td.IDString())
}
}
var selectors []*common.Selector
if mask.Selectors {
if len(e.Selectors) == 0 {
return nil, errors.New("selector list is empty")
}
selectors, err = SelectorsFromProto(e.Selectors)
if err != nil {
return nil, err
}
}
var revisionNumber int64
if mask.RevisionNumber {
revisionNumber = e.RevisionNumber
}
var storeSVID bool
if mask.StoreSvid {
storeSVID = e.StoreSvid
}
var x509SvidTTL int32
if mask.X509SvidTtl {
x509SvidTTL = e.X509SvidTtl
}
var jwtSvidTTL int32
if mask.JwtSvidTtl {
jwtSvidTTL = e.JwtSvidTtl
}
return &common.RegistrationEntry{
EntryId: e.Id,
ParentId: parentID.String(),
SpiffeId: spiffeID.String(),
Admin: admin,
DnsNames: dnsNames,
Downstream: downstream,
EntryExpiry: expiresAt,
FederatesWith: federatesWith,
Selectors: selectors,
RevisionNumber: revisionNumber,
StoreSvid: storeSVID,
X509SvidTtl: x509SvidTTL,
JwtSvidTtl: jwtSvidTTL,
}, nil
}