From 08596f886abc153e8783ddcd86e85a16d7b52cae Mon Sep 17 00:00:00 2001 From: zhongwencool Date: Tue, 2 Jul 2024 21:17:05 +0800 Subject: [PATCH] feat: add default csv file in authn-built-in-db --- apps/emqx_auth/etc/auth-built-in-db-bootstrap.csv | 1 + apps/emqx_auth_mnesia/src/emqx_authn_mnesia.erl | 10 +++++++--- apps/emqx_auth_mnesia/src/emqx_authn_mnesia_schema.erl | 4 ++-- apps/emqx_auth_mnesia/test/emqx_authn_mnesia_SUITE.erl | 4 ++-- rebar.config.erl | 2 ++ 5 files changed, 14 insertions(+), 7 deletions(-) create mode 100644 apps/emqx_auth/etc/auth-built-in-db-bootstrap.csv diff --git a/apps/emqx_auth/etc/auth-built-in-db-bootstrap.csv b/apps/emqx_auth/etc/auth-built-in-db-bootstrap.csv new file mode 100644 index 000000000..6e784190e --- /dev/null +++ b/apps/emqx_auth/etc/auth-built-in-db-bootstrap.csv @@ -0,0 +1 @@ +user_id,password,is_superuser diff --git a/apps/emqx_auth_mnesia/src/emqx_authn_mnesia.erl b/apps/emqx_auth_mnesia/src/emqx_authn_mnesia.erl index 1066db52b..3884b64ad 100644 --- a/apps/emqx_auth_mnesia/src/emqx_authn_mnesia.erl +++ b/apps/emqx_auth_mnesia/src/emqx_authn_mnesia.erl @@ -181,6 +181,10 @@ import_users({PasswordType, Filename, FileData}, State, Opts) -> case do_import_users(Users, Opts#{filename => Filename}) of ok -> ok; + %% Do not log empty user entries. + %% The default etc/auth-built-in-db.csv file contains an empty user entry. + {error, empty_users} -> + {error, empty_users}; {error, Reason} -> ?SLOG( warning, @@ -496,7 +500,7 @@ reader_fn(Filename0, Data) -> error(Reason) end; <<".csv">> -> - %% Example: data/user-credentials.csv + %% Example: etc/auth-built-in-db-bootstrap.csv emqx_utils_stream:csv(Data); <<>> -> error(unknown_file_format); @@ -541,11 +545,11 @@ is_superuser(#{<<"is_superuser">> := true}) -> true; is_superuser(_) -> false. boostrap_user_from_file(Config, State) -> - case maps:get(boostrap_file, Config, <<>>) of + case maps:get(bootstrap_file, Config, <<>>) of <<>> -> ok; FileName0 -> - #{boostrap_type := Type} = Config, + #{bootstrap_type := Type} = Config, FileName = emqx_schema:naive_env_interpolation(FileName0), case file:read_file(FileName) of {ok, FileData} -> diff --git a/apps/emqx_auth_mnesia/src/emqx_authn_mnesia_schema.erl b/apps/emqx_auth_mnesia/src/emqx_authn_mnesia_schema.erl index 85268a5f2..b298c3679 100644 --- a/apps/emqx_auth_mnesia/src/emqx_authn_mnesia_schema.erl +++ b/apps/emqx_auth_mnesia/src/emqx_authn_mnesia_schema.erl @@ -46,7 +46,7 @@ select_union_member(_Kind, _Value) -> fields(builtin_db) -> [ {password_hash_algorithm, fun emqx_authn_password_hashing:type_rw/1} - ] ++ common_fields(). + ] ++ common_fields(); fields(builtin_db_api) -> [ {password_hash_algorithm, fun emqx_authn_password_hashing:type_rw_api/1} @@ -79,7 +79,7 @@ bootstrap_fields() -> #{ desc => ?DESC(bootstrap_file), required => false, - default => <<>> + default => <<"${EMQX_ETC_DIR}/auth-built-in-db-bootstrap.csv">> } )}, {bootstrap_type, diff --git a/apps/emqx_auth_mnesia/test/emqx_authn_mnesia_SUITE.erl b/apps/emqx_auth_mnesia/test/emqx_authn_mnesia_SUITE.erl index e3d1858f3..ca751492c 100644 --- a/apps/emqx_auth_mnesia/test/emqx_authn_mnesia_SUITE.erl +++ b/apps/emqx_auth_mnesia/test/emqx_authn_mnesia_SUITE.erl @@ -124,8 +124,8 @@ test_bootstrap_file(Config0, Type, File) -> test_bootstrap_file(Config0, Type, File, Opts) -> {Type, Filename, _FileData} = sample_filename_and_data(Type, File), Config2 = Config0#{ - boostrap_file => Filename, - boostrap_type => Type + bootstrap_file => Filename, + bootstrap_type => Type }, {ok, State0} = emqx_authn_mnesia:create(?AUTHN_ID, Config2), Result = ets:tab2list(emqx_authn_mnesia), diff --git a/rebar.config.erl b/rebar.config.erl index 760da9fad..741f4ab99 100644 --- a/rebar.config.erl +++ b/rebar.config.erl @@ -469,6 +469,8 @@ relx_overlay(ReleaseType, Edition) -> {copy, "bin/install_upgrade.escript", "bin/install_upgrade.escript-{{release_version}}"}, {copy, "apps/emqx_gateway_lwm2m/lwm2m_xml", "etc/lwm2m_xml"}, {copy, "apps/emqx_auth/etc/acl.conf", "etc/acl.conf"}, + {copy, "apps/emqx_auth/etc/auth-built-in-db-bootstrap.csv", + "etc/auth-built-in-db-bootstrap.csv"}, {template, "bin/emqx.cmd", "bin/emqx.cmd"}, {template, "bin/emqx_ctl.cmd", "bin/emqx_ctl.cmd"}, {copy, "bin/nodetool", "bin/nodetool"},