chore(authz): formatting error returned

Signed-off-by: zhanghongtong <rory-z@outlook.com>
This commit is contained in:
zhanghongtong 2021-08-11 10:33:19 +08:00 committed by Rory Z
parent 69a0fafd6b
commit 632cc895d5
4 changed files with 136 additions and 16 deletions

View File

@ -95,8 +95,7 @@ api() ->
} }
} }
} }
}, }
<<"404">> => #{description => <<"Not Found">>}
} }
}, },
post => #{ post => #{
@ -115,8 +114,24 @@ api() ->
} }
}, },
responses => #{ responses => #{
<<"201">> => #{description => <<"Created">>}, <<"204">> => #{description => <<"Created">>},
<<"400">> => #{description => <<"Bad Request">>} <<"400">> => #{
description => <<"Bad Request">>,
content => #{
'application/json' => #{
schema => minirest:ref(<<"error">>),
examples => #{
example1 => #{
summary => <<"Bad Request">>,
value => #{
code => <<"BAD_REQUEST">>,
message => <<"Bad Request">>
}
}
}
}
}
}
} }
}, },
put => #{ put => #{
@ -138,8 +153,24 @@ api() ->
} }
}, },
responses => #{ responses => #{
<<"201">> => #{description => <<"Created">>}, <<"204">> => #{description => <<"Created">>},
<<"400">> => #{description => <<"Bad Request">>} <<"400">> => #{
description => <<"Bad Request">>,
content => #{
'application/json' => #{
schema => minirest:ref(<<"error">>),
examples => #{
example1 => #{
summary => <<"Bad Request">>,
value => #{
code => <<"BAD_REQUEST">>,
message => <<"Bad Request">>
}
}
}
}
}
}
} }
} }
}, },
@ -174,7 +205,23 @@ once_api() ->
} }
} }
}, },
<<"404">> => #{description => <<"Not Found">>} <<"404">> => #{
description => <<"Bad Request">>,
content => #{
'application/json' => #{
schema => minirest:ref(<<"error">>),
examples => #{
example1 => #{
summary => <<"Not Found">>,
value => #{
code => <<"NOT_FOUND">>,
message => <<"rule xxx not found">>
}
}
}
}
}
}
} }
}, },
put => #{ put => #{
@ -204,7 +251,40 @@ once_api() ->
}, },
responses => #{ responses => #{
<<"204">> => #{description => <<"No Content">>}, <<"204">> => #{description => <<"No Content">>},
<<"400">> => #{description => <<"Bad Request">>} <<"404">> => #{
description => <<"Bad Request">>,
content => #{
'application/json' => #{
schema => minirest:ref(<<"error">>),
examples => #{
example1 => #{
summary => <<"Not Found">>,
value => #{
code => <<"NOT_FOUND">>,
message => <<"rule xxx not found">>
}
}
}
}
}
},
<<"400">> => #{
description => <<"Bad Request">>,
content => #{
'application/json' => #{
schema => minirest:ref(<<"error">>),
examples => #{
example1 => #{
summary => <<"Bad Request">>,
value => #{
code => <<"BAD_REQUEST">>,
message => <<"Bad Request">>
}
}
}
}
}
}
} }
}, },
delete => #{ delete => #{
@ -221,7 +301,23 @@ once_api() ->
], ],
responses => #{ responses => #{
<<"204">> => #{description => <<"No Content">>}, <<"204">> => #{description => <<"No Content">>},
<<"400">> => #{description => <<"Bad Request">>} <<"400">> => #{
description => <<"Bad Request">>,
content => #{
'application/json' => #{
schema => minirest:ref(<<"error">>),
examples => #{
example1 => #{
summary => <<"Bad Request">>,
value => #{
code => <<"BAD_REQUEST">>,
message => <<"Bad Request">>
}
}
}
}
}
}
} }
} }
}, },
@ -258,15 +354,19 @@ authorization(post, Request) ->
{ok, Body, _} = cowboy_req:read_body(Request), {ok, Body, _} = cowboy_req:read_body(Request),
RawConfig = jsx:decode(Body, [return_maps]), RawConfig = jsx:decode(Body, [return_maps]),
case emqx_authz:update(head, [RawConfig]) of case emqx_authz:update(head, [RawConfig]) of
ok -> {201}; ok -> {204};
{error, Reason} -> {400, #{messgae => atom_to_binary(Reason)}} {error, Reason} ->
{400, #{code => <<"BAD_REQUEST">>,
messgae => atom_to_binary(Reason)}}
end; end;
authorization(put, Request) -> authorization(put, Request) ->
{ok, Body, _} = cowboy_req:read_body(Request), {ok, Body, _} = cowboy_req:read_body(Request),
RawConfig = jsx:decode(Body, [return_maps]), RawConfig = jsx:decode(Body, [return_maps]),
case emqx_authz:update(replace, RawConfig) of case emqx_authz:update(replace, RawConfig) of
ok -> {204}; ok -> {204};
{error, Reason} -> {400, #{messgae => atom_to_binary(Reason)}} {error, Reason} ->
{400, #{code => <<"BAD_REQUEST">>,
messgae => atom_to_binary(Reason)}}
end. end.
authorization_once(get, Request) -> authorization_once(get, Request) ->
@ -292,11 +392,18 @@ authorization_once(put, Request) ->
RawConfig = jsx:decode(Body, [return_maps]), RawConfig = jsx:decode(Body, [return_maps]),
case emqx_authz:update({replace_once, RuleId}, RawConfig) of case emqx_authz:update({replace_once, RuleId}, RawConfig) of
ok -> {204}; ok -> {204};
{error, Reason} -> {400, #{messgae => atom_to_binary(Reason)}} {error, not_found_rule} ->
{404, #{code => <<"NOT_FOUND">>,
messgae => <<"rule ", RuleId/binary, " not found">>}};
{error, Reason} ->
{400, #{code => <<"BAD_REQUEST">>,
messgae => atom_to_binary(Reason)}}
end; end;
authorization_once(delete, Request) -> authorization_once(delete, Request) ->
RuleId = cowboy_req:binding(id, Request), RuleId = cowboy_req:binding(id, Request),
case emqx_authz:update({replace_once, RuleId}, #{}) of case emqx_authz:update({replace_once, RuleId}, #{}) of
ok -> {204}; ok -> {204};
{error, Reason} -> {400, #{messgae => atom_to_binary(Reason)}} {error, Reason} ->
{400, #{code => <<"BAD_REQUEST">>,
messgae => atom_to_binary(Reason)}}
end. end.

View File

@ -134,6 +134,18 @@ definitions() ->
properties => #{ipaddress => #{type => string}}, properties => #{ipaddress => #{type => string}},
example => #{ipaddress => <<"127.0.0.1">>} example => #{ipaddress => <<"127.0.0.1">>}
}, },
ErrorDef = #{
type => object,
properties => #{
code => #{
type => string,
example => <<"BAD_REQUEST">>
},
message => #{
type => string
}
}
},
[ #{<<"returned_rules">> => RetruenedRules} [ #{<<"returned_rules">> => RetruenedRules}
, #{<<"rules">> => Rules} , #{<<"rules">> => Rules}
, #{<<"simple_rule">> => SimpleRule} , #{<<"simple_rule">> => SimpleRule}
@ -141,4 +153,5 @@ definitions() ->
, #{<<"principal_username">> => PrincipalUsername} , #{<<"principal_username">> => PrincipalUsername}
, #{<<"principal_clientid">> => PrincipalClientid} , #{<<"principal_clientid">> => PrincipalClientid}
, #{<<"principal_ipaddress">> => PrincipalIpaddress} , #{<<"principal_ipaddress">> => PrincipalIpaddress}
, #{<<"error">> => ErrorDef}
]. ].

View File

@ -76,7 +76,7 @@ t_post(_) ->
?assertEqual([], get_rules(Result1)), ?assertEqual([], get_rules(Result1)),
lists:foreach(fun(_) -> lists:foreach(fun(_) ->
{ok, 201, _} = request(post, uri(["authorization"]), {ok, 204, _} = request(post, uri(["authorization"]),
#{<<"action">> => <<"all">>, #{<<"action">> => <<"all">>,
<<"permission">> => <<"deny">>, <<"permission">> => <<"deny">>,
<<"principal">> => <<"all">>, <<"principal">> => <<"all">>,