Merge pull request #14210 from cloudamqp/mqtt_processor_SUITE_no_mnesia
Minor: Don't use Mnesia in rabbitmq_mqtt/test/processor_SUITE
This commit is contained in:
		
						commit
						37cb595ea3
					
				|  | @ -1185,33 +1185,31 @@ get_vhost(UserBin, SslLogin, Port) -> | ||||||
|     get_vhost_ssl(UserBin, SslLogin, Port). |     get_vhost_ssl(UserBin, SslLogin, Port). | ||||||
| 
 | 
 | ||||||
| get_vhost_no_ssl(UserBin, Port) -> | get_vhost_no_ssl(UserBin, Port) -> | ||||||
|     case vhost_in_username(UserBin) of |     case get_vhost_username(UserBin) of | ||||||
|         true  -> |  | ||||||
|             {vhost_in_username_or_default, get_vhost_username(UserBin)}; |  | ||||||
|         false -> |  | ||||||
|             PortVirtualHostMapping = rabbit_runtime_parameters:value_global( |  | ||||||
|                                        mqtt_port_to_vhost_mapping |  | ||||||
|                                       ), |  | ||||||
|             case get_vhost_from_port_mapping(Port, PortVirtualHostMapping) of |  | ||||||
|         undefined -> |         undefined -> | ||||||
|                     {plugin_configuration_or_default_vhost, {rabbit_mqtt_util:env(vhost), UserBin}}; |             case get_vhost_from_port_mapping(Port) of | ||||||
|                 VHost -> |                 undefined -> | ||||||
|                     {port_to_vhost_mapping, {VHost, UserBin}} |                     VhostFromConfig = rabbit_mqtt_util:env(vhost), | ||||||
|             end |                     {plugin_configuration_or_default_vhost, {VhostFromConfig, UserBin}}; | ||||||
|  |                 VHostFromPortMapping -> | ||||||
|  |                     {port_to_vhost_mapping, {VHostFromPortMapping, UserBin}} | ||||||
|  |             end; | ||||||
|  |         VHostUser -> | ||||||
|  |             {vhost_in_username, VHostUser} | ||||||
|     end. |     end. | ||||||
| 
 | 
 | ||||||
| get_vhost_ssl(UserBin, SslLoginName, Port) -> | get_vhost_ssl(UserBin, SslLoginName, Port) -> | ||||||
|     UserVirtualHostMapping = rabbit_runtime_parameters:value_global( |     case get_vhost_from_user_mapping(SslLoginName) of | ||||||
|                                mqtt_default_vhosts |  | ||||||
|                               ), |  | ||||||
|     case get_vhost_from_user_mapping(SslLoginName, UserVirtualHostMapping) of |  | ||||||
|         undefined -> |         undefined -> | ||||||
|             PortVirtualHostMapping = rabbit_runtime_parameters:value_global( |             case get_vhost_from_port_mapping(Port) of | ||||||
|                                        mqtt_port_to_vhost_mapping |  | ||||||
|                                       ), |  | ||||||
|             case get_vhost_from_port_mapping(Port, PortVirtualHostMapping) of |  | ||||||
|                 undefined -> |                 undefined -> | ||||||
|                     {vhost_in_username_or_default, get_vhost_username(UserBin)}; |                     case get_vhost_username(UserBin) of | ||||||
|  |                         undefined -> | ||||||
|  |                             VhostFromConfig = rabbit_mqtt_util:env(vhost), | ||||||
|  |                             {plugin_configuration_or_default_vhost, {VhostFromConfig, UserBin}}; | ||||||
|  |                         VHostUser -> | ||||||
|  |                             {vhost_in_username, VHostUser} | ||||||
|  |                     end; | ||||||
|                 VHostFromPortMapping -> |                 VHostFromPortMapping -> | ||||||
|                     {port_to_vhost_mapping, {VHostFromPortMapping, UserBin}} |                     {port_to_vhost_mapping, {VHostFromPortMapping, UserBin}} | ||||||
|             end; |             end; | ||||||
|  | @ -1219,31 +1217,24 @@ get_vhost_ssl(UserBin, SslLoginName, Port) -> | ||||||
|             {client_cert_to_vhost_mapping, {VHostFromCertMapping, UserBin}} |             {client_cert_to_vhost_mapping, {VHostFromCertMapping, UserBin}} | ||||||
|     end. |     end. | ||||||
| 
 | 
 | ||||||
