Skip to content

Commit

Permalink
Chore: validate search value against RegEx
Browse files Browse the repository at this point in the history
  • Loading branch information
rkpattnaik780 committed Feb 22, 2021
1 parent f08c864 commit 8b122a8
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cmd/rhoas/pkged.go

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions locales/kafka/active.en.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ Invalid Kafka instance name. Valid names must satisfy the following conditions:
[kafka.validation.name.error.lengthError]
one = 'Kafka instance name must be between 1 and 32 characters'

[kafka.validation.error.invalidSearchValue]
description = 'Error message when invalid search input is provided'
one = '''
Illegal search value "{{.Search}}", search input must satisfy the following conditions:
- must be of 1 or more characters
- must only consist of alphanumeric characters, '-', '_' and '%'
'''

[kafka.common.error.notFoundErrorById]
one = 'Kafka instance with ID "{{.ID}}" not found'
6 changes: 6 additions & 0 deletions pkg/cmd/kafka/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
kasclient "github.com/bf2fc6cc711aee1a0c2a/cli/pkg/api/kas/client"
flagutil "github.com/bf2fc6cc711aee1a0c2a/cli/pkg/cmdutil/flags"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/iostreams"
"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/kafka"

"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/dump"

Expand Down Expand Up @@ -101,6 +102,11 @@ func runList(opts *options) error {
a = a.Size(strconv.Itoa(opts.limit))

if opts.search != "" {

if err = kafka.ValidateSearchInput(opts.search); err != nil {
return err
}

logger.Debug(localizer.MustLocalize(&localizer.Config{
MessageID: "kafka.list.log.debug.filteringKafkaList",
TemplateData: map[string]interface{}{
Expand Down
30 changes: 29 additions & 1 deletion pkg/kafka/kafka_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (
)

var (
validNameRegexp = regexp.MustCompile(`^[a-z]([-a-z0-9]*[a-z0-9])?$`)
validNameRegexp = regexp.MustCompile(`^[a-z]([-a-z0-9]*[a-z0-9])?$`)
validSearchRegexp = regexp.MustCompile(`^([a-zA-Z0-9-_%]*[a-zA-Z0-9-_%])?$`)
)

// ValidateName validates the proposed name of a Kafka instance
Expand Down Expand Up @@ -69,3 +70,30 @@ func TransformKafkaRequest(kafka *kasclient.KafkaRequest) *kasclient.KafkaReques

return kafka
}

func ValidateSearchInput(val interface{}) error {
search, ok := val.(string)

if !ok {
return errors.New(localizer.MustLocalize(&localizer.Config{
MessageID: "common.error.castError",
TemplateData: map[string]interface{}{
"Value": val,
"Type": "string",
},
}))
}

matched := validSearchRegexp.MatchString(search)

if matched {
return nil
}

return errors.New(localizer.MustLocalize(&localizer.Config{
MessageID: "kafka.validation.error.invalidSearchValue",
TemplateData: map[string]interface{}{
"Search": search,
},
}))
}

0 comments on commit 8b122a8

Please sign in to comment.