Skip to content

Commit

Permalink
Replace endpoint.ParseHostPort with net.SplitHostPort to fix IPv6 cli…
Browse files Browse the repository at this point in the history
…ent endpoints
  • Loading branch information
jpbetz committed Oct 8, 2019
1 parent a0d2427 commit 1e0e3d5
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 15 deletions.
11 changes: 0 additions & 11 deletions clientv3/balancer/resolver/endpoint/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,17 +230,6 @@ func ParseTarget(target string) (string, string, error) {
return parts[0], parts[1], nil
}

// ParseHostPort splits a "<host>:<port>" string into the host and port parts.
// The port part is optional.
func ParseHostPort(hostPort string) (host string, port string) {
parts := strings.SplitN(hostPort, ":", 2)
host = parts[0]
if len(parts) > 1 {
port = parts[1]
}
return host, port
}

// Dialer dials a endpoint using net.Dialer.
// Context cancelation and timeout are supported.
func Dialer(ctx context.Context, dialEp string) (net.Conn, error) {
Expand Down
14 changes: 10 additions & 4 deletions clientv3/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,10 @@ func (c *Client) dialSetupOpts(creds grpccredentials.TransportCredentials, dopts

// Dial connects to a single endpoint using the client's config.
func (c *Client) Dial(ep string) (*grpc.ClientConn, error) {
creds := c.directDialCreds(ep)
creds, err := c.directDialCreds(ep)
if err != nil {
return nil, err
}
// Use the grpc passthrough resolver to directly dial a single endpoint.
// This resolver passes through the 'unix' and 'unixs' endpoints schemes used
// by etcd without modification, allowing us to directly dial endpoints and
Expand Down Expand Up @@ -362,7 +365,7 @@ func (c *Client) dial(target string, creds grpccredentials.TransportCredentials,
return conn, nil
}

func (c *Client) directDialCreds(ep string) grpccredentials.TransportCredentials {
func (c *Client) directDialCreds(ep string) (grpccredentials.TransportCredentials, error) {
_, hostPort, scheme := endpoint.ParseEndpoint(ep)
creds := c.creds
if len(scheme) != 0 {
Expand All @@ -372,12 +375,15 @@ func (c *Client) directDialCreds(ep string) grpccredentials.TransportCredentials
// Set the server name must to the endpoint hostname without port since grpc
// otherwise attempts to check if x509 cert is valid for the full endpoint
// including the scheme and port, which fails.
host, _ := endpoint.ParseHostPort(hostPort)
host, _, err := net.SplitHostPort(hostPort)
if err != nil {
return nil, err
}
clone.OverrideServerName(host)
creds = clone
}
}
return creds
return creds, nil
}

func (c *Client) dialWithBalancerCreds(ep string) grpccredentials.TransportCredentials {
Expand Down
19 changes: 19 additions & 0 deletions integration/fixtures/server-ca-csr-ipv6.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"O": "etcd",
"OU": "etcd Security",
"L": "San Francisco",
"ST": "California",
"C": "USA"
}
],
"CN": "example.com",
"hosts": [
"127.0.0.1"
]
}
24 changes: 24 additions & 0 deletions integration/fixtures/server-ipv6.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
-----BEGIN CERTIFICATE-----
MIIECDCCAvCgAwIBAgIUVidA2BA1FGpvYIR8qVAXSmjEMsswDQYJKoZIhvcNAQEL
BQAwbzEMMAoGA1UEBhMDVVNBMRMwEQYDVQQIEwpDYWxpZm9ybmlhMRYwFAYDVQQH
Ew1TYW4gRnJhbmNpc2NvMQ0wCwYDVQQKEwRldGNkMRYwFAYDVQQLEw1ldGNkIFNl
Y3VyaXR5MQswCQYDVQQDEwJjYTAeFw0xOTEwMDgxOTQ3MDBaFw0yOTEwMDUxOTQ3
MDBaMHgxDDAKBgNVBAYTA1VTQTETMBEGA1UECBMKQ2FsaWZvcm5pYTEWMBQGA1UE
BxMNU2FuIEZyYW5jaXNjbzENMAsGA1UEChMEZXRjZDEWMBQGA1UECxMNZXRjZCBT
ZWN1cml0eTEUMBIGA1UEAxMLZXhhbXBsZS5jb20wggEiMA0GCSqGSIb3DQEBAQUA
A4IBDwAwggEKAoIBAQDo/UZ4i/n97iLJck0CrynaUpmKCagV21YsYzfFXvA/7biB
nF7YzEbZkXv+YjTwo8huHGm0XFUs0WzS7OoIK7n1QPfPntb+3AoVpD2amsEt/9VY
P5O42xfN+C+3MqRBAKeAuRzwFODuJi4qYsvJasBkfd78/QIn2YeHoyfhGO0wicmo
DWeBwNNkEqxdWsVjRAg5g3njBrabPJRbhvlri9S7/1cEa5Jdy1PRw/foUtDAbDl4
2Zm9zDOj/1Hu5pUJoggsXycOty+xV9HwEHCBRZHxZgBgNElyZ5AhZ5aHc8tnpIVH
eLTlraYVxVWSvPgVXoZEO7fXxFTIlJPZdFiY39jvAgMBAAGjgZIwgY8wDgYDVR0P
AQH/BAQDAgWgMB0GA1UdJQQWMBQGCCsGAQUFBwMBBggrBgEFBQcDAjAMBgNVHRMB
Af8EAjAAMB0GA1UdDgQWBBRudySJBv0gSG51P7OpNWefd+YO6TAfBgNVHSMEGDAW
gBQ7mNcVfPjzEpOqH60986Kz0mUotjAQBgNVHREECTAHggVbOjoxXTANBgkqhkiG
9w0BAQsFAAOCAQEASGjBhkv/e9jjHLXCJSd2PtM0QhW1HQla2Ug/NvFvoTYdeTdU
Rs/dGsDYFm0G0zIYb1j7fjngWYVCjfMA8ccAlCPvlrC4+t3QDXqTrz2XLR72xzfs
Hmrr3nz7KRITIs6PCuBocGYQI64dkWvTHmfHsxRMqXOPQfaXAYruP6HUxOArGJsQ
Fs/B2C4hh5L9y+4KApTax4WIPSlp52MS7P7dKEK5HdNfzwvzAip4SBCSTwJ2w8Co
r3z/Lze0u4Ny/0C9AiOnKYlhcL0FacfxC41+a2SlxOc+0giGJMria5dHiWFkLFvg
hJ+SvGg2RVcdosh/+CiSHjqtGqFRZMdn/m+QVA==
-----END CERTIFICATE-----
27 changes: 27 additions & 0 deletions integration/fixtures/server-ipv6.key.insecure
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEpgIBAAKCAQEA6P1GeIv5/e4iyXJNAq8p2lKZigmoFdtWLGM3xV7wP+24gZxe
2MxG2ZF7/mI08KPIbhxptFxVLNFs0uzqCCu59UD3z57W/twKFaQ9mprBLf/VWD+T
uNsXzfgvt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