|
| 1 | +%% This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +%% License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +%% file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 4 | +%% |
| 5 | +%% Copyright (c) 2007-2022 VMware, Inc. or its affiliates. All rights reserved. |
| 6 | +%% |
| 7 | + |
| 8 | +-module(rabbit_mgmt_wm_connection_user_name). |
| 9 | + |
| 10 | +-export([init/2, to_json/2, content_types_provided/2, |
| 11 | + is_authorized/2, allowed_methods/2, delete_resource/2]). |
| 12 | +-export([variances/2]). |
| 13 | + |
| 14 | +-include_lib("rabbitmq_management_agent/include/rabbit_mgmt_records.hrl"). |
| 15 | +-include_lib("rabbit_common/include/rabbit.hrl"). |
| 16 | + |
| 17 | +%%-------------------------------------------------------------------- |
| 18 | + |
| 19 | +init(Req, _State) -> |
| 20 | + {cowboy_rest, rabbit_mgmt_headers:set_common_permission_headers(Req, ?MODULE), #context{}}. |
| 21 | + |
| 22 | +variances(Req, Context) -> |
| 23 | + {[<<"accept-encoding">>, <<"origin">>], Req, Context}. |
| 24 | + |
| 25 | +content_types_provided(ReqData, Context) -> |
| 26 | + {rabbit_mgmt_util:responder_map(to_json), ReqData, Context}. |
| 27 | + |
| 28 | +allowed_methods(ReqData, Context) -> |
| 29 | + {[<<"HEAD">>, <<"GET">>, <<"DELETE">>, <<"OPTIONS">>], ReqData, Context}. |
| 30 | + |
| 31 | +to_json(ReqData, Context) -> |
| 32 | + {ok, _Username, UserConns} = list_user_connections(ReqData), |
| 33 | + FilteredConns = rabbit_mgmt_util:filter_tracked_conn_list(UserConns, ReqData, Context), |
| 34 | + rabbit_mgmt_util:reply_list_or_paginate(FilteredConns, ReqData, Context). |
| 35 | + |
| 36 | +delete_resource(ReqData, Context) -> |
| 37 | + delete_resource(list_user_connections(ReqData), ReqData, Context). |
| 38 | + |
| 39 | +delete_resource({ok, _Username, []}, ReqData, Context) -> |
| 40 | + {true, ReqData, Context}; |
| 41 | +delete_resource({ok, Username, UserConns}, ReqData, Context) -> |
| 42 | + ok = close_user_connections(UserConns, Username, ReqData), |
| 43 | + {true, ReqData, Context}. |
| 44 | + |
| 45 | +is_authorized(ReqData, Context) -> |
| 46 | + try |
| 47 | + UserConns = list_user_connections(ReqData), |
| 48 | + rabbit_mgmt_util:is_authorized_user(ReqData, Context, UserConns) |
| 49 | + catch |
| 50 | + {error, invalid_range_parameters, Reason} -> |
| 51 | + rabbit_mgmt_util:bad_request(iolist_to_binary(Reason), ReqData, Context) |
| 52 | + end. |
| 53 | + |
| 54 | +%%-------------------------------------------------------------------- |
| 55 | + |
| 56 | +list_user_connections(ReqData) -> |
| 57 | + Username = rabbit_mgmt_util:id(username, ReqData), |
| 58 | + UserConns = rabbit_connection_tracking:list_of_user(Username), |
| 59 | + {ok, Username, UserConns}. |
| 60 | + |
| 61 | +close_user_connections([], _Username, _ReqData) -> |
| 62 | + ok; |
| 63 | +close_user_connections([Conn | Rest], Username, ReqData) -> |
| 64 | + ok = close_user_connection(Conn, Username, ReqData), |
| 65 | + close_user_connections(Rest, Username, ReqData). |
| 66 | + |
| 67 | +close_user_connection(#tracked_connection{name = Name, pid = Pid, username = Username, type = Type}, Username, ReqData) when is_pid(Pid) -> |
| 68 | + Conn = [{name, Name}, {pid, Pid}, {user, Username}, {type, Type}], |
| 69 | + force_close_connection(ReqData, Conn, Pid); |
| 70 | +close_user_connection(#tracked_connection{pid = undefined}, _Username, _ReqData) -> |
| 71 | + ok; |
| 72 | +close_user_connection(UnexpectedConn, Username, _ReqData) -> |
| 73 | + rabbit_log:debug("~p Username: ~p", [?MODULE, Username]), |
| 74 | + rabbit_log:debug("~p unexpected connection: ~p", [?MODULE, UnexpectedConn]), |
| 75 | + ok. |
| 76 | + |
| 77 | +force_close_connection(ReqData, Conn, Pid) -> |
| 78 | + Reason = case cowboy_req:header(<<"x-reason">>, ReqData) of |
| 79 | + undefined -> "Closed via management plugin"; |
| 80 | + V -> binary_to_list(V) |
| 81 | + end, |
| 82 | + case proplists:get_value(type, Conn) of |
| 83 | + direct -> |
| 84 | + amqp_direct_connection:server_close(Pid, 320, Reason); |
| 85 | + network -> |
| 86 | + rabbit_networking:close_connection(Pid, Reason); |
| 87 | + _ -> |
| 88 | + % best effort, this will work for connections to the stream plugin |
| 89 | + gen_server:cast(Pid, {shutdown, Reason}) |
| 90 | + end, |
| 91 | + ok. |
0 commit comments