Skip to content

Commit f136231

Browse files
Merge pull request #1232 from rabbitmq/rabbitmq-server-1229
Add tests for variables expansion in topic authz
2 parents 23514b5 + 1e00057 commit f136231

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

docs/rabbitmqctl.8

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,100 @@ has been granted access, and the permissions the user has for operations
784784
on resources in these virtual hosts:
785785
.sp
786786
.Dl rabbitmqctl list_user_permissions tonyg
787+
.\" ------------------------------------
788+
.It Cm set_topic_permissions Oo Fl p Ar vhost Oc Ar user Ar exchange Ar write Ar read
789+
.Bl -tag -width Ds
790+
.It Ar vhost
791+
The name of the virtual host to which to grant the user access,
792+
defaulting to
793+
.Qq / .
794+
.It Ar user
795+
The name of the user the permissions apply to in the target virtual host.
796+
.It Ar exchange
797+
The name of the topic exchange the authorisation check will be applied to.
798+
.It Ar write
799+
A regular expression matching the routing key of the published message.
800+
.It Ar read
801+
A regular expression matching the routing key of the consumed message.
802+
.El
803+
.Pp
804+
Sets user topic permissions.
805+
.Pp
806+
For example, this command instructs the RabbitMQ broker to let the
807+
user named
808+
.Qq tonyg
809+
publish and consume messages going through the
810+
.Qq amp.topic
811+
exchange of the
812+
.Qq /myvhost
813+
virtual host with a routing key starting with
814+
.Qq tonyg- :
815+
.sp
816+
.Dl rabbitmqctl set_topic_permissions -p /myvhost tonyg amq.topic Qo ^tonyg-.* Qc Qo ^tonyg-.* Qc
817+
.Pp
818+
Topic permissions support variable expansion for the following variables:
819+
username, vhost, and client_id. Note that client_id is expanded only when using MQTT.
820+
The previous example could be made more generic by using
821+
.Qq ^{username}-.* :
822+
.sp
823+
.Dl rabbitmqctl set_topic_permissions -p /myvhost tonyg amq.topic Qo ^{username}-.* Qc Qo ^{username}-.* Qc
824+
.\" ------------------------------------
825+
.It Cm clear_topic_permissions Oo Fl p Ar vhost Oc Ar username Oo Ar exchange Oc
826+
.Bl -tag -width Ds
827+
.It Ar vhost
828+
The name of the virtual host to which to clear the topic permissions,
829+
defaulting to
830+
.Qq / .
831+
.It Ar username
832+
The name of the user to clear topic permissions to the specified virtual host.
833+
.It Ar exchange
834+
The name of the topic exchange to clear topic permissions, defaulting to all the
835+
topic exchanges the given user has topic permissions for.
836+
.El
837+
.Pp
838+
Clear user topic permissions.
839+
.Pp
840+
For example, this command instructs the RabbitMQ broker to remove topic permissions for user
841+
named
842+
.Qq tonyg
843+
for the topic exchange
844+
.Qq amq.topic
845+
in the virtual host called
846+
.Qq /myvhost :
847+
.sp
848+
.Dl rabbitmqctl clear_topic_permissions -p /myvhost tonyg amq.topic
849+
.\" ------------------------------------
850+
.It Cm list_topic_permissions Op Fl p Ar vhost
851+
.Bl -tag -width Ds
852+
.It Ar vhost
853+
The name of the virtual host for which to list the users topic permissions.
854+
Defaults to
855+
.Qq / .
856+
.El
857+
.Pp
858+
Lists topic permissions in a virtual host.
859+
.Pp
860+
For example, this command instructs the RabbitMQ broker to list all the
861+
users which have been granted topic permissions in the virtual host called
862+
.Qq /myvhost:
863+
.sp
864+
.Dl rabbitmqctl list_topic_permissions -p /myvhost
865+
.\" ------------------------------------
866+
.It Cm list_user_topic_permissions Ar username
867+
.Bl -tag -width Ds
868+
.It Ar username
869+
The name of the user for which to list the topic permissions.
870+
.El
871+
.Pp
872+
Lists user topic permissions.
873+
.Pp
874+
For example, this command instructs the RabbitMQ broker to list all the
875+
virtual hosts to which the user named
876+
.Qq tonyg
877+
has been granted access, and the topic permissions the user has in these virtual hosts:
878+
.sp
879+
.Dl rabbitmqctl list_topic_user_permissions tonyg
880+
787881
.El
788882
.Ss Parameter Management
789883
Certain features of RabbitMQ (such as the federation plugin) are

test/topic_permission_SUITE.erl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,36 @@ topic_permission_checks1(_Config) ->
218218
Perm,
219219
Context
220220
) || Perm <- Permissions],
221+
222+
%% expand variables
223+
rabbit_auth_backend_internal:set_topic_permissions(
224+
<<"guest">>, <<"other-vhost">>, <<"amq.topic">>,
225+
"services.{vhost}.accounts.{username}.notifications",
226+
"services.{vhost}.accounts.{username}.notifications", <<"acting-user">>
227+
),
228+
%% routing key OK
229+
[true = rabbit_auth_backend_internal:check_topic_access(
230+
User,
231+
Topic#resource{virtual_host = <<"other-vhost">>},
232+
Perm,
233+
#{routing_key => <<"services.other-vhost.accounts.guest.notifications">>,
234+
variable_map => #{
235+
<<"username">> => <<"guest">>,
236+
<<"vhost">> => <<"other-vhost">>
237+
}
238+
}
239+
) || Perm <- Permissions],
240+
%% routing key KO
241+
[false = rabbit_auth_backend_internal:check_topic_access(
242+
User,
243+
Topic#resource{virtual_host = <<"other-vhost">>},
244+
Perm,
245+
#{routing_key => <<"services.default.accounts.dummy.notifications">>,
246+
variable_map => #{
247+
<<"username">> => <<"guest">>,
248+
<<"vhost">> => <<"other-vhost">>
249+
}
250+
}
251+
) || Perm <- Permissions],
252+
221253
ok.

0 commit comments

Comments
 (0)