From 0d5c32a706dac993116d650b669d534ce73d8213 Mon Sep 17 00:00:00 2001 From: JianBo He Date: Wed, 7 Sep 2022 10:14:39 +0800 Subject: [PATCH 1/3] fix(time): use erlang:system_time/0-1 consistently Avoid the problem of inaccurate timers caused by mixing erlang:system_time/0-1 and os:system_time/0-1 --- apps/emqx_auth_jwt/src/emqx_auth_jwt_svr.erl | 2 +- .../test/emqx_auth_jwt_SUITE.erl | 28 +++++++++---------- lib-ce/emqx_modules/src/emqx_mod_delayed.erl | 6 ++-- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/apps/emqx_auth_jwt/src/emqx_auth_jwt_svr.erl b/apps/emqx_auth_jwt/src/emqx_auth_jwt_svr.erl index 3a5c619aa..049f07533 100644 --- a/apps/emqx_auth_jwt/src/emqx_auth_jwt_svr.erl +++ b/apps/emqx_auth_jwt/src/emqx_auth_jwt_svr.erl @@ -200,7 +200,7 @@ do_verify(JwsCompacted, [Jwk|More]) -> end. check_claims(Claims) -> - Now = os:system_time(seconds), + Now = erlang:system_time(seconds), Checker = [{<<"exp">>, with_num_value( fun(ExpireTime) -> Now < ExpireTime end)}, {<<"iat">>, with_num_value( 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..7da708b13 100644 --- a/apps/emqx_auth_jwt/test/emqx_auth_jwt_SUITE.erl +++ b/apps/emqx_auth_jwt/test/emqx_auth_jwt_SUITE.erl @@ -74,7 +74,7 @@ t_check_auth(_Config) -> Plain = #{clientid => <<"client1">>, username => <<"plain">>, zone => external}, Jwt = sign([{clientid, <<"client1">>}, {username, <<"plain">>}, - {exp, os:system_time(seconds) + 2}], <<"HS256">>, <<"emqxsecret">>), + {exp, erlang:system_time(seconds) + 2}], <<"HS256">>, <<"emqxsecret">>), ct:pal("Jwt: ~p~n", [Jwt]), Result0 = emqx_access_control:authenticate(Plain#{password => Jwt}), @@ -100,7 +100,7 @@ t_check_nbf(_Config) -> Plain = #{clientid => <<"client1">>, username => <<"plain">>, zone => external}, Jwt = sign([{clientid, <<"client1">>}, {username, <<"plain">>}, - {nbf, os:system_time(seconds) + 3}], <<"HS256">>, <<"emqxsecret">>), + {nbf, erlang:system_time(seconds) + 3}], <<"HS256">>, <<"emqxsecret">>), ct:pal("Jwt: ~p~n", [Jwt]), Result0 = emqx_access_control:authenticate(Plain#{password => Jwt}), @@ -113,7 +113,7 @@ t_check_iat(_Config) -> Plain = #{clientid => <<"client1">>, username => <<"plain">>, zone => external}, Jwt = sign([{clientid, <<"client1">>}, {username, <<"plain">>}, - {iat, os:system_time(seconds) + 3}], <<"HS256">>, <<"emqxsecret">>), + {iat, erlang:system_time(seconds) + 3}], <<"HS256">>, <<"emqxsecret">>), ct:pal("Jwt: ~p~n", [Jwt]), Result0 = emqx_access_control:authenticate(Plain#{password => Jwt}), @@ -146,7 +146,7 @@ t_check_auth_str_exp(init, _Config) -> application:unset_env(emqx_auth_jwt, verify_claims). t_check_auth_str_exp(_Config) -> Plain = #{clientid => <<"client1">>, username => <<"plain">>, zone => external}, - Exp = integer_to_binary(os:system_time(seconds) + 3), + Exp = integer_to_binary(erlang:system_time(seconds) + 3), Jwt0 = sign([{clientid, <<"client1">>}, {username, <<"plain">>}, @@ -166,7 +166,7 @@ t_check_auth_str_exp(_Config) -> ct:pal("Auth result: ~p~n", [Result1]), ?assertMatch({error, _}, Result1), - Exp2 = float_to_binary(os:system_time(seconds) + 3.5), + Exp2 = float_to_binary(erlang:system_time(seconds) + 3.5), Jwt2 = sign([{clientid, <<"client1">>}, {username, <<"plain">>}, @@ -181,7 +181,7 @@ t_check_auth_float_exp(init, _Config) -> application:unset_env(emqx_auth_jwt, verify_claims). t_check_auth_float_exp(_Config) -> Plain = #{clientid => <<"client1">>, username => <<"plain">>, zone => external}, - Exp = os:system_time(seconds) + 3.5, + Exp = erlang:system_time(seconds) + 3.5, Jwt0 = sign([{clientid, <<"client1">>}, {username, <<"plain">>}, @@ -208,7 +208,7 @@ t_check_claims(_Config) -> Jwt = sign([{client_id, <<"client1">>}, {username, <<"plain">>}, {sub, value}, - {exp, os:system_time(seconds) + 3}], <<"HS256">>, <<"emqxsecret">>), + {exp, erlang:system_time(seconds) + 3}], <<"HS256">>, <<"emqxsecret">>), Result0 = emqx_access_control:authenticate(Plain#{password => Jwt}), ct:pal("Auth result: ~p~n", [Result0]), ?assertMatch({ok, #{auth_result := success, jwt_claims := _}}, Result0), @@ -224,7 +224,7 @@ t_check_claims_clientid(_Config) -> Plain = #{clientid => <<"client23">>, username => <<"plain">>, zone => external}, Jwt = sign([{clientid, <<"client23">>}, {username, <<"plain">>}, - {exp, os:system_time(seconds) + 3}], <<"HS256">>, <<"emqxsecret">>), + {exp, erlang:system_time(seconds) + 3}], <<"HS256">>, <<"emqxsecret">>), Result0 = emqx_access_control:authenticate(Plain#{password => Jwt}), ct:pal("Auth result: ~p~n", [Result0]), ?assertMatch({ok, #{auth_result := success, jwt_claims := _}}, Result0), @@ -240,7 +240,7 @@ t_check_claims_username(_Config) -> Plain = #{clientid => <<"client23">>, username => <<"plain">>, zone => external}, Jwt = sign([{client_id, <<"client23">>}, {username, <<"plain">>}, - {exp, os:system_time(seconds) + 3}], <<"HS256">>, <<"emqxsecret">>), + {exp, erlang:system_time(seconds) + 3}], <<"HS256">>, <<"emqxsecret">>), Result0 = emqx_access_control:authenticate(Plain#{password => Jwt}), ct:pal("Auth result: ~p~n", [Result0]), ?assertMatch({ok, #{auth_result := success, jwt_claims := _}}, Result0), @@ -256,7 +256,7 @@ t_check_claims_kid_in_header(_Config) -> Plain = #{clientid => <<"client23">>, username => <<"plain">>, zone => external}, Jwt = sign([{clientid, <<"client23">>}, {username, <<"plain">>}, - {exp, os:system_time(seconds) + 3}], + {exp, erlang:system_time(seconds) + 3}], #{<<"alg">> => <<"HS256">>, <<"kid">> => <<"a_kid_str">>}, <<"emqxsecret">>), Result0 = emqx_access_control:authenticate(Plain#{password => Jwt}), @@ -297,7 +297,7 @@ t_check_jwt_acl(_Config) -> {sub, value}, {acl, [{sub, [<<"a/b">>]}, {pub, [<<"c/d">>]}]}, - {exp, os:system_time(seconds) + 10}], + {exp, erlang:system_time(seconds) + 10}], <<"HS256">>, <<"emqxsecret">>), @@ -337,7 +337,7 @@ t_check_jwt_acl_no_recs(_Config) -> {username, <<"plain">>}, {sub, value}, {acl, []}, - {exp, os:system_time(seconds) + 10}], + {exp, erlang:system_time(seconds) + 10}], <<"HS256">>, <<"emqxsecret">>), @@ -360,7 +360,7 @@ t_check_jwt_acl_no_acl_claim(_Config) -> Jwt = sign([{client_id, <<"client1">>}, {username, <<"plain">>}, {sub, value}, - {exp, os:system_time(seconds) + 10}], + {exp, erlang:system_time(seconds) + 10}], <<"HS256">>, <<"emqxsecret">>), @@ -384,7 +384,7 @@ t_check_jwt_acl_expire(_Config) -> {username, <<"plain">>}, {sub, value}, {acl, [{sub, [<<"a/b">>]}]}, - {exp, os:system_time(seconds) + 1}], + {exp, erlang:system_time(seconds) + 1}], <<"HS256">>, <<"emqxsecret">>), diff --git a/lib-ce/emqx_modules/src/emqx_mod_delayed.erl b/lib-ce/emqx_modules/src/emqx_mod_delayed.erl index 4fed5e191..3525b8449 100644 --- a/lib-ce/emqx_modules/src/emqx_mod_delayed.erl +++ b/lib-ce/emqx_modules/src/emqx_mod_delayed.erl @@ -160,7 +160,7 @@ handle_cast(Msg, State) -> %% Do Publish... handle_info({timeout, TRef, do_publish}, State = #{timer := TRef}) -> - DeletedKeys = do_publish(mnesia:dirty_first(?TAB), os:system_time(seconds)), + DeletedKeys = do_publish(mnesia:dirty_first(?TAB), erlang:system_time(seconds)), lists:foreach(fun(Key) -> mnesia:dirty_delete(?TAB, Key) end, DeletedKeys), {noreply, ensure_publish_timer(State#{timer := undefined, publish_at := 0})}; @@ -203,11 +203,11 @@ ensure_publish_timer(State) -> ensure_publish_timer('$end_of_table', State) -> State#{timer := undefined, publish_at := 0}; ensure_publish_timer({Ts, _Id}, State = #{timer := undefined}) -> - ensure_publish_timer(Ts, os:system_time(seconds), State); + ensure_publish_timer(Ts, erlang:system_time(seconds), State); ensure_publish_timer({Ts, _Id}, State = #{timer := TRef, publish_at := PubAt}) when Ts < PubAt -> ok = emqx_misc:cancel_timer(TRef), - ensure_publish_timer(Ts, os:system_time(seconds), State); + ensure_publish_timer(Ts, erlang:system_time(seconds), State); ensure_publish_timer(_Key, State) -> State. From 271112ad68e3b34dae9ade73756b47ab8d0008fd Mon Sep 17 00:00:00 2001 From: JianBo He Date: Wed, 7 Sep 2022 10:35:46 +0800 Subject: [PATCH 2/3] chore: update changes --- CHANGES-4.3.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES-4.3.md b/CHANGES-4.3.md index 26e7075e2..2520478c5 100644 --- a/CHANGES-4.3.md +++ b/CHANGES-4.3.md @@ -16,6 +16,7 @@ File format: - Fix rule-engine update behaviour which may initialize actions for disabled rules. [#8849](https://github.com/emqx/emqx/pull/8849) - Fix JWT plugin don't support non-integer timestamp claims. [#8862](https://github.com/emqx/emqx/pull/8862) +- Fix delayed publish inaccurate caused by os time change. [#8908](https://github.com/emqx/emqx/pull/8908) ## v4.3.19 From 7b4fbfef3b7696ba7a8dcb05f38bada861d3c986 Mon Sep 17 00:00:00 2001 From: JianBo He Date: Wed, 7 Sep 2022 11:03:29 +0800 Subject: [PATCH 3/3] chore: update app.src & appup.src --- lib-ce/emqx_modules/src/emqx_modules.app.src | 2 +- lib-ce/emqx_modules/src/emqx_modules.appup.src | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/lib-ce/emqx_modules/src/emqx_modules.app.src b/lib-ce/emqx_modules/src/emqx_modules.app.src index 055e4c780..0c3328f8c 100644 --- a/lib-ce/emqx_modules/src/emqx_modules.app.src +++ b/lib-ce/emqx_modules/src/emqx_modules.app.src @@ -1,6 +1,6 @@ {application, emqx_modules, [{description, "EMQ X Module Management"}, - {vsn, "4.3.9"}, + {vsn, "4.3.10"}, {modules, []}, {applications, [kernel,stdlib]}, {mod, {emqx_modules_app, []}}, diff --git a/lib-ce/emqx_modules/src/emqx_modules.appup.src b/lib-ce/emqx_modules/src/emqx_modules.appup.src index e4a41be8c..461eefa15 100644 --- a/lib-ce/emqx_modules/src/emqx_modules.appup.src +++ b/lib-ce/emqx_modules/src/emqx_modules.appup.src @@ -1,10 +1,14 @@ %% -*- mode: erlang -*- %% Unless you know what you are doing, DO NOT edit manually!! {VSN, - [{"4.3.8",[{load_module,emqx_modules,brutal_purge,soft_purge,[]}]}, + [{"4.3.9",[{load_module,emqx_mod_delayed,brutal_purge,soft_purge,[]}]}, + {"4.3.8", + [{load_module,emqx_modules,brutal_purge,soft_purge,[]}, + {load_module,emqx_mod_delayed,brutal_purge,soft_purge,[]}]}, {<<"4\\.3\\.[5-7]">>, [{load_module,emqx_mod_rewrite,brutal_purge,soft_purge,[]}, - {load_module,emqx_modules,brutal_purge,soft_purge,[]}]}, + {load_module,emqx_modules,brutal_purge,soft_purge,[]}, + {load_module,emqx_mod_delayed,brutal_purge,soft_purge,[]}]}, {"4.3.4", [{load_module,emqx_modules,brutal_purge,soft_purge,[]}, {load_module,emqx_mod_subscription,brutal_purge,soft_purge,[]}, @@ -31,10 +35,14 @@ {load_module,emqx_mod_api_topic_metrics,brutal_purge,soft_purge,[]}, {load_module,emqx_mod_rewrite,brutal_purge,soft_purge,[]}]}, {<<".*">>,[]}], - [{"4.3.8",[{load_module,emqx_modules,brutal_purge,soft_purge,[]}]}, + [{"4.3.9",[{load_module,emqx_mod_delayed,brutal_purge,soft_purge,[]}]}, + {"4.3.8", + [{load_module,emqx_modules,brutal_purge,soft_purge,[]}, + {load_module,emqx_mod_delayed,brutal_purge,soft_purge,[]}]}, {<<"4\\.3\\.[5-7]">>, [{load_module,emqx_mod_rewrite,brutal_purge,soft_purge,[]}, - {load_module,emqx_modules,brutal_purge,soft_purge,[]}]}, + {load_module,emqx_modules,brutal_purge,soft_purge,[]}, + {load_module,emqx_mod_delayed,brutal_purge,soft_purge,[]}]}, {"4.3.4", [{load_module,emqx_modules,brutal_purge,soft_purge,[]}, {load_module,emqx_mod_subscription,brutal_purge,soft_purge,[]},