-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathrabbit_web_dispatch_access_control.erl
371 lines (321 loc) · 14.6 KB
/
rabbit_web_dispatch_access_control.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
%% 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(rabbit_web_dispatch_access_control).
-include("rabbitmq_web_dispatch_records.hrl").
-include_lib("amqp_client/include/amqp_client.hrl").
-export([is_authorized/3, is_authorized/7, is_authorized_admin/3,
is_authorized_admin/5, vhost/1, vhost_from_headers/1]).
-export([is_authorized_vhost/3, is_authorized_user/4,
is_authorized_user/5, is_authorized_user/6,
is_authorized_monitor/3, is_authorized_policies/3,
is_authorized_vhost_visible/3,
is_authorized_vhost_visible_for_monitoring/3,
is_authorized_global_parameters/3]).
-export([list_visible_vhosts/1, list_visible_vhosts_names/1, list_login_vhosts/2]).
-export([id/2]).
-export([not_authorised/3, halt_response/5]).
-export([is_admin/1, is_policymaker/1, is_monitor/1, is_mgmt_user/1]).
-import(rabbit_misc, [pget/2]).
is_authorized(ReqData, Context, AuthConfig) ->
is_authorized(ReqData, Context, '', fun(_) -> true end, AuthConfig).
is_authorized_admin(ReqData, Context, AuthConfig) ->
is_authorized(ReqData, Context,
<<"Not administrator user">>,
fun(#user{tags = Tags}) -> is_admin(Tags) end, AuthConfig).
is_authorized_admin(ReqData, Context, Username, Password, AuthConfig) ->
case is_basic_auth_disabled(AuthConfig) of
true ->
Msg = "HTTP access denied: basic auth disabled",
rabbit_log:warning(Msg),
not_authorised(Msg, ReqData, Context);
false ->
is_authorized(ReqData, Context, Username, Password,
<<"Not administrator user">>,
fun(#user{tags = Tags}) -> is_admin(Tags) end, AuthConfig)
end.
is_authorized_monitor(ReqData, Context, AuthConfig) ->
is_authorized(ReqData, Context,
<<"Not monitor user">>,
fun(#user{tags = Tags}) -> is_monitor(Tags) end,
AuthConfig).
is_authorized_vhost(ReqData, Context, AuthConfig) ->
is_authorized(ReqData, Context,
<<"User not authorised to access virtual host">>,
fun(#user{tags = Tags} = User) ->
is_admin(Tags) orelse user_matches_vhost(ReqData, User)
end,
AuthConfig).
is_authorized_vhost_visible(ReqData, Context, AuthConfig) ->
is_authorized(ReqData, Context,
<<"User not authorised to access virtual host">>,
fun(#user{tags = Tags} = User) ->
is_admin(Tags) orelse user_matches_vhost_visible(ReqData, User)
end,
AuthConfig).
is_authorized_vhost_visible_for_monitoring(ReqData, Context, AuthConfig) ->
is_authorized(ReqData, Context,
<<"User not authorised to access virtual host">>,
fun(#user{tags = Tags} = User) ->
is_admin(Tags)
orelse is_monitor(Tags)
orelse user_matches_vhost_visible(ReqData, User)
end,
AuthConfig).
is_authorized(ReqData, Context, ErrorMsg, Fun, AuthConfig) ->
case cowboy_req:method(ReqData) of
<<"OPTIONS">> -> {true, ReqData, Context};
_ -> is_authorized1(ReqData, Context, ErrorMsg, Fun, AuthConfig)
end.
is_authorized1(ReqData, Context, ErrorMsg, Fun, AuthConfig) ->
case cowboy_req:parse_header(<<"authorization">>, ReqData) of
{basic, Username, Password} ->
case is_basic_auth_disabled(AuthConfig) of
true ->
Msg = "HTTP access denied: basic auth disabled",
rabbit_log:warning(Msg),
not_authorised(Msg, ReqData, Context);
false ->
is_authorized(ReqData, Context,
Username, Password,
ErrorMsg, Fun, AuthConfig)
end;
{bearer, Token} ->
% Username is only used in case is_authorized is not able to parse the token
% and extact the username from it
Username = AuthConfig#auth_settings.oauth_client_id,
is_authorized(ReqData, Context, Username, Token, ErrorMsg, Fun, AuthConfig);
_ ->
case is_basic_auth_disabled(AuthConfig) of
true ->
Msg = "HTTP access denied: basic auth disabled",
rabbit_log:warning(Msg),
not_authorised(Msg, ReqData, Context);
false ->
{{false, AuthConfig#auth_settings.auth_realm}, ReqData, Context}
end
end.
is_authorized_user(ReqData, Context, Username, Password, AuthConfig) ->
Msg = <<"User not authorized">>,
Fun = fun(_) -> true end,
is_authorized(ReqData, Context, Username, Password, Msg, Fun, AuthConfig).
is_authorized_user(ReqData, Context, Username, Password, ReplyWhenFailed, AuthConfig) ->
Msg = <<"User not authorized">>,
Fun = fun(_) -> true end,
is_authorized(ReqData, Context, Username, Password, Msg, Fun, AuthConfig, ReplyWhenFailed).
is_authorized(ReqData, Context, Username, Password, ErrorMsg, Fun, AuthConfig) ->
is_authorized(ReqData, Context, Username, Password, ErrorMsg, Fun, AuthConfig, true).
is_authorized(ReqData, Context, Username, Password, ErrorMsg, Fun, AuthConfig, ReplyWhenFailed) ->
ErrFun = fun (ResolvedUserName, Msg) ->
rabbit_log:warning("HTTP access denied: user '~ts' - ~ts",
[ResolvedUserName, Msg]),
case ReplyWhenFailed of
true -> not_authorised(Msg, ReqData, Context);
false -> {false, ReqData, "Not_Authorized"}
end
end,
AuthProps = [{password, Password}] ++ case vhost(ReqData) of
VHost when is_binary(VHost) -> [{vhost, VHost}];
_ -> []
end,
{IP, _} = cowboy_req:peer(ReqData),
case rabbit_access_control:check_user_login(Username, AuthProps) of
{ok, User = #user{username = ResolvedUsername, tags = Tags}} ->
case rabbit_access_control:check_user_loopback(ResolvedUsername, IP) of
ok ->
case is_mgmt_user(Tags) of
true ->
case Fun(User) of
true ->
rabbit_core_metrics:auth_attempt_succeeded(IP, ResolvedUsername, http),
{true, ReqData,
Context#context{user = User,
password = Password}};
false ->
rabbit_core_metrics:auth_attempt_failed(IP, ResolvedUsername, http),
ErrFun(ResolvedUsername, ErrorMsg)
end;
false ->
rabbit_core_metrics:auth_attempt_failed(IP, ResolvedUsername, http),
ErrFun(ResolvedUsername, <<"Not management user">>)
end;
not_allowed ->
rabbit_core_metrics:auth_attempt_failed(IP, ResolvedUsername, http),
ErrFun(ResolvedUsername, <<"User can only log in via localhost">>)
end;
{refused, _Username, Msg, Args} ->
rabbit_core_metrics:auth_attempt_failed(IP, Username, http),
rabbit_log:warning("HTTP access denied: ~ts",
[rabbit_misc:format(Msg, Args)]),
case ReplyWhenFailed of
true -> not_authenticated(<<"Not_Authorized">>, ReqData, Context, AuthConfig);
false -> {false, ReqData, "Not_Authorized"}
end
end.
%% Used for connections / channels. A normal user can only see / delete
%% their own stuff. Monitors can see other users' and delete their
%% own. Admins can do it all.
is_authorized_user(ReqData, Context, Item, AuthConfig) ->
is_authorized(ReqData, Context,
<<"User not authorised to access object">>,
fun(#user{username = Username, tags = Tags}) ->
case cowboy_req:method(ReqData) of
<<"DELETE">> -> is_admin(Tags);
_ -> is_monitor(Tags)
end orelse Username == pget(user, Item)
end,
AuthConfig).
%% For policies / parameters. Like is_authorized_vhost but you have to
%% be a policymaker.
is_authorized_policies(ReqData, Context, AuthConfig) ->
is_authorized(ReqData, Context,
<<"User not authorised to access object">>,
fun(User = #user{tags = Tags}) ->
is_admin(Tags) orelse
(is_policymaker(Tags) andalso
user_matches_vhost(ReqData, User))
end,
AuthConfig).
%% For global parameters. Must be policymaker.
is_authorized_global_parameters(ReqData, Context, AuthConfig) ->
is_authorized(ReqData, Context,
<<"User not authorised to access object">>,
fun(#user{tags = Tags}) ->
is_policymaker(Tags)
end,
AuthConfig).
vhost_from_headers(ReqData) ->
case cowboy_req:header(<<"x-vhost">>, ReqData) of
undefined -> none;
%% blank x-vhost means "All hosts" is selected in the UI
<<>> -> none;
VHost -> VHost
end.
vhost(ReqData) ->
Value = case id(vhost, ReqData) of
none -> vhost_from_headers(ReqData);
VHost -> VHost
end,
case Value of
none -> none;
Name ->
case rabbit_vhost:exists(Name) of
true -> Name;
false -> not_found
end
end.
is_admin(T) -> intersects(T, [administrator]).
is_policymaker(T) -> intersects(T, [administrator, policymaker]).
is_monitor(T) -> intersects(T, [administrator, monitoring]).
is_mgmt_user(T) -> intersects(T, [administrator, monitoring, policymaker,
management]).
intersects(A, B) -> lists:any(fun(I) -> lists:member(I, B) end, A).
user_matches_vhost(ReqData, User) ->
case vhost(ReqData) of
not_found -> true;
none -> true;
V ->
AuthzData = get_authz_data(ReqData),
lists:member(V, list_login_vhosts_names(User, AuthzData))
end.
user_matches_vhost_visible(ReqData, User) ->
case vhost(ReqData) of
not_found -> true;
none -> true;
V ->
AuthzData = get_authz_data(ReqData),
lists:member(V, list_visible_vhosts_names(User, AuthzData))
end.
get_authz_data(ReqData) ->
{PeerAddress, _PeerPort} = cowboy_req:peer(ReqData),
{ip, PeerAddress}.
not_authorised(Reason, ReqData, Context) ->
%% TODO: consider changing to 403 in 4.0
halt_response(401, not_authorised, Reason, ReqData, Context).
halt_response(Code, Type, Reason, ReqData, Context) ->
ReasonFormatted = format_reason(Reason),
Json = #{<<"error">> => Type,
<<"reason">> => ReasonFormatted},
ReqData1 = cowboy_req:reply(Code,
#{<<"content-type">> => <<"application/json">>},
rabbit_json:encode(Json), ReqData),
{stop, ReqData1, Context}.
not_authenticated(Reason, ReqData, Context,
#auth_settings{auth_realm = AuthRealm} = AuthConfig) ->
case is_oauth2_enabled(AuthConfig) of
false ->
ReqData1 = cowboy_req:set_resp_header(<<"www-authenticate">>, AuthRealm, ReqData),
halt_response(401, not_authorized, Reason, ReqData1, Context);
true ->
halt_response(401, not_authorized, Reason, ReqData, Context)
end.
format_reason(Tuple) when is_tuple(Tuple) ->
tuple(Tuple);
format_reason(Binary) when is_binary(Binary) ->
Binary;
format_reason(Other) ->
case is_string(Other) of
true -> print("~ts", [Other]);
false -> print("~tp", [Other])
end.
print(Fmt, Val) when is_list(Val) ->
list_to_binary(lists:flatten(io_lib:format(Fmt, Val))).
is_string(List) when is_list(List) ->
lists:all(
fun(El) -> is_integer(El) andalso El > 0 andalso El < 16#10ffff end,
List);
is_string(_) -> false.
tuple(unknown) -> unknown;
tuple(Tuple) when is_tuple(Tuple) -> [tuple(E) || E <- tuple_to_list(Tuple)];
tuple(Term) -> Term.
id(Key, ReqData) when Key =:= exchange;
Key =:= source;
Key =:= destination ->
case id0(Key, ReqData) of
<<"amq.default">> -> <<"">>;
Name -> Name
end;
id(Key, ReqData) ->
id0(Key, ReqData).
id0(Key, ReqData) ->
case cowboy_req:binding(Key, ReqData) of
undefined -> none;
Id -> Id
end.
list_visible_vhosts_names(User) ->
list_visible_vhosts(User, undefined).
list_visible_vhosts_names(User, AuthzData) ->
list_visible_vhosts(User, AuthzData).
list_visible_vhosts(User) ->
list_visible_vhosts(User, undefined).
list_visible_vhosts(User = #user{tags = Tags}, AuthzData) ->
case is_monitor(Tags) of
true -> rabbit_vhost:list_names();
false -> list_login_vhosts_names(User, AuthzData)
end.
list_login_vhosts_names(User, AuthzData) ->
[V || V <- rabbit_vhost:list_names(),
case catch rabbit_access_control:check_vhost_access(User, V, AuthzData, #{}) of
ok -> true;
NotOK ->
log_access_control_result(NotOK),
false
end].
list_login_vhosts(User, AuthzData) ->
[V || V <- rabbit_vhost:all(),
case catch rabbit_access_control:check_vhost_access(User, vhost:get_name(V), AuthzData, #{}) of
ok -> true;
NotOK ->
log_access_control_result(NotOK),
false
end].
% rabbitmq/rabbitmq-auth-backend-http#100
log_access_control_result(NotOK) ->
rabbit_log:debug("rabbit_access_control:check_vhost_access result: ~tp", [NotOK]).
is_basic_auth_disabled(#auth_settings{basic_auth_enabled = Enabled}) ->
not Enabled.
is_oauth2_enabled(#auth_settings{oauth2_enabled = Enabled}) ->
Enabled.