From 0da68522f17031aae55e3a6ea851d1be02933cd5 Mon Sep 17 00:00:00 2001 From: Nick Cabatoff Date: Thu, 21 Oct 2021 09:29:54 -0400 Subject: [PATCH] Mirror the change made in https://github.com/hashicorp/vault/pull/9109 --- listenerutil/parse.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/listenerutil/parse.go b/listenerutil/parse.go index 9b9b8a0..636f3d9 100644 --- a/listenerutil/parse.go +++ b/listenerutil/parse.go @@ -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" ) @@ -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 { @@ -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) + } +}