-
Notifications
You must be signed in to change notification settings - Fork 485
/
update.go
253 lines (205 loc) · 7.16 KB
/
update.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package entry
import (
"context"
"errors"
"flag"
"github.com/mitchellh/cli"
entryv1 "github.com/spiffe/spire-api-sdk/proto/spire/api/server/entry/v1"
"github.com/spiffe/spire-api-sdk/proto/spire/api/types"
"github.com/spiffe/spire/cmd/spire-server/util"
commoncli "github.com/spiffe/spire/pkg/common/cli"
"github.com/spiffe/spire/pkg/common/cliprinter"
"google.golang.org/grpc/codes"
)
// NewUpdateCommand creates a new "update" subcommand for "entry" command.
func NewUpdateCommand() cli.Command {
return newUpdateCommand(commoncli.DefaultEnv)
}
func newUpdateCommand(env *commoncli.Env) cli.Command {
return util.AdaptCommand(env, &updateCommand{env: env})
}
type updateCommand struct {
// Path to an optional data file. If set, other
// opts will be ignored.
path string
// Registration entry id to update
entryID string
// Type and value are delimited by a colon (:)
// ex. "unix:uid:1000" or "spiffe_id:spiffe://example.org/foo"
selectors StringsFlag
// Workload parent spiffeID
parentID string
// Workload spiffeID
spiffeID string
// whether the entry is for a downstream SPIRE server
downstream bool
// TTL for x509 SVIDs issued to this workload
x509SvidTTL int
// TTL for JWT SVIDs issued to this workload
jwtSvidTTL int
// List of SPIFFE IDs of trust domains the registration entry is federated with
federatesWith StringsFlag
// whether the registration entry is for an "admin" workload
admin bool
// Expiry of entry
entryExpiry int64
// DNSNames entries for SVIDs based on this entry
dnsNames StringsFlag
// storeSVID determines if the issued SVID must be stored through an SVIDStore plugin
storeSVID bool
// Entry hint, used to disambiguate entries with the same SPIFFE ID
hint string
printer cliprinter.Printer
env *commoncli.Env
}
func (*updateCommand) Name() string {
return "entry update"
}
func (*updateCommand) Synopsis() string {
return "Updates registration entries"
}
func (c *updateCommand) AppendFlags(f *flag.FlagSet) {
f.StringVar(&c.entryID, "entryID", "", "The Registration Entry ID of the record to update")
f.StringVar(&c.parentID, "parentID", "", "The SPIFFE ID of this record's parent")
f.StringVar(&c.spiffeID, "spiffeID", "", "The SPIFFE ID that this record represents")
f.IntVar(&c.x509SvidTTL, "x509SVIDTTL", 0, "The lifetime, in seconds, for x509-SVIDs issued based on this registration entry.")
f.IntVar(&c.jwtSvidTTL, "jwtSVIDTTL", 0, "The lifetime, in seconds, for JWT-SVIDs issued based on this registration entry.")
f.StringVar(&c.path, "data", "", "Path to a file containing registration JSON (optional). If set to '-', read the JSON from stdin.")
f.Var(&c.selectors, "selector", "A colon-delimited type:value selector. Can be used more than once")
f.Var(&c.federatesWith, "federatesWith", "SPIFFE ID of a trust domain to federate with. Can be used more than once")
f.BoolVar(&c.admin, "admin", false, "If set, the SPIFFE ID in this entry will be granted access to the SPIRE Server's management APIs")
f.BoolVar(&c.downstream, "downstream", false, "A boolean value that, when set, indicates that the entry describes a downstream SPIRE server")
f.BoolVar(&c.storeSVID, "storeSVID", false, "A boolean value that, when set, indicates that the resulting issued SVID from this entry must be stored through an SVIDStore plugin")
f.Int64Var(&c.entryExpiry, "entryExpiry", 0, "An expiry, from epoch in seconds, for the resulting registration entry to be pruned")
f.Var(&c.dnsNames, "dns", "A DNS name that will be included in SVIDs issued based on this entry, where appropriate. Can be used more than once")
f.StringVar(&c.hint, "hint", "", "The entry hint, used to disambiguate entries with the same SPIFFE ID")
cliprinter.AppendFlagWithCustomPretty(&c.printer, f, c.env, prettyPrintUpdate)
}
func (c *updateCommand) Run(ctx context.Context, _ *commoncli.Env, serverClient util.ServerClient) error {
if err := c.validate(); err != nil {
return err
}
var entries []*types.Entry
var err error
if c.path != "" {
entries, err = parseFile(c.path)
} else {
entries, err = c.parseConfig()
}
if err != nil {
return err
}
resp, err := updateEntries(ctx, serverClient.NewEntryClient(), entries)
if err != nil {
return err
}
return c.printer.PrintProto(resp)
}
// validate performs basic validation, even on fields that we
// have defaults defined for
func (c *updateCommand) validate() (err error) {
// If a path is set, we have all we need
if c.path != "" {
return nil
}
if c.entryID == "" {
return errors.New("entry ID is required")
}
if len(c.selectors) < 1 {
return errors.New("at least one selector is required")
}
if c.parentID == "" {
return errors.New("a parent ID is required")
}
if c.spiffeID == "" {
return errors.New("a SPIFFE ID is required")
}
if c.x509SvidTTL < 0 {
return errors.New("a positive x509-SVID TTL is required")
}
if c.jwtSvidTTL < 0 {
return errors.New("a positive JWT-SVID TTL is required")
}
return nil
}
// parseConfig builds a registration entry from the given config
func (c *updateCommand) parseConfig() ([]*types.Entry, error) {
parentID, err := idStringToProto(c.parentID)
if err != nil {
return nil, err
}
spiffeID, err := idStringToProto(c.spiffeID)
if err != nil {
return nil, err
}
e := &types.Entry{
Id: c.entryID,
ParentId: parentID,
SpiffeId: spiffeID,
Downstream: c.downstream,
ExpiresAt: c.entryExpiry,
DnsNames: c.dnsNames,
X509SvidTtl: int32(c.x509SvidTTL),
JwtSvidTtl: int32(c.jwtSvidTTL),
Hint: c.hint,
}
selectors := []*types.Selector{}
for _, s := range c.selectors {
cs, err := util.ParseSelector(s)
if err != nil {
return nil, err
}
selectors = append(selectors, cs)
}
e.Selectors = selectors
e.FederatesWith = c.federatesWith
e.Admin = c.admin
e.StoreSvid = c.storeSVID
return []*types.Entry{e}, nil
}
func updateEntries(ctx context.Context, c entryv1.EntryClient, entries []*types.Entry) (resp *entryv1.BatchUpdateEntryResponse, err error) {
resp, err = c.BatchUpdateEntry(ctx, &entryv1.BatchUpdateEntryRequest{
Entries: entries,
})
if err != nil {
return
}
for i, r := range resp.Results {
if r.Status.Code != int32(codes.OK) {
// The Entry API does not include in the results the entries that
// failed to be updated, so we populate them from the request data.
r.Entry = entries[i]
}
}
return
}
func prettyPrintUpdate(env *commoncli.Env, results ...any) error {
var succeeded, failed []*entryv1.BatchUpdateEntryResponse_Result
updateResp, ok := results[0].(*entryv1.BatchUpdateEntryResponse)
if !ok {
return cliprinter.ErrInternalCustomPrettyFunc
}
for _, r := range updateResp.Results {
switch r.Status.Code {
case int32(codes.OK):
succeeded = append(succeeded, r)
default:
failed = append(failed, r)
}
}
// Print entries that succeeded to be updated
for _, e := range succeeded {
printEntry(e.Entry, env.Printf)
}
// Print entries that failed to be updated
for _, r := range failed {
env.ErrPrintf("Failed to update the following entry (code: %s, msg: %q):\n",
codes.Code(r.Status.Code),
r.Status.Message)
printEntry(r.Entry, env.ErrPrintf)
}
if len(failed) > 0 {
return errors.New("failed to update one or more entries")
}
return nil
}