• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

emqx / emqx / 12583614476

02 Jan 2025 02:04PM UTC coverage: 82.082%. First build
12583614476

Pull #14286

github

web-flow
Merge c9df97f1e into d5a56c20b
Pull Request #14286: Implement node-level authentication/authorization cache

305 of 338 new or added lines in 29 files covered. (90.24%)

56808 of 69209 relevant lines covered (82.08%)

15216.48 hits per line

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

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

17
-module(emqx_auth_cache_schema).
18

19
-include_lib("hocon/include/hoconsc.hrl").
20

21
-export([
22
    namespace/0,
23
    roots/0,
24
    fields/1,
25
    desc/1
26
]).
27

28
-export([
29
    fill_defaults/1,
30
    default_config/0
31
]).
32

33
-export([
34
    cache_settings_example/0,
35
    metrics_example/0
36
]).
37

38
namespace() -> auth_cache.
7,392✔
39

40
%% @doc auth cache schema is not exported but directly used
NEW
41
roots() -> [].
×
42

43
fields(config) ->
44
    [
6,124✔
45
        {enable, mk(boolean(), #{desc => ?DESC(enable), default => false})},
46
        {cache_ttl,
47
            mk(emqx_schema:timeout_duration_ms(), #{
48
                desc => ?DESC(cache_ttl), default => <<"1m">>
49
            })},
50
        {cleanup_interval,
51
            mk(emqx_schema:timeout_duration_ms(), #{
52
                desc => ?DESC(cleanup_interval),
53
                default => <<"1m">>,
54
                importance => ?IMPORTANCE_HIDDEN
55
            })},
56
        {stat_update_interval,
57
            mk(emqx_schema:timeout_duration_ms(), #{
58
                desc => ?DESC(stat_update_interval),
59
                default => <<"5s">>,
60
                importance => ?IMPORTANCE_HIDDEN
61
            })},
62
        {max_count,
63
            mk(hoconsc:union([unlimited, non_neg_integer()]), #{
64
                desc => ?DESC(max_count),
65
                default => 1000000
66
            })},
67
        {max_memory,
68
            mk(hoconsc:union([unlimited, emqx_schema:bytesize()]), #{
69
                desc => ?DESC(max_memory),
70
                default => <<"100MB">>
71
            })}
72
    ];
73
%% These fields are not used for the configuration.
74
%% They describe API responses.
75
fields(rate) ->
76
    [
1,344✔
77
        {rate, ?HOCON(float(), #{desc => "rate"})},
78
        {rate_max, ?HOCON(float(), #{desc => "rate_max"})},
79
        {rate_last5m, ?HOCON(float(), #{desc => "rate_last5m"})}
80
    ];
81
fields(counter) ->
82
    [
1,344✔
83
        {value, ?HOCON(integer(), #{desc => "counter_value"})},
84
        {rate, ?HOCON(?R_REF(rate), #{desc => "counter_rate"})}
85
    ];
86
fields(metrics) ->
87
    [
448✔
88
        {hits, ?HOCON(?R_REF(counter), #{desc => "metric_hits"})},
89
        {misses, ?HOCON(?R_REF(counter), #{desc => "metric_misses"})},
90
        {inserts, ?HOCON(?R_REF(counter), #{desc => "metric_inserts"})},
91
        {count, ?HOCON(integer(), #{desc => "metric_size"})},
92
        {memory, ?HOCON(integer(), #{desc => "metric_memory"})}
93
    ];
94
fields(node_metrics) ->
95
    [
224✔
96
        {node, ?HOCON(binary(), #{desc => "node", example => "emqx@127.0.0.1"})},
97
        {metrics, ?HOCON(?R_REF(metrics), #{desc => "metrics"})}
98
    ];
99
fields(status) ->
100
    [
224✔
101
        {metrics, ?HOCON(?R_REF(metrics), #{desc => "status_metrics"})},
102
        {node_metrics, ?HOCON(?ARRAY(?R_REF(node_metrics)), #{desc => "status_node_metrics"})}
103
    ].
104

105
desc(config) -> ?DESC(auth_cache_config);
5,674✔
NEW
106
desc(metrics) -> ?DESC(auth_cache_metrics);
×
NEW
107
desc(_) -> undefined.
×
108

109
fill_defaults(Config) ->
110
    WithRoot = #{<<"auth_cache">> => Config},
4✔
111
    Schema = #{roots => [{auth_cache, hoconsc:mk(?R_REF(config), #{})}]},
4✔
112
    case emqx_hocon:check(Schema, WithRoot, #{make_serializable => true}) of
4✔
113
        {ok, #{<<"auth_cache">> := WithDefaults}} ->
114
            WithDefaults;
4✔
115
        {error, Reason} ->
NEW
116
            throw(Reason)
×
117
    end.
118

119
default_config() ->
120
    #{
98,294✔
121
        <<"enable">> => false
122
    }.
123

124
%%------------------------------------------------------------------------------
125
%% Data examples
126
%%------------------------------------------------------------------------------
127

128
cache_settings_example() ->
129
    #{
460✔
130
        enable => true,
131
        cache_ttl => <<"1M">>,
132
        cleanup_interval => <<"1M">>,
133
        stat_update_interval => <<"1M">>,
134
        max_count => 100000,
135
        max_memory => <<"100MB">>
136
    }.
137

138
metrics_example() ->
139
    #{
228✔
140
        metrics =>
141
            #{
142
                memory => 1704,
143
                size => 0,
144
                hits =>
145
                    #{value => 0, rate => #{max => 0.0, current => 0.0, last5m => 0.0}},
146
                inserts =>
147
                    #{value => 0, rate => #{max => 0.0, current => 0.0, last5m => 0.0}},
148
                misses =>
149
                    #{value => 1, rate => #{max => 0.0, current => 0.0, last5m => 0.0}}
150
            },
151
        node_metrics =>
152
            [
153
                #{
154
                    node => <<"test@127.0.0.1">>,
155
                    metrics =>
156
                        #{
157
                            memory => 1704,
158
                            size => 0,
159
                            hits =>
160
                                #{
161
                                    value => 0,
162
                                    rate => #{max => 0.0, current => 0.0, last5m => 0.0}
163
                                },
164
                            inserts =>
165
                                #{
166
                                    value => 0,
167
                                    rate => #{max => 0.0, current => 0.0, last5m => 0.0}
168
                                },
169
                            misses =>
170
                                #{
171
                                    value => 1,
172
                                    rate => #{max => 0.0, current => 0.0, last5m => 0.0}
173
                                }
174
                        }
175
                }
176
            ]
177
    }.
178

179
%%------------------------------------------------------------------------------
180
%% Internal Functions
181
%%------------------------------------------------------------------------------
182

183
mk(Type, Meta) -> hoconsc:mk(Type, Meta).
36,744✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc