From a4824c81c333dbb01ce4d0e48e6b35bf7986f710 Mon Sep 17 00:00:00 2001 From: hashicorp-peter Date: Thu, 28 Apr 2022 23:52:35 +0100 Subject: [PATCH 1/9] Added check to only parse templates that are in the expected template format, non-template values are returned unchanged --- changelog/15210.txt | 4 ++ internalshared/configutil/listener.go | 6 +++ internalshared/configutil/listener_test.go | 49 ++++++++++++++++++++++ sdk/go.sum | 1 - 4 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 changelog/15210.txt create mode 100644 internalshared/configutil/listener_test.go diff --git a/changelog/15210.txt b/changelog/15210.txt new file mode 100644 index 000000000000..0297a8fad94c --- /dev/null +++ b/changelog/15210.txt @@ -0,0 +1,4 @@ +```release-note:bug +core/config: Handle case where config contains a non-template format value, only parse when it follows the format: +{{ ... }} +``` \ No newline at end of file diff --git a/internalshared/configutil/listener.go b/internalshared/configutil/listener.go index 0df97bdf03e0..2aa727e3eeb9 100644 --- a/internalshared/configutil/listener.go +++ b/internalshared/configutil/listener.go @@ -410,7 +410,13 @@ func ParseListeners(result *SharedConfig, list *ast.ObjectList) error { // ParseSingleIPTemplate is used as a helper function to parse out a single IP // address from a config parameter. +// If the input doesn't appear to be 'template' format, +// it will return the specified input. func ParseSingleIPTemplate(ipTmpl string) (string, error) { + if !(strings.HasPrefix(ipTmpl, "{{") && strings.HasSuffix(ipTmpl, "}}")) { + return ipTmpl, nil + } + out, err := template.Parse(ipTmpl) if err != nil { return "", fmt.Errorf("unable to parse address template %q: %v", ipTmpl, err) diff --git a/internalshared/configutil/listener_test.go b/internalshared/configutil/listener_test.go new file mode 100644 index 000000000000..920910f5a9be --- /dev/null +++ b/internalshared/configutil/listener_test.go @@ -0,0 +1,49 @@ +package configutil + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestParseSingleIPTemplate(t *testing.T) { + type args struct { + ipTmpl string + } + tests := []struct { + name string + args args + want string + wantErr assert.ErrorAssertionFunc + }{ + { + name: "test non-template addr", + args: args{"vaultproject.io"}, + want: "vaultproject.io", + wantErr: assert.NoError, + }, + { + name: "test invalid template func", + args: args{"{{FooBar}}"}, + want: "", + wantErr: assert.Error, + }, + { + name: "test partial template", + args: args{"{{FooBar"}, + want: "{{FooBar", + wantErr: assert.NoError, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := ParseSingleIPTemplate(tt.args.ipTmpl) + if !tt.wantErr(t, err, fmt.Sprintf("ParseSingleIPTemplate(%v)", tt.args.ipTmpl)) { + return + } + + assert.Equalf(t, tt.want, got, "ParseSingleIPTemplate(%v)", tt.args.ipTmpl) + }) + } +} diff --git a/sdk/go.sum b/sdk/go.sum index 957410de74d3..59bf6d3dd71b 100644 --- a/sdk/go.sum +++ b/sdk/go.sum @@ -107,7 +107,6 @@ github.com/hashicorp/go-secure-stdlib/base62 v0.1.1 h1:6KMBnfEv0/kLAz0O76sliN5mX github.com/hashicorp/go-secure-stdlib/base62 v0.1.1/go.mod h1:EdWO6czbmthiwZ3/PUsDV+UD1D5IRU4ActiaWGwt0Yw= github.com/hashicorp/go-secure-stdlib/mlock v0.1.1 h1:cCRo8gK7oq6A2L6LICkUZ+/a5rLiRXFMf1Qd4xSwxTc= github.com/hashicorp/go-secure-stdlib/mlock v0.1.1/go.mod h1:zq93CJChV6L9QTfGKtfBxKqD7BqqXx5O04A/ns2p5+I= -github.com/hashicorp/go-secure-stdlib/parseutil v0.1.1 h1:78ki3QBevHwYrVxnyVeaEz+7WtifHhauYF23es/0KlI= github.com/hashicorp/go-secure-stdlib/parseutil v0.1.1/go.mod h1:QmrqtbKuxxSWTN3ETMPuB+VtEiBJ/A9XhoYGv8E1uD8= github.com/hashicorp/go-secure-stdlib/parseutil v0.1.4 h1:hrIH/qrOTHfG9a1Jz6Z2jQf7Xe77AaD464W1fCFLwPQ= github.com/hashicorp/go-secure-stdlib/parseutil v0.1.4/go.mod h1:QmrqtbKuxxSWTN3ETMPuB+VtEiBJ/A9XhoYGv8E1uD8= From 044943a8efd97b3ca98047711253fd75d839c35c Mon Sep 17 00:00:00 2001 From: hashicorp-peter Date: Thu, 28 Apr 2022 23:55:39 +0100 Subject: [PATCH 2/9] Matched test input to format in GH issue --- changelog/15210.txt | 3 +-- internalshared/configutil/listener_test.go | 6 +++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/changelog/15210.txt b/changelog/15210.txt index 0297a8fad94c..50b07ebda767 100644 --- a/changelog/15210.txt +++ b/changelog/15210.txt @@ -1,4 +1,3 @@ ```release-note:bug -core/config: Handle case where config contains a non-template format value, only parse when it follows the format: -{{ ... }} +core/config: Handle case where config contains a non-template format value, only parse when it follows the format: {{ ... }} ``` \ No newline at end of file diff --git a/internalshared/configutil/listener_test.go b/internalshared/configutil/listener_test.go index 920910f5a9be..26bd3ebf7fc5 100644 --- a/internalshared/configutil/listener_test.go +++ b/internalshared/configutil/listener_test.go @@ -18,9 +18,9 @@ func TestParseSingleIPTemplate(t *testing.T) { wantErr assert.ErrorAssertionFunc }{ { - name: "test non-template addr", - args: args{"vaultproject.io"}, - want: "vaultproject.io", + name: "test https addr", + args: args{"https://vaultproject.io:8200"}, + want: "https://vaultproject.io:8200", wantErr: assert.NoError, }, { From e3d2eaf96467668324755af9005d7e77ab887ba8 Mon Sep 17 00:00:00 2001 From: hashicorp-peter Date: Fri, 29 Apr 2022 00:06:35 +0100 Subject: [PATCH 3/9] Moved changelog file to match PR number rather than issue number --- changelog/15210.txt => 15220.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename changelog/15210.txt => 15220.txt (100%) diff --git a/changelog/15210.txt b/15220.txt similarity index 100% rename from changelog/15210.txt rename to 15220.txt From 481d5ac46efc1bc4bc497bef67da6958b821aff5 Mon Sep 17 00:00:00 2001 From: hashicorp-peter Date: Fri, 29 Apr 2022 00:09:38 +0100 Subject: [PATCH 4/9] moved changelog file for PR to correct location --- 15220.txt => changelog/15220.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename 15220.txt => changelog/15220.txt (100%) diff --git a/15220.txt b/changelog/15220.txt similarity index 100% rename from 15220.txt rename to changelog/15220.txt From b2f3db2469f6c3ba4bc5dbc36c78fbac60345b38 Mon Sep 17 00:00:00 2001 From: hashicorp-peter Date: Fri, 29 Apr 2022 11:11:52 +0100 Subject: [PATCH 5/9] Replaced template check logic with regex that allows for non-template data to be alongside --- internalshared/configutil/listener.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/internalshared/configutil/listener.go b/internalshared/configutil/listener.go index 2aa727e3eeb9..16a0a701511c 100644 --- a/internalshared/configutil/listener.go +++ b/internalshared/configutil/listener.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "net/textproto" + "regexp" "strings" "time" @@ -410,10 +411,13 @@ func ParseListeners(result *SharedConfig, list *ast.ObjectList) error { // ParseSingleIPTemplate is used as a helper function to parse out a single IP // address from a config parameter. -// If the input doesn't appear to be 'template' format, -// it will return the specified input. +// If the input doesn't appear to contain the 'template' format, +// it will return the specified input unchanged. func ParseSingleIPTemplate(ipTmpl string) (string, error) { - if !(strings.HasPrefix(ipTmpl, "{{") && strings.HasSuffix(ipTmpl, "}}")) { + m, err := regexp.MatchString("(.*?)({{.*?}})(.*)", ipTmpl) + if err != nil { + return "", fmt.Errorf("unable to verify existence of template syntax in input %q: %v", ipTmpl, err) + } else if !m { return ipTmpl, nil } From 4d05eb9a2b445746ab98c1ad7c99cd3f3f4c68cd Mon Sep 17 00:00:00 2001 From: peteski22 Date: Fri, 29 Apr 2022 11:49:00 +0100 Subject: [PATCH 6/9] Renamed changelog txt file for new PR --- changelog/{15220.txt => 15224.txt} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename changelog/{15220.txt => 15224.txt} (100%) diff --git a/changelog/15220.txt b/changelog/15224.txt similarity index 100% rename from changelog/15220.txt rename to changelog/15224.txt From 8dc7e3be9e3de616330f7e7e0a6c91bedb24a3fe Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Fri, 29 Apr 2022 14:09:28 +0100 Subject: [PATCH 7/9] Update internalshared/configutil/listener.go More succinct regex Co-authored-by: Nick Cabatoff --- internalshared/configutil/listener.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internalshared/configutil/listener.go b/internalshared/configutil/listener.go index 16a0a701511c..0892589ac563 100644 --- a/internalshared/configutil/listener.go +++ b/internalshared/configutil/listener.go @@ -414,7 +414,7 @@ func ParseListeners(result *SharedConfig, list *ast.ObjectList) error { // If the input doesn't appear to contain the 'template' format, // it will return the specified input unchanged. func ParseSingleIPTemplate(ipTmpl string) (string, error) { - m, err := regexp.MatchString("(.*?)({{.*?}})(.*)", ipTmpl) + m, err := regexp.MatchString("{{.*?}}", ipTmpl) if err != nil { return "", fmt.Errorf("unable to verify existence of template syntax in input %q: %v", ipTmpl, err) } else if !m { From c3fe0cd05230fa611876e1716be5fab546b60b5b Mon Sep 17 00:00:00 2001 From: peteski22 Date: Fri, 29 Apr 2022 14:22:48 +0100 Subject: [PATCH 8/9] Updated to ensure regex compilation, and simplify test code --- internalshared/configutil/listener.go | 6 ++---- internalshared/configutil/listener_test.go | 14 +++++++------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/internalshared/configutil/listener.go b/internalshared/configutil/listener.go index 0892589ac563..a9e1d7b5cbde 100644 --- a/internalshared/configutil/listener.go +++ b/internalshared/configutil/listener.go @@ -414,10 +414,8 @@ func ParseListeners(result *SharedConfig, list *ast.ObjectList) error { // If the input doesn't appear to contain the 'template' format, // it will return the specified input unchanged. func ParseSingleIPTemplate(ipTmpl string) (string, error) { - m, err := regexp.MatchString("{{.*?}}", ipTmpl) - if err != nil { - return "", fmt.Errorf("unable to verify existence of template syntax in input %q: %v", ipTmpl, err) - } else if !m { + r := regexp.MustCompile("{{.*?}}") + if !r.MatchString(ipTmpl) { return ipTmpl, nil } diff --git a/internalshared/configutil/listener_test.go b/internalshared/configutil/listener_test.go index 26bd3ebf7fc5..803086e483e2 100644 --- a/internalshared/configutil/listener_test.go +++ b/internalshared/configutil/listener_test.go @@ -13,37 +13,37 @@ func TestParseSingleIPTemplate(t *testing.T) { } tests := []struct { name string - args args + arg string want string wantErr assert.ErrorAssertionFunc }{ { name: "test https addr", - args: args{"https://vaultproject.io:8200"}, + arg: "https://vaultproject.io:8200", want: "https://vaultproject.io:8200", wantErr: assert.NoError, }, { name: "test invalid template func", - args: args{"{{FooBar}}"}, + arg: "{{FooBar}}", want: "", wantErr: assert.Error, }, { name: "test partial template", - args: args{"{{FooBar"}, + arg: "{{FooBar", want: "{{FooBar", wantErr: assert.NoError, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got, err := ParseSingleIPTemplate(tt.args.ipTmpl) - if !tt.wantErr(t, err, fmt.Sprintf("ParseSingleIPTemplate(%v)", tt.args.ipTmpl)) { + got, err := ParseSingleIPTemplate(tt.arg) + if !tt.wantErr(t, err, fmt.Sprintf("ParseSingleIPTemplate(%v)", tt.arg)) { return } - assert.Equalf(t, tt.want, got, "ParseSingleIPTemplate(%v)", tt.args.ipTmpl) + assert.Equalf(t, tt.want, got, "ParseSingleIPTemplate(%v)", tt.arg) }) } } From 46a663ccb1e7664422392e072564f7bb9be36ec0 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Fri, 29 Apr 2022 14:35:29 +0100 Subject: [PATCH 9/9] Update changelog/15224.txt Co-authored-by: Nick Cabatoff --- changelog/15224.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog/15224.txt b/changelog/15224.txt index 50b07ebda767..c25ccf630609 100644 --- a/changelog/15224.txt +++ b/changelog/15224.txt @@ -1,3 +1,3 @@ ```release-note:bug -core/config: Handle case where config contains a non-template format value, only parse when it follows the format: {{ ... }} +core/config: Only ask the system about network interfaces when address configs contain a template having the format: {{ ... }} ``` \ No newline at end of file