Skip to content

Commit 1ab6236

Browse files
author
DerekStrickland
committed
consul-template: replace config rather than append
1 parent ddbbda6 commit 1ab6236

10 files changed

+124
-103
lines changed

.changelog/12071.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
```release-note:bug
2+
template: Fixes a bug in config parsing that resulted in the default `function_denylist`
3+
always being appended to whatever the user specified.
4+
```

client/config/config.go

+2-68
Original file line numberDiff line numberDiff line change
@@ -379,80 +379,14 @@ func (c *ClientTemplateConfig) Copy() *ClientTemplateConfig {
379379
return nc
380380
}
381381

382-
// Merge merges the values of two ClientTemplateConfigs. If first copies the receiver
383-
// instance, and then overrides those values with the instance to merge with.
384-
func (c *ClientTemplateConfig) Merge(b *ClientTemplateConfig) *ClientTemplateConfig {
385-
if c == nil {
386-
return b
387-
}
388-
389-
result := *c
390-
391-
if b == nil {
392-
return &result
393-
}
394-
395-
if b.BlockQueryWaitTime != nil {
396-
result.BlockQueryWaitTime = b.BlockQueryWaitTime
397-
}
398-
if b.BlockQueryWaitTimeHCL != "" {
399-
result.BlockQueryWaitTimeHCL = b.BlockQueryWaitTimeHCL
400-
}
401-
402-
if b.ConsulRetry != nil {
403-
result.ConsulRetry = result.ConsulRetry.Merge(b.ConsulRetry)
404-
}
405-
406-
result.DisableSandbox = b.DisableSandbox
407-
408-
// Maintain backward compatibility for older clients
409-
if len(b.FunctionBlacklist) > 0 {
410-
for _, fn := range b.FunctionBlacklist {
411-
if !helper.SliceStringContains(result.FunctionBlacklist, fn) {
412-
result.FunctionBlacklist = append(result.FunctionBlacklist, fn)
413-
}
414-
}
415-
}
416-
417-
if len(b.FunctionDenylist) > 0 {
418-
for _, fn := range b.FunctionDenylist {
419-
if !helper.SliceStringContains(result.FunctionDenylist, fn) {
420-
result.FunctionDenylist = append(result.FunctionDenylist, fn)
421-
}
422-
}
423-
}
424-
425-
if b.MaxStale != nil {
426-
result.MaxStale = b.MaxStale
427-
}
428-
429-
if b.MaxStaleHCL != "" {
430-
result.MaxStaleHCL = b.MaxStaleHCL
431-
}
432-
433-
if b.Wait != nil {
434-
result.Wait = result.Wait.Merge(b.Wait)
435-
}
436-
437-
if b.WaitBounds != nil {
438-
result.WaitBounds = result.WaitBounds.Merge(b.WaitBounds)
439-
}
440-
441-
if b.VaultRetry != nil {
442-
result.VaultRetry = result.VaultRetry.Merge(b.VaultRetry)
443-
}
444-
445-
return &result
446-
}
447-
448382
func (c *ClientTemplateConfig) IsEmpty() bool {
449383
if c == nil {
450384
return true
451385
}
452386

453387
return !c.DisableSandbox &&
454-
len(c.FunctionDenylist) == 0 &&
455-
len(c.FunctionBlacklist) == 0 &&
388+
c.FunctionDenylist == nil &&
389+
c.FunctionBlacklist == nil &&
456390
c.BlockQueryWaitTime == nil &&
457391
c.BlockQueryWaitTimeHCL == "" &&
458392
c.MaxStale == nil &&

command/agent/config.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -1697,11 +1697,8 @@ func (a *ClientConfig) Merge(b *ClientConfig) *ClientConfig {
16971697
result.DisableRemoteExec = b.DisableRemoteExec
16981698
}
16991699

1700-
if result.TemplateConfig == nil && b.TemplateConfig != nil {
1701-
templateConfig := *b.TemplateConfig
1702-
result.TemplateConfig = &templateConfig
1703-
} else if b.TemplateConfig != nil {
1704-
result.TemplateConfig = result.TemplateConfig.Merge(b.TemplateConfig)
1700+
if b.TemplateConfig != nil {
1701+
result.TemplateConfig = b.TemplateConfig
17051702
}
17061703

17071704
// Add the servers

command/agent/config_test.go

+70-30
Original file line numberDiff line numberDiff line change
@@ -1413,39 +1413,79 @@ func TestConfig_LoadConsulTemplateConfig(t *testing.T) {
14131413
require.Equal(t, 20*time.Second, *templateConfig.VaultRetry.MaxBackoff)
14141414
}
14151415

1416-
func TestConfig_LoadConsulTemplateBasic(t *testing.T) {
1417-
defaultConfig := DefaultConfig()
1418-
1419-
// hcl
1420-
agentConfig, err := LoadConfig("test-resources/client_with_basic_template.hcl")
1421-
require.NoError(t, err)
1422-
require.NotNil(t, agentConfig.Client.TemplateConfig)
1423-
1424-
agentConfig = defaultConfig.Merge(agentConfig)
1425-
1426-
clientAgent := Agent{config: agentConfig}
1427-
clientConfig, err := clientAgent.clientConfig()
1428-
require.NoError(t, err)
1429-
1430-
templateConfig := clientConfig.TemplateConfig
1431-
require.NotNil(t, templateConfig)
1432-
require.True(t, templateConfig.DisableSandbox)
1433-
require.Len(t, templateConfig.FunctionDenylist, 1)
1434-
1435-
// json
1436-
agentConfig, err = LoadConfig("test-resources/client_with_basic_template.json")
1437-
require.NoError(t, err)
1416+
func TestConfig_LoadConsulTemplate_FunctionDenylist(t *testing.T) {
1417+
cases := []struct {
1418+
File string
1419+
Expected *client.ClientTemplateConfig
1420+
}{
1421+
{
1422+
"test-resources/minimal_client.hcl",
1423+
nil,
1424+
},
1425+
{
1426+
"test-resources/client_with_basic_template.json",
1427+
&client.ClientTemplateConfig{
1428+
DisableSandbox: true,
1429+
FunctionDenylist: []string{},
1430+
},
1431+
},
1432+
{
1433+
"test-resources/client_with_basic_template.hcl",
1434+
&client.ClientTemplateConfig{
1435+
DisableSandbox: true,
1436+
FunctionDenylist: []string{},
1437+
},
1438+
},
1439+
{
1440+
"test-resources/client_with_function_denylist.hcl",
1441+
&client.ClientTemplateConfig{
1442+
DisableSandbox: false,
1443+
FunctionDenylist: []string{"foo"},
1444+
},
1445+
},
1446+
{
1447+
"test-resources/client_with_function_denylist_empty.hcl",
1448+
&client.ClientTemplateConfig{
1449+
DisableSandbox: false,
1450+
FunctionDenylist: []string{},
1451+
},
1452+
},
1453+
{
1454+
"test-resources/client_with_function_denylist_empty_string.hcl",
1455+
&client.ClientTemplateConfig{
1456+
DisableSandbox: true,
1457+
FunctionDenylist: []string{""},
1458+
},
1459+
},
1460+
{
1461+
"test-resources/client_with_function_denylist_empty_string.json",
1462+
&client.ClientTemplateConfig{
1463+
DisableSandbox: true,
1464+
FunctionDenylist: []string{""},
1465+
},
1466+
},
1467+
{
1468+
"test-resources/client_with_function_denylist_nil.hcl",
1469+
&client.ClientTemplateConfig{
1470+
DisableSandbox: true,
1471+
},
1472+
},
1473+
{
1474+
"test-resources/client_with_empty_template.hcl",
1475+
nil,
1476+
},
1477+
}
14381478

1439-
agentConfig = defaultConfig.Merge(agentConfig)
1479+
for _, tc := range cases {
1480+
t.Run(tc.File, func(t *testing.T) {
1481+
agentConfig, err := LoadConfig(tc.File)
14401482

1441-
clientAgent = Agent{config: agentConfig}
1442-
clientConfig, err = clientAgent.clientConfig()
1443-
require.NoError(t, err)
1483+
require.NoError(t, err)
14441484

1445-
templateConfig = clientConfig.TemplateConfig
1446-
require.NotNil(t, templateConfig)
1447-
require.True(t, templateConfig.DisableSandbox)
1448-
require.Len(t, templateConfig.FunctionDenylist, 1)
1485+
templateConfig := agentConfig.Client.TemplateConfig
1486+
require.Equal(t, tc.Expected, templateConfig)
1487+
})
1488+
}
14491489
}
14501490

14511491
func TestParseMultipleIPTemplates(t *testing.T) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
client {
2+
enabled = true
3+
4+
template {
5+
}
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
client {
2+
enabled = true
3+
4+
template {
5+
function_denylist = ["foo"]
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
client {
2+
enabled = true
3+
4+
template {
5+
function_denylist = []
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
client {
2+
enabled = true
3+
4+
template {
5+
disable_file_sandbox = true
6+
function_denylist = [""]
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"client": {
3+
"enabled": true,
4+
"template": {
5+
"disable_file_sandbox": true,
6+
"function_denylist": [
7+
""
8+
]
9+
}
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
client {
2+
enabled = true
3+
4+
template {
5+
disable_file_sandbox = true
6+
}
7+
}

0 commit comments

Comments
 (0)