From 7c419d5349837b50c9d87be88fc438f8c4e475b9 Mon Sep 17 00:00:00 2001 From: Mark Sargent <99003+sarge@users.noreply.github.com> Date: Fri, 10 Jan 2020 05:40:16 +1300 Subject: caddyfile: Preprocess env vars in {$THIS} format (#2963) * transform a caddyfile with environment variables * support adapt time and runtime variables in the caddyfile * caddyfile: Pre-process environment variables before parsing Co-authored-by: Matt Holt --- caddyconfig/caddyfile/parse_test.go | 155 +++++++++++++++++------------------- 1 file changed, 74 insertions(+), 81 deletions(-) (limited to 'caddyconfig/caddyfile/parse_test.go') diff --git a/caddyconfig/caddyfile/parse_test.go b/caddyconfig/caddyfile/parse_test.go index 0168cfa..640d0cd 100755 --- a/caddyconfig/caddyfile/parse_test.go +++ b/caddyconfig/caddyfile/parse_test.go @@ -15,6 +15,7 @@ package caddyfile import ( + "bytes" "io/ioutil" "os" "path/filepath" @@ -473,89 +474,81 @@ func TestParseAll(t *testing.T) { } func TestEnvironmentReplacement(t *testing.T) { - os.Setenv("PORT", "8080") - os.Setenv("ADDRESS", "servername.com") os.Setenv("FOOBAR", "foobar") - os.Setenv("PARTIAL_DIR", "r1") - // basic test; unix-style env vars - p := testParser(`{$ADDRESS}`) - blocks, _ := p.parseAll() - if actual, expected := blocks[0].Keys[0], "servername.com"; expected != actual { - t.Errorf("Expected key to be '%s' but was '%s'", expected, actual) - } - - // basic test; unix-style env vars - p = testParser(`di{$PARTIAL_DIR}`) - blocks, _ = p.parseAll() - if actual, expected := blocks[0].Keys[0], "dir1"; expected != actual { - t.Errorf("Expected key to be '%s' but was '%s'", expected, actual) - } - - // multiple vars per token - p = testParser(`{$ADDRESS}:{$PORT}`) - blocks, _ = p.parseAll() - if actual, expected := blocks[0].Keys[0], "servername.com:8080"; expected != actual { - t.Errorf("Expected key to be '%s' but was '%s'", expected, actual) - } - - // windows-style var and unix style in same token - p = testParser(`{%ADDRESS%}:{$PORT}`) - blocks, _ = p.parseAll() - if actual, expected := blocks[0].Keys[0], "servername.com:8080"; expected != actual { - t.Errorf("Expected key to be '%s' but was '%s'", expected, actual) - } - - // reverse order - p = testParser(`{$ADDRESS}:{%PORT%}`) - blocks, _ = p.parseAll() - if actual, expected := blocks[0].Keys[0], "servername.com:8080"; expected != actual { - t.Errorf("Expected key to be '%s' but was '%s'", expected, actual) - } - - // env var in server block body as argument - p = testParser(":{%PORT%}\ndir1 {$FOOBAR}") - blocks, _ = p.parseAll() - if actual, expected := blocks[0].Keys[0], ":8080"; expected != actual { - t.Errorf("Expected key to be '%s' but was '%s'", expected, actual) - } - if actual, expected := blocks[0].Segments[0][1].Text, "foobar"; expected != actual { - t.Errorf("Expected argument to be '%s' but was '%s'", expected, actual) - } - - // combined windows env vars in argument - p = testParser(":{%PORT%}\ndir1 {%ADDRESS%}/{%FOOBAR%}") - blocks, _ = p.parseAll() - if actual, expected := blocks[0].Segments[0][1].Text, "servername.com/foobar"; expected != actual { - t.Errorf("Expected argument to be '%s' but was '%s'", expected, actual) - } - - // malformed env var (windows) - p = testParser(":1234\ndir1 {%ADDRESS}") - blocks, _ = p.parseAll() - if actual, expected := blocks[0].Segments[0][1].Text, "{%ADDRESS}"; expected != actual { - t.Errorf("Expected host to be '%s' but was '%s'", expected, actual) - } - - // malformed (non-existent) env var (unix) - p = testParser(`:{$PORT$}`) - blocks, _ = p.parseAll() - if actual, expected := blocks[0].Keys[0], ":"; expected != actual { - t.Errorf("Expected key to be '%s' but was '%s'", expected, actual) - } - - // in quoted field - p = testParser(":1234\ndir1 \"Test {$FOOBAR} test\"") - blocks, _ = p.parseAll() - if actual, expected := blocks[0].Segments[0][1].Text, "Test foobar test"; expected != actual { - t.Errorf("Expected argument to be '%s' but was '%s'", expected, actual) - } - - // after end token - p = testParser(":1234\nanswer \"{{ .Name }} {$FOOBAR}\"") - blocks, _ = p.parseAll() - if actual, expected := blocks[0].Segments[0][1].Text, "{{ .Name }} foobar"; expected != actual { - t.Errorf("Expected argument to be '%s' but was '%s'", expected, actual) + for i, test := range []struct { + input string + expect string + }{ + { + input: "", + expect: "", + }, + { + input: "foo", + expect: "foo", + }, + { + input: "{$NOT_SET}", + expect: "", + }, + { + input: "foo{$NOT_SET}bar", + expect: "foobar", + }, + { + input: "{$FOOBAR}", + expect: "foobar", + }, + { + input: "foo {$FOOBAR} bar", + expect: "foo foobar bar", + }, + { + input: "foo{$FOOBAR}bar", + expect: "foofoobarbar", + }, + { + input: "foo\n{$FOOBAR}\nbar", + expect: "foo\nfoobar\nbar", + }, + { + input: "{$FOOBAR} {$FOOBAR}", + expect: "foobar foobar", + }, + { + input: "{$FOOBAR}{$FOOBAR}", + expect: "foobarfoobar", + }, + { + input: "{$FOOBAR", + expect: "{$FOOBAR", + }, + { + input: "{$LONGER_NAME $FOOBAR}", + expect: "", + }, + { + input: "{$}", + expect: "{$}", + }, + { + input: "{$$}", + expect: "", + }, + { + input: "{$", + expect: "{$", + }, + { + input: "}{$", + expect: "}{$", + }, + } { + actual := replaceEnvVars([]byte(test.input)) + if !bytes.Equal(actual, []byte(test.expect)) { + t.Errorf("Test %d: Expected: '%s' but got '%s'", i, test.expect, actual) + } } } -- cgit v1.2.3