Skip to content

Commit

Permalink
Mirror the change made in hashicorp/vault#9109
Browse files Browse the repository at this point in the history
  • Loading branch information
ncabatoff committed Oct 21, 2021
1 parent f7bda98 commit 0da6852
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions listenerutil/parse.go
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/hashicorp/go-secure-stdlib/strutil"
"github.com/hashicorp/go-secure-stdlib/tlsutil"
"github.com/hashicorp/go-sockaddr"
"github.com/hashicorp/go-sockaddr/template"
"github.com/hashicorp/hcl"
"github.com/hashicorp/hcl/hcl/ast"
)
Expand Down Expand Up @@ -107,6 +108,16 @@ func ParseListeners(list *ast.ObjectList) ([]*ListenerConfig, error) {
return nil, multierror.Prefix(err, fmt.Sprintf("listeners.%d:", i))
}

if rendered, err := ParseSingleIPTemplate(l.Address); err != nil {
return nil, multierror.Prefix(err, fmt.Sprintf("listeners.%d:", i))
} else {
l.Address = rendered
}
if rendered, err := ParseSingleIPTemplate(l.ClusterAddress); err != nil {
return nil, multierror.Prefix(err, fmt.Sprintf("listeners.%d:", i))
} else {
l.ClusterAddress = rendered
}
// Hacky way, for now, to get the values we want for sanitizing
var m map[string]interface{}
if err := hcl.DecodeObject(&m, item.Val); err != nil {
Expand Down Expand Up @@ -357,3 +368,22 @@ func ParseListeners(list *ast.ObjectList) ([]*ListenerConfig, error) {

return result, nil
}

// ParseSingleIPTemplate is used as a helper function to parse out a single IP
// address from a config parameter.
func ParseSingleIPTemplate(ipTmpl string) (string, error) {
out, err := template.Parse(ipTmpl)
if err != nil {
return "", fmt.Errorf("unable to parse address template %q: %v", ipTmpl, err)
}

ips := strings.Split(out, " ")
switch len(ips) {
case 0:
return "", errors.New("no addresses found, please configure one")
case 1:
return strings.TrimSpace(ips[0]), nil
default:
return "", fmt.Errorf("multiple addresses found (%q), please configure one", out)
}
}

0 comments on commit 0da6852

Please sign in to comment.