Skip to content

Commit 049db25

Browse files
feat: delete acl (#167)
* bump kafka-go to include acl apis * add acl interfaces and aclinfo type stub * pull latest kafka-go and use kafka-go aclresource type * wip * fix test * fix typos * get acls working * getacls working * upgrade cobra to latest * finish separating get into separate subcommands * remove unneeded variables * wip * pr feedback * Revert "upgrade cobra to latest" This reverts commit 7b8ee42. * use getCliRunnerAndCtx in get acls * more consistent variable names * custom cobra type * bring in new kafka-go * support resource pattern type * add support for acloperationtype and remove options for unknown * improve descriptions * support permissiontype and host filters * add resource name filter and fix permission type formatting * support principal filtering * improve docs * add examples * remove comment * remove TODOs that are complete * remove TODOs that are complete * update README * fix test * wip * fix error handling * error handling for zk * more consistent error msg * clean up createacl * add TestBrokerClientCreateACLReadOnly * improve zk tests * run acl tests in ci * enable acls for kafka 2.4.1 in ci * fix zk tests * skip TestBrokerClientCreateACLReadOnly on old versions of kafka * try to debug * handle nested errors from createacls * operations -> operation * operations -> operation * remove setting log level in test * clean up allowed types in help command * fix merge conflict * fix test * add json annotations * bump kafka-go to version on main * wip * basic tests * start on getusers cmd * add json annotations * get users working * wip * add todos and fix type annotaitons * improve test * use CanTestBrokerAdminSecurity to feature flag test * update README * remove duplicate test from merge conflicts * fix more merge conflicts * create user working * add uncommitted files * start adding validation * meta validation for users * wip * support dry run and skip confirm * wip * wip * add more files * resourcemta * consistency checking for acls * remove emacs backups * remove user stuff * remove diff from cluster.yaml file * remove diff from topic file * remove debug log * smaller diff * remove completed todos * remove unused error helper * add missing meta file * skip ACL tests when ACLs cannot be used due to kafka version limitations * fix loadacls test * add more todos * add validation and set defaults * don't use ioutil * move confirm to util package * move confirm to util package * add create to README * use validation and setdefaults * add example acl * fix formatting in readme * use released version of kafka-go * fix spelling * make invalid field more obvious * fix dryrun and skip confirm * stub out delete cli and implement admin * integrate cli and add docs * improve formatting * add read only test * improve documentation * fix docstring and error message * move things into new acl package and start on dry run * finish dry run * support deleting multiple acls * add test for multiple deletes * allow deleting multiple acls * remove starting deletion log * harden test * remove unused highlighter * rearrange plan for deletion * fix grammar * fix merge conflict
1 parent e9241f4 commit 049db25

File tree

12 files changed

+1319
-11
lines changed

12 files changed

+1319
-11
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,17 @@ The `create` command creates resources in the cluster from a configuration file.
164164
Currently, only ACLs are supported. The create command is separate from the apply
165165
command as it is intended for usage with immutable resources managed by topicctl.
166166

167+
#### delete
168+
```
169+
topicctl delete [flags] [operation]
170+
```
171+
172+
The `delete` subcommand deletes a particular resource type in the cluster.
173+
Currently, the following operations are supported:
174+
| Subcommand | Description |
175+
| --------- | ----------- |
176+
| `delete acl [flags]` | Deletes a single ACL in the cluster matching the provided flags |
177+
167178
#### get
168179

169180
```

cmd/topicctl/subcmd/create.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import (
88
"path/filepath"
99
"syscall"
1010

11+
"github.com/segmentio/topicctl/pkg/acl"
1112
"github.com/segmentio/topicctl/pkg/admin"
1213
"github.com/segmentio/topicctl/pkg/cli"
1314
"github.com/segmentio/topicctl/pkg/config"
14-
"github.com/segmentio/topicctl/pkg/create"
1515
log "github.com/sirupsen/logrus"
1616
"github.com/spf13/cobra"
1717
)
@@ -171,14 +171,14 @@ func createACL(
171171
clusterConfigPath,
172172
)
173173

174-
creatorConfig := create.ACLCreatorConfig{
174+
aclAdminConfig := acl.ACLAdminConfig{
175175
DryRun: createConfig.dryRun,
176176
SkipConfirm: createConfig.skipConfirm,
177177
ACLConfig: aclConfig,
178178
ClusterConfig: clusterConfig,
179179
}
180180

181-
if err := cliRunner.CreateACL(ctx, creatorConfig); err != nil {
181+
if err := cliRunner.CreateACL(ctx, aclAdminConfig); err != nil {
182182
return err
183183
}
184184
}

cmd/topicctl/subcmd/delete.go

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
package subcmd
2+
3+
import (
4+
"context"
5+
"strings"
6+
7+
"github.com/aws/aws-sdk-go/aws/session"
8+
"github.com/segmentio/kafka-go"
9+
"github.com/segmentio/topicctl/pkg/acl"
10+
"github.com/segmentio/topicctl/pkg/cli"
11+
log "github.com/sirupsen/logrus"
12+
"github.com/spf13/cobra"
13+
)
14+
15+
var deleteCmd = &cobra.Command{
16+
Use: "delete [resource type]",
17+
Short: "delete instances of a particular type",
18+
Long: strings.Join(
19+
[]string{
20+
"Deletes instances of a particular type.",
21+
},
22+
"\n",
23+
),
24+
PersistentPreRunE: deletePreRun,
25+
}
26+
27+
type deleteCmdConfig struct {
28+
dryRun bool
29+
30+
shared sharedOptions
31+
}
32+
33+
var deleteConfig deleteCmdConfig
34+
35+
func init() {
36+
deleteCmd.PersistentFlags().BoolVar(
37+
&deleteConfig.dryRun,
38+
"dry-run",
39+
false,
40+
"Do a dry-run",
41+
)
42+
43+
addSharedFlags(deleteCmd, &deleteConfig.shared)
44+
deleteCmd.AddCommand(
45+
deleteACLCmd(),
46+
)
47+
RootCmd.AddCommand(deleteCmd)
48+
}
49+
50+
func deletePreRun(cmd *cobra.Command, args []string) error {
51+
return deleteConfig.shared.validate()
52+
}
53+
54+
var deleteACLsConfig = aclsCmdConfig{}
55+
56+
func deleteACLCmd() *cobra.Command {
57+
cmd := &cobra.Command{
58+
Use: "acls [flags]",
59+
Short: "Delete ACLs. Requires providing flags to target ACLs for deletion.",
60+
Args: cobra.NoArgs,
61+
Example: `Delete read acls for topic my-topic, user 'User:default', and host '*'
62+
$ topicctl delete acls --resource-type topic --resource-pattern-type literal --resource-name my-topic --principal 'User:default' --host '*' --operation read --permission-type allow
63+
`,
64+
RunE: func(cmd *cobra.Command, args []string) error {
65+
ctx := context.Background()
66+
sess := session.Must(session.NewSession())
67+
68+
adminClient, err := deleteConfig.shared.getAdminClient(ctx, sess, deleteConfig.dryRun)
69+
if err != nil {
70+
return err
71+
}
72+
defer adminClient.Close()
73+
74+
cliRunner := cli.NewCLIRunner(adminClient, log.Infof, !noSpinner)
75+
76+
filter := kafka.DeleteACLsFilter{
77+
ResourceTypeFilter: kafka.ResourceType(deleteACLsConfig.resourceType),
78+
ResourceNameFilter: deleteACLsConfig.resourceNameFilter,
79+
ResourcePatternTypeFilter: kafka.PatternType(deleteACLsConfig.resourcePatternType),
80+
PrincipalFilter: deleteACLsConfig.principalFilter,
81+
HostFilter: deleteACLsConfig.hostFilter,
82+
Operation: kafka.ACLOperationType(deleteACLsConfig.operationType),
83+
PermissionType: kafka.ACLPermissionType(deleteACLsConfig.permissionType),
84+
}
85+
86+
aclAdminConfig := acl.ACLAdminConfig{
87+
// Omit fields we don't need for deletes
88+
DryRun: deleteConfig.dryRun,
89+
// Deletes cannot be skipped
90+
SkipConfirm: false,
91+
}
92+
93+
return cliRunner.DeleteACL(ctx, aclAdminConfig, filter)
94+
},
95+
}
96+
cmd.Flags().StringVar(
97+
&deleteACLsConfig.hostFilter,
98+
"host",
99+
"",
100+
`The host to filter on. (e.g. 198.51.100.0) (Required)`,
101+
)
102+
cmd.MarkFlagRequired("host")
103+
104+
cmd.Flags().Var(
105+
&deleteACLsConfig.operationType,
106+
"operation",
107+
`The operation that is being allowed or denied to filter on. allowed: [any, all, read, write, create, delete, alter, describe, clusteraction, describeconfigs, alterconfigs, idempotentwrite] (Required)`,
108+
)
109+
cmd.MarkFlagRequired("operation")
110+
111+
cmd.Flags().Var(
112+
&deleteACLsConfig.permissionType,
113+
"permission-type",
114+
`The permission type to filter on. allowed: [any, allow, deny] (Required)`,
115+
)
116+
cmd.MarkFlagRequired("permission-type")
117+
118+
cmd.Flags().StringVar(
119+
&deleteACLsConfig.principalFilter,
120+
"principal",
121+
"",
122+
`The principal to filter on in principalType:name format (e.g. User:alice). (Required)`,
123+
)
124+
cmd.MarkFlagRequired("principal")
125+
126+
cmd.Flags().StringVar(
127+
&deleteACLsConfig.resourceNameFilter,
128+
"resource-name",
129+
"",
130+
`The resource name to filter on. (e.g. my-topic) (Required)`,
131+
)
132+
cmd.MarkFlagRequired("resource-name")
133+
134+
cmd.Flags().Var(
135+
&deleteACLsConfig.resourcePatternType,
136+
"resource-pattern-type",
137+
`The type of the resource pattern or filter. allowed: [any, match, literal, prefixed]. "any" will match any pattern type (literal or prefixed), but will match the resource name exactly, where as "match" will perform pattern matching to list all acls that affect the supplied resource(s).`,
138+
)
139+
140+
cmd.Flags().Var(
141+
&deleteACLsConfig.resourceType,
142+
"resource-type",
143+
`The type of resource to filter on. allowed: [any, topic, group, cluster, transactionalid, delegationtoken] (Required)`,
144+
)
145+
cmd.MarkFlagRequired("resource-type")
146+
return cmd
147+
}

0 commit comments

Comments
 (0)