|
| 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) 2023-2023 VMware, Inc. or its affiliates. All rights reserved. |
| 6 | +%% |
| 7 | + |
| 8 | +-module(rabbit_db_vhost_defaults). |
| 9 | + |
| 10 | +-export([apply/2]). |
| 11 | +-export([list_limits/1, list_operator_policies/1, list_users/1]). |
| 12 | + |
| 13 | +-type definitions() :: [{binary(), term()}]. |
| 14 | + |
| 15 | +-record(seeding_policy, { |
| 16 | + name :: binary(), |
| 17 | + queue_pattern = <<".*">> :: binary(), |
| 18 | + definition = [] :: definitions() |
| 19 | +}). |
| 20 | + |
| 21 | +-type seeded_user_properties() :: #{ |
| 22 | + name := binary(), |
| 23 | + configure := binary(), |
| 24 | + read := binary(), |
| 25 | + write := binary(), |
| 26 | + password := binary(), |
| 27 | + tags := [atom()], |
| 28 | + _ => _ |
| 29 | +}. |
| 30 | + |
| 31 | +%% Apply all matching defaults to a VHost. |
| 32 | +-spec apply(vhost:name(), rabbit_types:username()) -> ok. |
| 33 | +apply(VHost, ActingUser) -> |
| 34 | + case list_limits(VHost) of |
| 35 | + [] -> |
| 36 | + ok; |
| 37 | + L -> |
| 38 | + ok = rabbit_vhost_limit:set(VHost, L, ActingUser), |
| 39 | + rabbit_log:info("Applied default limits to vhost '~tp': ~tp", [VHost, L]) |
| 40 | + end, |
| 41 | + lists:foreach( |
| 42 | + fun(P) -> |
| 43 | + ok = rabbit_policy:set_op(VHost, P#seeding_policy.name, P#seeding_policy.queue_pattern, P#seeding_policy.definition, |
| 44 | + undefined, undefined, ActingUser), |
| 45 | + rabbit_log:info("Applied default operator policy to vhost '~tp': ~tp", [VHost, P]) |
| 46 | + end, |
| 47 | + list_operator_policies(VHost) |
| 48 | + ), |
| 49 | + lists:foreach( |
| 50 | + fun(U) -> |
| 51 | + ok = add_user(VHost, U, ActingUser), |
| 52 | + rabbit_log:info("Added default user to vhost '~tp': ~tp", [VHost, maps:remove(password, U)]) |
| 53 | + end, |
| 54 | + list_users(VHost) |
| 55 | + ), |
| 56 | + ok. |
| 57 | + |
| 58 | +%% |
| 59 | +%% Helpers |
| 60 | +%% |
| 61 | + |
| 62 | +%% Limits that were configured with a matching vhost pattern. |
| 63 | +-spec list_limits(vhost:name()) -> proplists:proplist(). |
| 64 | +list_limits(VHost) -> |
| 65 | + AllLimits = application:get_env(rabbit, default_limits, []), |
| 66 | + VHostLimits = proplists:get_value(vhosts, AllLimits, []), |
| 67 | + Match = lists:search( |
| 68 | + fun({_, Ss}) -> |
| 69 | + RE = proplists:get_value(<<"pattern">>, Ss, ".*"), |
| 70 | + re:run(VHost, RE, [{capture, none}]) =:= match |
| 71 | + end, |
| 72 | + VHostLimits |
| 73 | + ), |
| 74 | + case Match of |
| 75 | + {value, {_, Ss}} -> |
| 76 | + Ss1 = proplists:delete(<<"pattern">>, Ss), |
| 77 | + underscore_to_dash(Ss1); |
| 78 | + _ -> |
| 79 | + [] |
| 80 | + end. |
| 81 | + |
| 82 | +%% Operator policies that were configured with a matching vhost pattern. |
| 83 | +-spec list_operator_policies(vhost:name()) -> [#seeding_policy{}]. |
| 84 | +list_operator_policies(VHost) -> |
| 85 | + AllPolicies = application:get_env(rabbit, default_policies, []), |
| 86 | + OpPolicies = proplists:get_value(operator, AllPolicies, []), |
| 87 | + lists:filtermap( |
| 88 | + fun({PolicyName, Ss}) -> |
| 89 | + RE = proplists:get_value(<<"vhost_pattern">>, Ss, ".*"), |
| 90 | + case re:run(VHost, RE, [{capture, none}]) of |
| 91 | + match -> |
| 92 | + QPattern = proplists:get_value(<<"queue_pattern">>, Ss, <<".*">>), |
| 93 | + Ss1 = proplists:delete(<<"queue_pattern">>, Ss), |
| 94 | + Ss2 = proplists:delete(<<"vhost_pattern">>, Ss1), |
| 95 | + {true, #seeding_policy{ |
| 96 | + name = PolicyName, |
| 97 | + queue_pattern = QPattern, |
| 98 | + definition = underscore_to_dash(Ss2) |
| 99 | + }}; |
| 100 | + _ -> |
| 101 | + false |
| 102 | + end |
| 103 | + end, |
| 104 | + OpPolicies |
| 105 | + ). |
| 106 | + |
| 107 | +%% Users (permissions) that were configured with a matching vhost pattern. |
| 108 | +-spec list_users(vhost:name()) -> [seeded_user_properties()]. |
| 109 | +list_users(VHost) -> |
| 110 | + Users = application:get_env(rabbit, default_users, []), |
| 111 | + lists:filtermap( |
| 112 | + fun({Username, Ss}) -> |
| 113 | + RE = proplists:get_value(<<"vhost_pattern">>, Ss, ".*"), |
| 114 | + case re:run(VHost, RE, [{capture, none}]) of |
| 115 | + match -> |
| 116 | + C = rabbit_data_coercion:to_binary( |
| 117 | + proplists:get_value(<<"configure">>, Ss, <<".*">>) |
| 118 | + ), |
| 119 | + R = rabbit_data_coercion:to_binary( |
| 120 | + proplists:get_value(<<"read">>, Ss, <<".*">>) |
| 121 | + ), |
| 122 | + W = rabbit_data_coercion:to_binary( |
| 123 | + proplists:get_value(<<"write">>, Ss, <<".*">>) |
| 124 | + ), |
| 125 | + U0 = #{ |
| 126 | + name => Username, |
| 127 | + tags => proplists:get_value(<<"tags">>, Ss, []), |
| 128 | + configure => C, |
| 129 | + read => R, |
| 130 | + write => W |
| 131 | + }, |
| 132 | + %% rabbit_auth_backend_internal:put_user relies on maps:is_key, can't pass |
| 133 | + %% undefined through. |
| 134 | + U1 = case proplists:get_value(<<"password">>, Ss, undefined) of |
| 135 | + undefined -> |
| 136 | + U0; |
| 137 | + V -> |
| 138 | + U0#{password => rabbit_data_coercion:to_binary(V)} |
| 139 | + end, |
| 140 | + {true, U1}; |
| 141 | + _ -> |
| 142 | + false |
| 143 | + end |
| 144 | + end, |
| 145 | + Users |
| 146 | + ). |
| 147 | + |
| 148 | +%% |
| 149 | +%% Private |
| 150 | +%% |
| 151 | + |
| 152 | +%% Translate underscores to dashes in prop keys. |
| 153 | +-spec underscore_to_dash(definitions()) -> definitions(). |
| 154 | +underscore_to_dash(Props) -> |
| 155 | + lists:map( |
| 156 | + fun({N, V}) -> |
| 157 | + {binary:replace(N, <<"_">>, <<"-">>, [global]), V} |
| 158 | + end, |
| 159 | + Props |
| 160 | + ). |
| 161 | + |
| 162 | +%% Add user iff it doesn't exist & set permissions per vhost. |
| 163 | +-spec add_user(rabbit_types:vhost(), seeded_user_properties(), rabbit_types:username()) -> ok. |
| 164 | +add_user(VHost, #{name := Name, configure := C, write := W, read := R} = User, ActingUser) -> |
| 165 | + %% put_user has its own existence check, but it still updates password if the user exists. |
| 166 | + %% We want only the newly created users to have password set from the config. |
| 167 | + rabbit_auth_backend_internal:exists(Name) orelse |
| 168 | + rabbit_auth_backend_internal:put_user(User, ActingUser), |
| 169 | + rabbit_auth_backend_internal:set_permissions(Name, VHost, C, W, R, ActingUser). |
0 commit comments