Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions grpc/lib/grpc/channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ defmodule GRPC.Channel do
## Fields

* `:host` - server's host to connect
* `:hostname` - the name the target resolved from, when `host` is an
address the resolver picked (e.g. a `dns://` target that resolved to an
IP). Used for TLS SNI and certificate hostname verification.
* `:port` - server's port to connect
* `:scheme` - scheme of connection, like `http`
* `:cred` - credentials used for authentication
Expand All @@ -17,6 +20,7 @@ defmodule GRPC.Channel do
"""

defstruct host: nil,
hostname: nil,
port: nil,
scheme: nil,
cred: nil,
Expand Down
6 changes: 6 additions & 0 deletions grpc/lib/grpc/client/adapters/mint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ if Code.ensure_loaded?(Mint.HTTP) do
|> merge_opts(module_opts)
|> Keyword.put(:retry, retry)

# A dns:// target resolves to an address (the host we dial) while the
# hostname it resolved from must still reach TLS for SNI and certificate
# verification.
opts =
if hostname = channel.hostname, do: Keyword.put(opts, :hostname, hostname), else: opts

Process.flag(:trap_exit, true)

channel
Expand Down
27 changes: 21 additions & 6 deletions grpc/lib/grpc/client/connection.ex
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,8 @@ defmodule GRPC.Client.Connection do
end

defp connect_new_channels(new_addresses, added, adapter, opts, state, real_channels) do
Enum.reduce(new_addresses, real_channels, fn %{address: host, port: port}, channels ->
Enum.reduce(new_addresses, real_channels, fn %{address: host, port: port} = address,
channels ->
key = build_address_key(host, port)
existing = Map.get(channels, key)

Expand All @@ -897,7 +898,14 @@ defmodule GRPC.Client.Connection do
_ -> :ok
end

case connect_real_channel(state.virtual_channel, host, port, opts, adapter) do
case connect_real_channel(
state.virtual_channel,
host,
port,
opts,
adapter,
address[:hostname]
) do
{:ok, ch} -> Map.put(channels, key, {:connected, ch})
{:error, reason} -> Map.put(channels, key, {:failed, reason})
end
Expand Down Expand Up @@ -1056,8 +1064,15 @@ defmodule GRPC.Client.Connection do
end

defp build_real_channels(addresses, %Channel{} = virtual_channel, norm_opts, adapter) do
Map.new(addresses, fn %{port: port, address: host} ->
case connect_real_channel(virtual_channel, host, port, norm_opts, adapter) do
Map.new(addresses, fn %{port: port, address: host} = address ->
case connect_real_channel(
virtual_channel,
host,
port,
norm_opts,
adapter,
address[:hostname]
) do
{:ok, ch} ->
{build_address_key(host, port), {:connected, ch}}

Expand Down Expand Up @@ -1112,8 +1127,8 @@ defmodule GRPC.Client.Connection do
defp choose_lb(:round_robin), do: GRPC.Client.LoadBalancing.RoundRobin
defp choose_lb(_), do: GRPC.Client.LoadBalancing.PickFirst

defp connect_real_channel(%Channel{} = vc, host, port, opts, adapter) do
%Channel{vc | host: host, port: port}
defp connect_real_channel(%Channel{} = vc, host, port, opts, adapter, hostname) do
%Channel{vc | host: host, port: port, hostname: hostname}
|> adapter.connect(opts[:adapter_opts])
end

Expand Down
2 changes: 1 addition & 1 deletion grpc/lib/grpc/client/resolver/dns.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ defmodule GRPC.Client.Resolver.DNS do
with {:ok, addresses} <- lookup_addresses(host) do
addrs =
Enum.map(addresses, fn ip ->
%{address: :inet.ntoa(ip) |> to_string(), port: port}
%{address: :inet.ntoa(ip) |> to_string(), port: port, hostname: host}
end)

case lookup_service_config(host) do
Expand Down
35 changes: 35 additions & 0 deletions grpc/test/grpc/adapters/mint_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ defmodule GRPC.Client.Adapters.MintTest do

alias GRPC.Client.Adapters.Mint

defmodule TLSEndpoint do
use GRPC.Endpoint
run(FeatureServer)
end

setup do
{:ok, _, port} = GRPC.Server.start(FeatureServer, 0)

Expand Down Expand Up @@ -250,4 +255,34 @@ defmodule GRPC.Client.Adapters.MintTest do
assert state.retry == 0
end
end

describe "connect/2 with hostname" do
setup do
server_credential = build(:credential)

{:ok, _, port} =
GRPC.Server.start_endpoint(TLSEndpoint, 0, adapter_opts: [cred: server_credential])

on_exit(fn -> :ok = GRPC.Server.stop_endpoint(TLSEndpoint) end)

%{port: port}
end

test "dials the resolved address but hands the hostname to Mint for TLS", %{port: port} do
channel =
build(:channel,
adapter: Mint,
port: port,
host: "127.0.0.1",
hostname: "my-service.local",
scheme: "https"
)

{:ok, connected} = Mint.connect(channel, [])
state = :sys.get_state(connected.adapter_payload.conn_pid)

assert state.host == "127.0.0.1"
assert state.connect_opts[:hostname] == "my-service.local"
end
end
end
30 changes: 30 additions & 0 deletions grpc/test/grpc/client/connection_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ defmodule GRPC.Client.ConnectionTest do
end
end

# Mirrors what GRPC.Client.Resolver.DNS returns for a dns:// target after
# the hostname fix: each resolved address keeps the target's hostname.
defmodule HostnameResolver do
def resolve(_target) do
{:ok,
%{
addresses: [%{address: "127.0.0.1", port: 50051, hostname: "my-service.local"}],
service_config: %{}
}}
end
end

setup do
%{
ref: make_ref(),
Expand Down Expand Up @@ -354,6 +366,24 @@ defmodule GRPC.Client.ConnectionTest do
end
end

describe "connect/2 - hostname from the resolver" do
test "carries the dns:// hostname onto the channel for TLS SNI", %{ref: ref} do
resolver = HostnameResolver

{:ok, channel} =
Connection.connect("dns://my-service.local:50051",
adapter: GRPC.Test.ClientAdapter,
name: ref,
resolver: resolver
)

assert channel.host == "127.0.0.1"
assert channel.hostname == "my-service.local"

Connection.disconnect(channel)
end
end

describe "connect/2 - distributed named channels" do
test "named channels do not conflict across connected nodes" do
{:ok, _, port} = GRPC.Server.start(FeatureServer, 0)
Expand Down
16 changes: 16 additions & 0 deletions grpc/test/grpc/resolver/dns_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,20 @@ defmodule GRPC.Client.Resolver.DNSTest do
assert [%{address: "127.0.0.1", port: 50051}] = addrs
assert {:ok, %{load_balancing_policy: :pick_first}} = config
end

test "keeps the hostname of a dns:// target alongside the resolved IP" do
host = "my-service.local"
config_name = "_grpc_config." <> host

DNS.MockAdapter
|> expect(:lookup, fn ^host, :a ->
{:ok, [{127, 0, 0, 1}]}
end)
|> expect(:lookup, fn ^config_name, :txt ->
{:ok, []}
end)

assert {:ok, %{addresses: addrs}} = DNS.resolve("dns://#{host}:50051")
assert [%{address: "127.0.0.1", port: 50051, hostname: ^host}] = addrs
end
end