Skip to content

Commit 8ee50b0

Browse files
committed
Update regex to handle quotes and variables
1 parent 44412c5 commit 8ee50b0

File tree

2 files changed

+118
-11
lines changed

2 files changed

+118
-11
lines changed

pkg/natsreloader/natsreloader.go

+20-4
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ func getIncludePaths(configFile string, checked map[string]interface{}) ([]strin
426426
checked[configFile] = nil
427427

428428
parentDirectory := filepath.Dir(configFile)
429-
includeRegex := regexp.MustCompile(`(?m)^\s*include\s+['"]?([^'"\n]*)`)
429+
includeRegex := regexp.MustCompile(`(?m)^\s*include\s+(['"]?[^'";\n]*)`)
430430

431431
content, err := os.ReadFile(configFile)
432432
if err != nil {
@@ -435,8 +435,16 @@ func getIncludePaths(configFile string, checked map[string]interface{}) ([]strin
435435

436436
includeMatches := includeRegex.FindAllStringSubmatch(string(content), -1)
437437
for _, match := range includeMatches {
438+
matchStr := match[1]
439+
if strings.HasPrefix(matchStr, "$") {
440+
continue
441+
}
442+
443+
matchStr = strings.TrimPrefix(matchStr, "'")
444+
matchStr = strings.TrimPrefix(matchStr, "\"")
445+
438446
// Include filepaths in NATS config are always relative
439-
fullyQualifiedPath := filepath.Join(parentDirectory, match[1])
447+
fullyQualifiedPath := filepath.Join(parentDirectory, matchStr)
440448
fullyQualifiedPath = filepath.Clean(fullyQualifiedPath)
441449

442450
if _, err := os.Stat(fullyQualifiedPath); os.IsNotExist(err) {
@@ -458,7 +466,7 @@ func getIncludePaths(configFile string, checked map[string]interface{}) ([]strin
458466

459467
func getCertPaths(configPaths []string) ([]string, error) {
460468
certPaths := []string{}
461-
certRegex := regexp.MustCompile(`(?m)^\s*(cert_file|key_file|ca_file)\s*:\s*"?([^"\n]*)"?`)
469+
certRegex := regexp.MustCompile(`(?m)^\s*(cert_file|key_file|ca_file)\s*:\s*(['"]?[^'";\n]*)"?`)
462470

463471
for _, configPath := range configPaths {
464472
content, err := os.ReadFile(configPath)
@@ -468,7 +476,15 @@ func getCertPaths(configPaths []string) ([]string, error) {
468476

469477
certMatches := certRegex.FindAllStringSubmatch(string(content), -1)
470478
for _, match := range certMatches {
471-
fullyQualifiedPath, err := filepath.Abs(match[2])
479+
matchStr := match[2]
480+
if strings.HasPrefix(matchStr, "$") {
481+
continue
482+
}
483+
484+
matchStr = strings.TrimPrefix(matchStr, "'")
485+
matchStr = strings.TrimPrefix(matchStr, "\"")
486+
487+
fullyQualifiedPath, err := filepath.Abs(matchStr)
472488
if err != nil {
473489
return nil, err
474490
}

pkg/natsreloader/natsreloadertest/natsreloader_test.go pkg/natsreloader/natsreloader_test.go

+98-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// See the License for the specific language governing permissions and
1212
// limitations under the License.
1313

14-
package natsreloadertest
14+
package natsreloader
1515

1616
import (
1717
"context"
@@ -27,8 +27,6 @@ import (
2727
"syscall"
2828
"testing"
2929
"time"
30-
31-
"github.com/nats-io/nack/pkg/natsreloader"
3230
)
3331

3432
const (
@@ -62,6 +60,31 @@ tls: {
6260
cert_file: "./test.pem"
6361
key_file: "./testkey.pem"
6462
}
63+
`
64+
includeTest_0 = `
65+
include nats_0.conf
66+
include nats_1.conf; // semicolon terminated
67+
include "nats_2.conf" // quoted
68+
include "nats_3.conf"; // quoted and semicolon terminated
69+
include 'nats_4.conf' // are single quotes valid in nats conf? if so also need this
70+
include 'nats_5.conf'; // are single quotes valid in nats conf? if so also need this
71+
include $NATS; // ignore, this is a variable. let's not worry about variable interpolation
72+
include "$NATS_6.conf" // don't ignore, this is a string not a variable
73+
include includeTest_1.conf
74+
`
75+
includeTest_1 = `
76+
tls: {
77+
cert_file: ./nats_0.pem
78+
key_file: 'nats_0.key'
79+
}
80+
tls: {
81+
cert_file: "./nats_1.pem"
82+
key_file: $test
83+
}
84+
tls: {
85+
cert_file: "$nats_2.pem";
86+
key_file: 'nats_1.key';
87+
}
6588
`
6689
)
6790

@@ -85,7 +108,7 @@ func TestReloader(t *testing.T) {
85108
defer os.Remove(pidfile.Name())
86109

87110
// Create tempfile with contents, then update it
88-
nconfig := &natsreloader.Config{
111+
nconfig := &Config{
89112
PidFile: pidfile.Name(),
90113
WatchedFiles: []string{},
91114
Signal: syscall.SIGHUP,
@@ -106,7 +129,7 @@ func TestReloader(t *testing.T) {
106129
nconfig.WatchedFiles = append(nconfig.WatchedFiles, configFile.Name())
107130
}
108131

109-
r, err := natsreloader.NewReloader(nconfig)
132+
r, err := NewReloader(nconfig)
110133
if err != nil {
111134
t.Fatal(err)
112135
}
@@ -173,6 +196,74 @@ func TestReloader(t *testing.T) {
173196
}
174197
}
175198

199+
func TestInclude(t *testing.T) {
200+
directory, err := os.Getwd()
201+
if err != nil {
202+
t.Fatal(err)
203+
}
204+
205+
dummyFiles := []string{
206+
"nats_0.conf",
207+
"nats_1.conf",
208+
"nats_2.conf",
209+
"nats_3.conf",
210+
"nats_4.conf",
211+
"nats_5.conf",
212+
"$NATS_6.conf",
213+
"nats_0.pem",
214+
"nats_1.pem",
215+
"$nats_2.pem",
216+
"nats_0.key",
217+
"nats_1.key",
218+
}
219+
220+
for _, f := range dummyFiles {
221+
p := filepath.Join(directory, f)
222+
err = writeFile("", p)
223+
defer os.Remove(p)
224+
if err != nil {
225+
t.Fatal(err)
226+
}
227+
}
228+
229+
includeTestConf_0 := filepath.Join(directory, "includeTest_0.conf")
230+
err = writeFile(includeTest_0, includeTestConf_0)
231+
defer os.Remove(includeTestConf_0)
232+
if err != nil {
233+
t.Fatal(err)
234+
}
235+
236+
includeTestConf_1 := filepath.Join(directory, "includeTest_1.conf")
237+
err = writeFile(includeTest_1, includeTestConf_1)
238+
defer os.Remove(includeTestConf_1)
239+
if err != nil {
240+
t.Fatal(err)
241+
}
242+
243+
includes, err := getServerFiles("includeTest_0.conf")
244+
if err != nil {
245+
t.Fatal(err)
246+
}
247+
248+
includePaths := make([]string, 0)
249+
for _, p := range includes {
250+
includePaths = append(includePaths, filepath.Base(p))
251+
}
252+
253+
dummyFiles = append(dummyFiles, "includeTest_0.conf")
254+
dummyFiles = append(dummyFiles, "includeTest_1.conf")
255+
256+
sort.Strings(dummyFiles)
257+
sort.Strings(includePaths)
258+
259+
for i, p := range dummyFiles {
260+
if p != includePaths[i] {
261+
t.Fatal("Expected include paths do not match")
262+
}
263+
}
264+
265+
}
266+
176267
func TestFileFinder(t *testing.T) {
177268
directory, err := os.Getwd()
178269
if err != nil {
@@ -218,13 +309,13 @@ func TestFileFinder(t *testing.T) {
218309
}
219310
defer os.Remove(pidFile)
220311

221-
nconfig := &natsreloader.Config{
312+
nconfig := &Config{
222313
PidFile: pidFile,
223314
WatchedFiles: []string{filepath.Join(directory, "testConfig_0.conf")},
224315
Signal: syscall.SIGHUP,
225316
}
226317

227-
r, err := natsreloader.NewReloader(nconfig)
318+
r, err := NewReloader(nconfig)
228319
if err != nil {
229320
t.Fatal(err)
230321
}

0 commit comments

Comments
 (0)