Merge pull request #6591 from zhongwencool/api-key-update-unexpired

fix(api_key): set api_key unexpired when update expired_at=undefined
This commit is contained in:
zhongwencool 2022-01-04 10:54:05 +08:00 committed by GitHub
commit aa4eec3127
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 71 additions and 46 deletions

View File

@ -0,0 +1,23 @@
name: Run static checks
concurrency:
group: static-check-${{ github.event_name }}-${{ github.ref }}
cancel-in-progress: true
on:
push:
tags:
- v*
- e*
pull_request:
jobs:
run_static_analysis:
runs-on: self-hosted
container: "ghcr.io/emqx/emqx-builder/5.0-3:24.1.5-3-alpine3.14"
steps:
- uses: actions/checkout@v2
- name: xref
run: make xref
- name: dialyzer
run: make dialyzer

View File

@ -12,22 +12,6 @@ on:
pull_request: pull_request:
jobs: jobs:
run_static_analysis:
strategy:
matrix:
emqx_builder:
- 5.0-3:24.1.5-3 # run dialyzer on latest OTP
runs-on: ubuntu-20.04
container: "ghcr.io/emqx/emqx-builder/${{ matrix.emqx_builder }}-ubuntu20.04"
steps:
- uses: actions/checkout@v2
- name: xref
run: make xref
- name: dialyzer
run: make dialyzer
run_proper_test: run_proper_test:
strategy: strategy:
matrix: matrix:

View File

@ -139,13 +139,7 @@ api_key(post, #{body := App}) ->
<<"desc">> := Desc0, <<"desc">> := Desc0,
<<"enable">> := Enable <<"enable">> := Enable
} = App, } = App,
%% undefined is never expired ExpiredAt = ensure_expired_at(App),
ExpiredAt0 = maps:get(<<"expired_at">>, App, <<"undefined">>),
ExpiredAt =
case ExpiredAt0 of
<<"undefined">> -> undefined;
_ -> ExpiredAt0
end,
Desc = unicode:characters_to_binary(Desc0, unicode), Desc = unicode:characters_to_binary(Desc0, unicode),
case emqx_mgmt_auth:create(Name, Enable, ExpiredAt, Desc) of case emqx_mgmt_auth:create(Name, Enable, ExpiredAt, Desc) of
{ok, NewApp} -> {200, format(NewApp)}; {ok, NewApp} -> {200, format(NewApp)};
@ -164,7 +158,7 @@ api_key_by_name(delete, #{bindings := #{name := Name}}) ->
end; end;
api_key_by_name(put, #{bindings := #{name := Name}, body := Body}) -> api_key_by_name(put, #{bindings := #{name := Name}, body := Body}) ->
Enable = maps:get(<<"enable">>, Body, undefined), Enable = maps:get(<<"enable">>, Body, undefined),
ExpiredAt = maps:get(<<"expired_at">>, Body, undefined), ExpiredAt = ensure_expired_at(Body),
Desc = maps:get(<<"desc">>, Body, undefined), Desc = maps:get(<<"desc">>, Body, undefined),
case emqx_mgmt_auth:update(Name, Enable, ExpiredAt, Desc) of case emqx_mgmt_auth:update(Name, Enable, ExpiredAt, Desc) of
{ok, App} -> {200, format(App)}; {ok, App} -> {200, format(App)};
@ -181,3 +175,6 @@ format(App = #{expired_at := ExpiredAt0, created_at := CreateAt}) ->
expired_at => ExpiredAt, expired_at => ExpiredAt,
created_at => list_to_binary(calendar:system_time_to_rfc3339(CreateAt)) created_at => list_to_binary(calendar:system_time_to_rfc3339(CreateAt))
}. }.
ensure_expired_at(#{<<"expired_at">> := ExpiredAt})when is_integer(ExpiredAt) -> ExpiredAt;
ensure_expired_at(_) -> undefined.

View File

@ -304,13 +304,16 @@ download_trace_log(get, #{bindings := #{name := Name}}) ->
Zips = group_trace_file(ZipDir, TraceLog, TraceFiles), Zips = group_trace_file(ZipDir, TraceLog, TraceFiles),
ZipFileName = ZipDir ++ binary_to_list(Name) ++ ".zip", ZipFileName = ZipDir ++ binary_to_list(Name) ++ ".zip",
{ok, ZipFile} = zip:zip(ZipFileName, Zips, [{cwd, ZipDir}]), {ok, ZipFile} = zip:zip(ZipFileName, Zips, [{cwd, ZipDir}]),
emqx_trace:delete_files_after_send(ZipFileName, Zips), %% emqx_trace:delete_files_after_send(ZipFileName, Zips),
%% TODO use file replace file_binary.(delete file after send is not ready now).
{ok, Binary} = file:read_file(ZipFile),
ZipName = filename:basename(ZipFile),
_ = file:delete(ZipFile),
Headers = #{ Headers = #{
<<"content-type">> => <<"application/x-zip">>, <<"content-type">> => <<"application/x-zip">>,
<<"content-disposition">> => <<"content-disposition">> => iolist_to_binary("attachment; filename=" ++ ZipName)
iolist_to_binary("attachment; filename=" ++ filename:basename(ZipFile))
}, },
{200, Headers, {file, ZipFile}}; {200, Headers, {file_binary, ZipName, Binary}};
{error, not_found} -> ?NOT_FOUND(Name) {error, not_found} -> ?NOT_FOUND(Name)
end. end.

View File

@ -68,11 +68,11 @@ update(Name, Enable, ExpiredAt, Desc) ->
Fun = fun() -> Fun = fun() ->
case mnesia:read(?APP, Name, write) of case mnesia:read(?APP, Name, write) of
[] -> mnesia:abort(not_found); [] -> mnesia:abort(not_found);
[App0 = #?APP{enable = Enable0, expired_at = ExpiredAt0, desc = Desc0}] -> [App0 = #?APP{enable = Enable0, desc = Desc0}] ->
App = App =
App0#?APP{ App0#?APP{
expired_at = ExpiredAt,
enable = ensure_not_undefined(Enable, Enable0), enable = ensure_not_undefined(Enable, Enable0),
expired_at = ensure_not_undefined(ExpiredAt, ExpiredAt0),
desc = ensure_not_undefined(Desc, Desc0) desc = ensure_not_undefined(Desc, Desc0)
}, },
ok = mnesia:write(App), ok = mnesia:write(App),

