Skip to content

Commit

Permalink
Added source validation to regex and json stages
Browse files Browse the repository at this point in the history
  • Loading branch information
pracucci authored and Ed committed Jul 15, 2019
1 parent 0a79e2a commit d235e48
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
5 changes: 5 additions & 0 deletions pkg/logentry/stages/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const (
ErrExpressionsRequired = "JMES expression is required"
ErrCouldNotCompileJMES = "could not compile JMES expression"
ErrEmptyJSONStageConfig = "empty json stage configuration"
ErrEmptyJSONStageSource = "empty source"
)

// JSONConfig represents a JSON Stage configuration
Expand All @@ -38,6 +39,10 @@ func validateJSONConfig(c *JSONConfig) (map[string]*jmespath.JMESPath, error) {
return nil, errors.New(ErrExpressionsRequired)
}

if c.Source != nil && *c.Source == "" {
return nil, errors.New(ErrEmptyJSONStageSource)
}

expressions := map[string]*jmespath.JMESPath{}

for n, e := range c.Expressions {
Expand Down
12 changes: 11 additions & 1 deletion pkg/logentry/stages/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func TestJSONConfig_validate(t *testing.T) {
wantExprCount int
err error
}{
"empty": {
"empty config": {
nil,
0,
errors.New(ErrExpressionsRequired),
Expand All @@ -149,6 +149,16 @@ func TestJSONConfig_validate(t *testing.T) {
0,
errors.Wrap(errors.New("SyntaxError: Unknown char: '#'"), ErrCouldNotCompileJMES),
},
"empty source": {
map[string]interface{}{
"expressions": map[string]interface{}{
"extr1": "expr",
},
"source": "",
},
0,
errors.New(ErrEmptyJSONStageSource),
},
"valid without source": {
map[string]interface{}{
"expressions": map[string]string{
Expand Down
5 changes: 5 additions & 0 deletions pkg/logentry/stages/regex.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const (
ErrExpressionRequired = "expression is required"
ErrCouldNotCompileRegex = "could not compile regular expression"
ErrEmptyRegexStageConfig = "empty regex stage configuration"
ErrEmptyRegexStageSource = "empty source"
)

// RegexConfig contains a regexStage configuration
Expand All @@ -35,6 +36,10 @@ func validateRegexConfig(c *RegexConfig) (*regexp.Regexp, error) {
return nil, errors.New(ErrExpressionRequired)
}

if c.Source != nil && *c.Source == "" {
return nil, errors.New(ErrEmptyRegexStageSource)
}

expr, err := regexp.Compile(c.Expression)
if err != nil {
return nil, errors.Wrap(err, ErrCouldNotCompileRegex)
Expand Down
9 changes: 8 additions & 1 deletion pkg/logentry/stages/regex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func TestRegexConfig_validate(t *testing.T) {
config interface{}
err error
}{
"empty": {
"empty config": {
nil,
errors.New(ErrExpressionRequired),
},
Expand All @@ -138,6 +138,13 @@ func TestRegexConfig_validate(t *testing.T) {
},
errors.New(ErrCouldNotCompileRegex + ": error parsing regexp: invalid named capture: `(?P<ts[0-9]+).*`"),
},
"empty source": {
map[string]interface{}{
"expression": "(?P<ts>[0-9]+).*",
"source": "",
},
errors.New(ErrEmptyRegexStageSource),
},
"valid without source": {
map[string]interface{}{
"expression": "(?P<ts>[0-9]+).*",
Expand Down

0 comments on commit d235e48

Please sign in to comment.