From 89ed4a350fd2efe5d9678075d59979acd9d0e4e4 Mon Sep 17 00:00:00 2001 From: JianBo He Date: Fri, 16 Sep 2022 17:41:25 +0800 Subject: [PATCH] chore: avoid generating crash logs for hook execution --- apps/emqx_auth_jwt/src/emqx_auth_jwt.erl | 6 ++- .../test/emqx_auth_jwt_SUITE.erl | 41 ++++++++++++++++++- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/apps/emqx_auth_jwt/src/emqx_auth_jwt.erl b/apps/emqx_auth_jwt/src/emqx_auth_jwt.erl index 1259bd263..26fd34365 100644 --- a/apps/emqx_auth_jwt/src/emqx_auth_jwt.erl +++ b/apps/emqx_auth_jwt/src/emqx_auth_jwt.erl @@ -18,6 +18,7 @@ -include_lib("emqx/include/emqx.hrl"). -include_lib("emqx/include/logger.hrl"). +-include_lib("snabbkaffe/include/snabbkaffe.hrl"). -logger_header("[JWT]"). @@ -70,7 +71,10 @@ check_acl(ClientInfo = #{jwt_claims := Claims}, _ -> ?DEBUG("no_acl_jwt_claim", []), ignore - end. + end; +check_acl(_ClientInfo, _PubSub, _Topic, _NoMatchAction, _Env) -> + ?tp(debug, no_jwt_claim, #{}), + ignore. is_expired(Exp) when is_binary(Exp) -> case string_to_number(Exp) of diff --git a/apps/emqx_auth_jwt/test/emqx_auth_jwt_SUITE.erl b/apps/emqx_auth_jwt/test/emqx_auth_jwt_SUITE.erl index 596b829a6..eb6d3c195 100644 --- a/apps/emqx_auth_jwt/test/emqx_auth_jwt_SUITE.erl +++ b/apps/emqx_auth_jwt/test/emqx_auth_jwt_SUITE.erl @@ -31,7 +31,8 @@ init_per_testcase(TestCase, Config) -> emqx_ct_helpers:start_apps([emqx_auth_jwt], fun set_special_configs/1), Config. -end_per_testcase(_Case, _Config) -> +end_per_testcase(TestCase, Config) -> + try ?MODULE:TestCase('end', Config) catch _:_ -> ok end, emqx_ct_helpers:stop_apps([emqx_auth_jwt]). set_special_configs(emqx) -> @@ -377,6 +378,44 @@ t_check_jwt_acl_no_acl_claim(_Config) -> ok = emqtt:disconnect(C). +t_check_jwt_acl_no_jwt_claims_helper(_ClientInfo, _LastAuthResult) -> + {stop, #{auth_result => success, anonymous => false}}. +t_check_jwt_acl_no_jwt_claims(init, _Config) -> + ok; +t_check_jwt_acl_no_jwt_claims('end', _Config) -> + ok = emqx_hooks:del( + 'client.authenticate', + {?MODULE, t_check_jwt_acl_no_jwt_claims_helper, []} + ). +t_check_jwt_acl_no_jwt_claims(_Config) -> + %% bypass the jwt authentication checking + ok = emqx_hooks:add( + 'client.authenticate', + {?MODULE, t_check_jwt_acl_no_jwt_claims_helper, []}, + _Priority = 99999 + ), + + {ok, C} = emqtt:start_link( + [{clean_start, true}, + {proto_ver, v5}, + {client_id, <<"client1">>}, + {username, <<"client1">>}, + {password, <<"password">>}]), + {ok, _} = emqtt:connect(C), + + ok = snabbkaffe:start_trace(), + + ?assertMatch( + {ok, #{}, [?RC_NOT_AUTHORIZED]}, + emqtt:subscribe(C, <<"a/b">>, 0)), + + {ok, _} = ?block_until(#{?snk_kind := no_jwt_claim}, 1000), + Trace = snabbkaffe:collect_trace(), + ?assertEqual(1, length(?of_kind(no_jwt_claim, Trace))), + + snabbkaffe:stop(), + ok = emqtt:disconnect(C). + t_check_jwt_acl_expire(init, _Config) -> application:set_env(emqx_auth_jwt, verify_claims, [{sub, <<"value">>}]). t_check_jwt_acl_expire(_Config) ->