View File

@ -96,6 +96,13 @@ t_update(_Config) ->
?assertEqual(calendar:rfc3339_to_system_time(binary_to_list(ExpiredAt)), ?assertEqual(calendar:rfc3339_to_system_time(binary_to_list(ExpiredAt)),
calendar:rfc3339_to_system_time(binary_to_list(maps:get(<<"expired_at">>, Update1))) calendar:rfc3339_to_system_time(binary_to_list(maps:get(<<"expired_at">>, Update1)))
), ),
Unexpired1 = maps:without([expired_at], Change),
{ok, Update2} = update_app(Name, Unexpired1),
?assertEqual(<<"undefined">>, maps:get(<<"expired_at">>, Update2)),
Unexpired2 = Change#{expired_at => <<"undefined">>},
{ok, Update3} = update_app(Name, Unexpired2),
?assertEqual(<<"undefined">>, maps:get(<<"expired_at">>, Update3)),
?assertEqual({error, {"HTTP/1.1", 404, "Not Found"}}, update_app(<<"Not-Exist">>, Change)), ?assertEqual({error, {"HTTP/1.1", 404, "Not Found"}}, update_app(<<"Not-Exist">>, Change)),
ok. ok.
@ -137,6 +144,10 @@ t_authorize(_Config) ->
}, },
?assertMatch({ok, #{<<"api_key">> := _, <<"enable">> := true}}, update_app(Name, Expired)), ?assertMatch({ok, #{<<"api_key">> := _, <<"enable">> := true}}, update_app(Name, Expired)),
?assertEqual(Unauthorized, emqx_mgmt_api_test_util:request_api(get, BanPath, BasicHeader)), ?assertEqual(Unauthorized, emqx_mgmt_api_test_util:request_api(get, BanPath, BasicHeader)),
UnExpired = #{expired_at => undefined},
?assertMatch({ok, #{<<"api_key">> := _, <<"expired_at">> := <<"undefined">>}},
update_app(Name, UnExpired)),
{ok, _Status1} = emqx_mgmt_api_test_util:request_api(get, BanPath, BasicHeader),
ok. ok.
t_create_unexpired_app(_Config) -> t_create_unexpired_app(_Config) ->

View File

@ -59,10 +59,12 @@ statsd(put, #{body := Body}) ->
Body, Body,
#{rawconf_with_defaults => true, override_to => cluster}) of #{rawconf_with_defaults => true, override_to => cluster}) of
{ok, #{raw_config := NewConfig, config := Config}} -> {ok, #{raw_config := NewConfig, config := Config}} ->
_ = emqx_statsd_sup:stop_child(?APP), ok = emqx_statsd_sup:ensure_child_stopped(?APP),
case maps:get(<<"enable">>, Body) of case maps:get(<<"enable">>, Body) of
true -> emqx_statsd_sup:start_child(?APP, maps:get(config, Config)); true ->
false -> ok ok = emqx_statsd_sup:ensure_child_started(?APP, maps:get(config, Config));
false ->
ok
end, end,
{200, NewConfig}; {200, NewConfig};
{error, Reason} -> {error, Reason} ->

View File

@ -34,7 +34,7 @@ stop(_) ->
maybe_enable_statsd() -> maybe_enable_statsd() ->
case emqx_conf:get([statsd, enable], false) of case emqx_conf:get([statsd, enable], false) of
true -> true ->
emqx_statsd_sup:start_child(?APP, emqx_conf:get([statsd], #{})); emqx_statsd_sup:ensure_child_started(?APP, emqx_conf:get([statsd], #{}));
false -> false ->
ok ok
end. end.

View File

@ -8,9 +8,9 @@
-behaviour(supervisor). -behaviour(supervisor).
-export([ start_link/0 -export([ start_link/0
, start_child/1 , ensure_child_started/1
, start_child/2 , ensure_child_started/2
, stop_child/1 , ensure_child_stopped/1
]). ]).
-export([init/1]). -export([init/1]).
@ -26,19 +26,24 @@
start_link() -> start_link() ->
supervisor:start_link({local, ?MODULE}, ?MODULE, []). supervisor:start_link({local, ?MODULE}, ?MODULE, []).
-spec start_child(supervisor:child_spec()) -> ok. -spec ensure_child_started(supervisor:child_spec()) -> ok.
start_child(ChildSpec) when is_map(ChildSpec) -> ensure_child_started(ChildSpec) when is_map(ChildSpec) ->
assert_started(supervisor:start_child(?MODULE, ChildSpec)). assert_started(supervisor:start_child(?MODULE, ChildSpec)).
-spec start_child(atom(), map()) -> ok. -spec ensure_child_started(atom(), map()) -> ok.
start_child(Mod, Opts) when is_atom(Mod) andalso is_map(Opts) -> ensure_child_started(Mod, Opts) when is_atom(Mod) andalso is_map(Opts) ->
assert_started(supervisor:start_child(?MODULE, ?CHILD(Mod, Opts))). assert_started(supervisor:start_child(?MODULE, ?CHILD(Mod, Opts))).
-spec(stop_child(any()) -> ok | {error, term()}). %% @doc Stop the child worker process.
stop_child(ChildId) -> -spec ensure_child_stopped(any()) -> ok.
ensure_child_stopped(ChildId) ->
case supervisor:terminate_child(?MODULE, ChildId) of case supervisor:terminate_child(?MODULE, ChildId) of
ok -> supervisor:delete_child(?MODULE, ChildId); ok ->
Error -> Error %% with terminate_child/2 returned 'ok', it's not possible
%% for supervisor:delete_child/2 to return {error, Reason}
ok = supervisor:delete_child(?MODULE, ChildId);
{error, not_found} ->
ok
end. end.
init([]) -> init([]) ->
@ -50,5 +55,5 @@ init([]) ->
assert_started({ok, _Pid}) -> ok; assert_started({ok, _Pid}) -> ok;
assert_started({ok, _Pid, _Info}) -> ok; assert_started({ok, _Pid, _Info}) -> ok;
assert_started({error, {already_tarted, _Pid}}) -> ok; assert_started({error, {already_started, _Pid}}) -> ok;
assert_started({error, Reason}) -> erlang:error(Reason). assert_started({error, Reason}) -> erlang:error(Reason).