Skip to content

Commit

Permalink
fix: RemoveYAMLComments considering hashtag within strings as comment
Browse files Browse the repository at this point in the history
  • Loading branch information
varas committed Feb 2, 2021
1 parent 6157241 commit f307b7c
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 6 deletions.
32 changes: 28 additions & 4 deletions pkg/config/envvar/envvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func ExpandInContent(content []byte) ([]byte, error) {
content, err := removeComments(content)
content, err := RemoveYAMLComments(content)
if err != nil {
return nil, fmt.Errorf("cannot remove configuration commented lines, error: %w", err)
}
Expand Down Expand Up @@ -49,9 +49,33 @@ func ExpandInContent(content []byte) ([]byte, error) {
return newContent, nil
}

func removeComments(content []byte) ([]byte, error) {
r := regexp.MustCompile(`(^[ \t#].*\n)|([ \t]*#.*)`)
matches := r.FindAllIndex(content, -1)
func RemoveYAMLComments(content []byte) ([]byte, error) {
rLines := regexp.MustCompile(`(?m:^[ \t]*#.*\n)`) // ?m: = multiline flag
matches := rLines.FindAllIndex(content, -1)

newContent, err := removeMatches(content, matches)
if err != nil {
return content, err
}

rInlined := regexp.MustCompile(`('.*'|".*")[ \t]*]?(?P<comment>[ \t].*)`)
subMatches := rInlined.FindAllSubmatchIndex(newContent, -1)

// retrieve matches only for "comment" capture group
var commentMatches [][]int
for _, indexes := range subMatches {
// 0,1: 1st capt group
// 2,3: 2nd capt group
// 4,5: 3rd capt group
if len(indexes) == 6 {
commentMatches = append(commentMatches, []int{indexes[4], indexes[5]})
}
}

return removeMatches(newContent, commentMatches)
}

func removeMatches(content []byte, matches [][]int) ([]byte, error) {
if len(matches) == 0 {
return content, nil
}
Expand Down
72 changes: 72 additions & 0 deletions pkg/config/envvar/envvar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,75 @@ func TestExpandInContent(t *testing.T) {
})
}
}

func TestRemoveYAMLComments(t *testing.T) {
noComments := `integration_name: com.newrelic.mysql
instances:
- name: mysql-server
`
comments := `integration_name: com.newrelic.mysql
instances:
- name: foo
command: bar
arguments:
username0: foo# comment
username1: foo # comment
username2: 'foo' # comment
username3: "foo" # comment
password1: '87l#154NUp'
password2: "87l#154NUp"
password3: 'foo#bar' # psw with comment char, single quotes
password4: "foo#bar" # psw with comment char, double queotes
password5: ['foo#bar', 'baz' ] # another inline comment
password6: ["foo#bar", "baz" ] # another inline comment
# some comments
# some comments
remote_monitoring: true
labels:
foo: bar
`
commentsStripped := `integration_name: com.newrelic.mysql
instances:
- name: foo
command: bar
arguments:
username0: foo
username1: foo
username2: 'foo'
username3: "foo"
password1: '87l#154NUp'
password2: "87l#154NUp"
password3: 'foo#bar'
password4: "foo#bar"
password5: ['foo#bar', 'baz' ]
password6: ["foo#bar", "baz" ]
remote_monitoring: true
labels:
foo: bar
`

tests := []struct {
name string
content string
want string
wantErr bool
}{
{"empty", ``, ``, false},
{"no comments", noComments, noComments, false},
{"comments", comments, commentsStripped, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := RemoveYAMLComments([]byte(tt.content))
assert.Equal(t, tt.want, string(got))
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
})
}
}
2 changes: 1 addition & 1 deletion pkg/integrations/cmdrequest/protocol/jsonlines.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Version int

// CmdRequestDiscriminator represents the JSON shape for an integration command request.
type CmdRequestDiscriminator struct {
CommandRequestVersion string `json:"command_request_version"`
CommandRequestVersion string `json:"config_protocol_version"`
}

// CmdRequestV1 carries an integration payload requesting command/s execution.
Expand Down
2 changes: 1 addition & 1 deletion pkg/integrations/legacy/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (pr *PluginRegistry) loadPluginInstance(dir string, dirOrFile os.FileInfo)
pflog.Debug("Found integration config file.")
instanceWrapper, err := loadPluginInstanceWrapper(dirOrFilePath)
if err != nil {
pflog.Error("cannot load integration config file")
pflog.WithError(err).Error("cannot load integration config file")
return
}

Expand Down

0 comments on commit f307b7c

Please sign in to comment.