diff --git a/apps/emqx_authz/README.md b/apps/emqx_authz/README.md index bc94578c0..a44297a55 100644 --- a/apps/emqx_authz/README.md +++ b/apps/emqx_authz/README.md @@ -26,7 +26,7 @@ authz:{ sql: "select ipaddress, username, clientid, action, permission, topic from mqtt_authz where ipaddr = '%a' or username = '%u' or clientid = '%c'" }, { - type: pgsql + type: postgresql config: { server: "127.0.0.1:5432" database: mqtt @@ -96,7 +96,7 @@ Sample data in the default configuration: INSERT INTO mqtt_authz (ipaddress, username, clientid, action, permission, topic) VALUES ('127.0.0.1', '', '', 'subscribe', 'allow', '$SYS/#'); ``` -#### Pgsql +#### PostgreSQL Create Example Table diff --git a/apps/emqx_authz/etc/emqx_authz.conf b/apps/emqx_authz/etc/emqx_authz.conf index c2856f0b5..19bb2737b 100644 --- a/apps/emqx_authz/etc/emqx_authz.conf +++ b/apps/emqx_authz/etc/emqx_authz.conf @@ -25,7 +25,7 @@ authorization { # query: "select ipaddress, username, clientid, action, permission, topic from mqtt_authz where ipaddr = '%a' or username = '%u' or clientid = '%c'" # }, # { - # type: pgsql + # type: postgresql # server: "127.0.0.1:5432" # database: mqtt # pool_size: 1 diff --git a/apps/emqx_authz/src/emqx_authz.erl b/apps/emqx_authz/src/emqx_authz.erl index 6c7ceb6c2..b4debf3fb 100644 --- a/apps/emqx_authz/src/emqx_authz.erl +++ b/apps/emqx_authz/src/emqx_authz.erl @@ -39,7 +39,7 @@ -export([post_config_update/4, pre_config_update/2]). -define(CONF_KEY_PATH, [authorization, sources]). --define(SOURCE_TYPES, [file, http, mongo, mysql, pgsql, redis]). +-define(SOURCE_TYPES, [file, http, mongo, mysql, postgresql, redis]). -spec(register_metrics() -> ok). register_metrics() -> @@ -309,7 +309,7 @@ init_source(#{enable := true, type := DB, query := SQL } = Source) when DB =:= mysql; - DB =:= pgsql -> + DB =:= postgresql -> Mod = authz_module(DB), case create_resource(Source) of {error, Reason} -> error({load_config_error, Reason}); @@ -407,6 +407,8 @@ create_resource(#{type := DB} = Source) -> authz_module(Type) -> list_to_existing_atom("emqx_authz_" ++ atom_to_list(Type)). +connector_module(postgresql) -> + emqx_connector_pgsql; connector_module(Type) -> list_to_existing_atom("emqx_connector_" ++ atom_to_list(Type)). diff --git a/apps/emqx_authz/src/emqx_authz_api_schema.erl b/apps/emqx_authz/src/emqx_authz_api_schema.erl index 158e26eab..4fd2f8ca1 100644 --- a/apps/emqx_authz/src/emqx_authz_api_schema.erl +++ b/apps/emqx_authz/src/emqx_authz_api_schema.erl @@ -46,7 +46,7 @@ definitions() -> , minirest:ref(<<"mongo_rs">>) , minirest:ref(<<"mongo_sharded">>) , minirest:ref(<<"mysql">>) - , minirest:ref(<<"pgsql">>) + , minirest:ref(<<"postgresql">>) , minirest:ref(<<"redis_single">>) , minirest:ref(<<"redis_sentinel">>) , minirest:ref(<<"redis_cluster">>) @@ -335,8 +335,8 @@ definitions() -> properties => #{ type => #{ type => string, - enum => [<<"pgsql">>], - example => <<"pgsql">> + enum => [<<"postgresql">>], + example => <<"postgresql">> }, enable => #{ type => boolean, @@ -501,7 +501,7 @@ definitions() -> , #{<<"mongo_rs">> => MongoRs} , #{<<"mongo_sharded">> => MongoSharded} , #{<<"mysql">> => Mysql} - , #{<<"pgsql">> => Pgsql} + , #{<<"postgresql">> => Pgsql} , #{<<"redis_single">> => RedisSingle} , #{<<"redis_sentinel">> => RedisSentinel} , #{<<"redis_cluster">> => RedisCluster} diff --git a/apps/emqx_authz/src/emqx_authz_pgsql.erl b/apps/emqx_authz/src/emqx_authz_postgresql.erl similarity index 96% rename from apps/emqx_authz/src/emqx_authz_pgsql.erl rename to apps/emqx_authz/src/emqx_authz_postgresql.erl index 3e1f40fb2..ead2d985d 100644 --- a/apps/emqx_authz/src/emqx_authz_pgsql.erl +++ b/apps/emqx_authz/src/emqx_authz_postgresql.erl @@ -14,7 +14,7 @@ %% limitations under the License. %%-------------------------------------------------------------------- --module(emqx_authz_pgsql). +-module(emqx_authz_postgresql). -include("emqx_authz.hrl"). -include_lib("emqx/include/emqx.hrl"). @@ -32,7 +32,7 @@ -endif. description() -> - "AuthZ with pgsql". + "AuthZ with postgresql". parse_query(undefined) -> undefined; @@ -59,7 +59,7 @@ authorize(Client, PubSub, Topic, {ok, Columns, Rows} -> do_authorize(Client, PubSub, Topic, Columns, Rows); {error, Reason} -> - ?LOG(error, "[AuthZ] Query pgsql error: ~p~n", [Reason]), + ?LOG(error, "[AuthZ] Query postgresql error: ~p~n", [Reason]), nomatch end. diff --git a/apps/emqx_authz/src/emqx_authz_schema.erl b/apps/emqx_authz/src/emqx_authz_schema.erl index e17a55d0a..bda8d8e74 100644 --- a/apps/emqx_authz/src/emqx_authz_schema.erl +++ b/apps/emqx_authz/src/emqx_authz_schema.erl @@ -33,7 +33,7 @@ fields("authorization") -> , hoconsc:ref(?MODULE, mongo_rs) , hoconsc:ref(?MODULE, mongo_sharded) , hoconsc:ref(?MODULE, mysql) - , hoconsc:ref(?MODULE, pgsql) + , hoconsc:ref(?MODULE, postgresql) , hoconsc:ref(?MODULE, redis_single) , hoconsc:ref(?MODULE, redis_sentinel) , hoconsc:ref(?MODULE, redis_cluster) @@ -131,9 +131,12 @@ fields(mongo_sharded) -> fields(mysql) -> connector_fields(mysql) ++ [ {query, query()} ]; -fields(pgsql) -> - connector_fields(pgsql) ++ - [ {query, query()} ]; +fields(postgresql) -> + [ {type, #{type => postgresql}} + , {enable, #{type => boolean(), + default => true}} + , {query, query()} + ] ++ emqx_connector_pgsql:fields(config); fields(redis_single) -> connector_fields(redis, single) ++ [ {cmd, query()} ]; @@ -181,4 +184,4 @@ connector_fields(DB, Fields) -> to_list(A) when is_atom(A) -> atom_to_list(A); to_list(B) when is_binary(B) -> - binary_to_list(B). \ No newline at end of file + binary_to_list(B). diff --git a/apps/emqx_authz/test/emqx_authz_SUITE.erl b/apps/emqx_authz/test/emqx_authz_SUITE.erl index fe1f04bd2..bed20e0e4 100644 --- a/apps/emqx_authz/test/emqx_authz_SUITE.erl +++ b/apps/emqx_authz/test/emqx_authz_SUITE.erl @@ -88,7 +88,7 @@ init_per_testcase(_, Config) -> <<"ssl">> => #{<<"enable">> => false}, <<"query">> => <<"abcb">> }). --define(SOURCE4, #{<<"type">> => <<"pgsql">>, +-define(SOURCE4, #{<<"type">> => <<"postgresql">>, <<"enable">> => true, <<"server">> => <<"127.0.0.1:27017">>, <<"pool_size">> => 1, @@ -130,7 +130,7 @@ t_update_source(_) -> ?assertMatch([ #{type := http, enable := true} , #{type := mongo, enable := true} , #{type := mysql, enable := true} - , #{type := pgsql, enable := true} + , #{type := postgresql, enable := true} , #{type := redis, enable := true} , #{type := file, enable := true} ], emqx:get_config([authorization, sources], [])), @@ -138,14 +138,14 @@ t_update_source(_) -> {ok, _} = emqx_authz:update({replace_once, http}, ?SOURCE1#{<<"enable">> := false}), {ok, _} = emqx_authz:update({replace_once, mongo}, ?SOURCE2#{<<"enable">> := false}), {ok, _} = emqx_authz:update({replace_once, mysql}, ?SOURCE3#{<<"enable">> := false}), - {ok, _} = emqx_authz:update({replace_once, pgsql}, ?SOURCE4#{<<"enable">> := false}), + {ok, _} = emqx_authz:update({replace_once, postgresql}, ?SOURCE4#{<<"enable">> := false}), {ok, _} = emqx_authz:update({replace_once, redis}, ?SOURCE5#{<<"enable">> := false}), {ok, _} = emqx_authz:update({replace_once, file}, ?SOURCE6#{<<"enable">> := false}), ?assertMatch([ #{type := http, enable := false} , #{type := mongo, enable := false} , #{type := mysql, enable := false} - , #{type := pgsql, enable := false} + , #{type := postgresql, enable := false} , #{type := redis, enable := false} , #{type := file, enable := false} ], emqx:get_config([authorization, sources], [])), @@ -157,13 +157,13 @@ t_move_source(_) -> ?assertMatch([ #{type := http} , #{type := mongo} , #{type := mysql} - , #{type := pgsql} + , #{type := postgresql} , #{type := redis} , #{type := file} ], emqx_authz:lookup()), - {ok, _} = emqx_authz:move(pgsql, <<"top">>), - ?assertMatch([ #{type := pgsql} + {ok, _} = emqx_authz:move(postgresql, <<"top">>), + ?assertMatch([ #{type := postgresql} , #{type := http} , #{type := mongo} , #{type := mysql} @@ -172,7 +172,7 @@ t_move_source(_) -> ], emqx_authz:lookup()), {ok, _} = emqx_authz:move(http, <<"bottom">>), - ?assertMatch([ #{type := pgsql} + ?assertMatch([ #{type := postgresql} , #{type := mongo} , #{type := mysql} , #{type := redis} @@ -180,9 +180,9 @@ t_move_source(_) -> , #{type := http} ], emqx_authz:lookup()), - {ok, _} = emqx_authz:move(mysql, #{<<"before">> => pgsql}), + {ok, _} = emqx_authz:move(mysql, #{<<"before">> => postgresql}), ?assertMatch([ #{type := mysql} - , #{type := pgsql} + , #{type := postgresql} , #{type := mongo} , #{type := redis} , #{type := file} @@ -191,7 +191,7 @@ t_move_source(_) -> {ok, _} = emqx_authz:move(mongo, #{<<"after">> => http}), ?assertMatch([ #{type := mysql} - , #{type := pgsql} + , #{type := postgresql} , #{type := redis} , #{type := file} , #{type := http} diff --git a/apps/emqx_authz/test/emqx_authz_api_sources_SUITE.erl b/apps/emqx_authz/test/emqx_authz_api_sources_SUITE.erl index 3862e06d0..678c6e894 100644 --- a/apps/emqx_authz/test/emqx_authz_api_sources_SUITE.erl +++ b/apps/emqx_authz/test/emqx_authz_api_sources_SUITE.erl @@ -67,7 +67,7 @@ <<"ssl">> => #{<<"enable">> => false}, <<"query">> => <<"abcb">> }). --define(SOURCE4, #{<<"type">> => <<"pgsql">>, +-define(SOURCE4, #{<<"type">> => <<"postgresql">>, <<"enable">> => true, <<"server">> => <<"127.0.0.1:5432">>, <<"pool_size">> => 1, @@ -183,7 +183,7 @@ t_api(_) -> ?assertMatch([ #{<<"type">> := <<"http">>} , #{<<"type">> := <<"mongo">>} , #{<<"type">> := <<"mysql">>} - , #{<<"type">> := <<"pgsql">>} + , #{<<"type">> := <<"postgresql">>} , #{<<"type">> := <<"redis">>} , #{<<"type">> := <<"file">>} ], Sources), @@ -227,13 +227,13 @@ t_move_source(_) -> ?assertMatch([ #{type := http} , #{type := mongo} , #{type := mysql} - , #{type := pgsql} + , #{type := postgresql} , #{type := redis} ], emqx_authz:lookup()), - {ok, 204, _} = request(post, uri(["authorization", "sources", "pgsql", "move"]), + {ok, 204, _} = request(post, uri(["authorization", "sources", "postgresql", "move"]), #{<<"position">> => <<"top">>}), - ?assertMatch([ #{type := pgsql} + ?assertMatch([ #{type := postgresql} , #{type := http} , #{type := mongo} , #{type := mysql} @@ -242,7 +242,7 @@ t_move_source(_) -> {ok, 204, _} = request(post, uri(["authorization", "sources", "http", "move"]), #{<<"position">> => <<"bottom">>}), - ?assertMatch([ #{type := pgsql} + ?assertMatch([ #{type := postgresql} , #{type := mongo} , #{type := mysql} , #{type := redis} @@ -250,9 +250,9 @@ t_move_source(_) -> ], emqx_authz:lookup()), {ok, 204, _} = request(post, uri(["authorization", "sources", "mysql", "move"]), - #{<<"position">> => #{<<"before">> => <<"pgsql">>}}), + #{<<"position">> => #{<<"before">> => <<"postgresql">>}}), ?assertMatch([ #{type := mysql} - , #{type := pgsql} + , #{type := postgresql} , #{type := mongo} , #{type := redis} , #{type := http} @@ -261,7 +261,7 @@ t_move_source(_) -> {ok, 204, _} = request(post, uri(["authorization", "sources", "mongo", "move"]), #{<<"position">> => #{<<"after">> => <<"http">>}}), ?assertMatch([ #{type := mysql} - , #{type := pgsql} + , #{type := postgresql} , #{type := redis} , #{type := http} , #{type := mongo} diff --git a/apps/emqx_authz/test/emqx_authz_pgsql_SUITE.erl b/apps/emqx_authz/test/emqx_authz_postgresql_SUITE.erl similarity index 98% rename from apps/emqx_authz/test/emqx_authz_pgsql_SUITE.erl rename to apps/emqx_authz/test/emqx_authz_postgresql_SUITE.erl index 570ea0e77..9526adcda 100644 --- a/apps/emqx_authz/test/emqx_authz_pgsql_SUITE.erl +++ b/apps/emqx_authz/test/emqx_authz_postgresql_SUITE.erl @@ -13,7 +13,7 @@ %% limitations under the License. %%-------------------------------------------------------------------- --module(emqx_authz_pgsql_SUITE). +-module(emqx_authz_postgresql_SUITE). -compile(nowarn_export_all). -compile(export_all). @@ -47,7 +47,7 @@ init_per_suite(Config) -> {ok, _} = emqx:update_config([authorization, cache, enable], false), {ok, _} = emqx:update_config([authorization, no_match], deny), - Rules = [#{<<"type">> => <<"pgsql">>, + Rules = [#{<<"type">> => <<"postgresql">>, <<"server">> => <<"127.0.0.1:27017">>, <<"pool_size">> => 1, <<"database">> => <<"mqtt">>,