Skip to content

Commit

Permalink
fix: add validation for search input
Browse files Browse the repository at this point in the history
  • Loading branch information
rkpattnaik780 committed Jul 9, 2021
1 parent afda40f commit 6ce22de
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
11 changes: 11 additions & 0 deletions pkg/cmd/kafka/consumergroup/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (
"github.com/redhat-developer/app-services-cli/pkg/cmd/flag"
"github.com/redhat-developer/app-services-cli/pkg/cmdutil"
flagutil "github.com/redhat-developer/app-services-cli/pkg/cmdutil/flags"
consumergrouputil "github.com/redhat-developer/app-services-cli/pkg/kafka/consumergroup"

"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/iostreams"
Expand Down Expand Up @@ -73,6 +75,15 @@ func NewListConsumerGroupCommand(f *factory.Factory) *cobra.Command {
return fmt.Errorf(opts.localizer.MustLocalize("kafka.consumerGroup.common.error.noKafkaSelected"))
}

if opts.search != "" {
validator := &consumergrouputil.Validator{
Localizer: opts.localizer,
}
if err = validator.ValidateSearchInput(opts.search); err != nil {
return err
}
}

opts.kafkaID = cfg.Services.Kafka.ClusterID

return runList(opts)
Expand Down
33 changes: 33 additions & 0 deletions pkg/kafka/consumergroup/validators.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package consumergroup

import (
"errors"
"regexp"

"github.com/redhat-developer/app-services-cli/pkg/common/commonerr"
"github.com/redhat-developer/app-services-cli/pkg/localize"
)

const (
legalSearchChars = "^[a-zA-Z0-9-]+$"
)

// Validator is a type for validating Kafka consumer group configuration values
type Validator struct {
Localizer localize.Localizer
}

func (v *Validator) ValidateSearchInput(val interface{}) error {
search, ok := val.(string)
if !ok {
return commonerr.NewCastError(val, "string")
}

matched, _ := regexp.Match(legalSearchChars, []byte(search))

if matched {
return nil
}

return errors.New(v.Localizer.MustLocalize("kafka.consumerGroup.list.error.illegalSearchValue", localize.NewEntry("Search", search)))
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ one = 'no Kafka instance is currently selected, run "rhoas kafka use" to set the
one = 'you are unauthorized to {{.Operation}} these consumer groups'

[kafka.consumerGroup.list.error.forbidden]
one = 'you are forbidden to {{.Operation}} these consumer groups'
one = 'you are forbidden to {{.Operation}} these consumer groups'

[kafka.consumerGroup.list.error.illegalSearchValue]
description = 'Error message when invalid chars are used for search flag'
one = 'illegal search value "{{.Search}}"; only letters (Aa-Zz), numbers and "-" are accepted'

0 comments on commit 6ce22de

Please sign in to comment.