From 43bb76480821e6be4d85fe19bbb63cd0b6c06906 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Fri, 29 Apr 2022 14:57:17 +0100 Subject: [PATCH] Do sockaddr template parsing only when needed (#15224) --- changelog/15224.txt | 3 ++ internalshared/configutil/listener.go | 8 ++++ internalshared/configutil/listener_test.go | 49 ++++++++++++++++++++++ sdk/go.sum | 1 - 4 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 changelog/15224.txt create mode 100644 internalshared/configutil/listener_test.go diff --git a/changelog/15224.txt b/changelog/15224.txt new file mode 100644 index 000000000..c25ccf630 --- /dev/null +++ b/changelog/15224.txt @@ -0,0 +1,3 @@ +```release-note:bug +core/config: Only ask the system about network interfaces when address configs contain a template having the format: {{ ... }} +``` \ No newline at end of file diff --git a/internalshared/configutil/listener.go b/internalshared/configutil/listener.go index 0df97bdf0..a9e1d7b5c 100644 --- a/internalshared/configutil/listener.go +++ b/internalshared/configutil/listener.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "net/textproto" + "regexp" "strings" "time" @@ -410,7 +411,14 @@ 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 contain the 'template' format, +// it will return the specified input unchanged. func ParseSingleIPTemplate(ipTmpl string) (string, error) { + r := regexp.MustCompile("{{.*?}}") + if !r.MatchString(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 000000000..803086e48 --- /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 + arg string + want string + wantErr assert.ErrorAssertionFunc + }{ + { + name: "test https addr", + arg: "https://vaultproject.io:8200", + want: "https://vaultproject.io:8200", + wantErr: assert.NoError, + }, + { + name: "test invalid template func", + arg: "{{FooBar}}", + want: "", + wantErr: assert.Error, + }, + { + name: "test partial template", + arg: "{{FooBar", + want: "{{FooBar", + wantErr: assert.NoError, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + 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.arg) + }) + } +} diff --git a/sdk/go.sum b/sdk/go.sum index 957410de7..59bf6d3dd 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=