Skip to content

Commit

Permalink
[7.17] (backport elastic-agent#718) Allow / to be used in variable na…
Browse files Browse the repository at this point in the history
…mes (#32528)

* [7.17] (backport elastic-agent#718) Allow / to be used in variable names

Backport elastic/elastic-agent#718

* Allow the / character to be used in variable names.

Allow / to be used in variable names from dynamic providers and eql
expressions. Ensure that k8s providers can provide variables with
slashes in their names.

* run antlr4

* Fix tests

* Fix broken test case
  • Loading branch information
michel-laterman authored Jul 28, 2022
1 parent 2cf7076 commit d2828cf
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 120 deletions.
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 @@ -95,6 +95,7 @@
- Add filemod times to files in diagnostics collect bundle. {pull}31986[31986]
- Allow ':' characters in dynamic variables. {pull}32407[32407]
- Allow the - char to appear as part of variable names in eql expressions. {pull}32350[32350]
- Allow the / char to appear as part of variable names in eql expressions. {pull}32528{32528}

==== New features

Expand Down
2 changes: 1 addition & 1 deletion x-pack/elastic-agent/pkg/agent/transpiler/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/elastic/beats/v7/libbeat/common"
)

var varsRegex = regexp.MustCompile(`\${([\p{L}\d\s\\\-_|.'":]*)}`)
var varsRegex = regexp.MustCompile(`\${([\p{L}\d\s\\\-_|.'":\/]*)}`)

// ErrNoMatch is return when the replace didn't fail, just that no vars match to perform the replace.
var ErrNoMatch = fmt.Errorf("no matching vars")
Expand Down
7 changes: 7 additions & 0 deletions x-pack/elastic-agent/pkg/agent/transpiler/vars_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func TestVars_Replace(t *testing.T) {
"array1",
"array2",
},
"with/slash": "some/path",
"dict": map[string]interface{}{
"key1": "value1",
"key2": "value2",
Expand Down Expand Up @@ -168,6 +169,12 @@ func TestVars_Replace(t *testing.T) {
false,
false,
},
{
`${un-der_score.with/slash}`,
NewStrVal(`some/path`),
false,
false,
},
{
`list inside string ${un-der_score.list} causes no match`,
NewList([]Node{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ func TestGenerateNodeData(t *testing.T) {
Name: "testnode",
UID: types.UID(uid),
Labels: map[string]string{
"foo": "bar",
"with-dash": "dash-value",
"foo": "bar",
"with-dash": "dash-value",
"with/slash": "some/path",
},
Annotations: map[string]string{
"baz": "ban",
Expand Down Expand Up @@ -56,8 +57,9 @@ func TestGenerateNodeData(t *testing.T) {
"baz": "ban",
},
"labels": common.MapStr{
"foo": "bar",
"with-dash": "dash-value",
"foo": "bar",
"with-dash": "dash-value",
"with/slash": "some/path",
},
}

Expand All @@ -68,8 +70,9 @@ func TestGenerateNodeData(t *testing.T) {
"url": "8.8.8.8:9090"},
}, "kubernetes": common.MapStr{
"labels": common.MapStr{
"foo": "bar",
"with-dash": "dash-value",
"foo": "bar",
"with-dash": "dash-value",
"with/slash": "some/path",
},
"annotations": common.MapStr{"baz": "ban"},
"node": common.MapStr{
Expand Down Expand Up @@ -129,8 +132,9 @@ func (n *nodeMeta) GenerateK8s(obj kubernetes.Resource, opts ...metadata.FieldOp
"ip": "node1",
},
"labels": common.MapStr{
"foo": "bar",
"with-dash": "dash-value",
"foo": "bar",
"with-dash": "dash-value",
"with/slash": "some/path",
},
"annotations": common.MapStr{
"baz": "ban",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ func TestGeneratePodData(t *testing.T) {
UID: types.UID(uid),
Namespace: "testns",
Labels: map[string]string{
"foo": "bar",
"with-dash": "dash-value",
"foo": "bar",
"with-dash": "dash-value",
"with/slash": "some/path",
},
Annotations: map[string]string{
"app": "production",
Expand Down Expand Up @@ -61,8 +62,9 @@ func TestGeneratePodData(t *testing.T) {
"nsa": "nsb",
},
"labels": common.MapStr{
"foo": "bar",
"with-dash": "dash-value",
"foo": "bar",
"with-dash": "dash-value",
"with/slash": "some/path",
},
"annotations": common.MapStr{
"app": "production",
Expand All @@ -77,8 +79,9 @@ func TestGeneratePodData(t *testing.T) {
}, "kubernetes": common.MapStr{
"namespace": "testns",
"labels": common.MapStr{
"foo": "bar",
"with-dash": "dash-value",
"foo": "bar",
"with-dash": "dash-value",
"with/slash": "some/path",
},
"annotations": common.MapStr{"app": "production"},
"pod": common.MapStr{
Expand Down Expand Up @@ -124,8 +127,9 @@ func TestGenerateContainerPodData(t *testing.T) {
UID: types.UID(uid),
Namespace: "testns",
Labels: map[string]string{
"foo": "bar",
"with-dash": "dash-value",
"foo": "bar",
"with-dash": "dash-value",
"with/slash": "some/path",
},
Annotations: map[string]string{
"app": "production",
Expand Down Expand Up @@ -182,8 +186,9 @@ func TestGenerateContainerPodData(t *testing.T) {
"app": "production",
},
"labels": common.MapStr{
"foo": "bar",
"with-dash": "dash-value",
"foo": "bar",
"with-dash": "dash-value",
"with/slash": "some/path",
},
}

Expand All @@ -200,8 +205,9 @@ func TestGenerateContainerPodData(t *testing.T) {
"namespace": "testns",
"annotations": common.MapStr{"app": "production"},
"labels": common.MapStr{
"foo": "bar",
"with-dash": "dash-value",
"foo": "bar",
"with-dash": "dash-value",
"with/slash": "some/path",
},
"pod": common.MapStr{
"ip": "127.0.0.5",
Expand Down Expand Up @@ -244,8 +250,9 @@ func TestEphemeralContainers(t *testing.T) {
UID: types.UID(uid),
Namespace: "testns",
Labels: map[string]string{
"foo": "bar",
"with-dash": "dash-value",
"foo": "bar",
"with-dash": "dash-value",
"with/slash": "some/path",
},
Annotations: map[string]string{
"app": "production",
Expand Down Expand Up @@ -288,8 +295,9 @@ func TestEphemeralContainers(t *testing.T) {
"ip": pod.Status.PodIP,
},
"labels": common.MapStr{
"foo": "bar",
"with-dash": "dash-value",
"foo": "bar",
"with-dash": "dash-value",
"with/slash": "some/path",
},
"container": common.MapStr{
"id": "asdfghdeadbeef",
Expand Down Expand Up @@ -317,8 +325,9 @@ func TestEphemeralContainers(t *testing.T) {
}, "kubernetes": common.MapStr{
"namespace": "testns",
"labels": common.MapStr{
"foo": "bar",
"with-dash": "dash-value",
"foo": "bar",
"with-dash": "dash-value",
"with/slash": "some/path",
},
"annotations": common.MapStr{"app": "production"},
"pod": common.MapStr{
Expand Down Expand Up @@ -401,8 +410,9 @@ func (p *podMeta) GenerateK8s(obj kubernetes.Resource, opts ...metadata.FieldOpt
"ip": k8sPod.Status.PodIP,
},
"labels": common.MapStr{
"foo": "bar",
"with-dash": "dash-value",
"foo": "bar",
"with-dash": "dash-value",
"with/slash": "some/path",
},
"annotations": common.MapStr{
"app": "production",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ func TestGenerateServiceData(t *testing.T) {
UID: types.UID(uid),
Namespace: "testns",
Labels: map[string]string{
"foo": "bar",
"with-dash": "dash-value",
"foo": "bar",
"with-dash": "dash-value",
"with/slash": "some/path",
},
Annotations: map[string]string{
"baz": "ban",
Expand Down Expand Up @@ -69,8 +70,9 @@ func TestGenerateServiceData(t *testing.T) {
"baz": "ban",
},
"labels": common.MapStr{
"foo": "bar",
"with-dash": "dash-value",
"foo": "bar",
"with-dash": "dash-value",
"with/slash": "some/path",
},
}

Expand All @@ -86,8 +88,9 @@ func TestGenerateServiceData(t *testing.T) {
"ip": "1.2.3.4",
},
"labels": common.MapStr{
"foo": "bar",
"with-dash": "dash-value",
"foo": "bar",
"with-dash": "dash-value",
"with/slash": "some/path",
},
"annotations": common.MapStr{
"baz": "ban",
Expand Down Expand Up @@ -146,8 +149,9 @@ func (s *svcMeta) GenerateK8s(obj kubernetes.Resource, opts ...metadata.FieldOpt
"ip": "1.2.3.4",
},
"labels": common.MapStr{
"foo": "bar",
"with-dash": "dash-value",
"foo": "bar",
"with-dash": "dash-value",
"with/slash": "some/path",
},
"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
10 changes: 6 additions & 4 deletions x-pack/elastic-agent/pkg/eql/eql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func TestEql(t *testing.T) {
{expression: "${'constant'} == 'constant'", result: true},
{expression: "${data.with-dash} == 'dash-value'", result: true},
{expression: "${'dash-value'} == 'dash-value'", result: true},
{expression: "${data.with/slash} == 'some/path'", result: true},

// boolean
{expression: "true", result: true},
Expand Down Expand Up @@ -308,10 +309,11 @@ 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"},
"data.with-dash": "dash-value",
"env.HOSTNAME": "my-hostname",
"host.name": "host-name",
"data.array": []interface{}{"array1", "array2", "array3"},
"data.with-dash": "dash-value",
"data.with/slash": "some/path",
"data.dict": map[string]interface{}{
"key1": "dict1",
"key2": "dict2",
Expand Down
Loading

0 comments on commit d2828cf

Please sign in to comment.