Skip to content

Commit ceaa6e0

Browse files
Allow / to be used in variable names (#718) (#744)
* 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) Co-authored-by: Michel Laterman <[email protected]>
1 parent fcee44f commit ceaa6e0

File tree

10 files changed

+150
-120
lines changed

10 files changed

+150
-120
lines changed

CHANGELOG.next.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
- diagnostics collect file mod times are set. {pull}570[570]
107107
- Allow ':' characters in dynamic variables {issue}624[624] {pull}680[680]
108108
- Allow the - char to appear as part of variable names in eql expressions. {issue}709[709] {pull}710[710]
109+
- Allow the / char in variable names in eql and transpiler. {issue}715[715] {pull}718[718]
109110

110111
==== New features
111112

internal/pkg/agent/transpiler/vars.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"github.com/elastic/elastic-agent/internal/pkg/core/composable"
1515
)
1616

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

1919
// ErrNoMatch is return when the replace didn't fail, just that no vars match to perform the replace.
2020
var ErrNoMatch = fmt.Errorf("no matching vars")

internal/pkg/agent/transpiler/vars_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ func TestVars_Replace(t *testing.T) {
2424
"array1",
2525
"array2",
2626
},
27+
"with/slash": "some/path",
2728
"dict": map[string]interface{}{
2829
"key1": "value1",
2930
"key2": "value2",
@@ -168,6 +169,12 @@ func TestVars_Replace(t *testing.T) {
168169
false,
169170
false,
170171
},
172+
{
173+
`${un-der_score.with/slash}`,
174+
NewStrVal(`some/path`),
175+
false,
176+
false,
177+
},
171178
{
172179
`list inside string ${un-der_score.list} causes no match`,
173180
NewList([]Node{

internal/pkg/composable/providers/kubernetes/node_test.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ func TestGenerateNodeData(t *testing.T) {
2626
Name: "testnode",
2727
UID: types.UID(uid),
2828
Labels: map[string]string{
29-
"foo": "bar",
30-
"with-dash": "dash-value",
29+
"foo": "bar",
30+
"with-dash": "dash-value",
31+
"with/slash": "some/path",
3132
},
3233
Annotations: map[string]string{
3334
"baz": "ban",
@@ -55,8 +56,9 @@ func TestGenerateNodeData(t *testing.T) {
5556
"baz": "ban",
5657
},
5758
"labels": mapstr.M{
58-
"foo": "bar",
59-
"with-dash": "dash-value",
59+
"foo": "bar",
60+
"with-dash": "dash-value",
61+
"with/slash": "some/path",
6062
},
6163
}
6264

@@ -67,8 +69,9 @@ func TestGenerateNodeData(t *testing.T) {
6769
"url": "8.8.8.8:9090"},
6870
}, "kubernetes": mapstr.M{
6971
"labels": mapstr.M{
70-
"foo": "bar",
71-
"with-dash": "dash-value",
72+
"foo": "bar",
73+
"with-dash": "dash-value",
74+
"with/slash": "some/path",
7275
},
7376
"annotations": mapstr.M{"baz": "ban"},
7477
"node": mapstr.M{
@@ -128,8 +131,9 @@ func (n *nodeMeta) GenerateK8s(obj kubernetes.Resource, opts ...metadata.FieldOp
128131
"ip": "node1",
129132
},
130133
"labels": mapstr.M{
131-
"foo": "bar",
132-
"with-dash": "dash-value",
134+
"foo": "bar",
135+
"with-dash": "dash-value",
136+
"with/slash": "some/path",
133137
},
134138
"annotations": mapstr.M{
135139
"baz": "ban",

internal/pkg/composable/providers/kubernetes/pod_test.go

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ func TestGeneratePodData(t *testing.T) {
2727
UID: types.UID(uid),
2828
Namespace: "testns",
2929
Labels: map[string]string{
30-
"foo": "bar",
31-
"with-dash": "dash-value",
30+
"foo": "bar",
31+
"with-dash": "dash-value",
32+
"with/slash": "some/path",
3233
},
3334
Annotations: map[string]string{
3435
"app": "production",
@@ -60,8 +61,9 @@ func TestGeneratePodData(t *testing.T) {
6061
"nsa": "nsb",
6162
},
6263
"labels": mapstr.M{
63-
"foo": "bar",
64-
"with-dash": "dash-value",
64+
"foo": "bar",
65+
"with-dash": "dash-value",
66+
"with/slash": "some/path",
6567
},
6668
"annotations": mapstr.M{
6769
"app": "production",
@@ -76,8 +78,9 @@ func TestGeneratePodData(t *testing.T) {
7678
}, "kubernetes": mapstr.M{
7779
"namespace": "testns",
7880
"labels": mapstr.M{
79-
"foo": "bar",
80-
"with-dash": "dash-value",
81+
"foo": "bar",
82+
"with-dash": "dash-value",
83+
"with/slash": "some/path",
8184
},
8285
"annotations": mapstr.M{"app": "production"},
8386
"pod": mapstr.M{
@@ -122,8 +125,9 @@ func TestGenerateContainerPodData(t *testing.T) {
122125
UID: types.UID(uid),
123126
Namespace: "testns",
124127
Labels: map[string]string{
125-
"foo": "bar",
126-
"with-dash": "dash-value",
128+
"foo": "bar",
129+
"with-dash": "dash-value",
130+
"with/slash": "some/path",
127131
},
128132
Annotations: map[string]string{
129133
"app": "production",
@@ -179,8 +183,9 @@ func TestGenerateContainerPodData(t *testing.T) {
179183
"app": "production",
180184
},
181185
"labels": mapstr.M{
182-
"foo": "bar",
183-
"with-dash": "dash-value",
186+
"foo": "bar",
187+
"with-dash": "dash-value",
188+
"with/slash": "some/path",
184189
},
185190
}
186191

@@ -196,8 +201,10 @@ func TestGenerateContainerPodData(t *testing.T) {
196201
}, "kubernetes": mapstr.M{
197202
"namespace": "testns",
198203
"annotations": mapstr.M{"app": "production"},
199-
"labels": mapstr.M{"foo": "bar",
200-
"with-dash": "dash-value",
204+
"labels": mapstr.M{
205+
"foo": "bar",
206+
"with-dash": "dash-value",
207+
"with/slash": "some/path",
201208
},
202209
"pod": mapstr.M{
203210
"ip": "127.0.0.5",
@@ -239,8 +246,9 @@ func TestEphemeralContainers(t *testing.T) {
239246
UID: types.UID(uid),
240247
Namespace: "testns",
241248
Labels: map[string]string{
242-
"foo": "bar",
243-
"with-dash": "dash-value",
249+
"foo": "bar",
250+
"with-dash": "dash-value",
251+
"with/slash": "some/path",
244252
},
245253
Annotations: map[string]string{
246254
"app": "production",
@@ -282,8 +290,9 @@ func TestEphemeralContainers(t *testing.T) {
282290
"ip": pod.Status.PodIP,
283291
},
284292
"labels": mapstr.M{
285-
"foo": "bar",
286-
"with-dash": "dash-value",
293+
"foo": "bar",
294+
"with-dash": "dash-value",
295+
"with/slash": "some/path",
287296
},
288297
"container": mapstr.M{
289298
"id": "asdfghdeadbeef",
@@ -310,8 +319,10 @@ func TestEphemeralContainers(t *testing.T) {
310319
"url": "8.8.8.8:9090"},
311320
}, "kubernetes": mapstr.M{
312321
"namespace": "testns",
313-
"labels": mapstr.M{"foo": "bar",
314-
"with-dash": "dash-value",
322+
"labels": mapstr.M{
323+
"foo": "bar",
324+
"with-dash": "dash-value",
325+
"with/slash": "some/path",
315326
},
316327
"annotations": mapstr.M{"app": "production"},
317328
"pod": mapstr.M{
@@ -394,8 +405,9 @@ func (p *podMeta) GenerateK8s(obj kubernetes.Resource, opts ...metadata.FieldOpt
394405
"ip": k8sPod.Status.PodIP,
395406
},
396407
"labels": mapstr.M{
397-
"foo": "bar",
398-
"with-dash": "dash-value",
408+
"foo": "bar",
409+
"with-dash": "dash-value",
410+
"with/slash": "some/path",
399411
},
400412
"annotations": mapstr.M{
401413
"app": "production",

internal/pkg/composable/providers/kubernetes/service_test.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ func TestGenerateServiceData(t *testing.T) {
2525
UID: types.UID(uid),
2626
Namespace: "testns",
2727
Labels: map[string]string{
28-
"foo": "bar",
29-
"with-dash": "dash-value",
28+
"foo": "bar",
29+
"with-dash": "dash-value",
30+
"with/slash": "some/path",
3031
},
3132
Annotations: map[string]string{
3233
"baz": "ban",
@@ -65,8 +66,9 @@ func TestGenerateServiceData(t *testing.T) {
6566
"baz": "ban",
6667
},
6768
"labels": mapstr.M{
68-
"foo": "bar",
69-
"with-dash": "dash-value",
69+
"foo": "bar",
70+
"with-dash": "dash-value",
71+
"with/slash": "some/path",
7072
},
7173
}
7274

@@ -82,8 +84,9 @@ func TestGenerateServiceData(t *testing.T) {
8284
"ip": "1.2.3.4",
8385
},
8486
"labels": mapstr.M{
85-
"foo": "bar",
86-
"with-dash": "dash-value",
87+
"foo": "bar",
88+
"with-dash": "dash-value",
89+
"with/slash": "some/path",
8790
},
8891
"annotations": mapstr.M{
8992
"baz": "ban",
@@ -142,8 +145,9 @@ func (s *svcMeta) GenerateK8s(obj kubernetes.Resource, opts ...metadata.FieldOpt
142145
"ip": "1.2.3.4",
143146
},
144147
"labels": mapstr.M{
145-
"foo": "bar",
146-
"with-dash": "dash-value",
148+
"foo": "bar",
149+
"with-dash": "dash-value",
150+
"with/slash": "some/path",
147151
},
148152
"annotations": mapstr.M{
149153
"baz": "ban",

internal/pkg/eql/Eql.g4

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ NUMBER: [\-]? [0-9]+;
2222
WHITESPACE: [ \r\n\t]+ -> skip;
2323
NOT: 'NOT' | 'not';
2424
NAME: [a-zA-Z_] [a-zA-Z0-9_]*;
25-
VNAME: [a-zA-Z0-9_.-]+('.'[a-zA-Z0-9_-]+)*;
25+
VNAME: [a-zA-Z0-9_.\-/]+('.'[a-zA-Z0-9_\-/]+)*;
2626
STEXT: '\'' ~[\r\n']* '\'';
2727
DTEXT: '"' ~[\r\n"]* '"';
2828
LPAR: '(';

internal/pkg/eql/eql_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ func TestEql(t *testing.T) {
4444
{expression: "${'constant'} == 'constant'", result: true},
4545
{expression: "${data.with-dash} == 'dash-value'", result: true},
4646
{expression: "${'dash-value'} == 'dash-value'", result: true},
47+
{expression: "${data.with/slash} == 'some/path'", result: true},
4748

4849
// boolean
4950
{expression: "true", result: true},
@@ -308,10 +309,11 @@ func TestEql(t *testing.T) {
308309

309310
store := &testVarStore{
310311
vars: map[string]interface{}{
311-
"env.HOSTNAME": "my-hostname",
312-
"host.name": "host-name",
313-
"data.array": []interface{}{"array1", "array2", "array3"},
314-
"data.with-dash": "dash-value",
312+
"env.HOSTNAME": "my-hostname",
313+
"host.name": "host-name",
314+
"data.array": []interface{}{"array1", "array2", "array3"},
315+
"data.with-dash": "dash-value",
316+
"data.with/slash": "some/path",
315317
"data.dict": map[string]interface{}{
316318
"key1": "dict1",
317319
"key2": "dict2",

0 commit comments

Comments
 (0)