Fix SRV lookup if address scheme is known (#8016)

This commit is contained in:
Michel Vocks
2019-12-16 09:34:40 +01:00
committed by GitHub
parent fc58f669e8
commit 2c21ef4df6

View File

@@ -678,6 +678,12 @@ func (c *Client) SetPolicyOverride(override bool) {
c.policyOverride = override
}
// portMap defines the standard port map
var portMap = map[string]string{
"http": "80",
"https": "443",
}
// NewRequest creates a new raw request object to query the Vault server
// configured for this client. This is an advanced method and generally
// doesn't need to be called externally.
@@ -694,10 +700,16 @@ func (c *Client) NewRequest(method, requestPath string) *Request {
// record and take the highest match; this is not designed for high-availability, just discovery
var host string = addr.Host
if addr.Port() == "" {
// Internet Draft specifies that the SRV record is ignored if a port is given
_, addrs, err := net.LookupSRV("http", "tcp", addr.Hostname())
if err == nil && len(addrs) > 0 {
host = fmt.Sprintf("%s:%d", addrs[0].Target, addrs[0].Port)
// Avoid lookup of SRV record if scheme is known
port, ok := portMap[addr.Scheme]
if ok {
host = net.JoinHostPort(host, port)
} else {
// Internet Draft specifies that the SRV record is ignored if a port is given
_, addrs, err := net.LookupSRV("http", "tcp", addr.Hostname())
if err == nil && len(addrs) > 0 {
host = fmt.Sprintf("%s:%d", addrs[0].Target, addrs[0].Port)
}
}
}