| vhost_in_username(UserBin) -> |  | ||||||
|     case application:get_env(?APP_NAME, ignore_colons_in_username) of |  | ||||||
|         {ok, true} -> false; |  | ||||||
|         _ -> |  | ||||||
|             %% split at the last colon, disallowing colons in username |  | ||||||
|             case re:split(UserBin, ":(?!.*?:)") of |  | ||||||
|                 [_, _]      -> true; |  | ||||||
|                 [UserBin]   -> false; |  | ||||||
|                 []          -> false |  | ||||||
|             end |  | ||||||
|     end. |  | ||||||
| 
 |  | ||||||
| get_vhost_username(UserBin) -> | get_vhost_username(UserBin) -> | ||||||
|     Default = {rabbit_mqtt_util:env(vhost), UserBin}, |  | ||||||
|     case application:get_env(?APP_NAME, ignore_colons_in_username) of |     case application:get_env(?APP_NAME, ignore_colons_in_username) of | ||||||
|         {ok, true} -> Default; |         {ok, true} -> undefined; | ||||||
|         _ -> |         _ -> | ||||||
|             %% split at the last colon, disallowing colons in username |             %% split at the last colon, disallowing colons in username | ||||||
|             case re:split(UserBin, ":(?!.*?:)") of |             case re:split(UserBin, ":(?!.*?:)") of | ||||||
|                 [Vhost, UserName] -> {Vhost,  UserName}; |                 [Vhost, UserName] -> {Vhost,  UserName}; | ||||||
|                 [UserBin]         -> Default; |                 [UserBin]         -> undefined; | ||||||
|                 []                -> Default |                 []                -> undefined | ||||||
|             end |             end | ||||||
|     end. |     end. | ||||||
| 
 | 
 | ||||||
|  | get_vhost_from_user_mapping(User) -> | ||||||
|  |     UserVirtualHostMapping = rabbit_runtime_parameters:value_global( | ||||||
|  |                                mqtt_default_vhosts | ||||||
|  |                               ), | ||||||
|  |     get_vhost_from_user_mapping(User, UserVirtualHostMapping). | ||||||
|  | 
 | ||||||
| get_vhost_from_user_mapping(_User, not_found) -> | get_vhost_from_user_mapping(_User, not_found) -> | ||||||
|     undefined; |     undefined; | ||||||
| get_vhost_from_user_mapping(User, Mapping) -> | get_vhost_from_user_mapping(User, Mapping) -> | ||||||
|  | @ -1255,6 +1246,12 @@ get_vhost_from_user_mapping(User, Mapping) -> | ||||||
|             VHost |             VHost | ||||||
|     end. |     end. | ||||||
| 
 | 
 | ||||||
|  | get_vhost_from_port_mapping(Port) -> | ||||||
|  |     PortVirtualHostMapping = rabbit_runtime_parameters:value_global( | ||||||
|  |                                mqtt_port_to_vhost_mapping | ||||||
|  |                               ), | ||||||
|  |     get_vhost_from_port_mapping(Port, PortVirtualHostMapping). | ||||||
|  | 
 | ||||||
| get_vhost_from_port_mapping(_Port, not_found) -> | get_vhost_from_port_mapping(_Port, not_found) -> | ||||||
|     undefined; |     undefined; | ||||||
| get_vhost_from_port_mapping(Port, Mapping) -> | get_vhost_from_port_mapping(Port, Mapping) -> | ||||||
|  |  | ||||||
|  | @ -31,38 +31,27 @@ suite() -> | ||||||
| 
 | 
 | ||||||
| init_per_suite(Config) -> | init_per_suite(Config) -> | ||||||
|     ok = application:load(rabbitmq_mqtt), |     ok = application:load(rabbitmq_mqtt), | ||||||
|  |     meck:new(rabbit_runtime_parameters, [passthrough, no_link]), | ||||||
|     Config. |     Config. | ||||||
| end_per_suite(Config) -> | end_per_suite(Config) -> | ||||||
|     ok = application:unload(rabbitmq_mqtt), |     ok = application:unload(rabbitmq_mqtt), | ||||||
|  |     meck:unload(rabbit_runtime_parameters), | ||||||
|     Config. |     Config. | ||||||
| init_per_group(_, Config) -> Config. | init_per_group(_, Config) -> Config. | ||||||
| end_per_group(_, Config) -> Config. | end_per_group(_, Config) -> Config. | ||||||
| init_per_testcase(get_vhost, Config) -> |  | ||||||
|     mnesia:start(), |  | ||||||
|     mnesia:create_table(rabbit_runtime_parameters, [ |  | ||||||
|         {attributes, record_info(fields, runtime_parameters)}, |  | ||||||
|         {record_name, runtime_parameters}]), |  | ||||||
|     meck:new(rabbit_feature_flags, [passthrough, no_link]), |  | ||||||
|     meck:expect( |  | ||||||
|       rabbit_feature_flags, is_enabled, |  | ||||||
|       fun |  | ||||||
|           (khepri_db, _) -> false; |  | ||||||
|           (FeatureNames, _)           -> meck:passthrough([FeatureNames]) |  | ||||||
|       end), |  | ||||||
|     Config; |  | ||||||
| init_per_testcase(_, Config) -> Config. | init_per_testcase(_, Config) -> Config. | ||||||
| end_per_testcase(get_vhost, Config) -> |  | ||||||
|     meck:unload(rabbit_feature_flags), |  | ||||||
|     mnesia:stop(), |  | ||||||
|     Config; |  | ||||||
| end_per_testcase(_, Config) -> Config. | end_per_testcase(_, Config) -> Config. | ||||||
| 
 | 
 | ||||||
