NaN

NaN

Getting :gun_error {:stream_error, :protocol_error, :"Stream reset by server."}

:gun_error : {:stream_error, :protocol_error, :"Stream reset by server."}

In Phoenix. The stream ref is created, but somewhere before upgrade the above error presents itself. The request is never passed to &Node.Socket.init/2 Non-tls connection work fine.

Certs from GlobalSign. The same cert works fine over the web for Phx.

Any ideas??

{:gun, "~> 2.0"}
{:plug_cowboy, "~> 2.5"}

server:

  defmodule Node.Server do
      use GenServer
      
      def start_link(opts) do
        GenServer.start_link(__MODULE__, opts, name: __MODULE__)
      end
      
      def init(opts) do
        routes = [
          {:_,
           [
             {"/", Node.Socket, []}
           ]}
        ]
      
        # Compile the routes into a dispatch list
        dispatch = :cowboy_router.compile(routes)
      
        {protocol, start_fn, opts} =
          case System.get_env("IS_DOCKER") do
            "true" ->
              {:https, &:cowboy.start_tls/3,
               [
                 {:port, opts.port},
                 {:cacertfile, "/app/priv/ssl/http/ca.crt"},
                 {:certfile, "/app/priv/ssl/http/server.crt"},
                 {:keyfile, "/app/priv/ssl/http/server.key"},
               ]}
      
            _ ->
              {:http, &:cowboy.start_clear/3,
               [
                 {:port, opts.port}
               ]}
          end
      
        {:ok, _} =
          start_fn.(
            protocol,
            opts,
            %{
              env: %{dispatch: dispatch}
            }
          )
      
        {:ok, %{}}
      end
      end

client:

  defp connect_to_node({admin_domain, admin_port, port} = node, token, state) do
    admin_domain = admin_domain |> String.to_charlist()

    options =
        case System.get_env("IS_DOCKER") do
        "true" ->
          %{
            transport: :tls,
            tls_opts: [
              {:port, port},
              {:verify, :verify_peer},
              {:server_name_indication, admin_domain},
              {:customize_hostname_check, [{:match_fun, :public_key.pkix_verify_hostname_match_fun(:https)}]},
              {:cacerts, :public_key.cacerts_get()}
            ]
          }

        _ ->
          %{
            tcp_opts: [{:port, port}]
          }
       end

    {:ok, conn_pid} = :gun.open(admin_domain, admin_port, options)

    Logger.debug("Attempting to connect to Admin Node @ #{admin_domain}:#{admin_port} from local port #{port}")

    case :gun.await_up(conn_pid) do
      {:ok, _} ->
        token = token || fetch_token()

        headers = %{
          "authorization" => "#{token}"
        }

        # created fine here
        stream_ref = :gun.ws_upgrade(conn_pid, ~c"/", headers) |> IO.inspect()

        Node.Identity.set(%{
          connected_node: node
        })

        if Node.Identity.get().primary_node != node do
          :timer.send_after(900_000, self(), :connect_to_primary)
        end

        {:ok, %{state | conn_pid: conn_pid, stream_ref: stream_ref}}

      {:error, error} ->
        Logger.debug("Failed to connect to Admin Node @ #{admin_domain}:#{admin_port} from local port #{port} | Error: #{inspect(error)}")
        :gun.shutdown(conn_pid)
        {:error, state}
    end
  end

Node.Socket.inti : (again, non-tls auths fine, but this is never called when using tls)

  def init(req, _opts) do
    Logger.debug("Initiating websocket connection with request: #{inspect(req)}")

    case Map.get(req.headers, "authorization") do
      nil ->
        :cowboy_req.reply(401, req)
        {:cowboy_websocket, req, %{}}

      auth ->
        {:ok, shared_key} = Node.Identity.shared_key()

        case Node.Auth.decrypt(auth, shared_key) do
          {:ok, _decrypted} ->
            {:cowboy_websocket, req, %{}, %{idle_timeout: :infinity}}

          _ ->
            :cowboy_req.reply(401, req)
            {:cowboy_websocket, req, %{}}
        end
    end
  end

1/9/23 ~6:30 moved due to cat mistake

First Post!

Tyson

Tyson

My guess would be TLS handshake failure because your cacerts don’t match. Where are you getting /app/priv/ssl/http/* from on the server?

Most Liked

NaN

NaN

No worries. TY… its just as likely you mention something that solves it. worst case someone learns something.

Last Post!

NaN

NaN

I have used multiple sets of tls_opts at this point.

verify_none, with cacerts, with a cacertfile, etc… all of them render the same error from rst_stream_frame unless of course I dont pass the opts needed to get past handshake. In which case I can see the hand shake fail on server and client

this is the only place I can find in the code that produces the error… any idea why?

rst_stream_frame(State0, StreamID, Reason, EvHandler, EvHandlerState0) ->
	case take_stream(State0, StreamID) of
		{#stream{ref=StreamRef, reply_to=ReplyTo}, State} ->
			ReplyTo ! {gun_error, self(), stream_ref(State0, StreamRef),
				{stream_error, Reason, 'Stream reset by server.'}},
			EvHandlerState = EvHandler:cancel(#{
				stream_ref => stream_ref(State, StreamRef),
				reply_to => ReplyTo,
				endpoint => remote,
				reason => Reason
			}, EvHandlerState0),
			{{state, State}, EvHandlerState};
		error ->
			{{state, State0}, EvHandlerState0}
	end.

Where Next?

Popular in Questions Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New

Other popular topics Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New