Skip to content

Commit

Permalink
Do sockaddr template parsing only when needed (#15224)
Browse files Browse the repository at this point in the history
  • Loading branch information
peteski22 authored Apr 29, 2022
1 parent dc91661 commit 5dd7b93
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
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,
},
{
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

0 comments on commit 5dd7b93

Please sign in to comment.