Do sockaddr template parsing only when needed (#15224)

This commit is contained in:
Peter Wilson 2022-04-29 14:57:17 +01:00 committed by GitHub
parent c5928c1d15
commit 43bb764808
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 60 additions and 1 deletions

3
changelog/15224.txt Normal file
View File

@ -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: {{ ... }}
```

View File

@ -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)

View File

@ -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)
})
}
}

View File

@ -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=