chore(authz api): delete healthy status for get method

This commit is contained in:
zhanghongtong 2021-09-23 17:59:32 +08:00 committed by Rory Z
parent 22e8c83a52
commit 4f0684e887
3 changed files with 15 additions and 65 deletions

View File

@ -36,8 +36,6 @@
, authorize/5
]).
-export([gen_id/1]).
-export([post_config_update/4, pre_config_update/2]).
-define(CONF_KEY_PATH, [authorization, sources]).

View File

@ -19,27 +19,6 @@
-export([definitions/0]).
definitions() ->
RetruenedSources = #{
allOf => [ #{type => object,
properties => #{
annotations => #{
type => object,
required => [status],
properties => #{
id => #{
type => string
},
status => #{
type => string,
example => <<"healthy">>
}
}
}
}
}
, minirest:ref(<<"sources">>)
]
},
Sources = #{
oneOf => [ minirest:ref(<<"http">>)
, minirest:ref(<<"mongo_single">>)
@ -493,8 +472,7 @@ definitions() ->
}
}
},
[ #{<<"returned_sources">> => RetruenedSources}
, #{<<"sources">> => Sources}
[ #{<<"sources">> => Sources}
, #{<<"ssl">> => SSL}
, #{<<"http">> => HTTP}
, #{<<"mongo_single">> => MongoSingle}

View File

@ -35,16 +35,9 @@
rules => <<"{allow,{username,\"^dashboard?\"},subscribe,[\"$SYS/#\"]}.\n{allow,{ipaddr,\"127.0.0.1\"},all,[\"$SYS/#\",\"#\"]}.">>
}).
-define(EXAMPLE_RETURNED_REDIS,
maps:put(annotations, #{status => healthy}, ?EXAMPLE_REDIS)
).
-define(EXAMPLE_RETURNED_FILE,
maps:put(annotations, #{status => healthy}, ?EXAMPLE_FILE)
).
-define(EXAMPLE_RETURNED,
#{sources => [ ?EXAMPLE_RETURNED_REDIS
, ?EXAMPLE_RETURNED_FILE
#{sources => [ ?EXAMPLE_REDIS
, ?EXAMPLE_FILE
]
}).
@ -76,7 +69,7 @@ sources_api() ->
required => [sources],
properties => #{sources => #{
type => array,
items => minirest:ref(<<"returned_sources">>)
items => minirest:ref(<<"sources">>)
}
}
},
@ -122,7 +115,7 @@ sources_api() ->
'application/json' => #{
schema => #{
type => array,
items => minirest:ref(<<"returned_sources">>)
items => minirest:ref(<<"sources">>)
},
examples => #{
redis => #{
@ -164,15 +157,15 @@ source_api() ->
description => <<"OK">>,
content => #{
'application/json' => #{
schema => minirest:ref(<<"returned_sources">>),
schema => minirest:ref(<<"sources">>),
examples => #{
redis => #{
summary => <<"Redis">>,
value => jsx:encode(?EXAMPLE_RETURNED_REDIS)
value => jsx:encode(?EXAMPLE_REDIS)
},
file => #{
summary => <<"File">>,
value => jsx:encode(?EXAMPLE_RETURNED_FILE)
value => jsx:encode(?EXAMPLE_FILE)
}
}
}
@ -302,26 +295,16 @@ sources(get, _) ->
{ok, Rules} ->
lists:append(AccIn, [#{type => file,
enable => Enable,
rules => Rules,
annotations => #{status => healthy}
rules => Rules
}]);
{error, _} ->
lists:append(AccIn, [#{type => file,
enable => Enable,
rules => <<"">>,
annotations => #{status => unhealthy}
rules => <<"">>
}])
end;
(#{type := DB, enable := true} = Source, AccIn) ->
NSource = case emqx_resource:health_check(emqx_authz:gen_id(DB)) of
ok ->
Source#{annotations => #{status => healthy}};
_ ->
Source#{annotations => #{status => unhealthy}}
end,
lists:append(AccIn, [read_cert(NSource)]);
(#{enable := false} = Source, AccIn) ->
lists:append(AccIn, [Source#{annotations => #{status => unhealthy}}])
(Source, AccIn) ->
lists:append(AccIn, [read_cert(Source)])
end, [], get_raw_sources()),
{200, #{sources => Sources}};
sources(post, #{body := #{<<"type">> := <<"file">>, <<"rules">> := Rules}}) ->
@ -348,24 +331,15 @@ source(get, #{bindings := #{type := Type}}) ->
{ok, Rules} ->
{200, #{type => file,
enable => Enable,
rules => Rules,
annotations => #{status => healthy}
rules => Rules
}
};
{error, Reason} ->
{400, #{code => <<"BAD_REQUEST">>,
message => atom_to_binary(Reason)}}
end;
[#{type := DB, enable := true} = Source] ->
NSource = case emqx_resource:health_check(emqx_authz:gen_id(DB)) of
ok ->
Source#{annotations => #{status => healthy}};
_ ->
Source#{annotations => #{status => unhealthy}}
end,
{200, read_cert(NSource)};
[#{enable := false} = Source] ->
{200, Source#{annotations => #{status => unhealthy}}}
[Source] ->
{200, read_cert(Source)}
end;
source(put, #{bindings := #{type := <<"file">>}, body := #{<<"type">> := <<"file">>, <<"rules">> := Rules, <<"enable">> := Enable}}) ->
{ok, Filename} = write_file(maps:get(path, emqx_authz:lookup(file), ""), Rules),