Coveralls logob
Coveralls logo
  • Home
  • Features
  • Pricing
  • Docs
  • Sign In

emqx / emqx / 5609

8 May 2019 - 13:18 coverage decreased (-0.2%) to 68.601%
5609

Pull #2521

travis-ci

9181eb84f9c35729a3bad740fb7f9d93?size=18&default=identiconweb-flow
Restore app.confg rule in makefile for debug and test
Pull Request #2521: Auto-pull-request-by-2019-05-09

2 of 7 new or added lines in 1 file covered. (28.57%)

23 existing lines in 7 files now uncovered.

3452 of 5032 relevant lines covered (68.6%)

208.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.0
/src/emqx_mod_acl_internal.erl
1
%% Copyright (c) 2013-2019 EMQ Technologies Co., Ltd. All Rights Reserved.
2
%%
3
%% Licensed under the Apache License, Version 2.0 (the "License");
4
%% you may not use this file except in compliance with the License.
5
%% You may obtain a copy of the License at
6
%%
7
%%     http://www.apache.org/licenses/LICENSE-2.0
8
%%
9
%% Unless required by applicable law or agreed to in writing, software
10
%% distributed under the License is distributed on an "AS IS" BASIS,
11
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
%% See the License for the specific language governing permissions and
13
%% limitations under the License.
14

15
-module(emqx_mod_acl_internal).
16

17
-behaviour(emqx_gen_mod).
18

19
-include("emqx.hrl").
20
-include("logger.hrl").
21

22
%% APIs
23
-export([ all_rules/0
24
        , check_acl/5
25
        , reload_acl/0
26
        ]).
27

28
%% emqx_gen_mod callbacks
29
-export([ load/1
30
        , unload/1
31
        ]).
32

33
-define(MFA(M, F, A), {M, F, A}).
34

35
-type(acl_rules() :: #{publish   => [emqx_access_rule:rule()],
36
                       subscribe => [emqx_access_rule:rule()]}).
37

38
%%------------------------------------------------------------------------------
39
%% API
40
%%------------------------------------------------------------------------------
41

42
load(_Env) ->
43
    Rules = rules_from_file(acl_file()),
20×
44
    emqx_hooks:add('client.check_acl', ?MFA(?MODULE, check_acl, [Rules]),  -1).
20×
45

46
unload(_Env) ->
47
    Rules = rules_from_file(acl_file()),
20×
48
    emqx_hooks:del('client.check_acl', ?MFA(?MODULE, check_acl, [Rules])).
20×
49

50
%% @doc Read all rules
51
-spec(all_rules() -> list(emqx_access_rule:rule())).
52
all_rules() ->
53
    rules_from_file(acl_file()).
!
54

55
%%------------------------------------------------------------------------------
56
%% ACL callbacks
57
%%------------------------------------------------------------------------------
58

59
%% @doc Check ACL
60
-spec(check_acl(emqx_types:credentials(), emqx_types:pubsub(), emqx_topic:topic(),
61
                emqx_access_rule:acl_result(), acl_rules())
62
      -> {ok, allow} | {ok, deny} | ok).
63
check_acl(Credentials, PubSub, Topic, _AclResult, Rules) ->
64
    case match(Credentials, Topic, lookup(PubSub, Rules)) of
56×
65
        {matched, allow} -> {ok, allow};
49×
66
        {matched, deny}  -> {ok, deny};
7×
UNCOV
67
        nomatch          -> ok
!
68
    end.
69

70
-spec(reload_acl() -> ok | {error, term()}).
71
reload_acl() ->
72
    unload([]), load([]).
!
73

74
%%------------------------------------------------------------------------------
75
%% Internal Functions
76
%%------------------------------------------------------------------------------
77

78
acl_file() ->
79
    emqx_config:get_env(acl_file).
40×
80

81
lookup(PubSub, Rules) ->
82
    maps:get(PubSub, Rules, []).
56×
83

84
match(_Credentials, _Topic, []) ->
UNCOV
85
    nomatch;
!
86
match(Credentials, Topic, [Rule|Rules]) ->
87
    case emqx_access_rule:match(Credentials, Topic, Rule) of
100×
88
        nomatch ->
89
            match(Credentials, Topic, Rules);
44×
90
        {matched, AllowDeny} ->
91
            {matched, AllowDeny}
56×
92
    end.
93

94
-spec(rules_from_file(file:filename()) -> map()).
95
rules_from_file(AclFile) ->
96
    case file:consult(AclFile) of
40×
97
        {ok, Terms} ->
98
            Rules = [emqx_access_rule:compile(Term) || Term <- Terms],
40×
99
            #{publish   => [Rule || Rule <- Rules, filter(publish, Rule)],
40×
100
              subscribe => [Rule || Rule <- Rules, filter(subscribe, Rule)]};
152×
101
        {error, Reason} ->
UNCOV
102
            ?LOG(alert, "[ACL_INTERNAL] Failed to read ~s: ~p", [AclFile, Reason]),
!
UNCOV
103
            #{}
!
104
    end.
105

106
filter(_PubSub, {allow, all}) ->
107
    true;
80×
108
filter(_PubSub, {deny, all}) ->
109
    true;
!
110
filter(publish, {_AllowDeny, _Who, publish, _Topics}) ->
111
    true;
4×
112
filter(_PubSub, {_AllowDeny, _Who, pubsub, _Topics}) ->
113
    true;
80×
114
filter(subscribe, {_AllowDeny, _Who, subscribe, _Topics}) ->
115
    true;
72×
116
filter(_PubSub, {_AllowDeny, _Who, _, _Topics}) ->
117
    false.
76×
118

Troubleshooting · Open an Issue · Sales · Support · ENTERPRISE · CAREERS · STATUS
BLOG · TWITTER · Legal & Privacy · Supported CI Services · What's a CI service? · Automated Testing

© 2019 Coveralls, LLC