Skip to content

Commit

Permalink
Allow - in eql variable names (#710)
Browse files Browse the repository at this point in the history
* fix to allow dashes in variable names in EQL expressions

extend eql to allow the '-' char to appear in variable names, i.e.,
${data.some-var} and additional test cases to eql, the transpiler, and
the k8s provider to verify this works. Note that the bug was caused by
the EQL limitation, the otehr test cases were added when attempting to
find it.

* Regenerate grammer with antlr 4.7.1, add CHANGELOG

* Fix linter issue

* Fix typo
  • Loading branch information
michel-laterman authored Jul 13, 2022
1 parent be33239 commit 28721eb
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 109 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
- Collects stdout and stderr of applications run as a process and logs them. {issue}[88]
- Remove VerificationMode option to empty string. Default value is `full`. {issue}[184]
- diagnostics collect file mod times are set. {pull}570[570]
- Allow the - char to appear as part of variable names in eql expressions. {issue}709[709] {pull}710[710]

==== New features

Expand Down
11 changes: 9 additions & 2 deletions internal/pkg/agent/transpiler/vars_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ import (
func TestVars_Replace(t *testing.T) {
vars := mustMakeVars(map[string]interface{}{
"un-der_score": map[string]interface{}{
"key1": "data1",
"key2": "data2",
"key1": "data1",
"key2": "data2",
"with-dash": "dash-value",
"list": []string{
"array1",
"array2",
Expand All @@ -44,6 +45,12 @@ func TestVars_Replace(t *testing.T) {
false,
false,
},
{
"${un-der_score.with-dash}",
NewStrVal("dash-value"),
false,
false,
},
{
"${un-der_score.missing}",
NewStrVal(""),
Expand Down
14 changes: 10 additions & 4 deletions internal/pkg/composable/providers/kubernetes/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ func TestGenerateNodeData(t *testing.T) {
Name: "testnode",
UID: types.UID(uid),
Labels: map[string]string{
"foo": "bar",
"foo": "bar",
"with-dash": "dash-value",
},
Annotations: map[string]string{
"baz": "ban",
Expand Down Expand Up @@ -54,7 +55,8 @@ func TestGenerateNodeData(t *testing.T) {
"baz": "ban",
},
"labels": mapstr.M{
"foo": "bar",
"foo": "bar",
"with-dash": "dash-value",
},
}

Expand All @@ -64,7 +66,10 @@ func TestGenerateNodeData(t *testing.T) {
"name": "devcluster",
"url": "8.8.8.8:9090"},
}, "kubernetes": mapstr.M{
"labels": mapstr.M{"foo": "bar"},
"labels": mapstr.M{
"foo": "bar",
"with-dash": "dash-value",
},
"annotations": mapstr.M{"baz": "ban"},
"node": mapstr.M{
"ip": "node1",
Expand Down Expand Up @@ -123,7 +128,8 @@ func (n *nodeMeta) GenerateK8s(obj kubernetes.Resource, opts ...metadata.FieldOp
"ip": "node1",
},
"labels": mapstr.M{
"foo": "bar",
"foo": "bar",
"with-dash": "dash-value",
},
"annotations": mapstr.M{
"baz": "ban",
Expand Down
34 changes: 23 additions & 11 deletions internal/pkg/composable/providers/kubernetes/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ func TestGeneratePodData(t *testing.T) {
UID: types.UID(uid),
Namespace: "testns",
Labels: map[string]string{
"foo": "bar",
"foo": "bar",
"with-dash": "dash-value",
},
Annotations: map[string]string{
"app": "production",
Expand Down Expand Up @@ -59,7 +60,8 @@ func TestGeneratePodData(t *testing.T) {
"nsa": "nsb",
},
"labels": mapstr.M{
"foo": "bar",
"foo": "bar",
"with-dash": "dash-value",
},
"annotations": mapstr.M{
"app": "production",
Expand All @@ -74,7 +76,8 @@ func TestGeneratePodData(t *testing.T) {
}, "kubernetes": mapstr.M{
"namespace": "testns",
"labels": mapstr.M{
"foo": "bar",
"foo": "bar",
"with-dash": "dash-value",
},
"annotations": mapstr.M{"app": "production"},
"pod": mapstr.M{
Expand Down Expand Up @@ -119,7 +122,8 @@ func TestGenerateContainerPodData(t *testing.T) {
UID: types.UID(uid),
Namespace: "testns",
Labels: map[string]string{
"foo": "bar",
"foo": "bar",
"with-dash": "dash-value",
},
Annotations: map[string]string{
"app": "production",
Expand Down Expand Up @@ -175,7 +179,8 @@ func TestGenerateContainerPodData(t *testing.T) {
"app": "production",
},
"labels": mapstr.M{
"foo": "bar",
"foo": "bar",
"with-dash": "dash-value",
},
}

Expand All @@ -191,7 +196,9 @@ func TestGenerateContainerPodData(t *testing.T) {
}, "kubernetes": mapstr.M{
"namespace": "testns",
"annotations": mapstr.M{"app": "production"},
"labels": mapstr.M{"foo": "bar"},
"labels": mapstr.M{"foo": "bar",
"with-dash": "dash-value",
},
"pod": mapstr.M{
"ip": "127.0.0.5",
"name": "testpod",
Expand Down Expand Up @@ -232,7 +239,8 @@ func TestEphemeralContainers(t *testing.T) {
UID: types.UID(uid),
Namespace: "testns",
Labels: map[string]string{
"foo": "bar",
"foo": "bar",
"with-dash": "dash-value",
},
Annotations: map[string]string{
"app": "production",
Expand Down Expand Up @@ -274,7 +282,8 @@ func TestEphemeralContainers(t *testing.T) {
"ip": pod.Status.PodIP,
},
"labels": mapstr.M{
"foo": "bar",
"foo": "bar",
"with-dash": "dash-value",
},
"container": mapstr.M{
"id": "asdfghdeadbeef",
Expand All @@ -300,8 +309,10 @@ func TestEphemeralContainers(t *testing.T) {
"name": "devcluster",
"url": "8.8.8.8:9090"},
}, "kubernetes": mapstr.M{
"namespace": "testns",
"labels": mapstr.M{"foo": "bar"},
"namespace": "testns",
"labels": mapstr.M{"foo": "bar",
"with-dash": "dash-value",
},
"annotations": mapstr.M{"app": "production"},
"pod": mapstr.M{
"ip": "127.0.0.5",
Expand Down Expand Up @@ -383,7 +394,8 @@ func (p *podMeta) GenerateK8s(obj kubernetes.Resource, opts ...metadata.FieldOpt
"ip": k8sPod.Status.PodIP,
},
"labels": mapstr.M{
"foo": "bar",
"foo": "bar",
"with-dash": "dash-value",
},
"annotations": mapstr.M{
"app": "production",
Expand Down
12 changes: 8 additions & 4 deletions internal/pkg/composable/providers/kubernetes/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ func TestGenerateServiceData(t *testing.T) {
UID: types.UID(uid),
Namespace: "testns",
Labels: map[string]string{
"foo": "bar",
"foo": "bar",
"with-dash": "dash-value",
},
Annotations: map[string]string{
"baz": "ban",
Expand Down Expand Up @@ -64,7 +65,8 @@ func TestGenerateServiceData(t *testing.T) {
"baz": "ban",
},
"labels": mapstr.M{
"foo": "bar",
"foo": "bar",
"with-dash": "dash-value",
},
}

Expand All @@ -80,7 +82,8 @@ func TestGenerateServiceData(t *testing.T) {
"ip": "1.2.3.4",
},
"labels": mapstr.M{
"foo": "bar",
"foo": "bar",
"with-dash": "dash-value",
},
"annotations": mapstr.M{
"baz": "ban",
Expand Down Expand Up @@ -139,7 +142,8 @@ func (s *svcMeta) GenerateK8s(obj kubernetes.Resource, opts ...metadata.FieldOpt
"ip": "1.2.3.4",
},
"labels": mapstr.M{
"foo": "bar",
"foo": "bar",
"with-dash": "dash-value",
},
"annotations": mapstr.M{
"baz": "ban",
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/eql/Eql.g4
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ NUMBER: [\-]? [0-9]+;
WHITESPACE: [ \r\n\t]+ -> skip;
NOT: 'NOT' | 'not';
NAME: [a-zA-Z_] [a-zA-Z0-9_]*;
VNAME: [a-zA-Z0-9_.]+('.'[a-zA-Z0-9_]+)*;
VNAME: [a-zA-Z0-9_.-]+('.'[a-zA-Z0-9_-]+)*;
STEXT: '\'' ~[\r\n']* '\'';
DTEXT: '"' ~[\r\n"]* '"';
LPAR: '(';
Expand Down
21 changes: 12 additions & 9 deletions internal/pkg/eql/eql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ func TestEql(t *testing.T) {
{expression: "${env.MISSING|host.MISSING|true} == true", result: true},
{expression: "${env.MISSING|host.MISSING|false} == false", result: true},
{expression: "${'constant'} == 'constant'", result: true},
{expression: "${data.with-dash} == 'dash-value'", result: true},
{expression: "${'dash-value'} == 'dash-value'", result: true},

// boolean
{expression: "true", result: true},
Expand Down Expand Up @@ -306,9 +308,10 @@ func TestEql(t *testing.T) {

store := &testVarStore{
vars: map[string]interface{}{
"env.HOSTNAME": "my-hostname",
"host.name": "host-name",
"data.array": []interface{}{"array1", "array2", "array3"},
"env.HOSTNAME": "my-hostname",
"host.name": "host-name",
"data.array": []interface{}{"array1", "array2", "array3"},
"data.with-dash": "dash-value",
"data.dict": map[string]interface{}{
"key1": "dict1",
"key2": "dict2",
Expand All @@ -327,7 +330,7 @@ func TestEql(t *testing.T) {
}
t.Run(title, func(t *testing.T) {
if showDebug == "1" {
debug(test.expression)
debug(t, test.expression)
}

r, err := Eval(test.expression, store)
Expand All @@ -343,17 +346,17 @@ func TestEql(t *testing.T) {
}
}

func debug(expression string) {
func debug(t *testing.T, expression string) {
raw := antlr.NewInputStream(expression)

lexer := parser.NewEqlLexer(raw)
for {
t := lexer.NextToken()
if t.GetTokenType() == antlr.TokenEOF {
token := lexer.NextToken()
if token.GetTokenType() == antlr.TokenEOF {
break
}
fmt.Printf("%s (%q)\n",
lexer.SymbolicNames[t.GetTokenType()], t.GetText())
t.Logf("%s (%q)\n",
lexer.SymbolicNames[token.GetTokenType()], token.GetText())
}
}

Expand Down
Loading

0 comments on commit 28721eb

Please sign in to comment.