| ignore_colons(B) -> application:set_env(rabbitmq_mqtt, ignore_colons_in_username, B). | ignore_colons(B) -> application:set_env(rabbitmq_mqtt, ignore_colons_in_username, B). | ||||||
| 
 | 
 | ||||||
| ignores_colons_in_username_if_option_set(_Config) -> | ignores_colons_in_username_if_option_set(_Config) -> | ||||||
|  |     clear_vhost_global_parameters(), | ||||||
|      ignore_colons(true), |      ignore_colons(true), | ||||||
|     ?assertEqual({rabbit_mqtt_util:env(vhost), <<"a:b:c">>}, |     ?assertEqual(undefined, | ||||||
|                   rabbit_mqtt_processor:get_vhost_username(<<"a:b:c">>)). |                   rabbit_mqtt_processor:get_vhost_username(<<"a:b:c">>)), | ||||||
|  |     ?assertEqual({plugin_configuration_or_default_vhost, | ||||||
|  |                   {rabbit_mqtt_util:env(vhost), <<"a:b:c">>}}, | ||||||
|  |                   rabbit_mqtt_processor:get_vhost(<<"a:b:c">>, none, 1883)). | ||||||
| 
 | 
 | ||||||
| interprets_colons_in_username_if_option_not_set(_Config) -> | interprets_colons_in_username_if_option_not_set(_Config) -> | ||||||
|    ignore_colons(false), |    ignore_colons(false), | ||||||
|  | @ -150,26 +139,32 @@ get_vhost(_Config) -> | ||||||
| 
 | 
 | ||||||
|     %% certificate user, port/vhost parameter but no mapping, cert/vhost mapping |     %% certificate user, port/vhost parameter but no mapping, cert/vhost mapping | ||||||
|     %% should use cert/vhost mapping |     %% should use cert/vhost mapping | ||||||
|     set_global_parameter(mqtt_default_vhosts, [ |     set_global_parameters( | ||||||
|  |       [{mqtt_default_vhosts, | ||||||
|  |         [ | ||||||
|          {<<"O=client,CN=dummy">>,     <<"somevhost">>}, |          {<<"O=client,CN=dummy">>,     <<"somevhost">>}, | ||||||
|          {<<"O=client,CN=otheruser">>, <<"othervhost">>} |          {<<"O=client,CN=otheruser">>, <<"othervhost">>} | ||||||
|     ]), |         ]}, | ||||||
|     set_global_parameter(mqtt_port_to_vhost_mapping, [ |        {mqtt_port_to_vhost_mapping, | ||||||
|  |         [ | ||||||
|          {<<"1884">>, <<"othervhost">>} |          {<<"1884">>, <<"othervhost">>} | ||||||
|     ]), |         ]}]), | ||||||
|     {_, {<<"somevhost">>, <<"guest">>}} = rabbit_mqtt_processor:get_vhost(<<"guest">>, <<"O=client,CN=dummy">>, 1883), |     {_, {<<"somevhost">>, <<"guest">>}} = rabbit_mqtt_processor:get_vhost(<<"guest">>, <<"O=client,CN=dummy">>, 1883), | ||||||
|     clear_vhost_global_parameters(), |     clear_vhost_global_parameters(), | ||||||
| 
 | 
 | ||||||
