Skip to content

Commit

Permalink
Allow / to be used in variable names (#718)
Browse files Browse the repository at this point in the history
* 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

(cherry picked from commit e34e0e7)
  • Loading branch information
michel-laterman authored and mergify[bot] committed Jul 19, 2022
1 parent fcee44f commit 5d597f6
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 120 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
- diagnostics collect file mod times are set. {pull}570[570]
- Allow ':' characters in dynamic variables {issue}624[624] {pull}680[680]
- Allow the - char to appear as part of variable names in eql expressions. {issue}709[709] {pull}710[710]
- Allow the / char in variable names in eql and transpiler. {issue}715[715] {pull}718[718]

==== New features

Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/agent/transpiler/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/elastic/elastic-agent/internal/pkg/core/composable"
)

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 internal/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
20 changes: 12 additions & 8 deletions internal/pkg/composable/providers/kubernetes/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,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 @@ -55,8 +56,9 @@ func TestGenerateNodeData(t *testing.T) {
"baz": "ban",
},
"labels": mapstr.M{
"foo": "bar",
"with-dash": "dash-value",
"foo": "bar",
"with-dash": "dash-value",
"with/slash": "some/path",
},
}

Expand All @@ -67,8 +69,9 @@ func TestGenerateNodeData(t *testing.T) {
"url": "8.8.8.8:9090"},
}, "kubernetes": mapstr.M{
"labels": mapstr.M{
"foo": "bar",
"with-dash": "dash-value",
"foo": "bar",
"with-dash": "dash-value",
"with/slash": "some/path",
},
"annotations": mapstr.M{"baz": "ban"},
"node": mapstr.M{
Expand Down Expand Up @@ -128,8 +131,9 @@ func (n *nodeMeta) GenerateK8s(obj kubernetes.Resource, opts ...metadata.FieldOp
"ip": "node1",
},
"labels": mapstr.M{
"foo": "bar",
"with-dash": "dash-value",
"foo": "bar",
"with-dash": "dash-value",
"with/slash": "some/path",
},
"annotations": mapstr.M{
"baz": "ban",
Expand Down
52 changes: 32 additions & 20 deletions internal/pkg/composable/providers/kubernetes/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,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 @@ -60,8 +61,9 @@ func TestGeneratePodData(t *testing.T) {
"nsa": "nsb",
},
"labels": mapstr.M{
"foo": "bar",
"with-dash": "dash-value",
"foo": "bar",
"with-dash": "dash-value",
"with/slash": "some/path",
},
"annotations": mapstr.M{
"app": "production",
Expand All @@ -76,8 +78,9 @@ func TestGeneratePodData(t *testing.T) {
}, "kubernetes": mapstr.M{
"namespace": "testns",
"labels": mapstr.M{
"foo": "bar",
"with-dash": "dash-value",
"foo": "bar",
"with-dash": "dash-value",
"with/slash": "some/path",
},
"annotations": mapstr.M{"app": "production"},
"pod": mapstr.M{
Expand Down Expand Up @@ -122,8 +125,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 @@ -179,8 +183,9 @@ func TestGenerateContainerPodData(t *testing.T) {
"app": "production",
},
"labels": mapstr.M{
"foo": "bar",
"with-dash": "dash-value",
"foo": "bar",
"with-dash": "dash-value",
"with/slash": "some/path",
},
}

Expand All @@ -196,8 +201,10 @@ func TestGenerateContainerPodData(t *testing.T) {
}, "kubernetes": mapstr.M{
"namespace": "testns",
"annotations": mapstr.M{"app": "production"},
"labels": mapstr.M{"foo": "bar",
"with-dash": "dash-value",
"labels": mapstr.M{
"foo": "bar",
"with-dash": "dash-value",
"with/slash": "some/path",
},
"pod": mapstr.M{
"ip": "127.0.0.5",
Expand Down Expand Up @@ -239,8 +246,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 @@ -282,8 +290,9 @@ func TestEphemeralContainers(t *testing.T) {
"ip": pod.Status.PodIP,
},
"labels": mapstr.M{
"foo": "bar",
"with-dash": "dash-value",
"foo": "bar",
"with-dash": "dash-value",
"with/slash": "some/path",
},
"container": mapstr.M{
"id": "asdfghdeadbeef",
Expand All @@ -310,8 +319,10 @@ func TestEphemeralContainers(t *testing.T) {
"url": "8.8.8.8:9090"},
}, "kubernetes": mapstr.M{
"namespace": "testns",
"labels": mapstr.M{"foo": "bar",
"with-dash": "dash-value",
"labels": mapstr.M{
"foo": "bar",
"with-dash": "dash-value",
"with/slash": "some/path",
},
"annotations": mapstr.M{"app": "production"},
"pod": mapstr.M{
Expand Down Expand Up @@ -394,8 +405,9 @@ func (p *podMeta) GenerateK8s(obj kubernetes.Resource, opts ...metadata.FieldOpt
"ip": k8sPod.Status.PodIP,
},
"labels": mapstr.M{
"foo": "bar",
"with-dash": "dash-value",
"foo": "bar",
"with-dash": "dash-value",
"with/slash": "some/path",
},
"annotations": mapstr.M{
"app": "production",
Expand Down
20 changes: 12 additions & 8 deletions internal/pkg/composable/providers/kubernetes/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,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 @@ -65,8 +66,9 @@ func TestGenerateServiceData(t *testing.T) {
"baz": "ban",
},
"labels": mapstr.M{
"foo": "bar",
"with-dash": "dash-value",
"foo": "bar",
"with-dash": "dash-value",
"with/slash": "some/path",
},
}

Expand All @@ -82,8 +84,9 @@ func TestGenerateServiceData(t *testing.T) {
"ip": "1.2.3.4",
},
"labels": mapstr.M{
"foo": "bar",
"with-dash": "dash-value",
"foo": "bar",
"with-dash": "dash-value",
"with/slash": "some/path",
},
"annotations": mapstr.M{
"baz": "ban",
Expand Down Expand Up @@ -142,8 +145,9 @@ func (s *svcMeta) GenerateK8s(obj kubernetes.Resource, opts ...metadata.FieldOpt
"ip": "1.2.3.4",
},
"labels": mapstr.M{
"foo": "bar",
"with-dash": "dash-value",
"foo": "bar",
"with-dash": "dash-value",
"with/slash": "some/path",
},
"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
10 changes: 6 additions & 4 deletions internal/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 5d597f6

Please sign in to comment.