From 86b684be2856d9fb77078f1390eeb3453c52d465 Mon Sep 17 00:00:00 2001 From: "Chris (ChrisJr404)" <11917633+ChrisJr404@users.noreply.github.com> Date: Wed, 26 Aug 2026 22:54:22 -0400 Subject: [PATCH] Add AMQP.Connection.update_secret/3 --- lib/amqp/connection.ex | 25 +++++++++++++++++++++++++ test/connection_test.exs | 7 +++++++ 2 files changed, 32 insertions(+) diff --git a/lib/amqp/connection.ex b/lib/amqp/connection.ex index 42ea1c2..7d875d6 100644 --- a/lib/amqp/connection.ex +++ b/lib/amqp/connection.ex @@ -263,6 +263,31 @@ defmodule AMQP.Connection do end end + @doc """ + Updates the secret used to authenticate the given Connection. + + This is useful when the credentials used to open the connection have a + limited lifetime, such as an OAuth 2 token, and need to be renewed before + they expire so the broker does not close the connection. + + `new_secret` is the new secret and `reason` is a short human readable string + explaining why the secret is being updated. + + ## Examples + + iex> {:ok, conn} = AMQP.Connection.open() + iex> AMQP.Connection.update_secret(conn, "new-token", "token refresh") + :ok + + """ + @spec update_secret(t, String.t(), String.t()) :: :ok | {:error, any} + def update_secret(conn, new_secret, reason) do + case :amqp_connection.update_secret(conn.pid, new_secret, reason) do + :ok -> :ok + error -> {:error, error} + end + end + defp normalize_ssl_options(options) when is_list(options) do for {k, v} <- options do if k in [:cacertfile, :certfile, :keyfile] do diff --git a/test/connection_test.exs b/test/connection_test.exs index 81aaa7e..0a4da1c 100644 --- a/test/connection_test.exs +++ b/test/connection_test.exs @@ -77,6 +77,13 @@ defmodule ConnectionTest do assert params[:host] == ~c"amqp.test.com" end + test "update the secret of an open connection" do + {:ok, conn} = Connection.open() + assert :ok = Connection.update_secret(conn, "guest", "token refresh") + assert Process.alive?(conn.pid) + assert :ok = Connection.close(conn) + end + defp get_connection_name(conn) do params = :amqp_connection.info(conn.pid, [:amqp_params])[:amqp_params] amqp_params_network(client_properties: props) = params