forked from spiffe/spire
-
Notifications
You must be signed in to change notification settings - Fork 4
/
show.go
247 lines (206 loc) · 7.25 KB
/
show.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
package entry
import (
"errors"
"flag"
"fmt"
"github.com/accuknox/spire/cmd/spire-server/util"
commoncli "github.com/accuknox/spire/pkg/common/cli"
"github.com/accuknox/spire/pkg/common/cliprinter"
commonutil "github.com/accuknox/spire/pkg/common/util"
"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"
"golang.org/x/net/context"
)
const listEntriesRequestPageSize = 500
// NewShowCommand creates a new "show" subcommand for "entry" command.
func NewShowCommand() cli.Command {
return newShowCommand(commoncli.DefaultEnv)
}
func newShowCommand(env *commoncli.Env) cli.Command {
return util.AdaptCommand(env, &showCommand{env: env})
}
type showCommand struct {
// Type and value are delimited by a colon (:)
// ex. "unix:uid:1000" or "spiffe_id:spiffe://example.org/foo"
selectors StringsFlag
// ID of the entry to be shown
entryID string
// Workload parent spiffeID
parentID string
// Workload spiffeID
spiffeID string
// List of SPIFFE IDs of trust domains the registration entry is federated with
federatesWith StringsFlag
// Whether or not the entry is for a downstream SPIRE server
downstream bool
// Match used when filtering by federates with
matchFederatesWithOn string
// Match used when filtering by selectors
matchSelectorsOn string
printer cliprinter.Printer
env *commoncli.Env
}
func (c *showCommand) Name() string {
return "entry show"
}
func (*showCommand) Synopsis() string {
return "Displays configured registration entries"
}
func (c *showCommand) AppendFlags(f *flag.FlagSet) {
f.StringVar(&c.entryID, "entryID", "", "The Entry ID of the records to show")
f.StringVar(&c.parentID, "parentID", "", "The Parent ID of the records to show")
f.StringVar(&c.spiffeID, "spiffeID", "", "The SPIFFE ID of the records to show")
f.BoolVar(&c.downstream, "downstream", false, "A boolean value that, when set, indicates that the entry describes a downstream SPIRE server")
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 an entry is federate with. Can be used more than once")
f.StringVar(&c.matchFederatesWithOn, "matchFederatesWithOn", "superset", "The match mode used when filtering by federates with. Options: exact, any, superset and subset")
f.StringVar(&c.matchSelectorsOn, "matchSelectorsOn", "superset", "The match mode used when filtering by selectors. Options: exact, any, superset and subset")
cliprinter.AppendFlagWithCustomPretty(&c.printer, f, c.env, prettyPrintShow)
}
// Run executes all logic associated with a single invocation of the
// `spire-server entry show` CLI command
func (c *showCommand) Run(ctx context.Context, env *commoncli.Env, serverClient util.ServerClient) error {
if err := c.validate(); err != nil {
return err
}
resp, err := c.fetchEntries(ctx, serverClient.NewEntryClient())
if err != nil {
return err
}
commonutil.SortTypesEntries(resp.Entries)
return c.printer.PrintProto(resp)
}
// validate ensures that the values in showCommand are valid
func (c *showCommand) validate() error {
// If entryID is given, it should be the only constraint
if c.entryID != "" {
if c.parentID != "" || c.spiffeID != "" || len(c.selectors) > 0 {
return errors.New("the -entryID flag can't be combined with others")
}
}
return nil
}
func (c *showCommand) fetchEntries(ctx context.Context, client entryv1.EntryClient) (*entryv1.ListEntriesResponse, error) {
listResp := &entryv1.ListEntriesResponse{}
// If an Entry ID was specified, look it up directly
if c.entryID != "" {
entry, err := c.fetchByEntryID(ctx, c.entryID, client)
if err != nil {
return nil, fmt.Errorf("error fetching entry ID %s: %w", c.entryID, err)
}
listResp.Entries = append(listResp.Entries, entry)
return listResp, nil
}
filter := &entryv1.ListEntriesRequest_Filter{}
if c.parentID != "" {
id, err := idStringToProto(c.parentID)
if err != nil {
return nil, fmt.Errorf("error parsing parent ID %q: %w", c.parentID, err)
}
filter.ByParentId = id
}
if c.spiffeID != "" {
id, err := idStringToProto(c.spiffeID)
if err != nil {
return nil, fmt.Errorf("error parsing SPIFFE ID %q: %w", c.spiffeID, err)
}
filter.BySpiffeId = id
}
if len(c.selectors) != 0 {
matchSelectorBehavior, err := parseToSelectorMatch(c.matchSelectorsOn)
if err != nil {
return nil, err
}
selectors := make([]*types.Selector, len(c.selectors))
for i, sel := range c.selectors {
selector, err := util.ParseSelector(sel)
if err != nil {
return nil, fmt.Errorf("error parsing selectors: %w", err)
}
selectors[i] = selector
}
filter.BySelectors = &types.SelectorMatch{
Selectors: selectors,
Match: matchSelectorBehavior,
}
}
if len(c.federatesWith) > 0 {
matchFederatesWithBehavior, err := parseToFederatesWithMatch(c.matchFederatesWithOn)
if err != nil {
return nil, err
}
filter.ByFederatesWith = &types.FederatesWithMatch{
TrustDomains: c.federatesWith,
Match: matchFederatesWithBehavior,
}
}
pageToken := ""
for {
resp, err := client.ListEntries(ctx, &entryv1.ListEntriesRequest{
PageSize: listEntriesRequestPageSize,
PageToken: pageToken,
Filter: filter,
})
if err != nil {
return nil, fmt.Errorf("error fetching entries: %w", err)
}
listResp.Entries = append(listResp.Entries, resp.Entries...)
if pageToken = resp.NextPageToken; pageToken == "" {
break
}
}
return listResp, nil
}
// fetchByEntryID uses the configured EntryID to fetch the appropriate registration entry
func (c *showCommand) fetchByEntryID(ctx context.Context, id string, client entryv1.EntryClient) (*types.Entry, error) {
entry, err := client.GetEntry(ctx, &entryv1.GetEntryRequest{Id: id})
if err != nil {
return nil, err
}
return entry, nil
}
func printEntries(entries []*types.Entry, env *commoncli.Env) {
msg := fmt.Sprintf("Found %v ", len(entries))
msg = util.Pluralizer(msg, "entry", "entries", len(entries))
env.Println(msg)
for _, e := range entries {
printEntry(e, env.Printf)
}
}
func parseToSelectorMatch(match string) (types.SelectorMatch_MatchBehavior, error) {
switch match {
case "exact":
return types.SelectorMatch_MATCH_EXACT, nil
case "any":
return types.SelectorMatch_MATCH_ANY, nil
case "superset":
return types.SelectorMatch_MATCH_SUPERSET, nil
case "subset":
return types.SelectorMatch_MATCH_SUBSET, nil
default:
return types.SelectorMatch_MATCH_SUPERSET, fmt.Errorf("match behavior %q unknown", match)
}
}
func parseToFederatesWithMatch(match string) (types.FederatesWithMatch_MatchBehavior, error) {
switch match {
case "exact":
return types.FederatesWithMatch_MATCH_EXACT, nil
case "any":
return types.FederatesWithMatch_MATCH_ANY, nil
case "superset":
return types.FederatesWithMatch_MATCH_SUPERSET, nil
case "subset":
return types.FederatesWithMatch_MATCH_SUBSET, nil
default:
return types.FederatesWithMatch_MATCH_SUPERSET, fmt.Errorf("match behavior %q unknown", match)
}
}
func prettyPrintShow(env *commoncli.Env, results ...interface{}) error {
listResp, ok := results[0].(*entryv1.ListEntriesResponse)
if !ok {
return cliprinter.ErrInternalCustomPrettyFunc
}
printEntries(listResp.Entries, env)
return nil
}