From e4a3102235090ffd966db4f1b4ca81d14413a95d Mon Sep 17 00:00:00 2001 From: kkerezsi <11377875+kkerezsi@users.noreply.github.com> Date: Wed, 11 Dec 2024 09:17:29 +0200 Subject: [PATCH] fix(master): ensure url_parser does not fail on missing txt record (#261) * fix url parser to avid failing on missing txt record * Fix lookup value parsing * Fix warning * fix comments --------- Co-authored-by: NI\akerezsi --- lib/mongo/url_parser.ex | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/mongo/url_parser.ex b/lib/mongo/url_parser.ex index 05454a3..661469f 100644 --- a/lib/mongo/url_parser.ex +++ b/lib/mongo/url_parser.ex @@ -155,8 +155,8 @@ defmodule Mongo.UrlParser do {:ok, {_, _, _, _, _, srv_record}} <- :inet_res.getbyname(~c"_mongodb._tcp." ++ url_char, :srv), {:ok, host} <- get_host_srv(srv_record), - {:ok, {_, _, _, _, _, txt_record}} <- :inet_res.getbyname(url_char, :txt), - txt <- "#{orig_options}&#{txt_record}&ssl=true" do + {:ok, txt_record} <- resolve_txt_record(url_char), + txt <- build_params(orig_options, txt_record) do frags |> Map.put("seeds", host) |> Map.put("options", txt) @@ -167,6 +167,24 @@ defmodule Mongo.UrlParser do defp resolve_srv_url(frags), do: frags + defp build_params(orig_options, nil) do + "#{orig_options}&ssl=true" + end + + defp build_params(orig_options, txt_record) do + "#{orig_options}&#{txt_record}&ssl=true" + end + + defp resolve_txt_record(url_char) do + case :inet_res.lookup(url_char, :in, :txt) do + [[txt_record] | _] -> + {:ok, txt_record} + + _other -> + {:ok, nil} + end + end + @spec get_host_srv([{term, term, term, term}]) :: {:ok, String.t()} defp get_host_srv(srv) when is_list(srv) do hosts = Enum.map_join(srv, ",", fn {_, _, port, host} -> "#{host}:#{port}" end)