ipaddress

This commit is contained in:
Feng Lee 2015-06-23 12:20:41 +08:00
parent 8b7304c0dd
commit 3e5675cc71
3 changed files with 10 additions and 93 deletions

View File

@ -36,6 +36,7 @@
%% API Function Exports %% API Function Exports
-export([start_link/0, -export([start_link/0,
start_link/1,
auth/2, % authentication auth/2, % authentication
check_acl/3, % acl check check_acl/3, % acl check
reload_acl/0, % reload acl reload_acl/0, % reload acl
@ -60,7 +61,12 @@
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
-spec start_link() -> {ok, pid()} | ignore | {error, any()}. -spec start_link() -> {ok, pid()} | ignore | {error, any()}.
start_link() -> start_link() ->
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []). {ok, AcOpts} = application:get_env(emqttd, access),
start_link(AcOpts).
-spec start_link(AcOpts :: list()) -> {ok, pid()} | ignore | {error, any()}.
start_link(AcOpts) ->
gen_server:start_link({local, ?SERVER}, ?MODULE, [AcOpts], []).
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
%% @doc Authenticate MQTT Client %% @doc Authenticate MQTT Client
@ -151,8 +157,7 @@ stop() ->
%%% gen_server callbacks %%% gen_server callbacks
%%%============================================================================= %%%=============================================================================
init([]) -> init([AcOpts]) ->
{ok, AcOpts} = application:get_env(emqttd, access),
ets:new(?ACCESS_CONTROL_TAB, [set, named_table, protected, {read_concurrency, true}]), ets:new(?ACCESS_CONTROL_TAB, [set, named_table, protected, {read_concurrency, true}]),
ets:insert(?ACCESS_CONTROL_TAB, {auth_modules, init_mods(auth, proplists:get_value(auth, AcOpts))}), ets:insert(?ACCESS_CONTROL_TAB, {auth_modules, init_mods(auth, proplists:get_value(auth, AcOpts))}),
ets:insert(?ACCESS_CONTROL_TAB, {acl_modules, init_mods(acl, proplists:get_value(acl, AcOpts))}), ets:insert(?ACCESS_CONTROL_TAB, {acl_modules, init_mods(acl, proplists:get_value(acl, AcOpts))}),

View File

@ -53,8 +53,8 @@ compile_test() ->
?assertEqual({deny, all}, compile({deny, all})). ?assertEqual({deny, all}, compile({deny, all})).
match_test() -> match_test() ->
User = #mqtt_client{ipaddr = {127,0,0,1}, clientid = <<"testClient">>, username = <<"TestUser">>}, User = #mqtt_client{ipaddress = {127,0,0,1}, clientid = <<"testClient">>, username = <<"TestUser">>},
User2 = #mqtt_client{ipaddr = {192,168,0,10}, clientid = <<"testClient">>, username = <<"TestUser">>}, User2 = #mqtt_client{ipaddress = {192,168,0,10}, clientid = <<"testClient">>, username = <<"TestUser">>},
?assertEqual({matched, allow}, match(User, <<"Test/Topic">>, {allow, all})), ?assertEqual({matched, allow}, match(User, <<"Test/Topic">>, {allow, all})),
?assertEqual({matched, deny}, match(User, <<"Test/Topic">>, {deny, all})), ?assertEqual({matched, deny}, match(User, <<"Test/Topic">>, {deny, all})),

View File

@ -1,88 +0,0 @@
%%%-----------------------------------------------------------------------------
%%% @Copyright (C) 2012-2015, Feng Lee <feng@emqtt.io>
%%%
%%% Permission is hereby granted, free of charge, to any person obtaining a copy
%%% of this software and associated documentation files (the "Software"), to deal
%%% in the Software without restriction, including without limitation the rights
%%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
%%% copies of the Software, and to permit persons to whom the Software is
%%% furnished to do so, subject to the following conditions:
%%%
%%% The above copyright notice and this permission notice shall be included in all
%%% copies or substantial portions of the Software.
%%%
%%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
%%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
%%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
%%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
%%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
%%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
%%% SOFTWARE.
%%%-----------------------------------------------------------------------------
%%% @doc
%%% emqttd_acl tests.
%%%
%%% @end
%%%-----------------------------------------------------------------------------
-module(emqttd_acl_tests).
-include("emqttd.hrl").
-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
all_modules_test() ->
with_acl(
fun() ->
?assertMatch([{emqttd_acl_internal, _State}], emqttd_acl:all_modules())
end).
reload_test() ->
with_acl(
fun() ->
?assertEqual([ok], emqttd_acl:reload())
end).
register_mod_test() ->
with_acl(
fun() ->
emqttd_acl:register_mod(emqttd_acl_test_mod, []),
?assertMatch([{emqttd_acl_test_mod, _}, {emqttd_acl_internal, _}],
emqttd_acl:all_modules())
end).
unregister_mod_test() ->
with_acl(
fun() ->
emqttd_acl:register_mod(emqttd_acl_test_mod, []),
?assertMatch([{emqttd_acl_test_mod, _}, {emqttd_acl_internal, _}],
emqttd_acl:all_modules()),
emqttd_acl:unregister_mod(emqttd_acl_test_mod),
timer:sleep(5),
?assertMatch([{emqttd_acl_internal, _}], emqttd_acl:all_modules())
end).
check_test() ->
with_acl(
fun() ->
User1 = #mqtt_user{clientid = <<"client1">>, username = <<"testuser">>},
User2 = #mqtt_user{clientid = <<"client2">>, username = <<"xyz">>},
?assertEqual(allow, emqttd_acl:check({User1, subscribe, <<"users/testuser/1">>})),
?assertEqual(allow, emqttd_acl:check({User1, subscribe, <<"clients/client1">>})),
?assertEqual(deny, emqttd_acl:check({User1, subscribe, <<"clients/client1/x/y">>})),
?assertEqual(allow, emqttd_acl:check({User1, publish, <<"users/testuser/1">>})),
?assertEqual(allow, emqttd_acl:check({User1, subscribe, <<"a/b/c">>})),
?assertEqual(deny, emqttd_acl:check({User2, subscribe, <<"a/b/c">>}))
end).
with_acl(Fun) ->
process_flag(trap_exit, true),
AclOpts = [{internal, [{file, "../test/test_acl.config"},
{nomatch, allow}]}],
{ok, _AclSrv} = emqttd_acl:start_link(AclOpts),
Fun(),
emqttd_acl:stop().
-endif.