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

Add unit tests for $$ appearing within policy inputs #5097

Merged
merged 8 commits into from
Jul 19, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,34 @@ func TestPolicyChange(t *testing.T) {
change := <-ch
require.Equal(t, config.MustNewConfigFrom(conf), change.Config())
})
t.Run("Received config with $$ in inputs", func(t *testing.T) {
ch := make(chan coordinator.ConfigChange, 1)

conf := map[string]interface{}{
"inputs": []interface{}{map[string]interface{}{
"type": "key",
"key": "$$$$",
}}}
action := &fleetapi.ActionPolicyChange{
ActionID: "abc123",
ActionType: "POLICY_CHANGE",
Data: fleetapi.ActionPolicyChangeData{
Policy: conf,
},
}

cfg := configuration.DefaultConfiguration()
handler := NewPolicyChangeHandler(log, agentInfo, cfg, nullStore, ch, nilLogLevelSet(t), &coordinator.Coordinator{})

err := handler.Handle(context.Background(), action, ack)
require.NoError(t, err)

change := <-ch
m, err := change.Config().ToMapStr()
require.NoError(t, err)

require.Equal(t, conf, m)
})
}

func TestPolicyAcked(t *testing.T) {
Expand Down
14 changes: 14 additions & 0 deletions internal/pkg/agent/transpiler/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,20 @@ func TestAST(t *testing.T) {
},
},
},
"special characters in strings": {
hashmap: map[string]interface{}{
"key": "$1$$2$$$3$$$$4",
"$1$$2$$$3$$$$4": "value",
},
ast: &AST{
root: &Dict{
value: []Node{
&Key{name: "key", value: &StrVal{value: "$1$$2$$$3$$$$4"}},
&Key{name: "$1$$2$$$3$$$$4", value: &StrVal{value: "value"}},
},
},
},
},
"integer as key": {
hashmap: map[string]interface{}{
"1": []string{"/var/log/log1", "/var/log/log2"},
Expand Down
44 changes: 44 additions & 0 deletions internal/pkg/agent/transpiler/vars_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ func TestVars_Replace(t *testing.T) {
"other": map[string]interface{}{
"data": "info",
},
"special": map[string]interface{}{
"key1": "$1$$2",
"key2": "1$2$$",
"key3": "${abcd}",
"key4": "$${abcd}",
"key5": "${",
"key6": "$${",
},
})
tests := []struct {
Input string
Expand Down Expand Up @@ -209,6 +217,42 @@ func TestVars_Replace(t *testing.T) {
false,
false,
},
{
`${special.key1}`,
NewStrVal("$1$$2"),
false,
false,
},
{
`${special.key2}`,
NewStrVal("1$2$$"),
false,
false,
},
{
`${special.key3}`,
NewStrVal("${abcd}"),
false,
false,
},
{
`${special.key4}`,
NewStrVal("$${abcd}"),
false,
false,
},
{
`${special.key5}`,
NewStrVal("${"),
false,
false,
},
{
`${special.key6}`,
NewStrVal("$${"),
false,
false,
},
}
for _, test := range tests {
t.Run(test.Input, func(t *testing.T) {
Expand Down
15 changes: 15 additions & 0 deletions internal/pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,18 @@ func dumpToYAML(t *testing.T, out string, in interface{}) {
err = os.WriteFile(out, b, 0600)
require.NoError(t, err)
}

func TestDollarSignsInInputs(t *testing.T) {
in := map[string]interface{}{
"inputs": []interface{}{
map[string]interface{}{
"type": "logfile",
"what": "$$$$",
},
},
}
c := MustNewConfigFrom(in)
out, err := c.ToMapStr()
assert.NoError(t, err)
assert.Equal(t, in, out)
}
Loading