fix(postgres): fix usage by authn/authz modules when prepared statements are disabled
Fixes https://emqx.atlassian.net/browse/EMQX-12550
This commit is contained in:
parent
fd5e844cc1
commit
6e702cd16c
|
@ -30,15 +30,28 @@
|
||||||
|
|
||||||
-define(PATH, [authentication]).
|
-define(PATH, [authentication]).
|
||||||
|
|
||||||
|
-import(emqx_common_test_helpers, [on_exit/1]).
|
||||||
|
|
||||||
all() ->
|
all() ->
|
||||||
|
AllTCs = emqx_common_test_helpers:all(?MODULE),
|
||||||
|
TCs = AllTCs -- require_seeds_tests(),
|
||||||
[
|
[
|
||||||
{group, require_seeds},
|
{group, require_seeds}
|
||||||
t_update_with_invalid_config,
|
| TCs
|
||||||
t_update_with_bad_config_value
|
|
||||||
].
|
].
|
||||||
|
|
||||||
groups() ->
|
groups() ->
|
||||||
[{require_seeds, [], [t_create, t_authenticate, t_update, t_destroy, t_is_superuser]}].
|
[{require_seeds, [], require_seeds_tests()}].
|
||||||
|
|
||||||
|
require_seeds_tests() ->
|
||||||
|
[
|
||||||
|
t_create,
|
||||||
|
t_authenticate,
|
||||||
|
t_authenticate_disabled_prepared_statements,
|
||||||
|
t_update,
|
||||||
|
t_destroy,
|
||||||
|
t_is_superuser
|
||||||
|
].
|
||||||
|
|
||||||
init_per_testcase(_, Config) ->
|
init_per_testcase(_, Config) ->
|
||||||
emqx_authn_test_lib:delete_authenticators(
|
emqx_authn_test_lib:delete_authenticators(
|
||||||
|
@ -47,6 +60,10 @@ init_per_testcase(_, Config) ->
|
||||||
),
|
),
|
||||||
Config.
|
Config.
|
||||||
|
|
||||||
|
end_per_testcase(_TestCase, _Config) ->
|
||||||
|
emqx_common_test_helpers:call_janitor(),
|
||||||
|
ok.
|
||||||
|
|
||||||
init_per_group(require_seeds, Config) ->
|
init_per_group(require_seeds, Config) ->
|
||||||
ok = init_seeds(),
|
ok = init_seeds(),
|
||||||
Config.
|
Config.
|
||||||
|
@ -70,7 +87,12 @@ init_per_suite(Config) ->
|
||||||
),
|
),
|
||||||
[{apps, Apps} | Config];
|
[{apps, Apps} | Config];
|
||||||
false ->
|
false ->
|
||||||
{skip, no_pgsql}
|
case os:getenv("IS_CI") of
|
||||||
|
"yes" ->
|
||||||
|
throw(no_postgres);
|
||||||
|
_ ->
|
||||||
|
{skip, no_postgres}
|
||||||
|
end
|
||||||
end.
|
end.
|
||||||
|
|
||||||
end_per_suite(Config) ->
|
end_per_suite(Config) ->
|
||||||
|
@ -174,6 +196,25 @@ test_user_auth(#{
|
||||||
?GLOBAL
|
?GLOBAL
|
||||||
).
|
).
|
||||||
|
|
||||||
|
t_authenticate_disabled_prepared_statements(Config) ->
|
||||||
|
ResConfig = maps:merge(pgsql_config(), #{disable_prepared_statements => true}),
|
||||||
|
{ok, _} = emqx_resource:recreate_local(?PGSQL_RESOURCE, emqx_postgresql, ResConfig),
|
||||||
|
on_exit(fun() ->
|
||||||
|
emqx_resource:recreate_local(?PGSQL_RESOURCE, emqx_postgresql, pgsql_config())
|
||||||
|
end),
|
||||||
|
ok = lists:foreach(
|
||||||
|
fun(Sample0) ->
|
||||||
|
Sample = maps:update_with(
|
||||||
|
config_params,
|
||||||
|
fun(Cfg) -> Cfg#{<<"disable_prepared_statements">> => true} end,
|
||||||
|
Sample0
|
||||||
|
),
|
||||||
|
ct:pal("test_user_auth sample: ~p", [Sample]),
|
||||||
|
test_user_auth(Sample)
|
||||||
|
end,
|
||||||
|
user_seeds()
|
||||||
|
).
|
||||||
|
|
||||||
t_destroy(_Config) ->
|
t_destroy(_Config) ->
|
||||||
AuthConfig = raw_pgsql_auth_config(),
|
AuthConfig = raw_pgsql_auth_config(),
|
||||||
|
|
||||||
|
|
|
@ -601,7 +601,7 @@ t_simple_sql_query(Config) ->
|
||||||
{ok, _},
|
{ok, _},
|
||||||
create_bridge(Config)
|
create_bridge(Config)
|
||||||
),
|
),
|
||||||
Request = {sql, <<"SELECT count(1) AS T">>},
|
Request = {query, <<"SELECT count(1) AS T">>},
|
||||||
Result =
|
Result =
|
||||||
case QueryMode of
|
case QueryMode of
|
||||||
sync ->
|
sync ->
|
||||||
|
@ -651,7 +651,7 @@ t_bad_sql_parameter(Config) ->
|
||||||
{ok, _},
|
{ok, _},
|
||||||
create_bridge(Config)
|
create_bridge(Config)
|
||||||
),
|
),
|
||||||
Request = {sql, <<"">>, [bad_parameter]},
|
Request = {query, <<"">>, [bad_parameter]},
|
||||||
Result =
|
Result =
|
||||||
case QueryMode of
|
case QueryMode of
|
||||||
sync ->
|
sync ->
|
||||||
|
|
|
@ -59,6 +59,9 @@
|
||||||
default_port => ?PGSQL_DEFAULT_PORT
|
default_port => ?PGSQL_DEFAULT_PORT
|
||||||
}).
|
}).
|
||||||
|
|
||||||
|
-type connector_resource_id() :: binary().
|
||||||
|
-type action_resource_id() :: binary().
|
||||||
|
|
||||||
-type template() :: {unicode:chardata(), emqx_template_sql:row_template()}.
|
-type template() :: {unicode:chardata(), emqx_template_sql:row_template()}.
|
||||||
-type state() ::
|
-type state() ::
|
||||||
#{
|
#{
|
||||||
|
@ -319,38 +322,40 @@ do_check_channel_sql(
|
||||||
on_get_channels(ResId) ->
|
on_get_channels(ResId) ->
|
||||||
emqx_bridge_v2:get_channels_for_connector(ResId).
|
emqx_bridge_v2:get_channels_for_connector(ResId).
|
||||||
|
|
||||||
on_query(InstId, {TypeOrKey, NameOrSQL}, State) ->
|
-spec on_query
|
||||||
on_query(InstId, {TypeOrKey, NameOrSQL, []}, State);
|
%% Called from authn and authz modules
|
||||||
|
(connector_resource_id(), {prepared_query, binary(), [term()]}, state()) ->
|
||||||
|
{ok, _} | {error, term()};
|
||||||
|
%% Called from bridges
|
||||||
|
(connector_resource_id(), {action_resource_id(), map()}, state()) ->
|
||||||
|
{ok, _} | {error, term()}.
|
||||||
|
on_query(InstId, {TypeOrKey, NameOrMap}, State) ->
|
||||||
|
on_query(InstId, {TypeOrKey, NameOrMap, []}, State);
|
||||||
on_query(
|
on_query(
|
||||||
InstId,
|
InstId,
|
||||||
{TypeOrKey, NameOrSQL, Params},
|
{TypeOrKey, NameOrMap, Params},
|
||||||
#{pool_name := PoolName} = State
|
#{pool_name := PoolName} = State
|
||||||
) ->
|
) ->
|
||||||
?SLOG(debug, #{
|
?SLOG(debug, #{
|
||||||
msg => "postgresql_connector_received_sql_query",
|
msg => "postgresql_connector_received_sql_query",
|
||||||
connector => InstId,
|
connector => InstId,
|
||||||
type => TypeOrKey,
|
type => TypeOrKey,
|
||||||
sql => NameOrSQL,
|
sql => NameOrMap,
|
||||||
state => State
|
state => State
|
||||||
}),
|
}),
|
||||||
Type = pgsql_query_type(TypeOrKey, State),
|
{QueryType, NameOrSQL2, Data} = proc_sql_params(TypeOrKey, NameOrMap, Params, State),
|
||||||
{NameOrSQL2, Data} = proc_sql_params(TypeOrKey, NameOrSQL, Params, State),
|
emqx_trace:rendered_action_template(
|
||||||
Res = on_sql_query(TypeOrKey, InstId, PoolName, Type, NameOrSQL2, Data),
|
TypeOrKey,
|
||||||
|
#{
|
||||||
|
statement_type => QueryType,
|
||||||
|
statement_or_name => NameOrSQL2,
|
||||||
|
data => Data
|
||||||
|
}
|
||||||
|
),
|
||||||
|
Res = on_sql_query(InstId, PoolName, QueryType, NameOrSQL2, Data),
|
||||||
?tp(postgres_bridge_connector_on_query_return, #{instance_id => InstId, result => Res}),
|
?tp(postgres_bridge_connector_on_query_return, #{instance_id => InstId, result => Res}),
|
||||||
handle_result(Res).
|
handle_result(Res).
|
||||||
|
|
||||||
pgsql_query_type(_TypeOrTag, #{prepares := disabled}) ->
|
|
||||||
query;
|
|
||||||
pgsql_query_type(sql, _ConnectorState) ->
|
|
||||||
query;
|
|
||||||
pgsql_query_type(query, _ConnectorState) ->
|
|
||||||
query;
|
|
||||||
pgsql_query_type(prepared_query, _ConnectorState) ->
|
|
||||||
prepared_query;
|
|
||||||
%% for bridge
|
|
||||||
pgsql_query_type(_, ConnectorState) ->
|
|
||||||
pgsql_query_type(prepared_query, ConnectorState).
|
|
||||||
|
|
||||||
on_batch_query(
|
on_batch_query(
|
||||||
InstId,
|
InstId,
|
||||||
[{Key, _} = Request | _] = BatchReq,
|
[{Key, _} = Request | _] = BatchReq,
|
||||||
|
@ -370,7 +375,15 @@ on_batch_query(
|
||||||
{_Statement, RowTemplate} ->
|
{_Statement, RowTemplate} ->
|
||||||
StatementTemplate = get_templated_statement(BinKey, State),
|
StatementTemplate = get_templated_statement(BinKey, State),
|
||||||
Rows = [render_prepare_sql_row(RowTemplate, Data) || {_Key, Data} <- BatchReq],
|
Rows = [render_prepare_sql_row(RowTemplate, Data) || {_Key, Data} <- BatchReq],
|
||||||
case on_sql_query(Key, InstId, PoolName, execute_batch, StatementTemplate, Rows) of
|
emqx_trace:rendered_action_template(
|
||||||
|
Key,
|
||||||
|
#{
|
||||||
|
statement_type => execute_batch,
|
||||||
|
statement_or_name => StatementTemplate,
|
||||||
|
data => Rows
|
||||||
|
}
|
||||||
|
),
|
||||||
|
case on_sql_query(InstId, PoolName, execute_batch, StatementTemplate, Rows) of
|
||||||
{error, _Error} = Result ->
|
{error, _Error} = Result ->
|
||||||
handle_result(Result);
|
handle_result(Result);
|
||||||
{_Column, Results} ->
|
{_Column, Results} ->
|
||||||
|
@ -386,25 +399,33 @@ on_batch_query(InstId, BatchReq, State) ->
|
||||||
}),
|
}),
|
||||||
{error, {unrecoverable_error, invalid_request}}.
|
{error, {unrecoverable_error, invalid_request}}.
|
||||||
|
|
||||||
proc_sql_params(query, SQLOrKey, Params, _State) ->
|
proc_sql_params(ActionResId, #{} = Map, [], State) when is_binary(ActionResId) ->
|
||||||
{SQLOrKey, Params};
|
%% When this connector is called from actions/bridges.
|
||||||
proc_sql_params(prepared_query, SQLOrKey, Params, _State) ->
|
|
||||||
{SQLOrKey, Params};
|
|
||||||
proc_sql_params(TypeOrKey, SQLOrData, Params, State) ->
|
|
||||||
DisablePreparedStatements = maps:get(prepares, State, #{}) =:= disabled,
|
DisablePreparedStatements = maps:get(prepares, State, #{}) =:= disabled,
|
||||||
BinKey = to_bin(TypeOrKey),
|
{ExprTemplate, RowTemplate} = get_template(ActionResId, State),
|
||||||
case get_template(BinKey, State) of
|
Rendered = render_prepare_sql_row(RowTemplate, Map),
|
||||||
undefined ->
|
case DisablePreparedStatements of
|
||||||
{SQLOrData, Params};
|
true ->
|
||||||
{Statement, RowTemplate} ->
|
{query, ExprTemplate, Rendered};
|
||||||
Rendered = render_prepare_sql_row(RowTemplate, SQLOrData),
|
false ->
|
||||||
case DisablePreparedStatements of
|
{prepared_query, ActionResId, Rendered}
|
||||||
true ->
|
end;
|
||||||
{Statement, Rendered};
|
proc_sql_params(prepared_query, ConnResId, Params, State) ->
|
||||||
false ->
|
%% When this connector is called from authn/authz modules
|
||||||
{BinKey, Rendered}
|
DisablePreparedStatements = maps:get(prepares, State, #{}) =:= disabled,
|
||||||
end
|
case DisablePreparedStatements of
|
||||||
end.
|
true ->
|
||||||
|
#{query_templates := #{ConnResId := {ExprTemplate, _VarsTemplate}}} = State,
|
||||||
|
{query, ExprTemplate, Params};
|
||||||
|
false ->
|
||||||
|
%% Connector resource id itself is the prepared statement name
|
||||||
|
{prepared_query, ConnResId, Params}
|
||||||
|
end;
|
||||||
|
proc_sql_params(QueryType, SQL, Params, _State) when
|
||||||
|
is_atom(QueryType), is_binary(SQL) orelse is_list(SQL), is_list(Params)
|
||||||
|
->
|
||||||
|
%% When called to do ad-hoc commands/queries.
|
||||||
|
{QueryType, SQL, Params}.
|
||||||
|
|
||||||
get_template(Key, #{installed_channels := Channels} = _State) when is_map_key(Key, Channels) ->
|
get_template(Key, #{installed_channels := Channels} = _State) when is_map_key(Key, Channels) ->
|
||||||
BinKey = to_bin(Key),
|
BinKey = to_bin(Key),
|
||||||
|
@ -430,15 +451,7 @@ get_templated_statement(Key, #{prepares := PrepStatements}) ->
|
||||||
BinKey = to_bin(Key),
|
BinKey = to_bin(Key),
|
||||||
maps:get(BinKey, PrepStatements).
|
maps:get(BinKey, PrepStatements).
|
||||||
|
|
||||||
on_sql_query(Key, InstId, PoolName, Type, NameOrSQL, Data) ->
|
on_sql_query(InstId, PoolName, Type, NameOrSQL, Data) ->
|
||||||
emqx_trace:rendered_action_template(
|
|
||||||
Key,
|
|
||||||
#{
|
|
||||||
statement_type => Type,
|
|
||||||
statement_or_name => NameOrSQL,
|
|
||||||
data => Data
|
|
||||||
}
|
|
||||||
),
|
|
||||||
try ecpool:pick_and_do(PoolName, {?MODULE, Type, [NameOrSQL, Data]}, no_handover) of
|
try ecpool:pick_and_do(PoolName, {?MODULE, Type, [NameOrSQL, Data]}, no_handover) of
|
||||||
{error, Reason} ->
|
{error, Reason} ->
|
||||||
?tp(
|
?tp(
|
||||||
|
|
Loading…
Reference in New Issue