-
Notifications
You must be signed in to change notification settings - Fork 66
fix: allow to delete all ACLs for principal #1441
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
72f616f
8de1f79
0f9aceb
d2baa6e
741bb5b
9fa50ff
cddb206
9ced870
8295746
d44f176
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,10 +17,11 @@ import ( | |
| ) | ||
|
|
||
| var ( | ||
| serviceAccount string | ||
| userID string | ||
| allAccounts bool | ||
| prefix bool | ||
| serviceAccount string | ||
| userID string | ||
| allAccounts bool | ||
| prefix bool | ||
| patternTypeFlag string | ||
| ) | ||
|
|
||
| type requestParams struct { | ||
|
|
@@ -56,12 +57,10 @@ func NewDeleteCommand(f *factory.Factory) *cobra.Command { | |
|
|
||
| var errorCollection []error | ||
|
|
||
| if opts.Operation == "" { | ||
| errorCollection = append(errorCollection, opts.Localizer.MustLocalizeError("kafka.acl.common.flag.operation.required")) | ||
| } | ||
|
|
||
| if resourceErrors := aclcmdutil.ValidateAndSetResources(opts, aclFlagUtil.ResourceTypeFlagEntries); resourceErrors != nil { | ||
| errorCollection = append(errorCollection, resourceErrors) | ||
| selectedResourceTypeCount := aclcmdutil.SetACLResources(opts) | ||
| if selectedResourceTypeCount > 1 { | ||
| errorCollection = append(errorCollection, | ||
| opts.Localizer.MustLocalizeError("kafka.acl.common.error.oneResourceTypeAllowed", aclFlagUtil.ResourceTypeFlagEntries...)) | ||
| } | ||
|
|
||
| if principalErrors := validateAndSetOpts(opts); principalErrors != nil { | ||
|
|
@@ -82,7 +81,7 @@ func NewDeleteCommand(f *factory.Factory) *cobra.Command { | |
| flags.AddOperationFilter(&opts.Operation) | ||
|
|
||
| flags.AddCluster(&opts.Cluster) | ||
| flags.AddPrefix(&prefix) | ||
|
|
||
| flags.AddTopic(&opts.Topic) | ||
| flags.AddConsumerGroup(&opts.Group) | ||
| flags.AddTransactionalID(&opts.TransactionalID) | ||
|
|
@@ -92,6 +91,19 @@ func NewDeleteCommand(f *factory.Factory) *cobra.Command { | |
| flags.AddServiceAccount(&serviceAccount) | ||
| flags.AddAllAccounts(&allAccounts) | ||
| flags.AddYes(&opts.SkipConfirm) | ||
| flags.AddPrefix(&prefix) | ||
|
|
||
| cmd.Flags().StringVar( | ||
| &patternTypeFlag, | ||
| "pattern-type", | ||
| aclcmdutil.PatternTypeLITERAL, | ||
| opts.Localizer.MustLocalize("kafka.acl.common.flag.patterntypes.description", | ||
| localize.NewEntry("Types", aclcmdutil.PatternTypes)), | ||
| ) | ||
|
|
||
| _ = cmd.RegisterFlagCompletionFunc("pattern-type", func(cmd *cobra.Command, _ []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
| return aclcmdutil.PatternTypes, cobra.ShellCompDirectiveNoSpace | ||
| }) | ||
|
|
||
| return cmd | ||
| } | ||
|
|
@@ -118,12 +130,15 @@ func runDelete(instanceID string, opts *aclcmdutil.CrudOptions) error { | |
| return err | ||
| } | ||
|
|
||
| if isValidOp, validResourceOperations := aclcmdutil.IsValidResourceOperation(opts.ResourceType, opts.Operation, resourceOperations); !isValidOp { | ||
| return opts.Localizer.MustLocalizeError("kafka.acl.common.error.invalidResourceOperation", | ||
| localize.NewEntry("ResourceType", opts.ResourceType), | ||
| localize.NewEntry("Operation", opts.Operation), | ||
| localize.NewEntry("ValidOperationList", cmdutil.StringSliceToListStringWithQuotes(validResourceOperations)), | ||
| ) | ||
| // Validate only when both are present | ||
| if opts.ResourceType != "" && opts.Operation != "" { | ||
| if isValidOp, validResourceOperations := aclcmdutil.IsValidResourceOperation(opts.ResourceType, opts.Operation, resourceOperations); !isValidOp { | ||
| return opts.Localizer.MustLocalizeError("kafka.acl.common.error.invalidResourceOperation", | ||
| localize.NewEntry("ResourceType", opts.ResourceType), | ||
| localize.NewEntry("Operation", opts.Operation), | ||
| localize.NewEntry("ValidOperationList", cmdutil.StringSliceToListStringWithQuotes(validResourceOperations)), | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| kafkaNameTmplEntry := localize.NewEntry("Name", kafkaInstance.GetName()) | ||
|
|
@@ -149,14 +164,29 @@ func runDelete(instanceID string, opts *aclcmdutil.CrudOptions) error { | |
|
|
||
| requestParams := getRequestParams(opts) | ||
|
|
||
| deletedACLs, httpRes, err := adminAPI.AclsApi.DeleteAcls(ctx). | ||
| ResourceType(requestParams.resourceType). | ||
| Principal(requestParams.principal). | ||
| PatternType(requestParams.patternType). | ||
| ResourceName(requestParams.resourceName). | ||
| Operation(requestParams.operation). | ||
| Permission(requestParams.permission). | ||
| Execute() | ||
| requestDeleteAcls := adminAPI.AclsApi.DeleteAcls(ctx) | ||
| if requestParams.resourceType != "" { | ||
| requestDeleteAcls = requestDeleteAcls.ResourceType(requestParams.resourceType) | ||
| } | ||
|
|
||
| if requestParams.principal != "" { | ||
| requestDeleteAcls = requestDeleteAcls.Principal(requestParams.principal) | ||
| } | ||
|
|
||
| if requestParams.resourceName != "" { | ||
| requestDeleteAcls = requestDeleteAcls.ResourceName(requestParams.resourceName) | ||
| } | ||
| if requestParams.patternType != "" { | ||
| requestDeleteAcls = requestDeleteAcls.PatternType(requestParams.patternType) | ||
| } | ||
| if requestParams.operation != "" { | ||
| requestDeleteAcls = requestDeleteAcls.Operation(requestParams.operation) | ||
| } | ||
| if requestParams.permission != "" { | ||
| requestDeleteAcls = requestDeleteAcls.Permission(requestParams.permission) | ||
| } | ||
|
|
||
| deletedACLs, httpRes, err := requestDeleteAcls.Execute() | ||
|
|
||
| if httpRes != nil { | ||
| defer httpRes.Body.Close() | ||
|
|
@@ -226,11 +256,20 @@ func validateAndSetOpts(opts *aclcmdutil.CrudOptions) error { | |
| return opts.Localizer.MustLocalizeError("kafka.acl.common.error.noPrincipalsSelected") | ||
| } | ||
|
|
||
| opts.PatternType = aclcmdutil.PatternTypeLITERAL | ||
| // Backwards compatibility: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be moved after the switch in 264.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh.. I did refactor if into switch and introduced that bug. Really hate that golanglint forces us to use switch statements even when they do not make sense
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could always look to remove that rule from the lint config file? It is not a rule coming from the Go stdlib or anything. |
||
| if prefix { | ||
| opts.PatternType = aclcmdutil.PatternTypePREFIX | ||
| } | ||
|
|
||
| switch patternTypeFlag { | ||
| case aclcmdutil.PatternTypeANY: | ||
| opts.PatternType = aclcmdutil.PatternTypeANY | ||
| case aclcmdutil.PatternTypePREFIX: | ||
| opts.PatternType = aclcmdutil.PatternTypePREFIX | ||
| case aclcmdutil.PatternTypeLITERAL: | ||
| opts.PatternType = aclcmdutil.PatternTypeLITERAL | ||
| } | ||
|
|
||
| if userID != "" { | ||
| opts.Principal = userID | ||
| } else if serviceAccount != "" { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| package flagutil | ||
|
|
||
| // DeprecateFlag provides a way to deprecate a flag by appending standard prefixes to the flag description. | ||
| func DeprecateFlag(flagDescription string) string { | ||
| return "DEPRECATED: " + flagDescription | ||
| } | ||
|
Comment on lines
+3
to
+6
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also think that when a deprecated flag is used, the command should print a message like: $ rhoas kafka acl delete --prefix
The "--prefix" flag has been deprecated, in the future please use "--pattern-type"
... delete outputThe reason being that existing users of the CLI will not be checking the flag descriptions any more as they already understand the purpose of the prefix flag, so will likely never see that it is deprecated.
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fully agree. I will remove that method as it is not used right now and it is not properly implemented. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -85,6 +85,9 @@ one = 'Set the resource type to cluster' | |
| [kafka.acl.common.flag.prefix.description] | ||
| one = 'Determine if the resource should be exact match or prefix' | ||
|
|
||
| [kafka.acl.common.flag.patterntypes.description] | ||
| one = 'Determine if the resource should be exact match, prefix or any {{.Types}}' | ||
|
||
|
|
||
| [kafka.acl.common.flag.topic.description] | ||
| one = 'Set the topic resource. When the --prefix option is also passed, this is used as the topic prefix' | ||
|
|
||
|
|
@@ -291,6 +294,9 @@ $ rhoas kafka acl delete --operation write --permission allow --topic all --user | |
| # Delete an ACL for a service account | ||
| $ rhoas kafka acl delete --operation all --permission any --topic "rhoas" --prefix --service-account "srvc-acct-11924479-43fe-42b4-9676-cf0c9aca81" | ||
|
|
||
| # Delete all ACLs for a service account | ||
| $ rhoas kafka acl delete --service-account "srvc-acct-11924479-43fe-42b4-9676-cf0c9aca81 --pattern-type=all" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is pattern type needed in order to do this?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. While it is counter intuitive we need backwards compatibility. So:
etc. |
||
|
|
||
| # Delete an ACL for all users on the consumer group resource | ||
| $ rhoas kafka acl delete --operation all --permission any --group "group-1" --all-accounts | ||
| ''' | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'd be nice to add this into the flag package we have created? I know it is not used in other commands but it does abstract it away and make the command setup much easier to read.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think flag package is overkill
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The package already exists though, so don't see how (there is no extra code involved).