Enable AMQP 1.0 clients to manage topologies

## What?

* Allow AMQP 1.0 clients to dynamically create and delete RabbitMQ
  topologies (exchanges, queues, bindings).
* Provide an Erlang AMQP 1.0 client that manages topologies.

 ## Why?

Today, RabbitMQ topologies can be created via:
* [Management HTTP API](https://www.rabbitmq.com/docs/management#http-api)
  (including Management UI and
  [messaging-topology-operator](https://github.com/rabbitmq/messaging-topology-operator))
* [Definition Import](https://www.rabbitmq.com/docs/definitions#import)
* AMQP 0.9.1 clients

Up to RabbitMQ 3.13 the RabbitMQ AMQP 1.0 plugin auto creates queues
and bindings depending on the terminus [address
format](https://github.com/rabbitmq/rabbitmq-server/tree/v3.13.x/deps/rabbitmq_amqp1_0#routing-and-addressing).

Such implicit creation of topologies is limiting and obscure.
For some address formats, queues will be created, but not deleted.

Some of RabbitMQ's success is due to its flexible routing topologies
that AMQP 0.9.1 clients can create and delete dynamically.

This commit allows dynamic management of topologies for AMQP 1.0 clients.
This commit builds on top of Native AMQP 1.0 (PR #9022) and will be
available in RabbitMQ 4.0.

 ## How?

This commits adds the following management operations for AMQP 1.0 clients:
* declare queue
* delete queue
* purge queue
* bind queue to exchange
* unbind queue from exchange
* declare exchange
* delete exchange
* bind exchange to exchange
* unbind exchange from exchange

Hence, at least the AMQP 0.9.1 management operations are supported for
AMQP 1.0 clients.

In addition the operation
* get queue

is provided which - similar to `declare queue` - returns queue
information including the current leader and replicas.
This allows clients to publish or consume locally on the node that hosts
the queue.

Compared to AMQP 0.9.1 whose commands and command fields are fixed, the
new AMQP Management API is extensible: New operations and new fields can
easily be added in the future.

There are different design options how management operations could be
supported for AMQP 1.0 clients:
1. Use a special exchange type as done in https://github.com/rabbitmq/rabbitmq-management-exchange
  This has the advantage that any protocol client (e.g. also STOMP clients) could
  dynamically manage topologies. However, a special exchange type is the wrong abstraction.
2. Clients could send "special" messages with special headers that the broker interprets.

This commit decided for a variation of the 2nd option using a more
standardized way by re-using a subest of the following latest AMQP 1.0 extension
specifications:
* [AMQP Request-Response Messaging with Link Pairing Version 1.0 - Committee Specification 01](https://docs.oasis-open.org/amqp/linkpair/v1.0/cs01/linkpair-v1.0-cs01.html) (February 2021)
* [HTTP Semantics and Content over AMQP Version 1.0 - Working Draft 06](https://groups.oasis-open.org/higherlogic/ws/public/document?document_id=65571) (July 2019)
* [AMQP Management Version 1.0 - Working Draft 16](https://groups.oasis-open.org/higherlogic/ws/public/document?document_id=65575) (July 2019)

An important goal is to keep the interaction between AMQP 1.0 client and RabbitMQ
simple to increase usage, development and adoptability of future RabbitMQ AMQP 1.0
client library wrappers.

The AMQP 1.0 client has to create a link pair to the special `/management` node.
This allows the client to send and receive from the management node.
Similar to AMQP 0.9.1, there is no need for a reply queue since the reply
will be sent directly to the client.

Requests and responses are modelled via HTTP, but sent via AMQP using
the `HTTP Semantics and Content over AMQP` extension (henceforth `HTTP
over AMQP` extension).

This commit tries to follow the `HTTP over AMQP` extension as much as
possible but deviates where this draft spec doesn't make sense.

The projected mode §4.1 is used as opposed to tunneled mode §4.2.
A named relay `/management` is used (§6.3) where the message field `to` is the URL.

Deviations are
* §3.1 mandates that URIs are not encoded in an AMQP message.
  However, we percent encode URIs in the AMQP message. Otherwise there
  is for example no way to distinguish a `/` in a queue name from the
  URI path separator `/`.
* §4.1.4 mandates a data section. This commit uses an amqp-value section
  as it's a better fit given that the content is AMQP encoded data.

Using an HTTP API allows for a common well understood interface and future extensibility.
Instead of re-using the current RabbitMQ HTTP API, this commit uses a
new HTTP API (let's call it v2) which could be used as a future API for
plain HTTP clients.

 ### HTTP API v1

The current HTTP API (let's call it v1) is **not** used since v1
comes with a couple of weaknesses:

1. Deep level of nesting becomes confusing and difficult to manage.

Examples of deep nesting in v1:
```
/api/bindings/vhost/e/source/e/destination/props
/api/bindings/vhost/e/exchange/q/queue/props
```

2. Redundant endpoints returning the same resources

v1 has 9 endpoints to list binding(s):
```
/api/exchanges/vhost/name/bindings/source
/api/exchanges/vhost/name/bindings/destination
/api/queues/vhost/name/bindings
/api/bindings
/api/bindings/vhost
/api/bindings/vhost/e/exchange/q/queue
/api/bindings/vhost/e/exchange/q/queue/props
/api/bindings/vhost/e/source/e/destination
/api/bindings/vhost/e/source/e/destination/props
```

3. Verbs in path names
Path names should be nouns instead.
v1 contains verbs:
```
/api/queues/vhost/name/get
/api/exchanges/vhost/name/publish
```

 ### AMQP Management extension

Only few aspects of the AMQP Management extension are used.

The central idea of the AMQP management spec is **dynamic discovery** such that broker independent AMQP 1.0
clients can discover objects, types, operations, and HTTP endpoints of specific brokers.
In fact, clients are only conformant if:
> All request addresses are dynamically discovered starting from the discovery document.
> A requesting container MUST NOT use fixed assumptions about the addressing structure of the management API.

While this is a nice and powerful idea, no AMQP 1.0 client and no AMQP 1.0 server implement the
latest AMQP 1.0 management spec from 2019, partly presumably due to its complexity.
Therefore, the idea of such dynamic discovery has failed to be implemented in practice.

The AMQP management spec mandates that the management endpoint returns a discovery document containing
broker specific collections, types, configuration, and operations including their endpoints.

The API endpoints of the AMQP management spec are therefore all designed around dynamic discovery.

For example, to create either a queue or an exchange, the client has to
```
POST /$management/entities
```
which shows that the entities collection acts as a generic factory, see section 2.2.
The server will then create the resource and reply with a location header containing a URI pointing to the resource.
For RabbitMQ, we don’t need such a generic factory to create queues or exchanges.

To list bindings for a queue Q1, the spec suggests
```
GET /$management/Queues/Q1/$management/entities
```
which again shows the generic entities endpoint as well as a `$management` endpoint under Q1 to
allow a queue to return a discovery document.
For RabbitMQ, we don’t need such generic endpoints and discovery documents.

Given we aim for our own thin RabbitMQ AMQP 1.0 client wrapper libraries which expose
the RabbitMQ model to the developer, we can directly use fixed HTTP endpoint assumptions
in our RabbitMQ specific libraries.

This is by far simpler than using the dynamic endpoints of the management spec.
Simplicity leads to higher adoption and enables more developers to write RabbitMQ AMQP 1.0 client
library wrappers.

The AMQP Management extension also suffers from deep level of nesting in paths
Examples:
```
/$management/Queues/Q1/$management/entities
/$management/Queues/Q1/Bindings/Binding1
```
as well as verbs in path names: Section 7.1.4 suggests using verbs in path names,
for example “purge”, due to the dynamic operations discovery document.

 ### HTTP API v2

This commit introduces a new HTTP API v2 following best practices.
It could serve as a future API for plain HTTP clients.

This commit and RabbitMQ 4.0 will only implement a minimal set of
HTTP API v2 endpoints and only for HTTP over AMQP.
In other words, the existing HTTP API v1 Cowboy handlers will continue to be
used for all plain HTTP requests in RabbitMQ 4.0 and will remain untouched for RabbitMQ 4.0.
Over time, after 4.0 shipped, we could ship a pure HTTP API implementation for HTTP API v2.
Hence, the new HTTP API v2 endpoints for HTTP over AMQP should be designed such that they
can be re-used in the future for a pure HTTP implementation.

The minimal set of endpoints for RabbitMQ 4.0 are:

``
GET / PUT / DELETE
/vhosts/:vhost/queues/:queue
```
read, create, delete a queue

```
DELETE
/vhosts/:vhost/queues/:queue/messages
```
purges a queue

```
GET / DELETE
/vhosts/:vhost/bindings/:binding
```
read, delete bindings
where `:binding` is a binding ID of the following path segment:
```
src=e1;dstq=q2;key=my-key;args=
```
Binding arguments `args` has an empty value by default, i.e. there are no binding arguments.
If the binding includes binding arguments, `args` will be an Erlang portable term hash
provided by the server similar to what’s provided in HTTP API v1 today.
Alternatively, we could use an arguments scheme of:
```
args=k1,utf8,v1&k2,uint,3
```
However, such a scheme leads to long URIs when there are many binding arguments.
Note that it’s perfectly fine for URI producing applications to include URI
reserved characters `=` / `;` / `,` / `$` in a path segment.

To create a binding, the client therefore needs to POST to a bindings factory URI:
```
POST
/vhosts/:vhost/bindings
```

To list all bindings between a source exchange e1 and destination exchange e2 with binding key k1:
```
GET
/vhosts/:vhost/bindings?src=e1&dste=e2&key=k1
```

This endpoint will be called by the RabbitMQ AMQP 1.0 client library to unbind a
binding with non-empty binding arguments to get the binding ID before invoking a
```
DELETE
/vhosts/:vhost/bindings/:binding
```

In future, after RabbitMQ 4.0 shipped, new API endpoints could be added.
The following is up for discussion and is only meant to show the clean and simple design of HTTP API v2.

Bindings endpoint can be queried as follows:

to list all bindings for a given source exchange e1:
```
GET
/vhosts/:vhost/bindings?src=e1
```

to list all bindings for a given destination queue q1:
```
GET
/vhosts/:vhost/bindings?dstq=q1
```

to list all bindings between a source exchange e1 and destination queue q1:
```
GET
/vhosts/:vhost/bindings?src=e1&dstq=q1
```

multiple bindings between source exchange e1 and destination queue q1 could be deleted at once as follows:
```
DELETE /vhosts/:vhost/bindings?src=e1&dstq=q1
```

GET could be supported globally across all vhosts:
```
/exchanges
/queues
/bindings
```

Publish a message:
```
POST
/vhosts/:vhost/queues/:queue/messages
```

Consume or peek a message (depending on query parameters):
```
GET
/vhosts/:vhost/queues/:queue/messages
```

Note that the AMQP 1.0 client omits the `/vhost/:vhost` path prefix.
Since an AMQP connection belongs to a single vhost, there is no need to
additionally include the vhost in every HTTP request.

Pros of HTTP API v2:

1. Low level of nesting

Queues, exchanges, bindings are top level entities directly under vhosts.
Although the HTTP API doesn’t have to reflect how resources are stored in the database,
v2 does nicely reflect the Khepri tree structure.

2. Nouns instead of verbs
HTTP API v2 is very simple to read and understand as shown by
```
POST    /vhosts/:vhost/queues/:queue/messages	to post messages, i.e. publish to a queue.
GET     /vhosts/:vhost/queues/:queue/messages	to get messages, i.e. consume or peek from a queue.
DELETE  /vhosts/:vhost/queues/:queue/messages	to delete messages, i.e. purge a queue.
```

A separate new HTTP API v2 allows us to ship only handlers for HTTP over AMQP for RabbitMQ 4.0
and therefore move faster while still keeping the option on the table to re-use the new v2 API
for pure HTTP in the future.
In contrast, re-using the HTTP API v1 for HTTP over AMQP is possible, but dirty because separate handlers
(HTTP over AMQP and pure HTTP) replying differently will be needed for the same v1 endpoints.
This commit is contained in:
David Ansari 2024-02-07 18:26:13 +01:00
parent be30abf480
commit 8a3f3c6f34
43 changed files with 3938 additions and 400 deletions

1
.gitignore vendored
View File

@ -14,6 +14,7 @@
!/deps/amqp10_common/
!/deps/oauth2_client/
!/deps/rabbitmq_amqp1_0/
!/deps/rabbitmq_amqp_client/
!/deps/rabbitmq_auth_backend_cache/
!/deps/rabbitmq_auth_backend_http/
!/deps/rabbitmq_auth_backend_ldap/

View File

@ -20,7 +20,7 @@ load(
APP_NAME = "amqp10_client"
APP_DESCRIPTION = "AMQP 1.0 client from the RabbitMQ Project"
APP_DESCRIPTION = "AMQP 1.0 client"
APP_MODULE = "amqp10_client_app"

View File

@ -1,5 +1,5 @@
PROJECT = amqp10_client
PROJECT_DESCRIPTION = AMQP 1.0 client from the RabbitMQ Project
PROJECT_DESCRIPTION = AMQP 1.0 client
PROJECT_MOD = amqp10_client_app
define PROJECT_APP_EXTRA_KEYS

View File

@ -42,8 +42,6 @@
parse_uri/1
]).
-define(DEFAULT_TIMEOUT, 5000).
-type snd_settle_mode() :: amqp10_client_session:snd_settle_mode().
-type rcv_settle_mode() :: amqp10_client_session:rcv_settle_mode().
@ -134,7 +132,7 @@ begin_session(Connection) when is_pid(Connection) ->
-spec begin_session_sync(pid()) ->
supervisor:startchild_ret() | session_timeout.
begin_session_sync(Connection) when is_pid(Connection) ->
begin_session_sync(Connection, ?DEFAULT_TIMEOUT).
begin_session_sync(Connection, ?TIMEOUT).
%% @doc Synchronously begins an amqp10 session using 'Connection'.
%% This is a convenience function that awaits the 'begun' event
@ -191,7 +189,7 @@ attach_sender_link_sync(Session, Name, Target, SettleMode, Durability) ->
{ok, Ref};
{amqp10_event, {link, Ref, {detached, Err}}} ->
{error, Err}
after ?DEFAULT_TIMEOUT -> link_timeout
after ?TIMEOUT -> link_timeout
end.
%% @doc Attaches a sender link to a target.
@ -357,7 +355,7 @@ stop_receiver_link(#link_ref{role = receiver,
send_msg(#link_ref{role = sender, session = Session,
link_handle = Handle}, Msg0) ->
Msg = amqp10_msg:set_handle(Handle, Msg0),
amqp10_client_session:transfer(Session, Msg, ?DEFAULT_TIMEOUT).
amqp10_client_session:transfer(Session, Msg, ?TIMEOUT).
%% @doc Accept a message on a the link referred to be the 'LinkRef'.
-spec accept_msg(link_ref(), amqp10_msg:amqp10_msg()) -> ok.
@ -376,7 +374,7 @@ settle_msg(LinkRef, Msg, Settlement) ->
%% Flows a single link credit then awaits delivery or timeout.
-spec get_msg(link_ref()) -> {ok, amqp10_msg:amqp10_msg()} | {error, timeout}.
get_msg(LinkRef) ->
get_msg(LinkRef, ?DEFAULT_TIMEOUT).
get_msg(LinkRef, ?TIMEOUT).
%% @doc Get a single message from a link.
%% Flows a single link credit then awaits delivery or timeout.

View File

@ -52,7 +52,6 @@
diff/2]).
-define(MAX_SESSION_WINDOW_SIZE, 65535).
-define(DEFAULT_TIMEOUT, 5000).
-define(UINT_OUTGOING_WINDOW, {uint, ?UINT_MAX}).
-define(INITIAL_OUTGOING_DELIVERY_ID, ?UINT_MAX).
%% "The next-outgoing-id MAY be initialized to an arbitrary value" [2.5.6]
@ -149,7 +148,7 @@
reader :: pid(),
socket :: amqp10_client_connection:amqp10_socket() | undefined,
links = #{} :: #{output_handle() => #link{}},
link_index = #{} :: #{link_name() => output_handle()},
link_index = #{} :: #{{link_role(), link_name()} => output_handle()},
link_handle_index = #{} :: #{input_handle() => output_handle()},
next_link_handle = 0 :: output_handle(),
early_attach_requests :: [term()],
@ -172,7 +171,7 @@
-spec begin_sync(pid()) -> supervisor:startchild_ret().
begin_sync(Connection) ->
begin_sync(Connection, ?DEFAULT_TIMEOUT).
begin_sync(Connection, ?TIMEOUT).
-spec begin_sync(pid(), non_neg_integer()) ->
supervisor:startchild_ret() | session_timeout.
@ -302,24 +301,28 @@ mapped(cast, #'v1_0.end'{error = Err}, State) ->
mapped(cast, #'v1_0.attach'{name = {utf8, Name},
initial_delivery_count = IDC,
handle = {uint, InHandle},
role = PeerRoleBool,
max_message_size = MaybeMaxMessageSize},
#state{links = Links, link_index = LinkIndex,
link_handle_index = LHI} = State0) ->
#{Name := OutHandle} = LinkIndex,
OurRoleBool = not PeerRoleBool,
OurRole = boolean_to_role(OurRoleBool),
LinkIndexKey = {OurRole, Name},
#{LinkIndexKey := OutHandle} = LinkIndex,
#{OutHandle := Link0} = Links,
ok = notify_link_attached(Link0),
{DeliveryCount, MaxMessageSize} =
case Link0 of
#link{role = sender,
#link{role = sender = OurRole,
delivery_count = DC} ->
MSS = case MaybeMaxMessageSize of
{ulong, S} when S > 0 -> S;
_ -> undefined
end,
{DC, MSS};
#link{role = receiver,
#link{role = receiver = OurRole,
max_message_size = MSS} ->
{unpack(IDC), MSS}
end,
@ -327,8 +330,8 @@ mapped(cast, #'v1_0.attach'{name = {utf8, Name},
input_handle = InHandle,
delivery_count = DeliveryCount,
max_message_size = MaxMessageSize},
State = State0#state{links = Links#{OutHandle => Link},
link_index = maps:remove(Name, LinkIndex),
State = State0#state{links = Links#{OutHandle := Link},
link_index = maps:remove(LinkIndexKey, LinkIndex),
link_handle_index = LHI#{InHandle => OutHandle}},
{keep_state, State};
mapped(cast, #'v1_0.detach'{handle = {uint, InHandle},
@ -648,8 +651,8 @@ build_frames(Channel, Trf, Payload, MaxPayloadSize, Acc) ->
make_source(#{role := {sender, _}}) ->
#'v1_0.source'{};
make_source(#{role := {receiver, #{address := Address} = Target, _Pid}, filter := Filter}) ->
Durable = translate_terminus_durability(maps:get(durable, Target, none)),
make_source(#{role := {receiver, #{address := Address} = Source, _Pid}, filter := Filter}) ->
Durable = translate_terminus_durability(maps:get(durable, Source, none)),
TranslatedFilter = translate_filters(Filter),
#'v1_0.source'{address = {utf8, Address},
durable = {uint, Durable},
@ -743,35 +746,34 @@ detach_with_error_cond(Link = #link{output_handle = OutHandle}, State, Cond) ->
ok = send(Detach, State),
Link#link{state = detach_sent}.
send_attach(Send, #{name := Name, role := Role} = Args, {FromPid, _},
#state{next_link_handle = OutHandle0, links = Links,
send_attach(Send, #{name := Name, role := RoleTuple} = Args, {FromPid, _},
#state{next_link_handle = OutHandle0, links = Links,
link_index = LinkIndex} = State) ->
Source = make_source(Args),
Target = make_target(Args),
Properties = amqp10_client_types:make_properties(Args),
{LinkTarget, RoleAsBool, InitialDeliveryCount, MaxMessageSize} =
case Role of
{LinkTarget, InitialDeliveryCount, MaxMessageSize} =
case RoleTuple of
{receiver, _, Pid} ->
{{pid, Pid}, true, undefined, max_message_size(Args)};
{{pid, Pid}, undefined, max_message_size(Args)};
{sender, #{address := TargetAddr}} ->
{TargetAddr, false, uint(?INITIAL_DELIVERY_COUNT), undefined}
end,
{OutHandle, NextLinkHandle} =
case Args of
#{handle := Handle} ->
%% Client app provided link handle.
%% Really only meant for integration tests.
{Handle, OutHandle0};
_ ->
{OutHandle0, OutHandle0 + 1}
{TargetAddr, uint(?INITIAL_DELIVERY_COUNT), undefined}
end,
{OutHandle, NextLinkHandle} = case Args of
#{handle := Handle} ->
%% Client app provided link handle.
%% Really only meant for integration tests.
{Handle, OutHandle0};
_ ->
{OutHandle0, OutHandle0 + 1}
end,
Role = element(1, RoleTuple),
% create attach performative
Attach = #'v1_0.attach'{name = {utf8, Name},
role = RoleAsBool,
role = role_to_boolean(Role),
handle = {uint, OutHandle},
source = Source,
properties = Properties,
@ -782,12 +784,12 @@ send_attach(Send, #{name := Name, role := Role} = Args, {FromPid, _},
max_message_size = MaxMessageSize},
ok = Send(Attach, State),
LinkRef = make_link_ref(element(1, Role), self(), OutHandle),
Ref = make_link_ref(Role, self(), OutHandle),
Link = #link{name = Name,
ref = LinkRef,
ref = Ref,
output_handle = OutHandle,
state = attach_sent,
role = element(1, Role),
role = Role,
notify = FromPid,
auto_flow = never,
target = LinkTarget,
@ -796,7 +798,7 @@ send_attach(Send, #{name := Name, role := Role} = Args, {FromPid, _},
{State#state{links = Links#{OutHandle => Link},
next_link_handle = NextLinkHandle,
link_index = LinkIndex#{Name => OutHandle}}, LinkRef}.
link_index = LinkIndex#{{Role, Name} => OutHandle}}, Ref}.
-spec handle_session_flow(#'v1_0.flow'{}, #state{}) -> #state{}.
handle_session_flow(#'v1_0.flow'{next_incoming_id = MaybeNII,
@ -1090,6 +1092,16 @@ sym(B) when is_atom(B) -> {symbol, atom_to_binary(B, utf8)}.
reason(undefined) -> normal;
reason(Other) -> Other.
role_to_boolean(sender) ->
?AMQP_ROLE_SENDER;
role_to_boolean(receiver) ->
?AMQP_ROLE_RECEIVER.
boolean_to_role(?AMQP_ROLE_SENDER) ->
sender;
boolean_to_role(?AMQP_ROLE_RECEIVER) ->
receiver.
format_status(Status = #{data := Data0}) ->
#state{channel = Channel,
remote_channel = RemoteChannel,

View File

@ -64,7 +64,7 @@
link_event_detail()}.
-type amqp10_event() :: {amqp10_event, amqp10_event_detail()}.
-type properties() :: #{binary() => tuple()}.
-type properties() :: #{binary() => amqp10_binary_generator:amqp10_prim()}.
-export_type([amqp10_performative/0, channel/0,
source/0, target/0, amqp10_msg_record/0,
@ -73,7 +73,6 @@
properties/0]).
unpack(undefined) -> undefined;
unpack({_, Value}) -> Value;
unpack(Value) -> Value.

View File

@ -6,6 +6,8 @@
%%
-module(amqp10_msg).
-include_lib("amqp10_common/include/amqp10_types.hrl").
-export([from_amqp_records/1,
to_amqp_records/1,
% "read" api
@ -256,12 +258,12 @@ body_bin(#amqp10_msg{body = #'v1_0.amqp_value'{} = Body}) ->
new(DeliveryTag, Body, Settled) when is_binary(Body) ->
#amqp10_msg{transfer = #'v1_0.transfer'{delivery_tag = {binary, DeliveryTag},
settled = Settled,
message_format = {uint, 0}},
message_format = {uint, ?MESSAGE_FORMAT}},
body = [#'v1_0.data'{content = Body}]};
new(DeliveryTag, Body, Settled) -> % TODO: constrain to amqp types
#amqp10_msg{transfer = #'v1_0.transfer'{delivery_tag = {binary, DeliveryTag},
settled = Settled,
message_format = {uint, 0}},
message_format = {uint, ?MESSAGE_FORMAT}},
body = Body}.
%% @doc Create a new settled amqp10 message using the specified delivery tag
@ -322,8 +324,13 @@ set_properties(Props, #amqp10_msg{properties = undefined} = Msg) ->
set_properties(Props, Msg#amqp10_msg{properties = #'v1_0.properties'{}});
set_properties(Props, #amqp10_msg{properties = Current} = Msg) ->
% TODO many fields are `any` types and we need to try to type tag them
P = maps:fold(fun(message_id, V, Acc) when is_binary(V) ->
% message_id can be any type but we restrict it here
P = maps:fold(fun(message_id, {T, _V} = TypeVal, Acc) when T =:= ulong orelse
T =:= uuid orelse
T =:= binary orelse
T =:= uf8 ->
Acc#'v1_0.properties'{message_id = TypeVal};
(message_id, V, Acc) when is_binary(V) ->
%% backward compat clause
Acc#'v1_0.properties'{message_id = utf8(V)};
(user_id, V, Acc) when is_binary(V) ->
Acc#'v1_0.properties'{user_id = {binary, V}};

View File

@ -10,3 +10,10 @@
-type transfer_number() :: sequence_no().
% [2.8.10]
-type sequence_no() :: uint().
% [2.8.1]
-define(AMQP_ROLE_SENDER, false).
-define(AMQP_ROLE_RECEIVER, true).
% [3.2.16]
-define(MESSAGE_FORMAT, 0).

View File

@ -52,6 +52,7 @@
-export_type([
amqp10_ctor/0,
amqp10_type/0,
amqp10_prim/0,
amqp10_described/0
]).

View File

@ -100,14 +100,16 @@ symbolify(FieldName) when is_atom(FieldName) ->
%% A sequence comes as an arbitrary list of values; it's not a
%% composite type.
decode({described, Descriptor, {list, Fields}}) ->
decode({described, Descriptor, {list, Fields} = Type}) ->
case amqp10_framing0:record_for(Descriptor) of
#'v1_0.amqp_sequence'{} ->
#'v1_0.amqp_sequence'{content = [decode(F) || F <- Fields]};
#'v1_0.amqp_value'{} ->
#'v1_0.amqp_value'{content = Type};
Else ->
fill_from_list(Else, Fields)
end;
decode({described, Descriptor, {map, Fields}}) ->
decode({described, Descriptor, {map, Fields} = Type}) ->
case amqp10_framing0:record_for(Descriptor) of
#'v1_0.application_properties'{} ->
#'v1_0.application_properties'{content = decode_map(Fields)};
@ -117,13 +119,15 @@ decode({described, Descriptor, {map, Fields}}) ->
#'v1_0.message_annotations'{content = decode_map(Fields)};
#'v1_0.footer'{} ->
#'v1_0.footer'{content = decode_map(Fields)};
#'v1_0.amqp_value'{} ->
#'v1_0.amqp_value'{content = Type};
Else ->
fill_from_map(Else, Fields)
end;
decode({described, Descriptor, {binary, Field}}) ->
decode({described, Descriptor, {binary, Field} = Type}) ->
case amqp10_framing0:record_for(Descriptor) of
#'v1_0.amqp_value'{} ->
#'v1_0.amqp_value'{content = {binary, Field}};
#'v1_0.amqp_value'{content = Type};
#'v1_0.data'{} ->
#'v1_0.data'{content = Field}
end;

View File

@ -1252,7 +1252,7 @@ rabbitmq_integration_suite(
],
shard_count = 3,
runtime_deps = [
"//deps/amqp10_client:erlang_app",
"//deps/rabbitmq_amqp_client:erlang_app",
],
)
@ -1279,7 +1279,7 @@ rabbitmq_integration_suite(
":test_event_recorder_beam",
],
runtime_deps = [
"//deps/amqp10_client:erlang_app",
"//deps/rabbitmq_amqp_client:erlang_app",
],
)

View File

@ -136,7 +136,7 @@ LOCAL_DEPS = sasl os_mon inets compiler public_key crypto ssl syntax_tools xmerl
BUILD_DEPS = rabbitmq_cli
DEPS = ranch rabbit_common amqp10_common rabbitmq_prelaunch ra sysmon_handler stdout_formatter recon redbug observer_cli osiris syslog systemd seshat khepri khepri_mnesia_migration
TEST_DEPS = rabbitmq_ct_helpers rabbitmq_ct_client_helpers meck proper amqp_client amqp10_client rabbitmq_amqp1_0
TEST_DEPS = rabbitmq_ct_helpers rabbitmq_ct_client_helpers meck proper amqp_client rabbitmq_amqp_client rabbitmq_amqp1_0
PLT_APPS += mnesia

3
deps/rabbit/app.bzl vendored
View File

@ -47,6 +47,7 @@ def all_beam_files(name = "all_beam_files"):
"src/rabbit_access_control.erl",
"src/rabbit_alarm.erl",
"src/rabbit_amqp1_0.erl",
"src/rabbit_amqp_management.erl",
"src/rabbit_amqp_reader.erl",
"src/rabbit_amqp_session.erl",
"src/rabbit_amqp_session_sup.erl",
@ -315,6 +316,7 @@ def all_test_beam_files(name = "all_test_beam_files"):
"src/rabbit_access_control.erl",
"src/rabbit_alarm.erl",
"src/rabbit_amqp1_0.erl",
"src/rabbit_amqp_management.erl",
"src/rabbit_amqp_reader.erl",
"src/rabbit_amqp_session.erl",
"src/rabbit_amqp_session_sup.erl",
@ -598,6 +600,7 @@ def all_srcs(name = "all_srcs"):
"src/rabbit_access_control.erl",
"src/rabbit_alarm.erl",
"src/rabbit_amqp1_0.erl",
"src/rabbit_amqp_management.erl",
"src/rabbit_amqp_reader.erl",
"src/rabbit_amqp_session.erl",
"src/rabbit_amqp_session_sup.erl",

View File

@ -25,9 +25,6 @@
%% [2.8.19]
-define(MIN_MAX_FRAME_1_0_SIZE, 512).
-define(SEND_ROLE, false).
-define(RECV_ROLE, true).
%% for rabbit_event user_authentication_success and user_authentication_failure
-define(AUTH_EVENT_KEYS,
[name,

View File

@ -26,7 +26,8 @@
message/4,
message/5,
from_basic_message/1,
to_091/2
to_091/2,
from_091/2
]).
-import(rabbit_misc,

View File

@ -1709,7 +1709,8 @@ persist_static_configuration() ->
_ ->
?MAX_MSG_SIZE
end,
ok = persistent_term:put(max_message_size, MaxMsgSize).
ok = persistent_term:put(max_message_size, MaxMsgSize),
ok = rabbit_amqp_management:persist_static_configuration().
persist_static_configuration(Params) ->
App = ?MODULE,

View File

@ -189,7 +189,7 @@ check_resource_access(User = #user{username = Username,
check_access(
fun() -> Module:check_resource_access(
auth_user(User, Impl), Resource, Permission, Context) end,
Module, "~s access to ~s refused for user '~s'",
Module, "~s access to ~ts refused for user '~ts'",
[Permission, rabbit_misc:rs(Resource), Username]);
(_, Else) -> Else
end, ok, Modules).
@ -202,7 +202,7 @@ check_topic_access(User = #user{username = Username,
check_access(
fun() -> Module:check_topic_access(
auth_user(User, Impl), Resource, Permission, Context) end,
Module, "~s access to topic '~s' in exchange ~s refused for user '~s'",
Module, "~s access to topic '~ts' in exchange ~ts refused for user '~ts'",
[Permission, maps:get(routing_key, Context), rabbit_misc:rs(Resource), Username]);
(_, Else) -> Else
end, ok, Modules).

View File

@ -0,0 +1,702 @@
-module(rabbit_amqp_management).
-include("rabbit_amqp.hrl").
-include_lib("rabbit_common/include/rabbit.hrl").
-export([persist_static_configuration/0,
handle_request/4]).
-define(DEAD_LETTER_EXCHANGE_KEY, <<"x-dead-letter-exchange">>).
-define(MP_BINDING_URI_PATH_SEGMENT, mp_binding_uri_path_segment).
-type resource_name() :: rabbit_types:exchange_name() | rabbit_types:rabbit_amqqueue_name().
-spec handle_request(binary(), rabbit_types:vhost(), rabbit_types:user(), pid()) -> iolist().
handle_request(Request, Vhost, User, ConnectionPid) ->
ReqSections = amqp10_framing:decode_bin(Request),
?DEBUG("~s Inbound request:~n ~tp",
[?MODULE, [amqp10_framing:pprint(Section) || Section <- ReqSections]]),
{#'v1_0.properties'{
message_id = MessageId,
to = {utf8, HttpRequestTarget},
subject = {utf8, HttpMethod},
%% see Link Pair CS 01 §2.1
%% https://docs.oasis-open.org/amqp/linkpair/v1.0/cs01/linkpair-v1.0-cs01.html#_Toc51331305
reply_to = {utf8, <<"$me">>}},
ReqBody
} = decode_req(ReqSections, {undefined, undefined}),
{StatusCode,
RespBody} = try {PathSegments, QueryMap} = parse_uri(HttpRequestTarget),
handle_http_req(HttpMethod,
PathSegments,
QueryMap,
ReqBody,
Vhost,
User,
ConnectionPid)
catch throw:{?MODULE, StatusCode0, Explanation} ->
rabbit_log:warning("request ~ts ~ts failed: ~ts",
[HttpMethod, HttpRequestTarget, Explanation]),
{StatusCode0, {utf8, Explanation}}
end,
RespProps = #'v1_0.properties'{
subject = {utf8, StatusCode},
%% "To associate a response with a request, the correlation-id value of the response
%% properties MUST be set to the message-id value of the request properties."
%% [HTTP over AMQP WD 06 §5.1]
correlation_id = MessageId},
RespAppProps = #'v1_0.application_properties'{
content = [
{{utf8, <<"http:response">>}, {utf8, <<"1.1">>}}
]},
RespDataSect = #'v1_0.amqp_value'{content = RespBody},
RespSections = [RespProps, RespAppProps, RespDataSect],
[amqp10_framing:encode_bin(Sect) || Sect <- RespSections].
handle_http_req(<<"GET">>,
[<<"queues">>, QNameBinQuoted],
_Query,
null,
Vhost,
_User,
_ConnPid) ->
QNameBin = uri_string:unquote(QNameBinQuoted),
QName = rabbit_misc:r(Vhost, queue, QNameBin),
case rabbit_amqqueue:with(
QName,
fun(Q) ->
{ok, NumMsgs, NumConsumers} = rabbit_amqqueue:stat(Q),
RespPayload = encode_queue(Q, NumMsgs, NumConsumers),
{ok, {<<"200">>, RespPayload}}
end) of
{ok, Result} ->
Result;
{error, not_found} ->
throw(<<"404">>, "~ts not found", [rabbit_misc:rs(QName)]);
{error, {absent, Q, Reason}} ->
absent(Q, Reason)
end;
handle_http_req(HttpMethod = <<"PUT">>,
PathSegments = [<<"queues">>, QNameBinQuoted],
Query,
ReqPayload,
Vhost,
User = #user{username = Username},
ConnPid) ->
#{durable := Durable,
auto_delete := AutoDelete,
exclusive := Exclusive,
arguments := QArgs0
} = decode_queue(ReqPayload),
QNameBin = uri_string:unquote(QNameBinQuoted),
Owner = case Exclusive of
true -> ConnPid;
false -> none
end,
QArgs = rabbit_amqqueue:augment_declare_args(
Vhost, Durable, Exclusive, AutoDelete, QArgs0),
case QNameBin of
<<>> -> throw(<<"400">>, "declare queue with empty name not allowed", []);
_ -> ok
end,
ok = prohibit_cr_lf(QNameBin),
QName = rabbit_misc:r(Vhost, queue, QNameBin),
ok = prohibit_reserved_amq(QName),
ok = check_resource_access(QName, configure, User),
rabbit_core_metrics:queue_declared(QName),
{Q1, NumMsgs, NumConsumers, StatusCode} =
case rabbit_amqqueue:with(
QName,
fun(Q) ->
try rabbit_amqqueue:assert_equivalence(
Q, Durable, AutoDelete, QArgs, Owner) of
ok ->
{ok, Msgs, Consumers} = rabbit_amqqueue:stat(Q),
{ok, {Q, Msgs, Consumers, <<"200">>}}
catch exit:#amqp_error{name = precondition_failed,
explanation = Expl} ->
throw(<<"409">>, Expl, []);
exit:#amqp_error{explanation = Expl} ->
throw(<<"400">>, Expl, [])
end
end) of
{ok, Result} ->
Result;
{error, not_found} ->
ok = check_vhost_queue_limit(QName),
ok = check_dead_letter_exchange(QName, QArgs, User),
case rabbit_amqqueue:declare(
QName, Durable, AutoDelete, QArgs, Owner, Username) of
{new, Q} ->
rabbit_core_metrics:queue_created(QName),
{Q, 0, 0, <<"201">>};
{owner_died, Q} ->
%% Presumably our own days are numbered since the
%% connection has died. Pretend the queue exists though,
%% just so nothing fails.
{Q, 0, 0, <<"201">>};
{absent, Q, Reason} ->
absent(Q, Reason);
{existing, _Q} ->
%% Must have been created in the meantime. Loop around again.
handle_http_req(HttpMethod, PathSegments, Query,
ReqPayload, Vhost, User, ConnPid);
{protocol_error, _ErrorType, Reason, ReasonArgs} ->
throw(<<"400">>, Reason, ReasonArgs)
end;
{error, {absent, Q, Reason}} ->
absent(Q, Reason)
end,
RespPayload = encode_queue(Q1, NumMsgs, NumConsumers),
{StatusCode, RespPayload};
handle_http_req(<<"PUT">>,
[<<"exchanges">>, XNameBinQuoted],
_Query,
ReqPayload,
Vhost,
User = #user{username = Username},
_ConnPid) ->
XNameBin = uri_string:unquote(XNameBinQuoted),
#{type := XTypeBin,
durable := Durable,
auto_delete := AutoDelete,
internal := Internal,
arguments := XArgs
} = decode_exchange(ReqPayload),
XTypeAtom = try rabbit_exchange:check_type(XTypeBin)
catch exit:#amqp_error{explanation = Explanation} ->
throw(<<"400">>, Explanation, [])
end,
XName = rabbit_misc:r(Vhost, exchange, XNameBin),
ok = prohibit_default_exchange(XName),
ok = check_resource_access(XName, configure, User),
X = case rabbit_exchange:lookup(XName) of
{ok, FoundX} ->
FoundX;
{error, not_found} ->
ok = prohibit_cr_lf(XNameBin),
ok = prohibit_reserved_amq(XName),
rabbit_exchange:declare(
XName, XTypeAtom, Durable, AutoDelete,
Internal, XArgs, Username)
end,
try rabbit_exchange:assert_equivalence(
X, XTypeAtom, Durable, AutoDelete, Internal, XArgs) of
ok ->
{<<"204">>, null}
catch exit:#amqp_error{name = precondition_failed,
explanation = Expl} ->
throw(<<"409">>, Expl, [])
end;
handle_http_req(<<"DELETE">>,
[<<"queues">>, QNameBinQuoted, <<"messages">>],
_Query,
null,
Vhost,
User,
ConnPid) ->
QNameBin = uri_string:unquote(QNameBinQuoted),
QName = rabbit_misc:r(Vhost, queue, QNameBin),
ok = check_resource_access(QName, read, User),
try rabbit_amqqueue:with_exclusive_access_or_die(
QName, ConnPid,
fun (Q) ->
case rabbit_queue_type:purge(Q) of
{ok, NumMsgs} ->
RespPayload = purge_or_delete_queue_response(NumMsgs),
{<<"200">>, RespPayload};
{error, not_supported} ->
throw(<<"400">>,
"purge not supported by ~ts",
[rabbit_misc:rs(QName)])
end
end)
catch exit:#amqp_error{explanation = Explanation} ->
throw(<<"400">>, Explanation, [])
end;
handle_http_req(<<"DELETE">>,
[<<"queues">>, QNameBinQuoted],
_Query,
null,
Vhost,
User = #user{username = Username},
ConnPid) ->
QNameBin = uri_string:unquote(QNameBinQuoted),
QName = rabbit_misc:r(Vhost, queue, QNameBin),
ok = prohibit_cr_lf(QNameBin),
ok = check_resource_access(QName, configure, User),
try rabbit_amqqueue:delete_with(QName, ConnPid, false, false, Username, true) of
{ok, NumMsgs} ->
RespPayload = purge_or_delete_queue_response(NumMsgs),
{<<"200">>, RespPayload}
catch exit:#amqp_error{explanation = Explanation} ->
throw(<<"400">>, Explanation, [])
end;
handle_http_req(<<"DELETE">>,
[<<"exchanges">>, XNameBinQuoted],
_Query,
null,
Vhost,
User = #user{username = Username},
_ConnPid) ->
XNameBin = uri_string:unquote(XNameBinQuoted),
XName = rabbit_misc:r(Vhost, exchange, XNameBin),
ok = prohibit_cr_lf(XNameBin),
ok = prohibit_default_exchange(XName),
ok = prohibit_reserved_amq(XName),
ok = check_resource_access(XName, configure, User),
_ = rabbit_exchange:delete(XName, false, Username),
{<<"204">>, null};
handle_http_req(<<"POST">>,
[<<"bindings">>],
_Query,
ReqPayload,
Vhost,
User = #user{username = Username},
ConnPid) ->
#{source := SrcXNameBin,
binding_key := BindingKey,
arguments := Args} = BindingMap = decode_binding(ReqPayload),
{DstKind, DstNameBin} = case BindingMap of
#{destination_queue := Bin} ->
{queue, Bin};
#{destination_exchange := Bin} ->
{exchange, Bin}
end,
SrcXName = rabbit_misc:r(Vhost, exchange, SrcXNameBin),
DstName = rabbit_misc:r(Vhost, DstKind, DstNameBin),
ok = binding_checks(SrcXName, DstName, BindingKey, User),
Binding = #binding{source = SrcXName,
destination = DstName,
key = BindingKey,
args = Args},
ok = binding_action(add, Binding, Username, ConnPid),
{<<"204">>, null};
handle_http_req(<<"DELETE">>,
[<<"bindings">>, BindingSegment],
_Query,
null,
Vhost,
User = #user{username = Username},
ConnPid) ->
{SrcXNameBin,
DstKind,
DstNameBin,
BindingKey,
ArgsHash} = decode_binding_path_segment(BindingSegment),
SrcXName = rabbit_misc:r(Vhost, exchange, SrcXNameBin),
DstName = rabbit_misc:r(Vhost, DstKind, DstNameBin),
ok = binding_checks(SrcXName, DstName, BindingKey, User),
Bindings = rabbit_binding:list_for_source_and_destination(SrcXName, DstName),
case search_binding(BindingKey, ArgsHash, Bindings) of
{value, Binding} ->
ok = binding_action(remove, Binding, Username, ConnPid);
false ->
ok
end,
{<<"204">>, null};
handle_http_req(<<"GET">>,
[<<"bindings">>],
QueryMap = #{<<"src">> := SrcXNameBin,
<<"key">> := Key},
null,
Vhost,
_User,
_ConnPid) ->
{DstKind,
DstNameBin} = case QueryMap of
#{<<"dste">> := DstX} ->
{exchange, DstX};
#{<<"dstq">> := DstQ} ->
{queue, DstQ};
_ ->
throw(<<"400">>,
"missing 'dste' or 'dstq' in query: ~tp",
QueryMap)
end,
SrcXName = rabbit_misc:r(Vhost, exchange, SrcXNameBin),
DstName = rabbit_misc:r(Vhost, DstKind, DstNameBin),
Bindings0 = rabbit_binding:list_for_source_and_destination(SrcXName, DstName),
Bindings = [B || B = #binding{key = K} <- Bindings0, K =:= Key],
RespPayload = encode_bindings(Bindings),
{<<"200">>, RespPayload}.
decode_queue({map, KVList}) ->
M = lists:foldl(
fun({{utf8, <<"durable">>}, V}, Acc)
when is_boolean(V) ->
Acc#{durable => V};
({{utf8, <<"exclusive">>}, V}, Acc)
when is_boolean(V) ->
Acc#{exclusive => V};
({{utf8, <<"auto_delete">>}, V}, Acc)
when is_boolean(V) ->
Acc#{auto_delete => V};
({{utf8, <<"arguments">>}, Args}, Acc) ->
Acc#{arguments => args_amqp_to_amqpl(Args)};
(Prop, _Acc) ->
throw(<<"400">>, "bad queue property ~tp", [Prop])
end, #{}, KVList),
Defaults = #{durable => true,
exclusive => false,
auto_delete => false,
arguments => []},
maps:merge(Defaults, M).
encode_queue(Q, NumMsgs, NumConsumers) ->
#resource{name = QNameBin} = amqqueue:get_name(Q),
Vhost = amqqueue:get_vhost(Q),
Durable = amqqueue:is_durable(Q),
AutoDelete = amqqueue:is_auto_delete(Q),
Exclusive = amqqueue:is_exclusive(Q),
QType = amqqueue:get_type(Q),
QArgs091 = amqqueue:get_arguments(Q),
QArgs = args_amqpl_to_amqp(QArgs091),
{Leader, Replicas} = queue_topology(Q),
KVList0 = [
{{utf8, <<"message_count">>}, {ulong, NumMsgs}},
{{utf8, <<"consumer_count">>}, {uint, NumConsumers}},
{{utf8, <<"name">>}, {utf8, QNameBin}},
{{utf8, <<"vhost">>}, {utf8, Vhost}},
{{utf8, <<"durable">>}, {boolean, Durable}},
{{utf8, <<"auto_delete">>}, {boolean, AutoDelete}},
{{utf8, <<"exclusive">>}, {boolean, Exclusive}},
{{utf8, <<"type">>}, {utf8, rabbit_queue_type:to_binary(QType)}},
{{utf8, <<"arguments">>}, QArgs}
],
KVList1 = if is_list(Replicas) ->
[{{utf8, <<"replicas">>},
{array, utf8, [{utf8, atom_to_binary(R)} || R <- Replicas]}
} | KVList0];
Replicas =:= undefined ->
KVList0
end,
KVList = if is_atom(Leader) ->
[{{utf8, <<"leader">>},
{utf8, atom_to_binary(Leader)}
} | KVList1];
Leader =:= undefined ->
KVList1
end,
{map, KVList}.
%% The returned Replicas contain both online and offline replicas.
-spec queue_topology(amqqueue:amqqueue()) ->
{Leader :: undefined | node(), Replicas :: undefined | [node(),...]}.
queue_topology(Q) ->
case amqqueue:get_type(Q) of
rabbit_quorum_queue ->
[{leader, Leader0},
{members, Members}] = rabbit_queue_type:info(Q, [leader, members]),
Leader = case Leader0 of
'' -> undefined;
_ -> Leader0
end,
{Leader, Members};
rabbit_stream_queue ->
#{name := StreamId} = amqqueue:get_type_state(Q),
case rabbit_stream_coordinator:members(StreamId) of
{ok, Members} ->
maps:fold(fun(Node, {_Pid, writer}, {_, Replicas}) ->
{Node, [Node | Replicas]};
(Node, {_Pid, replica}, {Writer, Replicas}) ->
{Writer, [Node | Replicas]}
end, {undefined, []}, Members);
{error, _} ->
{undefined, undefined}
end;
_ ->
Pid = amqqueue:get_pid(Q),
Node = node(Pid),
{Node, [Node]}
end.
decode_exchange({map, KVList}) ->
M = lists:foldl(
fun({{utf8, <<"durable">>}, V}, Acc)
when is_boolean(V) ->
Acc#{durable => V};
({{utf8, <<"auto_delete">>}, V}, Acc)
when is_boolean(V) ->
Acc#{auto_delete => V};
({{utf8, <<"type">>}, {utf8, V}}, Acc) ->
Acc#{type => V};
({{utf8, <<"internal">>}, V}, Acc)
when is_boolean(V) ->
Acc#{internal => V};
({{utf8, <<"arguments">>}, Args}, Acc) ->
Acc#{arguments => args_amqp_to_amqpl(Args)};
(Prop, _Acc) ->
throw(<<"400">>, "bad exchange property ~tp", [Prop])
end, #{}, KVList),
Defaults = #{durable => true,
auto_delete => false,
type => <<"direct">>,
internal => false,
arguments => []},
maps:merge(Defaults, M).
decode_binding({map, KVList}) ->
lists:foldl(
fun({{utf8, <<"source">>}, {utf8, V}}, Acc) ->
Acc#{source => V};
({{utf8, <<"destination_queue">>}, {utf8, V}}, Acc) ->
Acc#{destination_queue => V};
({{utf8, <<"destination_exchange">>}, {utf8, V}}, Acc) ->
Acc#{destination_exchange => V};
({{utf8, <<"binding_key">>}, {utf8, V}}, Acc) ->
Acc#{binding_key => V};
({{utf8, <<"arguments">>}, Args}, Acc) ->
Acc#{arguments => args_amqp_to_amqpl(Args)};
(Field, _Acc) ->
throw(<<"400">>, "bad binding field ~tp", [Field])
end, #{}, KVList).
encode_bindings(Bindings) ->
Bs = lists:map(
fun(#binding{source = #resource{name = SrcName},
key = BindingKey,
destination = #resource{kind = DstKind,
name = DstName},
args = Args091}) ->
DstKindBin = case DstKind of
queue -> <<"queue">>;
exchange -> <<"exchange">>
end,
Args = args_amqpl_to_amqp(Args091),
Location = compose_binding_uri(
SrcName, DstKind, DstName, BindingKey, Args091),
KVList = [
{{utf8, <<"source">>}, {utf8, SrcName}},
{{utf8, <<"destination_", DstKindBin/binary>>}, {utf8, DstName}},
{{utf8, <<"binding_key">>}, {utf8, BindingKey}},
{{utf8, <<"arguments">>}, Args},
{{utf8, <<"location">>}, {utf8, Location}}
],
{map, KVList}
end, Bindings),
{list, Bs}.
args_amqp_to_amqpl({map, KVList}) ->
lists:map(fun({{T, Key}, TypeVal})
when T =:= utf8 orelse
T =:= symbol ->
mc_amqpl:to_091(Key, TypeVal);
(Arg) ->
throw(<<"400">>,
"unsupported argument ~tp",
[Arg])
end, KVList).
args_amqpl_to_amqp(Args) ->
{map, [{{utf8, K}, mc_amqpl:from_091(T, V)} || {K, T, V} <- Args]}.
decode_req([], Acc) ->
Acc;
decode_req([#'v1_0.properties'{} = P | Rem], Acc) ->
decode_req(Rem, setelement(1, Acc, P));
decode_req([#'v1_0.amqp_value'{content = C} | Rem], Acc) ->
decode_req(Rem, setelement(2, Acc, C));
decode_req([_IgnoreSection | Rem], Acc) ->
decode_req(Rem, Acc).
parse_uri(Uri) ->
case uri_string:normalize(Uri, [return_map]) of
UriMap = #{path := Path} ->
[<<>> | Segments] = binary:split(Path, <<"/">>, [global]),
QueryMap = case maps:find(query, UriMap) of
{ok, Query} ->
case uri_string:dissect_query(Query) of
QueryList
when is_list(QueryList) ->
maps:from_list(QueryList);
{error, Atom, Term} ->
throw(<<"400">>,
"failed to dissect query '~ts': ~s ~tp",
[Query, Atom, Term])
end;
error ->
#{}
end,
{Segments, QueryMap};
{error, Atom, Term} ->
throw(<<"400">>,
"failed to normalize URI '~ts': ~s ~tp",
[Uri, Atom, Term])
end.
compose_binding_uri(Src, DstKind, Dst, Key, Args) ->
SrcQ = uri_string:quote(Src),
DstQ = uri_string:quote(Dst),
KeyQ = uri_string:quote(Key),
ArgsHash = args_hash(Args),
DstChar = destination_kind_to_char(DstKind),
<<"/bindings/src=", SrcQ/binary,
";dst", DstChar, $=, DstQ/binary,
";key=", KeyQ/binary,
";args=", ArgsHash/binary>>.
-spec persist_static_configuration() -> ok.
persist_static_configuration() ->
%% This regex matches for example binding:
%% src=e1;dstq=q2;key=my-key;args=
%% Source, destination, and binding key values must be percent encoded.
%% Binding args use the URL safe Base 64 Alphabet: https://datatracker.ietf.org/doc/html/rfc4648#section-5
{ok, MP} = re:compile(
<<"^src=([0-9A-Za-z\-.\_\~%]+);dst([eq])=([0-9A-Za-z\-.\_\~%]+);",
"key=([0-9A-Za-z\-.\_\~%]*);args=([0-9A-Za-z\-\_]*)$">>),
ok = persistent_term:put(?MP_BINDING_URI_PATH_SEGMENT, MP).
decode_binding_path_segment(Segment) ->
MP = persistent_term:get(?MP_BINDING_URI_PATH_SEGMENT),
case re:run(Segment, MP, [{capture, all_but_first, binary}]) of
{match, [SrcQ, <<DstKindChar>>, DstQ, KeyQ, ArgsHash]} ->
Src = uri_string:unquote(SrcQ),
Dst = uri_string:unquote(DstQ),
Key = uri_string:unquote(KeyQ),
DstKind = destination_char_to_kind(DstKindChar),
{Src, DstKind, Dst, Key, ArgsHash};
nomatch ->
throw(<<"400">>, "bad binding path segment '~s'", [Segment])
end.
destination_kind_to_char(exchange) -> $e;
destination_kind_to_char(queue) -> $q.
destination_char_to_kind($e) -> exchange;
destination_char_to_kind($q) -> queue.
search_binding(BindingKey, ArgsHash, Bindings) ->
lists:search(fun(#binding{key = Key,
args = Args})
when Key =:= BindingKey ->
args_hash(Args) =:= ArgsHash;
(_) ->
false
end, Bindings).
-spec args_hash(rabbit_framing:amqp_table()) -> binary().
args_hash([]) ->
<<>>;
args_hash(Args)
when is_list(Args) ->
%% Args is already sorted.
Bin = <<(erlang:phash2(Args, 1 bsl 32)):32>>,
base64:encode(Bin, #{mode => urlsafe,
padding => false}).
-spec binding_checks(rabbit_types:exchange_name(),
resource_name(),
rabbit_types:binding_key(),
rabbit_types:user()) -> ok.
binding_checks(SrcXName, DstName, BindingKey, User) ->
lists:foreach(fun(#resource{name = NameBin} = Name) ->
ok = prohibit_default_exchange(Name),
ok = prohibit_cr_lf(NameBin)
end, [SrcXName, DstName]),
ok = check_resource_access(DstName, write, User),
ok = check_resource_access(SrcXName, read, User),
case rabbit_exchange:lookup(SrcXName) of
{ok, SrcX} ->
rabbit_amqp_session:check_read_permitted_on_topic(SrcX, User, BindingKey);
{error, not_found} ->
ok
end.
binding_action(Action, Binding, Username, ConnPid) ->
try rabbit_channel:binding_action(Action, Binding, Username, ConnPid)
catch exit:#amqp_error{explanation = Explanation} ->
throw(<<"400">>, Explanation, [])
end.
purge_or_delete_queue_response(NumMsgs) ->
{map, [{{utf8, <<"message_count">>}, {ulong, NumMsgs}}]}.
prohibit_cr_lf(NameBin) ->
case binary:match(NameBin, [<<"\n">>, <<"\r">>]) of
nomatch ->
ok;
_Found ->
throw(<<"400">>,
<<"Bad name '~ts': line feed and carriage return characters not allowed">>,
[NameBin])
end.
prohibit_default_exchange(#resource{kind = exchange,
name = <<"">>}) ->
throw(<<"403">>, <<"operation not permitted on the default exchange">>, []);
prohibit_default_exchange(_) ->
ok.
-spec prohibit_reserved_amq(resource_name()) -> ok.
prohibit_reserved_amq(Res = #resource{name = <<"amq.", _/binary>>}) ->
throw(<<"403">>,
"~ts starts with reserved prefix 'amq.'",
[rabbit_misc:rs(Res)]);
prohibit_reserved_amq(#resource{}) ->
ok.
-spec check_resource_access(resource_name(),
rabbit_types:permission_atom(),
rabbit_types:user()) -> ok.
check_resource_access(Resource, Perm, User) ->
try rabbit_access_control:check_resource_access(User, Resource, Perm, #{})
catch exit:#amqp_error{name = access_refused,
explanation = Explanation} ->
%% For authorization failures, let's be more strict: Close the entire
%% AMQP session instead of only returning an HTTP Status Code 403.
rabbit_amqp_util:protocol_error(
?V_1_0_AMQP_ERROR_UNAUTHORIZED_ACCESS, Explanation, [])
end.
check_vhost_queue_limit(QName = #resource{virtual_host = Vhost}) ->
case rabbit_vhost_limit:is_over_queue_limit(Vhost) of
false ->
ok;
{true, Limit} ->
throw(<<"403">>,
"refused to declare ~ts because vhost queue limit ~b is reached",
[rabbit_misc:rs(QName), Limit])
end.
check_dead_letter_exchange(QName = #resource{virtual_host = Vhost}, QArgs, User) ->
case rabbit_misc:r_arg(Vhost, exchange, QArgs, ?DEAD_LETTER_EXCHANGE_KEY) of
undefined ->
ok;
{error, {invalid_type, Type}} ->
throw(<<"400">>,
"invalid type '~ts' for arg '~s'",
[Type, ?DEAD_LETTER_EXCHANGE_KEY]);
DLX ->
ok = check_resource_access(QName, read, User),
ok = check_resource_access(DLX, write, User)
end.
-spec absent(amqqueue:amqqueue(),
rabbit_amqqueue:absent_reason()) ->
no_return().
absent(Queue, Reason) ->
{'EXIT',
#amqp_error{explanation = Explanation}
} = catch rabbit_amqqueue:absent(Queue, Reason),
throw(<<"400">>, Explanation, []).
-spec throw(binary(), io:format(), [term()]) -> no_return().
throw(StatusCode, Format, Data) ->
Reason0 = lists:flatten(io_lib:format(Format, Data)),
Reason = unicode:characters_to_binary(Reason0),
throw({?MODULE, StatusCode, Reason}).

View File

@ -127,9 +127,10 @@ system_code_change(Misc, _Module, _OldVsn, _Extra) ->
{ok, Misc}.
server_properties() ->
%% The atom doesn't match anything, it's just "not 0-9-1".
Raw = lists:keydelete(<<"capabilities">>, 1, rabbit_reader:server_properties(amqp_1_0)),
{map, [{{symbol, K}, {utf8, V}} || {K, longstr, V} <- Raw]}.
Props0 = rabbit_reader:server_properties(amqp_1_0),
Props1 = [{{symbol, K}, {utf8, V}} || {K, longstr, V} <- Props0],
Props = [{{symbol, <<"node">>}, {utf8, atom_to_binary(node())}} | Props1],
{map, Props}.
%%--------------------------------------------------------------------------
@ -491,6 +492,7 @@ handle_1_0_connection_frame(
%% "the value in idle-time-out SHOULD be half the peer's actual timeout threshold" [2.4.5]
idle_time_out = {uint, ReceiveTimeoutMillis div 2},
container_id = {utf8, rabbit_nodes:cluster_name()},
offered_capabilities = {array, symbol, [{symbol, <<"LINK_PAIR_V1_0">>}]},
properties = server_properties()}),
State;
handle_1_0_connection_frame(#'v1_0.close'{}, State0) ->

File diff suppressed because it is too large Load Diff

View File

@ -13,7 +13,7 @@
-spec protocol_error(term(), io:format(), [term()]) ->
no_return().
protocol_error(Condition, Msg, Args) ->
Description = list_to_binary(lists:flatten(io_lib:format(Msg, Args))),
Description = unicode:characters_to_binary(lists:flatten(io_lib:format(Msg, Args))),
Reason = #'v1_0.error'{condition = Condition,
description = {utf8, Description}},
exit(Reason).

View File

@ -66,7 +66,8 @@
-export([list_queue_states/1]).
%% Mgmt HTTP API refactor
-export([handle_method/6]).
-export([handle_method/6,
binding_action/4]).
-import(rabbit_misc, [maps_put_truthy/3]).
@ -1819,9 +1820,10 @@ queue_down_consumer_action(CTag, CMap) ->
_ -> {recover, ConsumeSpec}
end.
binding_action(Fun, SourceNameBin0, DestinationType, DestinationNameBin0,
RoutingKey, Arguments, VHostPath, ConnPid, AuthzContext,
#user{username = Username} = User) ->
binding_action_with_checks(
Action, SourceNameBin0, DestinationType, DestinationNameBin0,
RoutingKey, Arguments, VHostPath, ConnPid, AuthzContext,
#user{username = Username} = User) ->
ExchangeNameBin = strip_cr_lf(SourceNameBin0),
DestinationNameBin = strip_cr_lf(DestinationNameBin0),
DestinationName = name_to_resource(DestinationType, DestinationNameBin, VHostPath),
@ -1835,18 +1837,27 @@ binding_action(Fun, SourceNameBin0, DestinationType, DestinationNameBin0,
{ok, Exchange} ->
check_read_permitted_on_topic(Exchange, User, RoutingKey, AuthzContext)
end,
case Fun(#binding{source = ExchangeName,
destination = DestinationName,
key = RoutingKey,
args = Arguments},
fun (_X, Q) when ?is_amqqueue(Q) ->
try rabbit_amqqueue:check_exclusive_access(Q, ConnPid)
catch exit:Reason -> {error, Reason}
end;
(_X, #exchange{}) ->
ok
end,
Username) of
Binding = #binding{source = ExchangeName,
destination = DestinationName,
key = RoutingKey,
args = Arguments},
binding_action(Action, Binding, Username, ConnPid).
-spec binding_action(add | remove,
rabbit_types:binding(),
rabbit_types:username(),
pid()) -> ok.
binding_action(Action, Binding, Username, ConnPid) ->
case rabbit_binding:Action(
Binding,
fun (_X, Q) when ?is_amqqueue(Q) ->
try rabbit_amqqueue:check_exclusive_access(Q, ConnPid)
catch exit:Reason -> {error, Reason}
end;
(_X, #exchange{}) ->
ok
end,
Username) of
{error, {resources_missing, [{not_found, Name} | _]}} ->
rabbit_amqqueue:not_found(Name);
{error, {resources_missing, [{absent, Q, Reason} | _]}} ->
@ -2375,33 +2386,33 @@ handle_method(#'exchange.bind'{destination = DestinationNameBin,
routing_key = RoutingKey,
arguments = Arguments},
ConnPid, AuthzContext, _CollectorId, VHostPath, User) ->
binding_action(fun rabbit_binding:add/3,
SourceNameBin, exchange, DestinationNameBin,
RoutingKey, Arguments, VHostPath, ConnPid, AuthzContext, User);
binding_action_with_checks(
add, SourceNameBin, exchange, DestinationNameBin,
RoutingKey, Arguments, VHostPath, ConnPid, AuthzContext, User);
handle_method(#'exchange.unbind'{destination = DestinationNameBin,
source = SourceNameBin,
routing_key = RoutingKey,
arguments = Arguments},
ConnPid, AuthzContext, _CollectorId, VHostPath, User) ->
binding_action(fun rabbit_binding:remove/3,
SourceNameBin, exchange, DestinationNameBin,
RoutingKey, Arguments, VHostPath, ConnPid, AuthzContext, User);
binding_action_with_checks(
remove, SourceNameBin, exchange, DestinationNameBin,
RoutingKey, Arguments, VHostPath, ConnPid, AuthzContext, User);
handle_method(#'queue.unbind'{queue = QueueNameBin,
exchange = ExchangeNameBin,
routing_key = RoutingKey,
arguments = Arguments},
ConnPid, AuthzContext, _CollectorId, VHostPath, User) ->
binding_action(fun rabbit_binding:remove/3,
ExchangeNameBin, queue, QueueNameBin,
RoutingKey, Arguments, VHostPath, ConnPid, AuthzContext, User);
binding_action_with_checks(
remove, ExchangeNameBin, queue, QueueNameBin,
RoutingKey, Arguments, VHostPath, ConnPid, AuthzContext, User);
handle_method(#'queue.bind'{queue = QueueNameBin,
exchange = ExchangeNameBin,
routing_key = RoutingKey,
arguments = Arguments},
ConnPid, AuthzContext, _CollectorId, VHostPath, User) ->
binding_action(fun rabbit_binding:add/3,
ExchangeNameBin, queue, QueueNameBin,
RoutingKey, Arguments, VHostPath, ConnPid, AuthzContext, User);
binding_action_with_checks(
add, ExchangeNameBin, queue, QueueNameBin,
RoutingKey, Arguments, VHostPath, ConnPid, AuthzContext, User);
%% Note that all declares to these are effectively passive. If it
%% exists it by definition has one consumer.
handle_method(#'queue.declare'{queue = <<"amq.rabbitmq.reply-to",
@ -2433,6 +2444,7 @@ handle_method(#'queue.declare'{queue = QueueNameBin,
Args0),
StrippedQueueNameBin = strip_cr_lf(QueueNameBin),
Durable = DurableDeclare andalso not ExclusiveDeclare,
Kind = queue,
ActualNameBin = case StrippedQueueNameBin of
<<>> ->
case rabbit_amqqueue:is_server_named_allowed(Args) of
@ -2444,9 +2456,9 @@ handle_method(#'queue.declare'{queue = QueueNameBin,
"Cannot declare a server-named queue for type ~tp",
[rabbit_amqqueue:get_queue_type(Args)])
end;
Other -> check_name('queue', Other)
Other -> check_name(Kind, Other)
end,
QueueName = rabbit_misc:r(VHostPath, queue, ActualNameBin),
QueueName = rabbit_misc:r(VHostPath, Kind, ActualNameBin),
check_configure_permitted(QueueName, User, AuthzContext),
rabbit_core_metrics:queue_declared(QueueName),
case rabbit_amqqueue:with(
@ -2565,7 +2577,7 @@ handle_method(#'queue.purge'{queue = QueueNameBin},
[rabbit_misc:rs(amqqueue:get_name(Q))])
end
end);
handle_method(#'exchange.declare'{exchange = ExchangeNameBin,
handle_method(#'exchange.declare'{exchange = XNameBin,
type = TypeNameBin,
passive = false,
durable = Durable,
@ -2575,13 +2587,14 @@ handle_method(#'exchange.declare'{exchange = ExchangeNameBin,
_ConnPid, AuthzContext, _CollectorPid, VHostPath,
#user{username = Username} = User) ->
CheckedType = rabbit_exchange:check_type(TypeNameBin),
ExchangeName = rabbit_misc:r(VHostPath, exchange, strip_cr_lf(ExchangeNameBin)),
XNameBinStripped = strip_cr_lf(XNameBin),
ExchangeName = rabbit_misc:r(VHostPath, exchange, XNameBinStripped),
check_not_default_exchange(ExchangeName),
check_configure_permitted(ExchangeName, User, AuthzContext),
X = case rabbit_exchange:lookup(ExchangeName) of
{ok, FoundX} -> FoundX;
{error, not_found} ->
_ = check_name('exchange', strip_cr_lf(ExchangeNameBin)),
_ = check_name('exchange', XNameBinStripped),
AeKey = <<"alternate-exchange">>,
case rabbit_misc:r_arg(VHostPath, exchange, Args, AeKey) of
undefined -> ok;

View File

@ -358,7 +358,7 @@ update_in_khepri_tx(Name, Fun) ->
-spec create_or_get(Exchange) -> Ret when
Exchange :: rabbit_types:exchange(),
Ret :: {new, Exchange} | {existing, Exchange} | {error, any()}.
Ret :: {new, Exchange} | {existing, Exchange}.
%% @doc Writes an exchange record if it doesn't exist already or returns
%% the existing one.
%%

View File

@ -123,9 +123,7 @@ declare(XName, Type, Durable, AutoDelete, Internal, Args, Username) ->
rabbit_event:notify(exchange_created, info(Exchange)),
Exchange;
{existing, Exchange} ->
Exchange;
Err ->
Err
Exchange
end;
_ ->
rabbit_log:warning("ignoring exchange.declare for exchange ~tp,

View File

@ -18,6 +18,7 @@
close/1,
discover/1,
feature_flag_name/1,
to_binary/1,
default/0,
is_enabled/1,
is_compatible/4,
@ -277,6 +278,16 @@ feature_flag_name(_) ->
default() ->
rabbit_classic_queue.
-spec to_binary(module()) -> binary().
to_binary(rabbit_classic_queue) ->
<<"classic">>;
to_binary(rabbit_quorum_queue) ->
<<"quorum">>;
to_binary(rabbit_stream_queue) ->
<<"stream">>;
to_binary(Other) ->
atom_to_binary(Other).
%% is a specific queue type implementation enabled
-spec is_enabled(module()) -> boolean().
is_enabled(Type) ->

View File

@ -231,7 +231,7 @@ server_properties(Protocol) ->
NormalizedConfigServerProps =
[{<<"capabilities">>, table, server_capabilities(Protocol)} |
[case X of
{KeyAtom, Value} -> {list_to_binary(atom_to_list(KeyAtom)),
{KeyAtom, Value} -> {atom_to_binary(KeyAtom),
longstr,
maybe_list_to_binary(Value)};
{BinKey, Type, Value} -> {BinKey, Type, Value}

View File

@ -47,7 +47,25 @@ groups() ->
vhost_absent,
vhost_connection_limit,
user_connection_limit,
vhost_queue_limit
vhost_queue_limit,
%% AMQP Management operations against HTTP API v2
declare_exchange,
delete_exchange,
declare_queue,
declare_queue_dlx_queue,
declare_queue_dlx_exchange,
declare_queue_vhost_queue_limit,
delete_queue,
purge_queue,
bind_queue_source,
bind_queue_destination,
bind_exchange_source,
bind_exchange_destination,
bind_to_topic_exchange,
unbind_queue_source,
unbind_queue_target,
unbind_from_topic_exchange
]
}
].
@ -537,6 +555,265 @@ vhost_queue_limit(Config) ->
ok = close_connection_sync(C2),
ok = rabbit_ct_broker_helpers:clear_vhost_limit(Config, 0, Vhost).
declare_exchange(Config) ->
{Conn, _Session, LinkPair} = init_pair(Config),
XName = <<"📮"/utf8>>,
ExpectedErr = error_unauthorized(
<<"configure access to exchange '", XName/binary,
"' in vhost 'test vhost' refused for user 'test user'">>),
?assertEqual({error, {session_ended, ExpectedErr}},
rabbitmq_amqp_client:declare_exchange(LinkPair, XName, #{})),
ok = close_connection_sync(Conn).
delete_exchange(Config) ->
{Conn1, _, LinkPair1} = init_pair(Config),
XName = <<"📮"/utf8>>,
ok = set_permissions(Config, XName, <<>>, <<>>),
ok = rabbitmq_amqp_client:declare_exchange(LinkPair1, XName, #{}),
ok = clear_permissions(Config),
ExpectedErr = error_unauthorized(
<<"configure access to exchange '", XName/binary,
"' in vhost 'test vhost' refused for user 'test user'">>),
?assertEqual({error, {session_ended, ExpectedErr}},
rabbitmq_amqp_client:delete_exchange(LinkPair1, XName)),
ok = close_connection_sync(Conn1),
ok = set_permissions(Config, XName, <<>>, <<>>),
Init = {_, _, LinkPair2} = init_pair(Config),
ok = rabbitmq_amqp_client:delete_exchange(LinkPair2, XName),
ok = cleanup_pair(Init).
declare_queue(Config) ->
{Conn, _, LinkPair} = init_pair(Config),
QName = <<"🍿"/utf8>>,
ExpectedErr = error_unauthorized(
<<"configure access to queue '", QName/binary,
"' in vhost 'test vhost' refused for user 'test user'">>),
?assertEqual({error, {session_ended, ExpectedErr}},
rabbitmq_amqp_client:declare_queue(LinkPair, QName, #{})),
ok = close_connection_sync(Conn).
declare_queue_dlx_queue(Config) ->
{Conn, _, LinkPair} = init_pair(Config),
QName = <<"🍿"/utf8>>,
DlxName = <<"📥"/utf8>>,
QProps = #{arguments => #{<<"x-dead-letter-exchange">> => {utf8, DlxName}}},
%% missing read permission to queue
ok = set_permissions(Config, QName, DlxName, <<>>),
ExpectedErr = error_unauthorized(
<<"read access to queue '", QName/binary,
"' in vhost 'test vhost' refused for user 'test user'">>),
?assertEqual({error, {session_ended, ExpectedErr}},
rabbitmq_amqp_client:declare_queue(LinkPair, QName, QProps)),
ok = close_connection_sync(Conn).
declare_queue_dlx_exchange(Config) ->
{Conn, _, LinkPair} = init_pair(Config),
QName = <<"🍿"/utf8>>,
DlxName = <<"📥"/utf8>>,
QProps = #{arguments => #{<<"x-dead-letter-exchange">> => {utf8, DlxName}}},
%% missing write permission to dead letter exchange
ok = set_permissions(Config, QName, <<>>, QName),
ExpectedErr = error_unauthorized(
<<"write access to exchange '", DlxName/binary,
"' in vhost 'test vhost' refused for user 'test user'">>),
?assertEqual({error, {session_ended, ExpectedErr}},
rabbitmq_amqp_client:declare_queue(LinkPair, QName, QProps)),
ok = close_connection_sync(Conn).
declare_queue_vhost_queue_limit(Config) ->
QName = <<"🍿"/utf8>>,
ok = set_permissions(Config, QName, <<>>, <<>>),
Vhost = proplists:get_value(test_vhost, Config),
ok = rabbit_ct_broker_helpers:set_vhost_limit(Config, 0, Vhost, max_queues, 0),
Init = {_, _, LinkPair} = init_pair(Config),
{error, Resp} = rabbitmq_amqp_client:declare_queue(LinkPair, QName, #{}),
?assertMatch(#{subject := <<"403">>}, amqp10_msg:properties(Resp)),
?assertEqual(
#'v1_0.amqp_value'{
content = {utf8, <<"refused to declare queue '", QName/binary, "' in vhost 'test vhost' ",
"because vhost queue limit 0 is reached">>}},
amqp10_msg:body(Resp)),
ok = cleanup_pair(Init),
ok = rabbit_ct_broker_helpers:clear_vhost_limit(Config, 0, Vhost).
delete_queue(Config) ->
{Conn, _, LinkPair} = init_pair(Config),
QName = <<"🍿"/utf8>>,
ok = set_permissions(Config, QName, <<>>, <<>>),
{ok, _} = rabbitmq_amqp_client:declare_queue(LinkPair, QName, #{}),
ok = clear_permissions(Config),
ExpectedErr = error_unauthorized(
<<"configure access to queue '", QName/binary,
"' in vhost 'test vhost' refused for user 'test user'">>),
?assertEqual({error, {session_ended, ExpectedErr}},
rabbitmq_amqp_client:delete_queue(LinkPair, QName)),
ok = close_connection_sync(Conn).
purge_queue(Config) ->
{Conn, _, LinkPair} = init_pair(Config),
QName = <<"🍿"/utf8>>,
%% missing read permission to queue
ok = set_permissions(Config, QName, <<>>, <<>>),
{ok, _} = rabbitmq_amqp_client:declare_queue(LinkPair, QName, #{}),
ExpectedErr = error_unauthorized(
<<"read access to queue '", QName/binary,
"' in vhost 'test vhost' refused for user 'test user'">>),
?assertEqual({error, {session_ended, ExpectedErr}},
rabbitmq_amqp_client:purge_queue(LinkPair, QName)),
ok = close_connection_sync(Conn).
bind_queue_source(Config) ->
{Conn, _, LinkPair} = init_pair(Config),
QName = atom_to_binary(?FUNCTION_NAME),
%% missing read permission to source exchange
ok = set_permissions(Config, QName, QName, QName),
{ok, #{}} = rabbitmq_amqp_client:declare_queue(LinkPair, QName, #{}),
XName = <<"amq.direct">>,
ExpectedErr = error_unauthorized(
<<"read access to exchange '", XName/binary,
"' in vhost 'test vhost' refused for user 'test user'">>),
?assertEqual({error, {session_ended, ExpectedErr}},
rabbitmq_amqp_client:bind_queue(LinkPair, QName, XName, <<"key">>, #{})),
ok = close_connection_sync(Conn).
bind_queue_destination(Config) ->
{Conn, _, LinkPair} = init_pair(Config),
QName = <<"my 🐇"/utf8>>,
XName = <<"amq.direct">>,
%% missing write permission to destination queue
ok = set_permissions(Config, QName, <<>>, XName),
{ok, #{}} = rabbitmq_amqp_client:declare_queue(LinkPair, QName, #{}),
ExpectedErr = error_unauthorized(
<<"write access to queue '", QName/binary,
"' in vhost 'test vhost' refused for user 'test user'">>),
?assertEqual({error, {session_ended, ExpectedErr}},
rabbitmq_amqp_client:bind_queue(LinkPair, QName, XName, <<"key">>, #{})),
ok = close_connection_sync(Conn).
bind_exchange_source(Config) ->
{Conn, _, LinkPair} = init_pair(Config),
SrcXName = <<"amq.fanout">>,
DstXName = <<"amq.direct">>,
%% missing read permission to source exchange
ok = set_permissions(Config, <<>>, DstXName, <<>>),
ExpectedErr = error_unauthorized(
<<"read access to exchange '", SrcXName/binary,
"' in vhost 'test vhost' refused for user 'test user'">>),
?assertEqual({error, {session_ended, ExpectedErr}},
rabbitmq_amqp_client:bind_exchange(LinkPair, DstXName, SrcXName, <<"key">>, #{})),
ok = close_connection_sync(Conn).
bind_exchange_destination(Config) ->
{Conn, _, LinkPair} = init_pair(Config),
SrcXName = <<"amq.fanout">>,
DstXName = <<"amq.direct">>,
%% missing write permission to destination exchange
ok = set_permissions(Config, <<>>, <<>>, SrcXName),
ExpectedErr = error_unauthorized(
<<"write access to exchange '", DstXName/binary,
"' in vhost 'test vhost' refused for user 'test user'">>),
?assertEqual({error, {session_ended, ExpectedErr}},
rabbitmq_amqp_client:bind_exchange(LinkPair, DstXName, SrcXName, <<"key">>, #{})),
ok = close_connection_sync(Conn).
bind_to_topic_exchange(Config) ->
{Conn, _, LinkPair} = init_pair(Config),
SrcXName = <<"amq.topic">>,
DstXName = <<"amq.direct">>,
Topic = <<"a.b.🐇"/utf8>>,
User = ?config(test_user, Config),
Vhost = ?config(test_vhost, Config),
ok = rabbit_ct_broker_helpers:set_full_permissions(Config, User, Vhost),
%% missing read permission to Topic
ok = set_topic_permissions(Config, SrcXName, <<".*">>, <<"wrong.topic">>),
ExpectedErr = error_unauthorized(
<<"read access to topic '", Topic/binary,
"' in exchange 'amq.topic' in vhost 'test vhost' refused for user 'test user'">>),
?assertEqual({error, {session_ended, ExpectedErr}},
rabbitmq_amqp_client:bind_exchange(LinkPair, DstXName, SrcXName, Topic, #{})),
ok = close_connection_sync(Conn).
unbind_queue_source(Config) ->
{Conn, _, LinkPair} = init_pair(Config),
QName = BindingKey = atom_to_binary(?FUNCTION_NAME),
XName = <<"amq.direct">>,
ok = set_permissions(Config, QName, QName, XName),
{ok, #{}} = rabbitmq_amqp_client:declare_queue(LinkPair, QName, #{}),
ok = rabbitmq_amqp_client:bind_queue(LinkPair, QName, XName, BindingKey, #{}),
%% remove read permission to source exchange
ok = set_permissions(Config, QName, QName, <<"^$">>),
ExpectedErr = error_unauthorized(
<<"read access to exchange '", XName/binary,
"' in vhost 'test vhost' refused for user 'test user'">>),
?assertEqual({error, {session_ended, ExpectedErr}},
rabbitmq_amqp_client:unbind_queue(LinkPair, QName, XName, BindingKey, #{})),
ok = close_connection_sync(Conn).
unbind_queue_target(Config) ->
{Conn, _, LinkPair} = init_pair(Config),
QName = BindingKey = atom_to_binary(?FUNCTION_NAME),
XName = <<"amq.direct">>,
ok = set_permissions(Config, QName, QName, XName),
{ok, #{}} = rabbitmq_amqp_client:declare_queue(LinkPair, QName, #{}),
ok = rabbitmq_amqp_client:bind_queue(LinkPair, QName, XName, BindingKey, #{}),
%% remove write permission to destination queue
ok = set_permissions(Config, QName, <<"^$">>, XName),
ExpectedErr = error_unauthorized(
<<"write access to queue '", QName/binary,
"' in vhost 'test vhost' refused for user 'test user'">>),
?assertEqual({error, {session_ended, ExpectedErr}},
rabbitmq_amqp_client:unbind_queue(LinkPair, QName, XName, BindingKey, #{})),
ok = close_connection_sync(Conn).
unbind_from_topic_exchange(Config) ->
Init = {_, _, LinkPair1} = init_pair(Config),
SrcXName = <<"amq.topic">>,
DstXName = <<"amq.direct">>,
Topic = <<"a.b.🐇"/utf8>>,
User = ?config(test_user, Config),
Vhost = ?config(test_vhost, Config),
ok = rabbit_ct_broker_helpers:set_full_permissions(Config, User, Vhost),
ok = set_topic_permissions(Config, SrcXName, <<"^$">>, Topic),
ok = rabbitmq_amqp_client:bind_exchange(LinkPair1, DstXName, SrcXName, Topic, #{}),
%% remove Topic read permission
ok = set_topic_permissions(Config, SrcXName, <<"^$">>, <<"^$">>),
%% Start a new connection since topic permissions are cached by the AMQP session process.
ok = cleanup_pair(Init),
{Conn, _, LinkPair2} = init_pair(Config),
ExpectedErr = error_unauthorized(
<<"read access to topic '", Topic/binary,
"' in exchange 'amq.topic' in vhost 'test vhost' refused for user 'test user'">>),
?assertEqual({error, {session_ended, ExpectedErr}},
rabbitmq_amqp_client:unbind_exchange(LinkPair2, DstXName, SrcXName, Topic, #{})),
ok = close_connection_sync(Conn).
init_pair(Config) ->
OpnConf = connection_config(Config),
{ok, Connection} = amqp10_client:open_connection(OpnConf),
{ok, Session} = amqp10_client:begin_session_sync(Connection),
{ok, LinkPair} = rabbitmq_amqp_client:attach_management_link_pair_sync(Session, <<"mgmt link pair">>),
{Connection, Session, LinkPair}.
cleanup_pair({Connection, Session, LinkPair}) ->
ok = rabbitmq_amqp_client:detach_management_link_pair_sync(LinkPair),
ok = amqp10_client:end_session(Session),
ok = amqp10_client:close_connection(Connection).
connection_config(Config) ->
Vhost = ?config(test_vhost, Config),
connection_config(Config, Vhost).

View File

@ -167,8 +167,7 @@ init_per_testcase(T, Config)
T =:= roundtrip_with_drain_quorum_queue orelse
T =:= timed_get_quorum_queue orelse
T =:= available_messages_quorum_queue ->
case rabbit_ct_broker_helpers:rpc(
Config, rabbit_feature_flags, is_enabled, [credit_api_v2]) of
case rpc(Config, rabbit_feature_flags, is_enabled, [credit_api_v2]) of
true ->
rabbit_ct_helpers:testcase_started(Config, T);
false ->
@ -221,19 +220,16 @@ reliable_send_receive(QType, Outcome, Config) ->
end,
ct:pal("~s testing ~s", [?FUNCTION_NAME, OutcomeBin]),
QName = <<QType/binary, OutcomeBin/binary>>,
Ch = rabbit_ct_client_helpers:open_channel(Config),
#'queue.declare_ok'{} = amqp_channel:call(
Ch, #'queue.declare'{
queue = QName,
durable = true,
arguments = [{<<"x-queue-type">>, longstr, QType}]}),
ok = rabbit_ct_client_helpers:close_channel(Ch),
%% reliable send and consume
OpnConf = connection_config(Config),
{ok, Connection} = amqp10_client:open_connection(OpnConf),
{ok, Session} = amqp10_client:begin_session_sync(Connection),
{ok, LinkPair} = rabbitmq_amqp_client:attach_management_link_pair_sync(Session, <<"pair">>),
QName = <<QType/binary, OutcomeBin/binary>>,
QProps = #{arguments => #{<<"x-queue-type">> => {utf8, QType}}},
{ok, _} = rabbitmq_amqp_client:declare_queue(LinkPair, QName, QProps),
ok = rabbitmq_amqp_client:detach_management_link_pair_sync(LinkPair),
%% reliable send and consume
Address = <<"/amq/queue/", QName/binary>>,
{ok, Sender} = amqp10_client:attach_sender_link(
Session, <<"test-sender">>, Address),
@ -268,24 +264,17 @@ reliable_send_receive(QType, Outcome, Config) ->
flush("post accept"),
ok = amqp10_client:detach_link(Receiver),
ok = delete_queue(Session2, QName),
ok = end_session_sync(Session2),
ok = amqp10_client:close_connection(Connection2),
ok = delete_queue(Config, QName).
ok = amqp10_client:close_connection(Connection2).
%% Tests that confirmations are returned correctly
%% when sending many messages async to a quorum queue.
sender_settle_mode_unsettled(Config) ->
QName = atom_to_binary(?FUNCTION_NAME),
Ch = rabbit_ct_client_helpers:open_channel(Config),
#'queue.declare_ok'{} = amqp_channel:call(
Ch, #'queue.declare'{
queue = QName,
durable = true,
arguments = [{<<"x-queue-type">>, longstr, <<"quorum">>}]}),
OpnConf = connection_config(Config),
{ok, Connection} = amqp10_client:open_connection(OpnConf),
{ok, Session} = amqp10_client:begin_session_sync(Connection),
{Connection, Session, LinkPair} = init(Config),
QProps = #{arguments => #{<<"x-queue-type">> => {utf8, <<"quorum">>}}},
{ok, _} = rabbitmq_amqp_client:declare_queue(LinkPair, QName, QProps),
Address = <<"/amq/queue/", QName/binary>>,
{ok, Sender} = amqp10_client:attach_sender_link(
Session, <<"test-sender">>, Address, unsettled),
@ -306,24 +295,20 @@ sender_settle_mode_unsettled(Config) ->
end || DTag <- DTags],
ok = amqp10_client:detach_link(Sender),
?assertMatch({ok, #{message_count := NumMsgs}},
rabbitmq_amqp_client:delete_queue(LinkPair, QName)),
ok = rabbitmq_amqp_client:detach_management_link_pair_sync(LinkPair),
ok = end_session_sync(Session),
ok = amqp10_client:close_connection(Connection),
?assertEqual(#'queue.delete_ok'{message_count = NumMsgs},
amqp_channel:call(Ch, #'queue.delete'{queue = QName})),
ok = rabbit_ct_client_helpers:close_channel(Ch).
ok = amqp10_client:close_connection(Connection).
sender_settle_mode_unsettled_fanout(Config) ->
{Connection, Session, LinkPair} = init(Config),
QNames = [<<"q1">>, <<"q2">>, <<"q3">>],
Ch = rabbit_ct_client_helpers:open_channel(Config),
[begin
#'queue.declare_ok'{} = amqp_channel:call(Ch, #'queue.declare'{queue = QName}),
#'queue.bind_ok'{} = amqp_channel:call(Ch, #'queue.bind'{queue = QName,
exchange = <<"amq.fanout">>})
{ok, _} = rabbitmq_amqp_client:declare_queue(LinkPair, QName, #{}),
ok = rabbitmq_amqp_client:bind_queue(LinkPair, QName, <<"amq.fanout">>, <<>>, #{})
end || QName <- QNames],
OpnConf = connection_config(Config),
{ok, Connection} = amqp10_client:open_connection(OpnConf),
{ok, Session} = amqp10_client:begin_session_sync(Connection),
Address = <<"/exchange/amq.fanout">>,
{ok, Sender} = amqp10_client:attach_sender_link(Session, <<"test-sender">>, Address, unsettled),
ok = wait_for_credit(Sender),
@ -343,28 +328,22 @@ sender_settle_mode_unsettled_fanout(Config) ->
end || DTag <- DTags],
ok = amqp10_client:detach_link(Sender),
[?assertMatch({ok, #{message_count := NumMsgs}},
rabbitmq_amqp_client:delete_queue(LinkPair, QName))
|| QName <- QNames],
ok = rabbitmq_amqp_client:detach_management_link_pair_sync(LinkPair),
ok = end_session_sync(Session),
ok = amqp10_client:close_connection(Connection),
[?assertEqual(#'queue.delete_ok'{message_count = NumMsgs},
amqp_channel:call(Ch, #'queue.delete'{queue = QName})) ||
QName <- QNames],
ok = rabbit_ct_client_helpers:close_channel(Ch).
ok = amqp10_client:close_connection(Connection).
%% Tests that confirmations are returned correctly
%% when sending many messages async to a quorum queue where
%% every 3rd message is settled by the sender.
sender_settle_mode_mixed(Config) ->
{Connection, Session, LinkPair} = init(Config),
QName = atom_to_binary(?FUNCTION_NAME),
Ch = rabbit_ct_client_helpers:open_channel(Config),
#'queue.declare_ok'{} = amqp_channel:call(
Ch, #'queue.declare'{
queue = QName,
durable = true,
arguments = [{<<"x-queue-type">>, longstr, <<"quorum">>}]}),
QProps = #{arguments => #{<<"x-queue-type">> => {utf8, <<"quorum">>}}},
{ok, _} = rabbitmq_amqp_client:declare_queue(LinkPair, QName, QProps),
OpnConf = connection_config(Config),
{ok, Connection} = amqp10_client:open_connection(OpnConf),
{ok, Session} = amqp10_client:begin_session_sync(Connection),
Address = <<"/amq/queue/", QName/binary>>,
{ok, Sender} = amqp10_client:attach_sender_link(
Session, <<"test-sender">>, Address, mixed),
@ -391,27 +370,20 @@ sender_settle_mode_mixed(Config) ->
end || DTag <- DTags],
ok = amqp10_client:detach_link(Sender),
?assertMatch({ok, #{message_count := NumMsgs}},
rabbitmq_amqp_client:delete_queue(LinkPair, QName)),
ok = rabbitmq_amqp_client:detach_management_link_pair_sync(LinkPair),
ok = end_session_sync(Session),
ok = amqp10_client:close_connection(Connection),
?assertEqual(#'queue.delete_ok'{message_count = NumMsgs},
amqp_channel:call(Ch, #'queue.delete'{queue = QName})),
ok = rabbit_ct_client_helpers:close_channel(Ch).
ok = amqp10_client:close_connection(Connection).
quorum_queue_rejects(Config) ->
{Connection, Session, LinkPair} = init(Config),
QName = atom_to_binary(?FUNCTION_NAME),
Ch = rabbit_ct_client_helpers:open_channel(Config),
#'queue.declare_ok'{} = amqp_channel:call(
Ch, #'queue.declare'{
queue = QName,
durable = true,
arguments = [{<<"x-queue-type">>, longstr, <<"quorum">>},
{<<"x-max-length">>, long, 1},
{<<"x-overflow">>, longstr, <<"reject-publish">>}
]}),
QProps = #{arguments => #{<<"x-queue-type">> => {utf8, <<"quorum">>},
<<"x-max-length">> => {ulong, 1},
<<"x-overflow">> => {utf8, <<"reject-publish">>}}},
{ok, _} = rabbitmq_amqp_client:declare_queue(LinkPair, QName, QProps),
OpnConf = connection_config(Config),
{ok, Connection} = amqp10_client:open_connection(OpnConf),
{ok, Session} = amqp10_client:begin_session_sync(Connection),
Address = <<"/amq/queue/", QName/binary>>,
{ok, Sender} = amqp10_client:attach_sender_link(
Session, <<"test-sender">>, Address, mixed),
@ -444,12 +416,11 @@ quorum_queue_rejects(Config) ->
end || DTag <- DTags ++ [<<"tag d">>]],
ok = amqp10_client:detach_link(Sender),
?assertMatch({ok, #{message_count := 2}},
rabbitmq_amqp_client:delete_queue(LinkPair, QName)),
ok = rabbitmq_amqp_client:detach_management_link_pair_sync(LinkPair),
ok = amqp10_client:end_session(Session),
ok = amqp10_client:close_connection(Connection),
?assertEqual(#'queue.delete_ok'{message_count = 2},
amqp_channel:call(Ch, #'queue.delete'{queue = QName})),
ok = rabbit_ct_client_helpers:close_channel(Ch).
ok = amqp10_client:close_connection(Connection).
receiver_settle_mode_first(Config) ->
QName = atom_to_binary(?FUNCTION_NAME),
@ -536,9 +507,9 @@ receiver_settle_mode_first(Config) ->
assert_messages(QName, 0, 0, Config),
ok = amqp10_client:detach_link(Receiver),
ok = delete_queue(Session, QName),
ok = amqp10_client:end_session(Session),
ok = amqp10_client:close_connection(Connection),
ok = delete_queue(Config, QName).
ok = amqp10_client:close_connection(Connection).
publishing_to_non_existing_queue_should_settle_with_released(Config) ->
OpnConf = connection_config(Config),
@ -593,16 +564,9 @@ roundtrip_with_drain_stream(Config) ->
roundtrip_with_drain(Config, QueueType, QName)
when is_binary(QueueType) ->
Address = <<"/amq/queue/", QName/binary>>,
Ch = rabbit_ct_client_helpers:open_channel(Config),
Args = [{<<"x-queue-type">>, longstr, QueueType}],
#'queue.declare_ok'{} = amqp_channel:call(
Ch, #'queue.declare'{
queue = QName,
durable = true,
arguments = Args}),
OpnConf = connection_config(Config),
{ok, Connection} = amqp10_client:open_connection(OpnConf),
{ok, Session} = amqp10_client:begin_session_sync(Connection),
{Connection, Session, LinkPair} = init(Config),
QProps = #{arguments => #{<<"x-queue-type">> => {utf8, QueueType}}},
{ok, _} = rabbitmq_amqp_client:declare_queue(LinkPair, QName, QProps),
{ok, Sender} = amqp10_client:attach_sender_link(
Session, <<"test-sender">>, Address),
wait_for_credit(Sender),
@ -641,24 +605,19 @@ roundtrip_with_drain(Config, QueueType, QName)
flush("final"),
ok = amqp10_client:detach_link(Sender),
ok = amqp10_client:close_connection(Connection),
ok = delete_queue(Config, QName).
{ok, _} = rabbitmq_amqp_client:delete_queue(LinkPair, QName),
ok = rabbitmq_amqp_client:detach_management_link_pair_sync(LinkPair),
ok = amqp10_client:close_connection(Connection).
%% Send a message with a body containing a single AMQP 1.0 value section
%% to a stream and consume via AMQP 0.9.1.
amqp_stream_amqpl(Config) ->
Ch = rabbit_ct_client_helpers:open_channel(Config),
{Connection, Session, LinkPair} = init(Config),
QName = atom_to_binary(?FUNCTION_NAME),
amqp_channel:call(Ch, #'queue.declare'{
queue = QName,
durable = true,
arguments = [{<<"x-queue-type">>, longstr, <<"stream">>}]}),
QProps = #{arguments => #{<<"x-queue-type">> => {utf8, <<"stream">>}}},
{ok, #{type := <<"stream">>}} = rabbitmq_amqp_client:declare_queue(LinkPair, QName, QProps),
Address = <<"/amq/queue/", QName/binary>>,
OpnConf = connection_config(Config),
{ok, Connection} = amqp10_client:open_connection(OpnConf),
{ok, Session} = amqp10_client:begin_session_sync(Connection),
{ok, Sender} = amqp10_client:attach_sender_link(
Session, <<"test-sender">>, Address),
wait_for_credit(Sender),
@ -666,8 +625,8 @@ amqp_stream_amqpl(Config) ->
ok = amqp10_client:send_msg(Sender, OutMsg),
flush("final"),
ok = amqp10_client:detach_link(Sender),
ok = amqp10_client:close_connection(Connection),
Ch = rabbit_ct_client_helpers:open_channel(Config),
#'basic.qos_ok'{} = amqp_channel:call(Ch, #'basic.qos'{global = false,
prefetch_count = 1}),
CTag = <<"my-tag">>,
@ -686,8 +645,11 @@ amqp_stream_amqpl(Config) ->
after 5000 ->
ct:fail(basic_deliver_timeout)
end,
#'queue.delete_ok'{} = amqp_channel:call(Ch, #'queue.delete'{queue = QName}),
ok = rabbit_ct_client_helpers:close_channel(Ch).
ok = rabbit_ct_client_helpers:close_channel(Ch),
{ok, _} = rabbitmq_amqp_client:delete_queue(LinkPair, QName),
ok = rabbitmq_amqp_client:detach_management_link_pair_sync(LinkPair),
ok = amqp10_client:close_connection(Connection).
message_headers_conversion(Config) ->
QName = atom_to_binary(?FUNCTION_NAME),
@ -706,7 +668,7 @@ message_headers_conversion(Config) ->
amqp091_to_amqp10_header_conversion(Session, Ch, QName, Address),
ok = rabbit_ct_client_helpers:close_channel(Ch),
ok = delete_queue(Config, QName),
ok = delete_queue(Session, QName),
ok = amqp10_client:close_connection(Connection).
amqp10_to_amqp091_header_conversion(Session,Ch, QName, Address) ->
@ -836,10 +798,10 @@ multiple_sessions(Config) ->
%% Clean up.
[ok = amqp10_client:detach_link(Link) || Link <- [Receiver1, Receiver2, Sender1, Sender2]],
[ok = delete_queue(Session1, Q) || Q <- Qs],
ok = end_session_sync(Session1),
ok = end_session_sync(Session2),
ok = amqp10_client:close_connection(Connection),
[ok = delete_queue(Config, Q) || Q <- Qs].
ok = amqp10_client:close_connection(Connection).
server_closes_link_classic_queue(Config) ->
server_closes_link(<<"classic">>, Config).
@ -897,7 +859,7 @@ server_closes_link(QType, Config) ->
%% Server closes the link endpoint due to some AMQP 1.0 external condition:
%% In this test, the external condition is that an AMQP 0.9.1 client deletes the queue.
delete_queue(Config, QName),
ok = delete_queue(Session, QName),
%% We expect that the server closes the link endpoints,
%% i.e. the server sends us DETACH frames.
@ -1012,7 +974,7 @@ link_target_queue_deleted(QType, Config) ->
%% Now, the server AMQP session contains a delivery that did not get confirmed by the target queue.
%% If we now delete that target queue, RabbitMQ must not reply to us with ACCEPTED.
%% Instead, we expect RabbitMQ to reply with RELEASED since no queue ever received our 2nd message.
delete_queue(Config, QName),
ok = delete_queue(Session, QName),
ok = wait_for_settlement(DTag2, released),
%% After the 2nd message got released, we additionally expect RabbitMQ to close the link given
@ -1136,7 +1098,7 @@ events(Config) ->
Pid = proplists:lookup(pid, Props),
ClientProperties = {client_properties, List} = proplists:lookup(client_properties, Props),
?assert(lists:member(
{<<"product">>, longstr, <<"AMQP 1.0 client from the RabbitMQ Project">>},
{<<"product">>, longstr, <<"AMQP 1.0 client">>},
List)),
?assert(lists:member(
{<<"ignore-maintenance">>, bool, true},
@ -1581,19 +1543,12 @@ single_active_consumer_quorum_queue(Config) ->
single_active_consumer(QType, Config) ->
QName = atom_to_binary(?FUNCTION_NAME),
Ch = rabbit_ct_client_helpers:open_channel(Config),
#'queue.declare_ok'{} = amqp_channel:call(
Ch, #'queue.declare'{
queue = QName,
durable = true,
arguments = [{<<"x-single-active-consumer">>, bool, true},
{<<"x-queue-type">>, longstr, QType}]}),
ok = rabbit_ct_client_helpers:close_channel(Ch),
{Connection, Session, LinkPair} = init(Config),
QProps = #{arguments => #{<<"x-queue-type">> => {utf8, QType},
<<"x-single-active-consumer">> => true}},
{ok, #{type := QType}} = rabbitmq_amqp_client:declare_queue(LinkPair, QName, QProps),
%% Attach 1 sender and 2 receivers to the queue.
OpnConf = connection_config(Config),
{ok, Connection} = amqp10_client:open_connection(OpnConf),
{ok, Session} = amqp10_client:begin_session_sync(Connection),
Address = <<"/amq/queue/", QName/binary>>,
{ok, Sender} = amqp10_client:attach_sender_link(
Session, <<"test-sender">>, Address),
@ -1661,9 +1616,10 @@ single_active_consumer(QType, Config) ->
end,
ok = amqp10_client:detach_link(Receiver2),
{ok, _} = rabbitmq_amqp_client:delete_queue(LinkPair, QName),
ok = rabbitmq_amqp_client:detach_management_link_pair_sync(LinkPair),
ok = end_session_sync(Session),
ok = amqp10_client:close_connection(Connection),
delete_queue(Config, QName).
ok = amqp10_client:close_connection(Connection).
%% "A session endpoint can choose to unmap its output handle for a link. In this case, the endpoint MUST
%% send a detach frame to inform the remote peer that the handle is no longer attached to the link endpoint.
@ -2048,9 +2004,9 @@ receive_transfer_flow_order(Config) ->
after 5000 -> ct:fail("timeout receiving credit_exhausted")
end,
ok = delete_queue(Session, QName),
ok = end_session_sync(Session),
ok = amqp10_client:close_connection(Connection),
ok = delete_queue(Config, QName).
ok = amqp10_client:close_connection(Connection).
last_queue_confirms(Config) ->
ClassicQ = <<"my classic queue">>,
@ -2255,9 +2211,9 @@ target_classic_queue_down(Config) ->
ok = amqp10_client:detach_link(Sender),
ok = amqp10_client:detach_link(Receiver2),
ok = delete_queue(Session, QName),
ok = end_session_sync(Session),
ok = amqp10_client:close_connection(Connection),
delete_queue(Config, QName).
ok = amqp10_client:close_connection(Connection).
async_notify_settled_classic_queue(Config) ->
%% TODO Bump old version in mixed version tests to 3.13.x,
@ -2322,9 +2278,8 @@ async_notify(SenderSettleMode, QType, Config) ->
%% If it is a stream we need to wait until there is a local member
%% on the node we want to subscibe from before proceeding.
rabbit_ct_helpers:await_condition(
fun() -> rabbit_ct_broker_helpers:rpc(
Config, 0, ?MODULE, has_local_member,
[rabbit_misc:r(<<"/">>, queue, QName)])
fun() -> rpc(Config, 0, ?MODULE, has_local_member,
[rabbit_misc:r(<<"/">>, queue, QName)])
end, 30_000);
_ ->
ok
@ -2439,10 +2394,10 @@ link_flow_control(Config) ->
end,
[ok = amqp10_client:detach_link(Link) || Link <- [ReceiverCQ, ReceiverQQ, SenderCQ, SenderQQ]],
ok = delete_queue(Session, QQ),
ok = delete_queue(Session, CQ),
ok = end_session_sync(Session),
ok = amqp10_client:close_connection(Connection),
delete_queue(Config, QQ),
delete_queue(Config, CQ).
ok = amqp10_client:close_connection(Connection).
classic_queue_on_old_node(Config) ->
%% TODO Bump old version in mixed version tests to 3.13.x,
@ -2860,7 +2815,7 @@ stream_filtering(Config) ->
?assertEqual(WaveCount * 2, length(AppleUnfilteredFilteredMessages)),
ok = amqp10_client:detach_link(AppleUnfilteredReceiver),
delete_queue(Config, Stream),
ok = delete_queue(Session, Stream),
ok = amqp10_client:close_connection(Connection).
available_messages_classic_queue(Config) ->
@ -2968,9 +2923,9 @@ incoming_message_interceptors(Config) ->
ok = amqp10_client:detach_link(Sender),
ok = amqp10_client:detach_link(Receiver),
ok = delete_queue(Session, QName),
ok = end_session_sync(Session),
ok = amqp10_client:close_connection(Connection),
delete_queue(Config, QName),
true = rpc(Config, persistent_term, erase, [Key]).
trace(Config) ->
@ -3052,10 +3007,10 @@ trace(Config) ->
ok = amqp10_client:detach_link(Sender),
ok = amqp10_client:detach_link(Receiver),
[delete_queue(SessionSender, Q0) || Q0 <- Qs],
ok = end_session_sync(SessionSender),
ok = end_session_sync(SessionReceiver),
ok = amqp10_client:close_connection(Connection),
[delete_queue(Config, Q0) || Q0 <- Qs].
ok = amqp10_client:close_connection(Connection).
%% https://www.rabbitmq.com/validated-user-id.html
user_id(Config) ->
@ -3125,8 +3080,8 @@ message_ttl(Config) ->
ok = amqp10_client:detach_link(Sender),
ok = amqp10_client:detach_link(Receiver),
ok = amqp10_client:close_connection(Connection),
ok = delete_queue(Config, QName).
ok = delete_queue(Session, QName),
ok = amqp10_client:close_connection(Connection).
%% For backward compatibility, deployment tools should be able to
%% enable and disable the deprecated no-op AMQP 1.0 plugin.
@ -3344,13 +3299,20 @@ classic_priority_queue(Config) ->
ok = amqp10_client:detach_link(Receiver1),
ok = amqp10_client:detach_link(Receiver2),
ok = amqp10_client:detach_link(Sender),
ok = delete_queue(Session, QName),
ok = end_session_sync(Session),
ok = amqp10_client:close_connection(Connection),
ok = delete_queue(Config, QName).
ok = amqp10_client:close_connection(Connection).
%% internal
%%
init(Config) ->
OpnConf = connection_config(Config),
{ok, Connection} = amqp10_client:open_connection(OpnConf),
{ok, Session} = amqp10_client:begin_session_sync(Connection),
{ok, LinkPair} = rabbitmq_amqp_client:attach_management_link_pair_sync(Session, <<"my link pair">>),
{Connection, Session, LinkPair}.
receive_all_messages(Receiver, Accept) ->
receive_all_messages0(Receiver, Accept, []).
@ -3467,10 +3429,11 @@ wait_for_accepts(N) ->
ct:fail({missing_accepted, N})
end.
delete_queue(Config, QName) ->
Ch = rabbit_ct_client_helpers:open_channel(Config),
#'queue.delete_ok'{} = amqp_channel:call(Ch, #'queue.delete'{queue = QName}),
ok = rabbit_ct_client_helpers:close_channel(Ch).
delete_queue(Session, QName) ->
{ok, LinkPair} = rabbitmq_amqp_client:attach_management_link_pair_sync(
Session, <<"delete queue">>),
{ok, _} = rabbitmq_amqp_client:delete_queue(LinkPair, QName),
ok = rabbitmq_amqp_client:detach_management_link_pair_sync(LinkPair).
amqp091_get_msg_headers(Channel, QName) ->
{#'basic.get_ok'{}, #amqp_msg{props = #'P_basic'{ headers= Headers}}}

View File

@ -21,7 +21,7 @@ parse_endpoint(Destination, AllowAnonymousQueue) when is_binary(Destination) ->
parse_endpoint(unicode:characters_to_list(Destination),
AllowAnonymousQueue);
parse_endpoint(Destination, AllowAnonymousQueue) when is_list(Destination) ->
case re:split(Destination, "/", [{return, list}]) of
case re:split(Destination, "/", [unicode, {return, list}]) of
[Name] ->
{ok, {queue, unescape(Name)}};
["", Type | Rest]

17
deps/rabbitmq_amqp_client/.gitignore vendored Normal file
View File

@ -0,0 +1,17 @@
.sw?
.*.sw?
*.beam
/.erlang.mk/
/cover/
/deps/
/doc/
/ebin/
/escript/
/escript.lock
/logs/
/plugins/
/plugins.lock
/sbin/
/sbin.lock
/rabbitmq_amqp_client.d

91
deps/rabbitmq_amqp_client/BUILD.bazel vendored Normal file
View File

@ -0,0 +1,91 @@
load("@rules_erlang//:eunit2.bzl", "eunit")
load("@rules_erlang//:xref2.bzl", "xref")
load("@rules_erlang//:dialyze.bzl", "dialyze", "plt")
load(
"//:rabbitmq.bzl",
"RABBITMQ_DIALYZER_OPTS",
"assert_suites",
"broker_for_integration_suites",
"rabbitmq_app",
"rabbitmq_integration_suite",
"rabbitmq_suite",
)
load(
":app.bzl",
"all_beam_files",
"all_srcs",
"all_test_beam_files",
"test_suite_beam_files",
)
APP_NAME = "rabbitmq_amqp_client"
APP_DESCRIPTION = "AMQP 1.0 client for RabbitMQ"
all_beam_files(name = "all_beam_files")
all_test_beam_files(name = "all_test_beam_files")
all_srcs(name = "all_srcs")
test_suite_beam_files(name = "test_suite_beam_files")
rabbitmq_app(
name = "erlang_app",
srcs = [":all_srcs"],
hdrs = [":public_hdrs"],
app_description = APP_DESCRIPTION,
app_name = APP_NAME,
beam_files = [":beam_files"],
license_files = [":license_files"],
priv = [":priv"],
deps = [
"//deps/amqp10_client:erlang_app",
"//deps/amqp10_common:erlang_app",
],
)
xref(
name = "xref",
target = ":erlang_app",
)
plt(
name = "deps_plt",
for_target = ":erlang_app",
plt = "//:base_plt",
)
dialyze(
name = "dialyze",
dialyzer_opts = RABBITMQ_DIALYZER_OPTS,
plt = ":deps_plt",
target = ":erlang_app",
)
broker_for_integration_suites(
)
TEST_DEPS = [
"//deps/amqp10_client:erlang_app",
]
rabbitmq_integration_suite(
name = "management_SUITE",
size = "medium",
shard_count = 2,
deps = TEST_DEPS,
)
assert_suites()
alias(
name = "rabbitmq_amqp_client",
actual = ":erlang_app",
visibility = ["//visibility:public"],
)
eunit(
name = "eunit",
target = ":test_erlang_app",
)

4
deps/rabbitmq_amqp_client/LICENSE vendored Normal file
View File

@ -0,0 +1,4 @@
This package is licensed under the MPL 2.0. For the MPL 2.0, please see LICENSE-MPL-RabbitMQ.
If you have any questions regarding licensing, please contact us at
rabbitmq-core@groups.vmware.com.

View File

@ -0,0 +1,373 @@
Mozilla Public License Version 2.0
==================================
1. Definitions
--------------
1.1. "Contributor"
means each individual or legal entity that creates, contributes to
the creation of, or owns Covered Software.
1.2. "Contributor Version"
means the combination of the Contributions of others (if any) used
by a Contributor and that particular Contributor's Contribution.
1.3. "Contribution"
means Covered Software of a particular Contributor.
1.4. "Covered Software"
means Source Code Form to which the initial Contributor has attached
the notice in Exhibit A, the Executable Form of such Source Code
Form, and Modifications of such Source Code Form, in each case
including portions thereof.
1.5. "Incompatible With Secondary Licenses"
means
(a) that the initial Contributor has attached the notice described
in Exhibit B to the Covered Software; or
(b) that the Covered Software was made available under the terms of
version 1.1 or earlier of the License, but not also under the
terms of a Secondary License.
1.6. "Executable Form"
means any form of the work other than Source Code Form.
1.7. "Larger Work"
means a work that combines Covered Software with other material, in
a separate file or files, that is not Covered Software.
1.8. "License"
means this document.
1.9. "Licensable"
means having the right to grant, to the maximum extent possible,
whether at the time of the initial grant or subsequently, any and
all of the rights conveyed by this License.
1.10. "Modifications"
means any of the following:
(a) any file in Source Code Form that results from an addition to,
deletion from, or modification of the contents of Covered
Software; or
(b) any new file in Source Code Form that contains any Covered
Software.
1.11. "Patent Claims" of a Contributor
means any patent claim(s), including without limitation, method,
process, and apparatus claims, in any patent Licensable by such
Contributor that would be infringed, but for the grant of the
License, by the making, using, selling, offering for sale, having
made, import, or transfer of either its Contributions or its
Contributor Version.
1.12. "Secondary License"
means either the GNU General Public License, Version 2.0, the GNU
Lesser General Public License, Version 2.1, the GNU Affero General
Public License, Version 3.0, or any later versions of those
licenses.
1.13. "Source Code Form"
means the form of the work preferred for making modifications.
1.14. "You" (or "Your")
means an individual or a legal entity exercising rights under this
License. For legal entities, "You" includes any entity that
controls, is controlled by, or is under common control with You. For
purposes of this definition, "control" means (a) the power, direct
or indirect, to cause the direction or management of such entity,
whether by contract or otherwise, or (b) ownership of more than
fifty percent (50%) of the outstanding shares or beneficial
ownership of such entity.
2. License Grants and Conditions
--------------------------------
2.1. Grants
Each Contributor hereby grants You a world-wide, royalty-free,
non-exclusive license:
(a) under intellectual property rights (other than patent or trademark)
Licensable by such Contributor to use, reproduce, make available,
modify, display, perform, distribute, and otherwise exploit its
Contributions, either on an unmodified basis, with Modifications, or
as part of a Larger Work; and
(b) under Patent Claims of such Contributor to make, use, sell, offer
for sale, have made, import, and otherwise transfer either its
Contributions or its Contributor Version.
2.2. Effective Date
The licenses granted in Section 2.1 with respect to any Contribution
become effective for each Contribution on the date the Contributor first
distributes such Contribution.
2.3. Limitations on Grant Scope
The licenses granted in this Section 2 are the only rights granted under
this License. No additional rights or licenses will be implied from the
distribution or licensing of Covered Software under this License.
Notwithstanding Section 2.1(b) above, no patent license is granted by a
Contributor:
(a) for any code that a Contributor has removed from Covered Software;
or
(b) for infringements caused by: (i) Your and any other third party's
modifications of Covered Software, or (ii) the combination of its
Contributions with other software (except as part of its Contributor
Version); or
(c) under Patent Claims infringed by Covered Software in the absence of
its Contributions.
This License does not grant any rights in the trademarks, service marks,
or logos of any Contributor (except as may be necessary to comply with
the notice requirements in Section 3.4).
2.4. Subsequent Licenses
No Contributor makes additional grants as a result of Your choice to
distribute the Covered Software under a subsequent version of this
License (see Section 10.2) or under the terms of a Secondary License (if
permitted under the terms of Section 3.3).
2.5. Representation
Each Contributor represents that the Contributor believes its
Contributions are its original creation(s) or it has sufficient rights
to grant the rights to its Contributions conveyed by this License.
2.6. Fair Use
This License is not intended to limit any rights You have under
applicable copyright doctrines of fair use, fair dealing, or other
equivalents.
2.7. Conditions
Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted
in Section 2.1.
3. Responsibilities
-------------------
3.1. Distribution of Source Form
All distribution of Covered Software in Source Code Form, including any
Modifications that You create or to which You contribute, must be under
the terms of this License. You must inform recipients that the Source
Code Form of the Covered Software is governed by the terms of this
License, and how they can obtain a copy of this License. You may not
attempt to alter or restrict the recipients' rights in the Source Code
Form.
3.2. Distribution of Executable Form
If You distribute Covered Software in Executable Form then:
(a) such Covered Software must also be made available in Source Code
Form, as described in Section 3.1, and You must inform recipients of
the Executable Form how they can obtain a copy of such Source Code
Form by reasonable means in a timely manner, at a charge no more
than the cost of distribution to the recipient; and
(b) You may distribute such Executable Form under the terms of this
License, or sublicense it under different terms, provided that the
license for the Executable Form does not attempt to limit or alter
the recipients' rights in the Source Code Form under this License.
3.3. Distribution of a Larger Work
You may create and distribute a Larger Work under terms of Your choice,
provided that You also comply with the requirements of this License for
the Covered Software. If the Larger Work is a combination of Covered
Software with a work governed by one or more Secondary Licenses, and the
Covered Software is not Incompatible With Secondary Licenses, this
License permits You to additionally distribute such Covered Software
under the terms of such Secondary License(s), so that the recipient of
the Larger Work may, at their option, further distribute the Covered
Software under the terms of either this License or such Secondary
License(s).
3.4. Notices
You may not remove or alter the substance of any license notices
(including copyright notices, patent notices, disclaimers of warranty,
or limitations of liability) contained within the Source Code Form of
the Covered Software, except that You may alter any license notices to
the extent required to remedy known factual inaccuracies.
3.5. Application of Additional Terms
You may choose to offer, and to charge a fee for, warranty, support,
indemnity or liability obligations to one or more recipients of Covered
Software. However, You may do so only on Your own behalf, and not on
behalf of any Contributor. You must make it absolutely clear that any
such warranty, support, indemnity, or liability obligation is offered by
You alone, and You hereby agree to indemnify every Contributor for any
liability incurred by such Contributor as a result of warranty, support,
indemnity or liability terms You offer. You may include additional
disclaimers of warranty and limitations of liability specific to any
jurisdiction.
4. Inability to Comply Due to Statute or Regulation
---------------------------------------------------
If it is impossible for You to comply with any of the terms of this
License with respect to some or all of the Covered Software due to
statute, judicial order, or regulation then You must: (a) comply with
the terms of this License to the maximum extent possible; and (b)
describe the limitations and the code they affect. Such description must
be placed in a text file included with all distributions of the Covered
Software under this License. Except to the extent prohibited by statute
or regulation, such description must be sufficiently detailed for a
recipient of ordinary skill to be able to understand it.
5. Termination
--------------
5.1. The rights granted under this License will terminate automatically
if You fail to comply with any of its terms. However, if You become
compliant, then the rights granted under this License from a particular
Contributor are reinstated (a) provisionally, unless and until such
Contributor explicitly and finally terminates Your grants, and (b) on an
ongoing basis, if such Contributor fails to notify You of the
non-compliance by some reasonable means prior to 60 days after You have
come back into compliance. Moreover, Your grants from a particular
Contributor are reinstated on an ongoing basis if such Contributor
notifies You of the non-compliance by some reasonable means, this is the
first time You have received notice of non-compliance with this License
from such Contributor, and You become compliant prior to 30 days after
Your receipt of the notice.
5.2. If You initiate litigation against any entity by asserting a patent
infringement claim (excluding declaratory judgment actions,
counter-claims, and cross-claims) alleging that a Contributor Version
directly or indirectly infringes any patent, then the rights granted to
You by any and all Contributors for the Covered Software under Section
2.1 of this License shall terminate.
5.3. In the event of termination under Sections 5.1 or 5.2 above, all
end user license agreements (excluding distributors and resellers) which
have been validly granted by You or Your distributors under this License
prior to termination shall survive termination.
************************************************************************
* *
* 6. Disclaimer of Warranty *
* ------------------------- *
* *
* Covered Software is provided under this License on an "as is" *
* basis, without warranty of any kind, either expressed, implied, or *
* statutory, including, without limitation, warranties that the *
* Covered Software is free of defects, merchantable, fit for a *
* particular purpose or non-infringing. The entire risk as to the *
* quality and performance of the Covered Software is with You. *
* Should any Covered Software prove defective in any respect, You *
* (not any Contributor) assume the cost of any necessary servicing, *
* repair, or correction. This disclaimer of warranty constitutes an *
* essential part of this License. No use of any Covered Software is *
* authorized under this License except under this disclaimer. *
* *
************************************************************************
************************************************************************
* *
* 7. Limitation of Liability *
* -------------------------- *
* *
* Under no circumstances and under no legal theory, whether tort *
* (including negligence), contract, or otherwise, shall any *
* Contributor, or anyone who distributes Covered Software as *
* permitted above, be liable to You for any direct, indirect, *
* special, incidental, or consequential damages of any character *
* including, without limitation, damages for lost profits, loss of *
* goodwill, work stoppage, computer failure or malfunction, or any *
* and all other commercial damages or losses, even if such party *
* shall have been informed of the possibility of such damages. This *
* limitation of liability shall not apply to liability for death or *
* personal injury resulting from such party's negligence to the *
* extent applicable law prohibits such limitation. Some *
* jurisdictions do not allow the exclusion or limitation of *
* incidental or consequential damages, so this exclusion and *
* limitation may not apply to You. *
* *
************************************************************************
8. Litigation
-------------
Any litigation relating to this License may be brought only in the
courts of a jurisdiction where the defendant maintains its principal
place of business and such litigation shall be governed by laws of that
jurisdiction, without reference to its conflict-of-law provisions.
Nothing in this Section shall prevent a party's ability to bring
cross-claims or counter-claims.
9. Miscellaneous
----------------
This License represents the complete agreement concerning the subject
matter hereof. If any provision of this License is held to be
unenforceable, such provision shall be reformed only to the extent
necessary to make it enforceable. Any law or regulation which provides
that the language of a contract shall be construed against the drafter
shall not be used to construe this License against a Contributor.
10. Versions of the License
---------------------------
10.1. New Versions
Mozilla Foundation is the license steward. Except as provided in Section
10.3, no one other than the license steward has the right to modify or
publish new versions of this License. Each version will be given a
distinguishing version number.
10.2. Effect of New Versions
You may distribute the Covered Software under the terms of the version
of the License under which You originally received the Covered Software,
or under the terms of any subsequent version published by the license
steward.
10.3. Modified Versions
If you create software not governed by this License, and you want to
create a new license for such software, you may create and use a
modified version of this License if you rename the license and remove
any references to the name of the license steward (except to note that
such modified license differs from this License).
10.4. Distributing Source Code Form that is Incompatible With Secondary
Licenses
If You choose to distribute Source Code Form that is Incompatible With
Secondary Licenses under the terms of this version of the License, the
notice described in Exhibit B of this License must be attached.
Exhibit A - Source Code Form License Notice
-------------------------------------------
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
If it is not possible or desirable to put the notice in a particular
file, then You may include the notice in a location (such as a LICENSE
file in a relevant directory) where a recipient would be likely to look
for such a notice.
You may add additional accurate notices of copyright ownership.
Exhibit B - "Incompatible With Secondary Licenses" Notice
---------------------------------------------------------
This Source Code Form is "Incompatible With Secondary Licenses", as
defined by the Mozilla Public License, v. 2.0.

22
deps/rabbitmq_amqp_client/Makefile vendored Normal file
View File

@ -0,0 +1,22 @@
PROJECT = rabbitmq_amqp_client
PROJECT_DESCRIPTION = AMQP 1.0 client for RabbitMQ
DEPS = amqp10_client
TEST_DEPS = rabbitmq_ct_helpers
BUILD_DEPS = rabbit_common
DEP_EARLY_PLUGINS = rabbit_common/mk/rabbitmq-early-plugin.mk
TEST_DEPS = rabbit rabbitmq_ct_helpers
DEP_PLUGINS = rabbit_common/mk/rabbitmq-macros.mk \
rabbit_common/mk/rabbitmq-build.mk \
rabbit_common/mk/rabbitmq-hexpm.mk \
rabbit_common/mk/rabbitmq-dist.mk \
rabbit_common/mk/rabbitmq-run.mk \
rabbit_common/mk/rabbitmq-test.mk \
rabbit_common/mk/rabbitmq-tools.mk
.DEFAULT_GOAL = all
include rabbitmq-components.mk
include erlang.mk

29
deps/rabbitmq_amqp_client/README.md vendored Normal file
View File

@ -0,0 +1,29 @@
# Erlang RabbitMQ AMQP 1.0 Client
The [Erlang AMQP 1.0 client](../amqp10_client/) is a client that can communicate with any AMQP 1.0 broker.
In contrast, this project (Erlang **RabbitMQ** AMQP 1.0 Client) can only communicate with RabbitMQ.
This project wraps (i.e. depends on) the Erlang AMQP 1.0 client providing additionally the following RabbitMQ management operations:
* declare queue
* get queue
* delete queue
* purge queue
* bind queue to exchange
* unbind queue from exchange
* declare exchange
* delete exchange
* bind exchange to exchange
* unbind exchange from exchange
Except for `get queue`, these management operations are defined in the [AMQP 0.9.1 protocol](https://www.rabbitmq.com/amqp-0-9-1-reference.html).
To support these AMQP 0.9.1 / RabbitMQ specific operations over AMQP 1.0, this project implements a subset of the following (most recent) AMQP 1.0 extension specifications:
* [AMQP Request-Response Messaging with Link Pairing Version 1.0 - Committee Specification 01](https://docs.oasis-open.org/amqp/linkpair/v1.0/cs01/linkpair-v1.0-cs01.html) (February 2021)
* [HTTP Semantics and Content over AMQP Version 1.0 - Working Draft 06](https://groups.oasis-open.org/higherlogic/ws/public/document?document_id=65571) (July 2019)
* [AMQP Management Version 1.0 - Working Draft 16](https://groups.oasis-open.org/higherlogic/ws/public/document?document_id=65575) (July 2019)
This project might support more (non AMQP 0.9.1) RabbitMQ operations via AMQP 1.0 in the future.
Topologies (exchanges, bindings, queues) in RabbitMQ can be created via
* [Management HTTP API](https://www.rabbitmq.com/docs/management#http-api)
* [Definition Import](https://www.rabbitmq.com/docs/definitions#import)
* AMQP 0.9.1 clients
* RabbitMQ AMQP 1.0 clients, such as this project

73
deps/rabbitmq_amqp_client/app.bzl vendored Normal file
View File

@ -0,0 +1,73 @@
load("@rules_erlang//:erlang_bytecode2.bzl", "erlang_bytecode")
load("@rules_erlang//:filegroup.bzl", "filegroup")
def all_beam_files(name = "all_beam_files"):
filegroup(
name = "beam_files",
srcs = [":other_beam"],
)
erlang_bytecode(
name = "other_beam",
srcs = ["src/rabbitmq_amqp_client.erl"],
hdrs = [":public_and_private_hdrs"],
app_name = "rabbitmq_amqp_client",
dest = "ebin",
erlc_opts = "//:erlc_opts",
deps = ["//deps/amqp10_common:erlang_app"],
)
def all_srcs(name = "all_srcs"):
filegroup(
name = "srcs",
srcs = ["src/rabbitmq_amqp_client.erl"],
)
filegroup(name = "private_hdrs")
filegroup(
name = "public_hdrs",
srcs = ["include/rabbitmq_amqp_client.hrl"],
)
filegroup(name = "priv")
filegroup(
name = "license_files",
srcs = [
"LICENSE",
"LICENSE-MPL-RabbitMQ",
],
)
filegroup(
name = "public_and_private_hdrs",
srcs = [":private_hdrs", ":public_hdrs"],
)
filegroup(
name = "all_srcs",
srcs = [":public_and_private_hdrs", ":srcs"],
)
def all_test_beam_files(name = "all_test_beam_files"):
erlang_bytecode(
name = "test_other_beam",
testonly = True,
srcs = ["src/rabbitmq_amqp_client.erl"],
hdrs = [":public_and_private_hdrs"],
app_name = "rabbitmq_amqp_client",
dest = "test",
erlc_opts = "//:test_erlc_opts",
deps = ["//deps/amqp10_common:erlang_app"],
)
filegroup(
name = "test_beam_files",
testonly = True,
srcs = [":test_other_beam"],
)
def test_suite_beam_files(name = "test_suite_beam_files"):
erlang_bytecode(
name = "management_SUITE_beam_files",
testonly = True,
srcs = ["test/management_SUITE.erl"],
outs = ["test/management_SUITE.beam"],
hdrs = ["include/rabbitmq_amqp_client.hrl"],
app_name = "rabbitmq_amqp_client",
erlc_opts = "//:test_erlc_opts",
deps = ["//deps/amqp10_common:erlang_app", "//deps/rabbit_common:erlang_app"],
)

1
deps/rabbitmq_amqp_client/erlang.mk vendored Symbolic link
View File

@ -0,0 +1 @@
../../erlang.mk

View File

@ -0,0 +1,4 @@
-record(link_pair, {session :: pid(),
outgoing_link :: amqp10_client:link_ref(),
incoming_link :: amqp10_client:link_ref()}).
-type link_pair() :: #link_pair{}.

View File

@ -0,0 +1 @@
../../rabbitmq-components.mk

View File

@ -0,0 +1,470 @@
%% This Source Code Form is subject to the terms of the Mozilla Public
%% License, v. 2.0. If a copy of the MPL was not distributed with this
%% file, You can obtain one at https://mozilla.org/MPL/2.0/.
%%
%% Copyright (c) 2007-2024 Broadcom. All Rights Reserved. The term Broadcom refers to Broadcom Inc. and/or its subsidiaries. All rights reserved.
-module(rabbitmq_amqp_client).
-feature(maybe_expr, enable).
-include("rabbitmq_amqp_client.hrl").
-include_lib("amqp10_common/include/amqp10_framing.hrl").
-export[
%% link pair operations
attach_management_link_pair_sync/2,
detach_management_link_pair_sync/1,
%% queue operations
get_queue/2,
declare_queue/3,
bind_queue/5,
unbind_queue/5,
purge_queue/2,
delete_queue/2,
%% exchange operations
declare_exchange/3,
bind_exchange/5,
unbind_exchange/5,
delete_exchange/2
].
-define(TIMEOUT, 20_000).
-define(MANAGEMENT_NODE_ADDRESS, <<"/management">>).
-type arguments() :: #{binary() => {atom(), term()}}.
-type queue_info() :: #{name := binary(),
vhost := binary(),
durable := boolean(),
exclusive := boolean(),
auto_delete := boolean(),
arguments := arguments(),
type := binary(),
message_count := non_neg_integer(),
consumer_count := non_neg_integer(),
replicas => [binary()],
leader => binary()}.
-type queue_properties() :: #{name := binary(),
durable => boolean(),
exclusive => boolean(),
auto_delete => boolean(),
arguments => arguments()}.
-type exchange_properties() :: #{name := binary(),
type => binary(),
durable => boolean(),
auto_delete => boolean(),
internal => boolean(),
arguments => arguments()}.
-type amqp10_prim() :: amqp10_binary_generator:amqp10_prim().
-spec attach_management_link_pair_sync(pid(), binary()) ->
{ok, link_pair()} | {error, term()}.
attach_management_link_pair_sync(Session, Name) ->
Terminus = #{address => ?MANAGEMENT_NODE_ADDRESS,
durable => none},
OutgoingAttachArgs = #{name => Name,
role => {sender, Terminus},
snd_settle_mode => settled,
rcv_settle_mode => first,
properties => #{<<"paired">> => true}},
IncomingAttachArgs = OutgoingAttachArgs#{role := {receiver, Terminus, self()},
filter => #{}},
maybe
{ok, OutgoingRef} ?= attach(Session, OutgoingAttachArgs),
{ok, IncomingRef} ?= attach(Session, IncomingAttachArgs),
ok ?= await_attached(OutgoingRef),
ok ?= await_attached(IncomingRef),
{ok, #link_pair{session = Session,
outgoing_link = OutgoingRef,
incoming_link = IncomingRef}}
end.
-spec attach(pid(), amqp10_client:attach_args()) ->
{ok, amqp10_client:link_ref()} | {error, term()}.
attach(Session, AttachArgs) ->
try amqp10_client:attach_link(Session, AttachArgs)
catch exit:Reason ->
{error, Reason}
end.
-spec await_attached(amqp10_client:link_ref()) ->
ok | {error, term()}.
await_attached(Ref) ->
receive
{amqp10_event, {link, Ref, attached}} ->
ok;
{amqp10_event, {link, Ref, {detached, Err}}} ->
{error, Err}
after ?TIMEOUT ->
{error, timeout}
end.
-spec detach_management_link_pair_sync(link_pair()) ->
ok | {error, term()}.
detach_management_link_pair_sync(
#link_pair{outgoing_link = OutgoingLink,
incoming_link = IncomingLink}) ->
maybe
ok ?= detach(OutgoingLink),
ok ?= detach(IncomingLink),
ok ?= await_detached(OutgoingLink),
await_detached(IncomingLink)
end.
-spec detach(amqp10_client:link_ref()) ->
ok | {error, term()}.
detach(Ref) ->
try amqp10_client:detach_link(Ref)
catch exit:Reason ->
{error, Reason}
end.
-spec await_detached(amqp10_client:link_ref()) ->
ok | {error, term()}.
await_detached(Ref) ->
receive
{amqp10_event, {link, Ref, {detached, normal}}} ->
ok;
{amqp10_event, {link, Ref, {detached, Err}}} ->
{error, Err}
after ?TIMEOUT ->
{error, timeout}
end.
-spec get_queue(link_pair(), binary()) ->
{ok, queue_info()} | {error, term()}.
get_queue(LinkPair, QueueName) ->
QNameQuoted = uri_string:quote(QueueName),
Props = #{subject => <<"GET">>,
to => <<"/queues/", QNameQuoted/binary>>},
case request(LinkPair, Props, null) of
{ok, Resp} ->
case is_success(Resp) of
true -> get_queue_info(Resp);
false -> {error, Resp}
end;
Err ->
Err
end.
-spec declare_queue(link_pair(), binary(), queue_properties()) ->
{ok, queue_info()} | {error, term()}.
declare_queue(LinkPair, QueueName, QueueProperties) ->
Body0 = maps:fold(
fun(durable, V, L) when is_boolean(V) ->
[{{utf8, <<"durable">>}, {boolean, V}} | L];
(exclusive, V, L) when is_boolean(V) ->
[{{utf8, <<"exclusive">>}, {boolean, V}} | L];
(auto_delete, V, L) when is_boolean(V) ->
[{{utf8, <<"auto_delete">>}, {boolean, V}} | L];
(arguments, V, L) ->
Args = encode_arguments(V),
[{{utf8, <<"arguments">>}, Args} | L]
end, [], QueueProperties),
Body = {map, Body0},
QNameQuoted = uri_string:quote(QueueName),
Props = #{subject => <<"PUT">>,
to => <<"/queues/", QNameQuoted/binary>>},
case request(LinkPair, Props, Body) of
{ok, Resp} ->
case is_success(Resp) of
true -> get_queue_info(Resp);
false -> {error, Resp}
end;
Err ->
Err
end.
-spec bind_queue(link_pair(), binary(), binary(), binary(), #{binary() => amqp10_prim()}) ->
ok | {error, term()}.
bind_queue(LinkPair, QueueName, ExchangeName, BindingKey, BindingArguments) ->
bind(<<"destination_queue">>, LinkPair, QueueName, ExchangeName, BindingKey, BindingArguments).
-spec bind_exchange(link_pair(), binary(), binary(), binary(), #{binary() => amqp10_prim()}) ->
ok | {error, term()}.
bind_exchange(LinkPair, Destination, Source, BindingKey, BindingArguments) ->
bind(<<"destination_exchange">>, LinkPair, Destination, Source, BindingKey, BindingArguments).
-spec bind(binary(), link_pair(), binary(), binary(), binary(), #{binary() => amqp10_prim()}) ->
ok | {error, term()}.
bind(DestinationKind, LinkPair, Destination, Source, BindingKey, BindingArguments) ->
Args = encode_arguments(BindingArguments),
Body = {map, [
{{utf8, <<"source">>}, {utf8, Source}},
{{utf8, DestinationKind}, {utf8, Destination}},
{{utf8, <<"binding_key">>}, {utf8, BindingKey}},
{{utf8, <<"arguments">>}, Args}
]},
Props = #{subject => <<"POST">>,
to => <<"/bindings">>},
case request(LinkPair, Props, Body) of
{ok, Resp} ->
case is_success(Resp) of
true -> ok;
false -> {error, Resp}
end;
Err ->
Err
end.
-spec unbind_queue(link_pair(), binary(), binary(), binary(), #{binary() => amqp10_prim()}) ->
ok | {error, term()}.
unbind_queue(LinkPair, QueueName, ExchangeName, BindingKey, BindingArguments) ->
unbind($q, LinkPair, QueueName, ExchangeName, BindingKey, BindingArguments).
-spec unbind_exchange(link_pair(), binary(), binary(), binary(), #{binary() => amqp10_prim()}) ->
ok | {error, term()}.
unbind_exchange(LinkPair, DestinationExchange, SourceExchange, BindingKey, BindingArguments) ->
unbind($e, LinkPair, DestinationExchange, SourceExchange, BindingKey, BindingArguments).
-spec unbind(byte(), link_pair(), binary(), binary(), binary(), #{binary() => amqp10_prim()}) ->
ok | {error, term()}.
unbind(DestinationChar, LinkPair, Destination, Source, BindingKey, BindingArguments)
when map_size(BindingArguments) =:= 0 ->
SrcQ = uri_string:quote(Source),
DstQ = uri_string:quote(Destination),
KeyQ = uri_string:quote(BindingKey),
Uri = <<"/bindings/src=", SrcQ/binary,
";dst", DestinationChar, $=, DstQ/binary,
";key=", KeyQ/binary,
";args=">>,
delete_binding(LinkPair, Uri);
unbind(DestinationChar, LinkPair, Destination, Source, BindingKey, BindingArguments) ->
Path = <<"/bindings">>,
Query = uri_string:compose_query(
[{<<"src">>, Source},
{<<"dst", DestinationChar>>, Destination},
{<<"key">>, BindingKey}]),
Uri0 = uri_string:recompose(#{path => Path,
query => Query}),
Props = #{subject => <<"GET">>,
to => Uri0},
case request(LinkPair, Props, null) of
{ok, Resp} ->
case is_success(Resp) of
true ->
#'v1_0.amqp_value'{content = {list, Bindings}} = amqp10_msg:body(Resp),
case search_binding_uri(BindingArguments, Bindings) of
{ok, Uri} ->
delete_binding(LinkPair, Uri);
not_found ->
ok
end;
false ->
{error, Resp}
end;
Err ->
Err
end.
search_binding_uri(_, []) ->
not_found;
search_binding_uri(BindingArguments, [{map, Binding} | Bindings]) ->
case maps:from_list(Binding) of
#{{utf8, <<"arguments">>} := {map, Args0},
{utf8, <<"location">>} := {utf8, Uri}} ->
Args = lists:map(fun({{utf8, Key}, TypeVal}) ->
{Key, TypeVal}
end, Args0),
case maps:from_list(Args) =:= BindingArguments of
true ->
{ok, Uri};
false ->
search_binding_uri(BindingArguments, Bindings)
end;
_ ->
search_binding_uri(BindingArguments, Bindings)
end.
-spec delete_binding(link_pair(), binary()) ->
ok | {error, term()}.
delete_binding(LinkPair, BindingUri) ->
Props = #{subject => <<"DELETE">>,
to => BindingUri},
case request(LinkPair, Props, null) of
{ok, Resp} ->
case is_success(Resp) of
true -> ok;
false -> {error, Resp}
end;
Err ->
Err
end.
-spec delete_queue(link_pair(), binary()) ->
{ok, map()} | {error, term()}.
delete_queue(LinkPair, QueueName) ->
purge_or_delete_queue(LinkPair, QueueName, <<>>).
-spec purge_queue(link_pair(), binary()) ->
{ok, map()} | {error, term()}.
purge_queue(LinkPair, QueueName) ->
purge_or_delete_queue(LinkPair, QueueName, <<"/messages">>).
-spec purge_or_delete_queue(link_pair(), binary(), binary()) ->
{ok, map()} | {error, term()}.
purge_or_delete_queue(LinkPair, QueueName, PathSuffix) ->
QNameQuoted = uri_string:quote(QueueName),
HttpRequestTarget = <<"/queues/", QNameQuoted/binary, PathSuffix/binary>>,
Props = #{subject => <<"DELETE">>,
to => HttpRequestTarget},
case request(LinkPair, Props, null) of
{ok, Resp} ->
case is_success(Resp) of
true ->
#'v1_0.amqp_value'{content = Content} = amqp10_msg:body(Resp),
{map, [
{{utf8, <<"message_count">>}, {ulong, Count}}
]
} = Content,
{ok, #{message_count => Count}};
false ->
{error, Resp}
end;
Err ->
Err
end.
-spec declare_exchange(link_pair(), binary(), exchange_properties()) ->
ok | {error, term()}.
declare_exchange(LinkPair, ExchangeName, ExchangeProperties) ->
Body0 = maps:fold(
fun(type, V, L) when is_binary(V) ->
[{{utf8, <<"type">>}, {utf8, V}} | L];
(durable, V, L) when is_boolean(V) ->
[{{utf8, <<"durable">>}, {boolean, V}} | L];
(auto_delete, V, L) when is_boolean(V) ->
[{{utf8, <<"auto_delete">>}, {boolean, V}} | L];
(internal, V, L) when is_boolean(V) ->
[{{utf8, <<"internal">>}, {boolean, V}} | L];
(arguments, V, L) ->
Args = encode_arguments(V),
[{{utf8, <<"arguments">>}, Args} | L]
end, [], ExchangeProperties),
Body = {map, Body0},
XNameQuoted = uri_string:quote(ExchangeName),
Props = #{subject => <<"PUT">>,
to => <<"/exchanges/", XNameQuoted/binary>>},
case request(LinkPair, Props, Body) of
{ok, Resp} ->
case is_success(Resp) of
true -> ok;
false -> {error, Resp}
end;
Err ->
Err
end.
-spec delete_exchange(link_pair(), binary()) ->
ok | {error, term()}.
delete_exchange(LinkPair, ExchangeName) ->
XNameQuoted = uri_string:quote(ExchangeName),
Props = #{subject => <<"DELETE">>,
to => <<"/exchanges/", XNameQuoted/binary>>},
case request(LinkPair, Props, null) of
{ok, Resp} ->
case is_success(Resp) of
true -> ok;
false -> {error, Resp}
end;
Err ->
Err
end.
-spec request(link_pair(), amqp10_msg:amqp10_properties(), amqp10_prim()) ->
{ok, Response :: amqp10_msg:amqp10_msg()} | {error, term()}.
request(#link_pair{session = Session,
outgoing_link = OutgoingLink,
incoming_link = IncomingLink}, Properties, Body) ->
MessageId = message_id(),
Properties1 = Properties#{message_id => {binary, MessageId},
reply_to => <<"$me">>},
Request = amqp10_msg:new(<<>>, #'v1_0.amqp_value'{content = Body}, true),
Request1 = amqp10_msg:set_properties(Properties1, Request),
ok = amqp10_client:flow_link_credit(IncomingLink, 1, never),
case amqp10_client:send_msg(OutgoingLink, Request1) of
ok ->
receive {amqp10_msg, IncomingLink, Response} ->
#{correlation_id := MessageId} = amqp10_msg:properties(Response),
{ok, Response};
{amqp10_event, {session, Session, {ended, Reason}}} ->
{error, {session_ended, Reason}}
after ?TIMEOUT ->
{error, timeout}
end;
Err ->
Err
end.
-spec get_queue_info(amqp10_msg:amqp10_msg()) ->
{ok, queue_info()}.
get_queue_info(Response) ->
#'v1_0.amqp_value'{content = {map, KVList}} = amqp10_msg:body(Response),
Map = lists:foldl(
fun({{utf8, Key = <<"arguments">>}, {map, KVList0}}, Acc) ->
Map = lists:foldl(fun({{utf8, K}, TypeVal}, M) ->
M#{K => TypeVal}
end, #{}, KVList0),
Acc#{to_atom(Key) => Map};
({{utf8, Key = <<"replicas">>}, {array, utf8, Arr}}, Acc) ->
L = lists:map(fun({utf8, Replica}) ->
Replica
end, Arr),
Acc#{to_atom(Key) => L};
({{utf8, Key}, TypeVal}, Acc) ->
Acc#{to_atom(Key) => amqp10_client_types:unpack(TypeVal)}
end, #{}, KVList),
{ok, Map}.
to_atom(<<"name">>) -> name;
to_atom(<<"vhost">>) -> vhost;
to_atom(<<"durable">>) -> durable;
to_atom(<<"exclusive">>) -> exclusive;
to_atom(<<"auto_delete">>) -> auto_delete;
to_atom(<<"arguments">>) -> arguments;
to_atom(<<"type">>) -> type;
to_atom(<<"message_count">>) -> message_count;
to_atom(<<"consumer_count">>) -> consumer_count;
to_atom(<<"replicas">>) -> replicas;
to_atom(<<"leader">>) -> leader.
-spec encode_arguments(arguments()) ->
{map, list(tuple())}.
encode_arguments(Arguments) ->
KVList = maps:fold(
fun(Key, TaggedVal, L)
when is_binary(Key) ->
[{{utf8, Key}, TaggedVal} | L]
end, [], Arguments),
{map, KVList}.
%% "The message producer is usually responsible for setting the message-id in
%% such a way that it is assured to be globally unique." [3.2.4]
-spec message_id() -> binary().
message_id() ->
rand:bytes(8).
%% All successful 2xx and redirection 3xx status codes are interpreted as success.
%% We don't hard code any specific status code for now as the returned status
%% codes from RabbitMQ are subject to change.
-spec is_success(amqp10_msg:amqp10_msg()) -> boolean().
is_success(Response) ->
case amqp10_msg:properties(Response) of
#{subject := <<C, _, _>>}
when C =:= $2 orelse
C =:= $3 ->
true;
_ ->
false
end.

File diff suppressed because it is too large Load Diff

View File

@ -979,7 +979,7 @@ connections_test_amqp(Config) ->
auth_mechanism := <<"PLAIN">>,
protocol := <<"AMQP 1-0">>,
client_properties := #{version := _,
product := <<"AMQP 1.0 client from the RabbitMQ Project">>,
product := <<"AMQP 1.0 client">>,
platform := _}},
Connection1),
ConnectionName = maps:get(name, Connection1),

View File

@ -545,6 +545,7 @@ rabbit:
- rabbit_access_control
- rabbit_alarm
- rabbit_amqp1_0
- rabbit_amqp_management
- rabbit_amqp_reader
- rabbit_amqp_session
- rabbit_amqp_session_sup
@ -824,6 +825,8 @@ rabbit_common:
- worker_pool
- worker_pool_sup
- worker_pool_worker
rabbitmq_amqp_client:
- rabbitmq_amqp_client
rabbitmq_amqp1_0:
- rabbitmq_amqp1_0_noop
rabbitmq_auth_backend_cache: