Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/gh 15210/sockaddr template parsing when needed #15224

Merged
merged 9 commits into from
Apr 29, 2022
3 changes: 3 additions & 0 deletions changelog/15224.txt
Original file line number Diff line number Diff line change
@@ -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: {{ ... }}
```
8 changes: 8 additions & 0 deletions internalshared/configutil/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"net/textproto"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -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)
Expand Down
49 changes: 49 additions & 0 deletions internalshared/configutil/listener_test.go
Original file line number Diff line number Diff line change
@@ -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,
peteski22 marked this conversation as resolved.
Show resolved Hide resolved
},
{
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)
})
}
}
1 change: 0 additions & 1 deletion sdk/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down