|     %% certificate user, port/vhost parameter, cert/vhost parameter |     %% certificate user, port/vhost parameter, cert/vhost parameter | ||||||
|     %% cert/vhost parameter takes precedence |     %% cert/vhost parameter takes precedence | ||||||
|     set_global_parameter(mqtt_default_vhosts, [ |     set_global_parameters( | ||||||
|  |       [{mqtt_default_vhosts, | ||||||
|  |         [ | ||||||
|          {<<"O=client,CN=dummy">>,     <<"cert-somevhost">>}, |          {<<"O=client,CN=dummy">>,     <<"cert-somevhost">>}, | ||||||
|          {<<"O=client,CN=otheruser">>, <<"othervhost">>} |          {<<"O=client,CN=otheruser">>, <<"othervhost">>} | ||||||
|     ]), |         ]}, | ||||||
|     set_global_parameter(mqtt_port_to_vhost_mapping, [ |        {mqtt_port_to_vhost_mapping, | ||||||
|  |         [ | ||||||
|          {<<"1883">>, <<"port-vhost">>}, |          {<<"1883">>, <<"port-vhost">>}, | ||||||
|          {<<"1884">>, <<"othervhost">>} |          {<<"1884">>, <<"othervhost">>} | ||||||
|     ]), |         ]}]), | ||||||
|     {_, {<<"cert-somevhost">>, <<"guest">>}} = rabbit_mqtt_processor:get_vhost(<<"guest">>, <<"O=client,CN=dummy">>, 1883), |     {_, {<<"cert-somevhost">>, <<"guest">>}} = rabbit_mqtt_processor:get_vhost(<<"guest">>, <<"O=client,CN=dummy">>, 1883), | ||||||
|     clear_vhost_global_parameters(), |     clear_vhost_global_parameters(), | ||||||
| 
 | 
 | ||||||
|  | @ -179,28 +174,30 @@ get_vhost(_Config) -> | ||||||
| 
 | 
 | ||||||
|     %% not a certificate user, port/vhost parameter, cert/vhost parameter |     %% not a certificate user, port/vhost parameter, cert/vhost parameter | ||||||
|     %% port/vhost mapping is used, as cert/vhost should not be used |     %% port/vhost mapping is used, as cert/vhost should not be used | ||||||
|     set_global_parameter(mqtt_default_vhosts, [ |     set_global_parameters( | ||||||
|  |       [{mqtt_default_vhosts, | ||||||
|  |         [ | ||||||
|          {<<"O=cert">>,                <<"cert-somevhost">>}, |          {<<"O=cert">>,                <<"cert-somevhost">>}, | ||||||
|          {<<"O=client,CN=otheruser">>, <<"othervhost">>} |          {<<"O=client,CN=otheruser">>, <<"othervhost">>} | ||||||
|     ]), |         ]}, | ||||||
|     set_global_parameter(mqtt_port_to_vhost_mapping, [ |        {mqtt_port_to_vhost_mapping, | ||||||
|  |         [ | ||||||
|          {<<"1883">>, <<"port-vhost">>}, |          {<<"1883">>, <<"port-vhost">>}, | ||||||
|          {<<"1884">>, <<"othervhost">>} |          {<<"1884">>, <<"othervhost">>} | ||||||
|     ]), |         ]}]), | ||||||
|     {_, {<<"port-vhost">>, <<"guest">>}} = rabbit_mqtt_processor:get_vhost(<<"guest">>, none, 1883), |     {_, {<<"port-vhost">>, <<"guest">>}} = rabbit_mqtt_processor:get_vhost(<<"guest">>, none, 1883), | ||||||
|     clear_vhost_global_parameters(), |     clear_vhost_global_parameters(), | ||||||
|     ok. |     ok. | ||||||
| 
 | 
 | ||||||
| set_global_parameter(Key, Term) -> | set_global_parameter(Key, Term) -> | ||||||
|     InsertParameterFun = fun () -> |     set_global_parameters([{Key, Term}]). | ||||||
|         mnesia:write(rabbit_runtime_parameters, #runtime_parameters{key = Key, value = Term}, write) |  | ||||||
|                          end, |  | ||||||
| 
 | 
 | ||||||
|     {atomic, ok} = mnesia:transaction(InsertParameterFun). | set_global_parameters(KVList) -> | ||||||
|  |     meck:expect( | ||||||
|  |       rabbit_runtime_parameters, value_global, | ||||||
|  |       fun(Key) -> proplists:get_value(Key, KVList, not_found) end). | ||||||
| 
 | 
 | ||||||
| clear_vhost_global_parameters() -> | clear_vhost_global_parameters() -> | ||||||
|     DeleteParameterFun = fun () -> |     meck:expect( | ||||||
|         ok = mnesia:delete(rabbit_runtime_parameters, mqtt_default_vhosts, write), |       rabbit_runtime_parameters, value_global, | ||||||
|         ok = mnesia:delete(rabbit_runtime_parameters, mqtt_port_to_vhost_mapping, write) |       fun(_) -> not_found end). | ||||||
|                          end, |  | ||||||
|     {atomic, ok} = mnesia:transaction(DeleteParameterFun). |  | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue