Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.17] (backport elastic-agent#710) Allow - in eql variable names #32350

Merged
merged 4 commits into from
Jul 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions x-pack/elastic-agent/CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
- Allow HTTP metrics to run in bootstrap mode. Add ability to adjust timeouts for Fleet Server. {pull}28260[28260]
- Increase the download artifact timeout to 10mins and add log download statistics. {pull}31461[31461]
- Add filemod times to files in diagnostics collect bundle. {pull}31986[31986]
- Allow the - char to appear as part of variable names in eql expressions. {pull}32350[32350]

==== New features

Expand Down
11 changes: 9 additions & 2 deletions x-pack/elastic-agent/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
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,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 @@ -55,7 +56,8 @@ func TestGenerateNodeData(t *testing.T) {
"baz": "ban",
},
"labels": common.MapStr{
"foo": "bar",
"foo": "bar",
"with-dash": "dash-value",
},
}

Expand All @@ -65,7 +67,10 @@ func TestGenerateNodeData(t *testing.T) {
"name": "devcluster",
"url": "8.8.8.8:9090"},
}, "kubernetes": common.MapStr{
"labels": common.MapStr{"foo": "bar"},
"labels": common.MapStr{
"foo": "bar",
"with-dash": "dash-value",
},
"annotations": common.MapStr{"baz": "ban"},
"node": common.MapStr{
"ip": "node1",
Expand Down Expand Up @@ -124,7 +129,8 @@ func (n *nodeMeta) GenerateK8s(obj kubernetes.Resource, opts ...metadata.FieldOp
"ip": "node1",
},
"labels": common.MapStr{
"foo": "bar",
"foo": "bar",
"with-dash": "dash-value",
},
"annotations": common.MapStr{
"baz": "ban",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,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 @@ -60,7 +61,8 @@ func TestGeneratePodData(t *testing.T) {
"nsa": "nsb",
},
"labels": common.MapStr{
"foo": "bar",
"foo": "bar",
"with-dash": "dash-value",
},
"annotations": common.MapStr{
"app": "production",
Expand All @@ -75,7 +77,8 @@ func TestGeneratePodData(t *testing.T) {
}, "kubernetes": common.MapStr{
"namespace": "testns",
"labels": common.MapStr{
"foo": "bar",
"foo": "bar",
"with-dash": "dash-value",
},
"annotations": common.MapStr{"app": "production"},
"pod": common.MapStr{
Expand Down Expand Up @@ -121,7 +124,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 @@ -178,7 +182,8 @@ func TestGenerateContainerPodData(t *testing.T) {
"app": "production",
},
"labels": common.MapStr{
"foo": "bar",
"foo": "bar",
"with-dash": "dash-value",
},
}

Expand All @@ -194,7 +199,10 @@ func TestGenerateContainerPodData(t *testing.T) {
}, "kubernetes": common.MapStr{
"namespace": "testns",
"annotations": common.MapStr{"app": "production"},
"labels": common.MapStr{"foo": "bar"},
"labels": common.MapStr{
"foo": "bar",
"with-dash": "dash-value",
},
"pod": common.MapStr{
"ip": "127.0.0.5",
"name": "testpod",
Expand Down Expand Up @@ -236,7 +244,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 @@ -279,7 +288,8 @@ func TestEphemeralContainers(t *testing.T) {
"ip": pod.Status.PodIP,
},
"labels": common.MapStr{
"foo": "bar",
"foo": "bar",
"with-dash": "dash-value",
},
"container": common.MapStr{
"id": "asdfghdeadbeef",
Expand All @@ -305,8 +315,11 @@ func TestEphemeralContainers(t *testing.T) {
"name": "devcluster",
"url": "8.8.8.8:9090"},
}, "kubernetes": common.MapStr{
"namespace": "testns",
"labels": common.MapStr{"foo": "bar"},
"namespace": "testns",
"labels": common.MapStr{
"foo": "bar",
"with-dash": "dash-value",
},
"annotations": common.MapStr{"app": "production"},
"pod": common.MapStr{
"ip": "127.0.0.5",
Expand Down Expand Up @@ -388,7 +401,8 @@ func (p *podMeta) GenerateK8s(obj kubernetes.Resource, opts ...metadata.FieldOpt
"ip": k8sPod.Status.PodIP,
},
"labels": common.MapStr{
"foo": "bar",
"foo": "bar",
"with-dash": "dash-value",
},
"annotations": common.MapStr{
"app": "production",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,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 @@ -68,7 +69,8 @@ func TestGenerateServiceData(t *testing.T) {
"baz": "ban",
},
"labels": common.MapStr{
"foo": "bar",
"foo": "bar",
"with-dash": "dash-value",
},
}

Expand All @@ -84,7 +86,8 @@ func TestGenerateServiceData(t *testing.T) {
"ip": "1.2.3.4",
},
"labels": common.MapStr{
"foo": "bar",
"foo": "bar",
"with-dash": "dash-value",
},
"annotations": common.MapStr{
"baz": "ban",
Expand Down Expand Up @@ -143,7 +146,8 @@ func (s *svcMeta) GenerateK8s(obj kubernetes.Resource, opts ...metadata.FieldOpt
"ip": "1.2.3.4",
},
"labels": common.MapStr{
"foo": "bar",
"foo": "bar",
"with-dash": "dash-value",
},
"annotations": common.MapStr{
"baz": "ban",
Expand Down
2 changes: 1 addition & 1 deletion x-pack/elastic-agent/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 x-pack/elastic-agent/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