feat: authz & authn mysql resource support prepare sql query

This commit is contained in:
DDDHuang 2022-04-20 10:50:38 +08:00
parent d2c4b862b6
commit 039619ee23
3 changed files with 24 additions and 21 deletions

View File

@ -89,12 +89,12 @@ create(
} = Config } = Config
) -> ) ->
ok = emqx_authn_password_hashing:init(Algorithm), ok = emqx_authn_password_hashing:init(Algorithm),
{Query, PlaceHolders} = emqx_authn_utils:parse_sql(Query0, '?'), {PrepareSqlKey, PrepareStatement} = emqx_authn_utils:parse_sql(Query0, '?'),
ResourceId = emqx_authn_utils:make_resource_id(?MODULE), ResourceId = emqx_authn_utils:make_resource_id(?MODULE),
State = #{ State = #{
password_hash_algorithm => Algorithm, password_hash_algorithm => Algorithm,
query => Query, prepare_sql_key => PrepareSqlKey,
placeholders => PlaceHolders, prepare_sql_statement => PrepareStatement,
query_timeout => QueryTimeout, query_timeout => QueryTimeout,
resource_id => ResourceId resource_id => ResourceId
}, },
@ -107,12 +107,16 @@ create(
#{} #{}
) )
of of
{ok, already_created} ->
{ok, State};
{ok, _} -> {ok, _} ->
case emqx_resource:query(ResourceId,
{prepare_sql, [{PrepareSqlKey, PrepareStatement}]}) of
ok ->
{ok, State}; {ok, State};
{error, Reason} -> {error, Reason} ->
{error, Reason} {error, Reason}
end;
{error, Reason} ->
{error, Reason}
end. end.
update(Config, State) -> update(Config, State) ->

View File

@ -52,17 +52,13 @@ init(#{query := SQL} = Source) ->
{error, Reason} -> {error, Reason} ->
error({load_config_error, Reason}); error({load_config_error, Reason});
{ok, Id} -> {ok, Id} ->
Source#{ {PrepareKey, PrepareStatement} = emqx_authz_utils:parse_sql(SQL, '?', ?PLACEHOLDERS),
annotations => case emqx_resource:query(Id, {prepare_sql, [{PrepareKey, PrepareStatement}]}) of
#{ ok ->
id => Id, Source#{annotations => #{id => Id, prepare => {PrepareKey, PrepareStatement}}};
query => emqx_authz_utils:parse_sql( {error, Reason} ->
SQL, error({load_config_error, Reason})
'?', end
?PLACEHOLDERS
)
}
}
end. end.
destroy(#{annotations := #{id := Id}}) -> destroy(#{annotations := #{id := Id}}) ->
@ -75,12 +71,12 @@ authorize(
#{ #{
annotations := #{ annotations := #{
id := ResourceID, id := ResourceID,
query := {Query, Params} prepare := {PrepareKey, PrepareStatement}
} }
} }
) -> ) ->
RenderParams = emqx_authz_utils:render_sql_params(Params, Client), RenderParams = emqx_authz_utils:render_sql_params(PrepareStatement, Client),
case emqx_resource:query(ResourceID, {sql, Query, RenderParams}) of case emqx_resource:query(ResourceID, {sql, PrepareKey, RenderParams}) of
{ok, _Columns, []} -> {ok, _Columns, []} ->
nomatch; nomatch;
{ok, Columns, Rows} -> {ok, Columns, Rows} ->
@ -89,7 +85,7 @@ authorize(
?SLOG(error, #{ ?SLOG(error, #{
msg => "query_mysql_error", msg => "query_mysql_error",
reason => Reason, reason => Reason,
query => Query, prepare => {PrepareKey, PrepareStatement},
params => RenderParams, params => RenderParams,
resource_id => ResourceID resource_id => ResourceID
}), }),

View File

@ -94,6 +94,9 @@ on_stop(InstId, #{poolname := PoolName}) ->
connector => InstId}), connector => InstId}),
emqx_plugin_libs_pool:stop_pool(PoolName). emqx_plugin_libs_pool:stop_pool(PoolName).
on_query(_InstId, {prepare_sql, Prepares}, _AfterQuery, #{poolname := PoolName}) ->
prepare_sql(Prepares, PoolName);
on_query(InstId, {Type, SQLOrKey}, AfterQuery, #{poolname := _PoolName} = State) -> on_query(InstId, {Type, SQLOrKey}, AfterQuery, #{poolname := _PoolName} = State) ->
on_query(InstId, {Type, SQLOrKey, [], default_timeout}, AfterQuery, State); on_query(InstId, {Type, SQLOrKey, [], default_timeout}, AfterQuery, State);
on_query(InstId, {Type, SQLOrKey, Params}, AfterQuery, #{poolname := _PoolName} = State) -> on_query(InstId, {Type, SQLOrKey, Params}, AfterQuery, #{poolname := _PoolName} = State) ->