-
Notifications
You must be signed in to change notification settings - Fork 66
feat(kafka acl): add grant-admin command #1230
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b9a8bd2
feat(kafka acl): add admin command
rkpattnaik780 cdb7aee
fix: address review comments
rkpattnaik780 38b300a
fix: outdates examples
rkpattnaik780 1597031
fix: failing build
rkpattnaik780 f7c1357
fix: add review and confirm prompt
rkpattnaik780 02b3a35
fix: failing build for docs
rkpattnaik780 0cce846
fix: build failing for rebase
rkpattnaik780 e3da262
fix: address review comments
rkpattnaik780 778cfb6
fix: address review comments
rkpattnaik780 9fef977
fix: typos and punctuations
rkpattnaik780 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| package admin | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/AlecAivazis/survey/v2" | ||
| "github.com/redhat-developer/app-services-cli/internal/config" | ||
| "github.com/redhat-developer/app-services-cli/pkg/cmd/factory" | ||
| "github.com/redhat-developer/app-services-cli/pkg/connection" | ||
| "github.com/redhat-developer/app-services-cli/pkg/dump" | ||
| "github.com/redhat-developer/app-services-cli/pkg/icon" | ||
| "github.com/redhat-developer/app-services-cli/pkg/iostreams" | ||
| "github.com/redhat-developer/app-services-cli/pkg/kafka/aclutil" | ||
| "github.com/redhat-developer/app-services-cli/pkg/localize" | ||
| "github.com/redhat-developer/app-services-cli/pkg/logging" | ||
| "github.com/spf13/cobra" | ||
|
|
||
| flagset "github.com/redhat-developer/app-services-cli/pkg/cmd/kafka/acl/flagutil" | ||
|
|
||
| kafkainstanceclient "github.com/redhat-developer/app-services-sdk-go/kafkainstance/apiv1internal/client" | ||
| ) | ||
|
|
||
| var ( | ||
| serviceAccount string | ||
| userID string | ||
| allAccounts bool | ||
| ) | ||
|
|
||
| type options struct { | ||
| config config.IConfig | ||
| connection factory.ConnectionFunc | ||
| logger logging.Logger | ||
| io *iostreams.IOStreams | ||
| localizer localize.Localizer | ||
| context context.Context | ||
|
|
||
| kafkaID string | ||
| principal string | ||
| skipConfirm bool | ||
| } | ||
|
|
||
| // NewAdminACLCommand creates ACL rule to aloow user to add and delete ACL rules | ||
| func NewAdminACLCommand(f *factory.Factory) *cobra.Command { | ||
|
|
||
| opts := &options{ | ||
| config: f.Config, | ||
| connection: f.Connection, | ||
| logger: f.Logger, | ||
| io: f.IOStreams, | ||
| localizer: f.Localizer, | ||
| context: f.Context, | ||
| } | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "grant-admin", | ||
| Short: f.Localizer.MustLocalize("kafka.acl.grantAdmin.cmd.shortDescription"), | ||
| Long: f.Localizer.MustLocalize("kafka.acl.grantAdmin.cmd.longDescription"), | ||
| Example: f.Localizer.MustLocalize("kafka.acl.grantAdmin.cmd.example"), | ||
| Args: cobra.NoArgs, | ||
| RunE: func(cmd *cobra.Command, _ []string) error { | ||
|
|
||
| cfg, err := opts.config.Load() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| instanceID, ok := cfg.GetKafkaIdOk() | ||
|
|
||
| if !ok { | ||
| return opts.localizer.MustLocalizeError("kafka.acl.common.error.noKafkaSelected") | ||
| } | ||
|
|
||
| opts.kafkaID = instanceID | ||
|
|
||
| // check if principal is provided | ||
| if userID == "" && serviceAccount == "" && !allAccounts { | ||
| return opts.localizer.MustLocalizeError("kafka.acl.common.error.noPrincipalsSelected") | ||
| } | ||
|
|
||
| // user and service account can't be along with "--all-accounts" flag | ||
| if allAccounts && (serviceAccount != "" || userID != "") { | ||
| return opts.localizer.MustLocalizeError("kafka.acl.common.error.allAccountsCannotBeUsedWithUserFlag") | ||
| } | ||
|
|
||
| // user and service account should not allow wildcard | ||
| if userID == aclutil.Wildcard || serviceAccount == aclutil.Wildcard { | ||
| return opts.localizer.MustLocalizeError("kafka.acl.common.error.useAllAccountsFlag") | ||
| } | ||
|
|
||
| if userID != "" { | ||
| opts.principal = userID | ||
| } | ||
|
|
||
| if serviceAccount != "" { | ||
| opts.principal = serviceAccount | ||
| } | ||
|
|
||
| if allAccounts { | ||
| opts.principal = aclutil.Wildcard | ||
| } | ||
|
|
||
| return runAdmin(opts) | ||
| }, | ||
| } | ||
|
|
||
| fs := flagset.NewFlagSet(cmd, opts.localizer, opts.connection) | ||
|
|
||
| fs.AddUser(&userID) | ||
| fs.AddServiceAccount(&serviceAccount) | ||
| fs.AddAllAccounts(&allAccounts) | ||
| fs.AddYes(&opts.skipConfirm) | ||
|
|
||
| return cmd | ||
| } | ||
|
|
||
| func runAdmin(opts *options) (err error) { | ||
|
|
||
| conn, err := opts.connection(connection.DefaultConfigRequireMasAuth) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| api, kafkaInstance, err := conn.API().KafkaAdmin(opts.kafkaID) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| kafkaName := kafkaInstance.GetName() | ||
|
|
||
| req := api.AclsApi.CreateAcl(opts.context) | ||
|
|
||
| aclBindClusterAlter := kafkainstanceclient.NewAclBinding( | ||
| kafkainstanceclient.ACLRESOURCETYPE_CLUSTER, | ||
| aclutil.KafkaCluster, | ||
| kafkainstanceclient.ACLPATTERNTYPE_LITERAL, | ||
| aclutil.FormatPrincipal(opts.principal), | ||
| kafkainstanceclient.ACLOPERATION_ALTER, | ||
| kafkainstanceclient.ACLPERMISSIONTYPE_ALLOW, | ||
| ) | ||
|
|
||
| opts.logger.Info(opts.localizer.MustLocalize("kafka.acl.grantPermissions.log.info.aclsPreview")) | ||
| opts.logger.Info() | ||
|
|
||
| rows := aclutil.MapACLsToTableRows([]kafkainstanceclient.AclBinding{*aclBindClusterAlter}, opts.localizer) | ||
| dump.Table(opts.io.Out, rows) | ||
| opts.logger.Info() | ||
|
|
||
| if !opts.skipConfirm { | ||
| var confirmGrant bool | ||
| promptConfirmGrant := &survey.Confirm{ | ||
| Message: opts.localizer.MustLocalize("kafka.acl.common.input.confirmGrant.message"), | ||
| } | ||
|
|
||
| err = survey.AskOne(promptConfirmGrant, &confirmGrant) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if !confirmGrant { | ||
| opts.logger.Debug(opts.localizer.MustLocalize("kafka.acl.grantAdmin.log.debug.grantNotConfirmed")) | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| req = req.AclBinding(*aclBindClusterAlter) | ||
|
|
||
| err = aclutil.ExecuteACLRuleCreate(req, opts.localizer, kafkaName) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| opts.logger.Info(icon.SuccessPrefix(), opts.localizer.MustLocalize("kafka.acl.grantAdmin.log.info.successful", localize.NewEntry("Account", opts.principal), localize.NewEntry("InstanceName", kafkaName))) | ||
|
|
||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
We could probably skip that when
-yis passed.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 would prefer to keep
-yfor the confirmation action only and display the ACLs to be generated in both scenario.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.
That's fair. I think it needs an explanation to give context to the table though because right now it prints without info about it.
It might be nice to add something like:
above it.