-
Notifications
You must be signed in to change notification settings - Fork 552
/
list.go
161 lines (142 loc) · 5.38 KB
/
list.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
// Copyright 2021 Vectorized, Inc.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.md
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0
package acl
import (
"context"
"fmt"
"github.com/spf13/afero"
"github.com/spf13/cobra"
"github.com/twmb/franz-go/pkg/kadm"
"github.com/twmb/types"
"github.com/vectorizedio/redpanda/src/go/rpk/pkg/config"
"github.com/vectorizedio/redpanda/src/go/rpk/pkg/kafka"
"github.com/vectorizedio/redpanda/src/go/rpk/pkg/out"
)
func NewListCommand(fs afero.Fs) *cobra.Command {
var a acls
var printAllFilters bool
cmd := &cobra.Command{
Use: "list",
Aliases: []string{"ls", "describe"},
Short: "List ACLs.",
Long: `List ACLs.
See the 'rpk acl' help text for a full write up on ACLs. List flags work in a
similar multiplying effect as creating ACLs, but list is more advanced:
listing works on a filter basis. Any unspecified flag defaults to matching
everything (all operations, or all allowed principals, etc).
As mentioned, not specifying flags matches everything. If no resources are
specified, all resources are matched. If no operations are specified, all
operations are matched. You can also opt in to matching everything with "any":
--operation any matches any operation.
The --resource-pattern-type, defaulting to "any", configures how to filter
resource names:
* "any" returns exact name matches of either prefixed or literal pattern type
* "match" returns wildcard matches, prefix patterns that match your input, and literal matches
* "prefix" returns prefix patterns that match your input (prefix "fo" matches "foo")
* "literal" returns exact name matches
`,
Args: cobra.ExactArgs(0),
Run: func(cmd *cobra.Command, _ []string) {
p := config.ParamsFromCommand(cmd)
cfg, err := p.Load(fs)
out.MaybeDie(err, "unable to load config: %v", err)
adm, err := kafka.NewAdmin(fs, p, cfg)
out.MaybeDie(err, "unable to initialize kafka client: %v", err)
defer adm.Close()
b, err := a.createDeletionsAndDescribes(true)
out.MaybeDieErr(err)
describeReqResp(adm, printAllFilters, false, b)
},
}
a.addListFlags(cmd)
cmd.Flags().BoolVarP(&printAllFilters, "print-filters", "f", false, "print the filters that were requested (failed filters are always printed)")
return cmd
}
func (a *acls) addListFlags(cmd *cobra.Command) {
a.addDeprecatedFlags(cmd)
// List has a few more extra deprecated flags.
cmd.Flags().StringSliceVar(&a.listPermissions, "permission", nil, "")
cmd.Flags().StringSliceVar(&a.listPrincipals, "principal", nil, "")
cmd.Flags().StringSliceVar(&a.listHosts, "host", nil, "")
cmd.Flags().MarkDeprecated("permission", "use --{allow,deny}-{host,principal}")
cmd.Flags().MarkDeprecated("principal", "use --{allow,deny}-{host,principal}")
cmd.Flags().MarkDeprecated("host", "use --{allow,deny}-{host,principal}")
cmd.Flags().StringSliceVar(&a.topics, topicFlag, nil, "topic to match ACLs for (repeatable)")
cmd.Flags().StringSliceVar(&a.groups, groupFlag, nil, "group to match ACLs for (repeatable)")
cmd.Flags().BoolVar(&a.cluster, clusterFlag, false, "whether to match ACLs to the cluster")
cmd.Flags().StringSliceVar(&a.txnIDs, txnIDFlag, nil, "transactional IDs to match ACLs for (repeatable)")
cmd.Flags().StringVar(&a.resourcePatternType, patternFlag, "any", "pattern to use when matching resource names (any, match, literal, or prefixed)")
cmd.Flags().StringSliceVar(&a.operations, operationFlag, nil, "operation to match (repeatable)")
cmd.Flags().StringSliceVar(&a.allowPrincipals, allowPrincipalFlag, nil, "allowed principal ACLs to match (repeatable)")
cmd.Flags().StringSliceVar(&a.allowHosts, allowHostFlag, nil, "allowed host ACLs to match (repeatable)")
cmd.Flags().StringSliceVar(&a.denyPrincipals, denyPrincipalFlag, nil, "denied principal ACLs to match (repeatable)")
cmd.Flags().StringSliceVar(&a.denyHosts, denyHostFlag, nil, "denied host ACLs to match (repeatable)")
}
func describeReqResp(
adm *kadm.Client,
printAllFilters bool,
printMatchesHeader bool,
b *kadm.ACLBuilder,
) {
results, err := adm.DescribeACLs(context.Background(), b)
out.MaybeDie(err, "unable to list ACLs: %v", err)
types.Sort(results)
// If any filters failed, or if all filters are
// requested, we print the filter section.
var printFailedFilters bool
for _, f := range results {
if f.Err != nil {
printFailedFilters = true
break
}
}
if printAllFilters || printFailedFilters {
out.Section("filters")
printDescribeFilters(printAllFilters, results)
fmt.Println()
printMatchesHeader = true
}
if printMatchesHeader {
out.Section("matches")
}
printDescribedACLs(results)
}
func printDescribeFilters(all bool, results kadm.DescribeACLsResults) {
tw := out.NewTable(headersWithError...)
defer tw.Flush()
for _, f := range results {
tw.PrintStructFields(aclWithMessage{
unptr(f.Principal),
unptr(f.Host),
f.Type,
unptr(f.Name),
f.Pattern,
f.Operation,
f.Permission,
kafka.ErrMessage(f.Err),
})
}
}
func printDescribedACLs(results kadm.DescribeACLsResults) {
tw := out.NewTable(headersWithError...)
defer tw.Flush()
for _, f := range results {
for _, d := range f.Described {
tw.PrintStructFields(acl{
d.Principal,
d.Host,
d.Type,
d.Name,
d.Pattern,
d.Operation,
d.Permission,
})
}
}
}