refactor(gw): deps on emqx_dasboard_swagger
This commit is contained in:
parent
90a65b8d04
commit
f033fad7b3
|
@ -65,6 +65,15 @@
|
||||||
, response_users_example/0
|
, response_users_example/0
|
||||||
]).
|
]).
|
||||||
|
|
||||||
|
%% export these funcs for gateway
|
||||||
|
-export([ list_users/3
|
||||||
|
, add_user/3
|
||||||
|
, delete_user/3
|
||||||
|
, find_user/3
|
||||||
|
, update_user/4
|
||||||
|
, serialize_error/1
|
||||||
|
]).
|
||||||
|
|
||||||
api_spec() ->
|
api_spec() ->
|
||||||
emqx_dashboard_swagger:spec(?MODULE, #{check_schema => true}).
|
emqx_dashboard_swagger:spec(?MODULE, #{check_schema => true}).
|
||||||
|
|
||||||
|
|
|
@ -18,21 +18,34 @@
|
||||||
|
|
||||||
-behaviour(minirest_api).
|
-behaviour(minirest_api).
|
||||||
|
|
||||||
|
-include_lib("typerefl/include/types.hrl").
|
||||||
|
|
||||||
|
-define(BAD_REQUEST, 'BAD_REQUEST').
|
||||||
|
-define(NOT_FOUND, 'NOT_FOUND').
|
||||||
|
-define(INTERNAL_ERROR, 'INTERNAL_SERVER_ERROR').
|
||||||
|
|
||||||
|
-import(hoconsc, [mk/2, ref/2]).
|
||||||
|
-import(emqx_dashboard_swagger, [error_codes/2]).
|
||||||
|
|
||||||
-import(emqx_gateway_http,
|
-import(emqx_gateway_http,
|
||||||
[ return_http_error/2
|
[ return_http_error/2
|
||||||
, schema_bad_request/0
|
|
||||||
, schema_not_found/0
|
|
||||||
, schema_internal_error/0
|
|
||||||
, schema_no_content/0
|
|
||||||
, with_gateway/2
|
, with_gateway/2
|
||||||
|
, with_authn/2
|
||||||
, checks/2
|
, checks/2
|
||||||
]).
|
]).
|
||||||
|
|
||||||
%% minirest behaviour callbacks
|
%% minirest/dashbaord_swagger behaviour callbacks
|
||||||
-export([api_spec/0]).
|
-export([ api_spec/0
|
||||||
|
, paths/0
|
||||||
|
, schema/1
|
||||||
|
]).
|
||||||
|
|
||||||
%% http handlers
|
%% http handlers
|
||||||
-export([authn/2]).
|
-export([ authn/2
|
||||||
|
, users/2
|
||||||
|
, users_insta/2
|
||||||
|
, import_users/2
|
||||||
|
]).
|
||||||
|
|
||||||
%% internal export for emqx_gateway_api_listeners module
|
%% internal export for emqx_gateway_api_listeners module
|
||||||
-export([schema_authn/0]).
|
-export([schema_authn/0]).
|
||||||
|
@ -42,10 +55,13 @@
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
api_spec() ->
|
api_spec() ->
|
||||||
{metadata(apis()), []}.
|
emqx_dashboard_swagger:spec(?MODULE, #{check_schema => true}).
|
||||||
|
|
||||||
apis() ->
|
paths() ->
|
||||||
[ {"/gateway/:name/authentication", authn}
|
[ "/gateway/:name/authentication"
|
||||||
|
, "/gateway/:name/authentication/users"
|
||||||
|
, "/gateway/:name/authentication/users/:uid"
|
||||||
|
, "/gateway/:name/authentication/import_users"
|
||||||
].
|
].
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
@ -83,87 +99,245 @@ authn(delete, #{bindings := #{name := Name0}}) ->
|
||||||
{204}
|
{204}
|
||||||
end).
|
end).
|
||||||
|
|
||||||
|
users(get, #{bindings := #{name := Name0}, query_string := Qs}) ->
|
||||||
|
with_authn(Name0, fun(_GwName, #{id := AuthId,
|
||||||
|
chain_name := ChainName}) ->
|
||||||
|
emqx_authn_api:list_users(ChainName, AuthId, page_pramas(Qs))
|
||||||
|
end);
|
||||||
|
users(post, #{bindings := #{name := Name0},
|
||||||
|
body := Body}) ->
|
||||||
|
with_authn(Name0, fun(_GwName, #{id := AuthId,
|
||||||
|
chain_name := ChainName}) ->
|
||||||
|
emqx_authn_api:add_user(ChainName, AuthId, Body)
|
||||||
|
end).
|
||||||
|
|
||||||
|
users_insta(get, #{bindings := #{name := Name0, uid := UserId}}) ->
|
||||||
|
with_authn(Name0, fun(_GwName, #{id := AuthId,
|
||||||
|
chain_name := ChainName}) ->
|
||||||
|
emqx_authn_api:find_user(ChainName, AuthId, UserId)
|
||||||
|
end);
|
||||||
|
users_insta(put, #{bindings := #{name := Name0, uid := UserId},
|
||||||
|
body := Body}) ->
|
||||||
|
with_authn(Name0, fun(_GwName, #{id := AuthId,
|
||||||
|
chain_name := ChainName}) ->
|
||||||
|
emqx_authn_api:update_user(ChainName, AuthId, UserId, Body)
|
||||||
|
end);
|
||||||
|
users_insta(delete, #{bindings := #{name := Name0, uid := UserId}}) ->
|
||||||
|
with_authn(Name0, fun(_GwName, #{id := AuthId,
|
||||||
|
chain_name := ChainName}) ->
|
||||||
|
emqx_authn_api:delete_user(ChainName, AuthId, UserId)
|
||||||
|
end).
|
||||||
|
|
||||||
|
import_users(post, #{bindings := #{name := Name0},
|
||||||
|
body := Body}) ->
|
||||||
|
with_authn(Name0, fun(_GwName, #{id := AuthId,
|
||||||
|
chain_name := ChainName}) ->
|
||||||
|
case maps:get(<<"filename">>, Body, undefined) of
|
||||||
|
undefined ->
|
||||||
|
emqx_authn_api:serialize_error({missing_parameter, filename});
|
||||||
|
Filename ->
|
||||||
|
case emqx_authentication:import_users(
|
||||||
|
ChainName, AuthId, Filename) of
|
||||||
|
ok -> {204};
|
||||||
|
{error, Reason} ->
|
||||||
|
emqx_authn_api:serialize_error(Reason)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end).
|
||||||
|
|
||||||
|
%%--------------------------------------------------------------------
|
||||||
|
%% Utils
|
||||||
|
|
||||||
|
page_pramas(Qs) ->
|
||||||
|
maps:with([<<"page">>, <<"limit">>], Qs).
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Swagger defines
|
%% Swagger defines
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
metadata(APIs) ->
|
|
||||||
metadata(APIs, []).
|
|
||||||
metadata([], APIAcc) ->
|
|
||||||
lists:reverse(APIAcc);
|
|
||||||
metadata([{Path, Fun}|More], APIAcc) ->
|
|
||||||
Methods = [get, post, put, delete, patch],
|
|
||||||
Mds = lists:foldl(fun(M, Acc) ->
|
|
||||||
try
|
|
||||||
Acc#{M => swagger(Path, M)}
|
|
||||||
catch
|
|
||||||
error : function_clause ->
|
|
||||||
Acc
|
|
||||||
end
|
|
||||||
end, #{}, Methods),
|
|
||||||
metadata(More, [{Path, Mds, Fun} | APIAcc]).
|
|
||||||
|
|
||||||
swagger("/gateway/:name/authentication", get) ->
|
schema("/gateway/:name/authentication") ->
|
||||||
#{ description => <<"Get the gateway authentication">>
|
#{ 'operationId' => authn,
|
||||||
, parameters => params_gateway_name_in_path()
|
get =>
|
||||||
, responses =>
|
#{ description => <<"Get the gateway authentication">>
|
||||||
#{ <<"400">> => schema_bad_request()
|
, parameters => params_gateway_name_in_path()
|
||||||
, <<"404">> => schema_not_found()
|
, responses =>
|
||||||
, <<"500">> => schema_internal_error()
|
#{ 400 => error_codes([?BAD_REQUEST], <<"Bad Request">>)
|
||||||
, <<"200">> => schema_authn()
|
, 404 => error_codes([?NOT_FOUND], <<"Not Found">>)
|
||||||
, <<"204">> => schema_no_content()
|
, 500 => error_codes([?INTERNAL_ERROR],
|
||||||
}
|
<<"Ineternal Server Error">>)
|
||||||
|
, 200 => schema_authn()
|
||||||
|
, 204 => <<"Authentication does not initiated">>
|
||||||
|
}
|
||||||
|
},
|
||||||
|
put =>
|
||||||
|
#{ description => <<"Update authentication for the gateway">>
|
||||||
|
, parameters => params_gateway_name_in_path()
|
||||||
|
, requestBody => schema_authn()
|
||||||
|
, responses =>
|
||||||
|
#{ 400 => error_codes([?BAD_REQUEST], <<"Bad Request">>)
|
||||||
|
, 404 => error_codes([?NOT_FOUND], <<"Not Found">>)
|
||||||
|
, 500 => error_codes([?INTERNAL_ERROR],
|
||||||
|
<<"Ineternal Server Error">>)
|
||||||
|
, 204 => <<"Updated">> %% XXX: ??? return the updated object
|
||||||
|
}
|
||||||
|
},
|
||||||
|
post =>
|
||||||
|
#{ description => <<"Add authentication for the gateway">>
|
||||||
|
, parameters => params_gateway_name_in_path()
|
||||||
|
, requestBody => schema_authn()
|
||||||
|
, responses =>
|
||||||
|
#{ 400 => error_codes([?BAD_REQUEST], <<"Bad Request">>)
|
||||||
|
, 404 => error_codes([?NOT_FOUND], <<"Not Found">>)
|
||||||
|
, 500 => error_codes([?INTERNAL_ERROR],
|
||||||
|
<<"Ineternal Server Error">>)
|
||||||
|
, 204 => <<"Added">>
|
||||||
|
}
|
||||||
|
},
|
||||||
|
delete =>
|
||||||
|
#{ description => <<"Remove the gateway authentication">>
|
||||||
|
, parameters => params_gateway_name_in_path()
|
||||||
|
, responses =>
|
||||||
|
#{ 400 => error_codes([?BAD_REQUEST], <<"Bad Request">>)
|
||||||
|
, 404 => error_codes([?NOT_FOUND], <<"Not Found">>)
|
||||||
|
, 500 => error_codes([?INTERNAL_ERROR],
|
||||||
|
<<"Ineternal Server Error">>)
|
||||||
|
, 204 => <<"Deleted">>
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
swagger("/gateway/:name/authentication", put) ->
|
schema("/gateway/:name/authentication/users") ->
|
||||||
#{ description => <<"Update authentication for the gateway">>
|
#{ 'operationId' => users
|
||||||
, parameters => params_gateway_name_in_path()
|
, get =>
|
||||||
, requestBody => schema_authn()
|
#{ description => <<"Get the users for the authentication">>
|
||||||
, responses =>
|
, parameters => params_gateway_name_in_path()
|
||||||
#{ <<"400">> => schema_bad_request()
|
, responses =>
|
||||||
, <<"404">> => schema_not_found()
|
#{ 400 => error_codes([?BAD_REQUEST], <<"Bad Request">>)
|
||||||
, <<"500">> => schema_internal_error()
|
, 404 => error_codes([?NOT_FOUND], <<"Not Found">>)
|
||||||
, <<"204">> => schema_no_content()
|
, 500 => error_codes([?INTERNAL_ERROR],
|
||||||
}
|
<<"Ineternal Server Error">>)
|
||||||
|
, 200 => emqx_dashboard_swagger:schema_with_example(
|
||||||
|
ref(emqx_authn_api, response_user),
|
||||||
|
emqx_authn_api:response_user_examples())
|
||||||
|
}
|
||||||
|
},
|
||||||
|
post =>
|
||||||
|
#{ description => <<"Add user for the authentication">>
|
||||||
|
, parameters => params_gateway_name_in_path()
|
||||||
|
, responses =>
|
||||||
|
#{ 400 => error_codes([?BAD_REQUEST], <<"Bad Request">>)
|
||||||
|
, 404 => error_codes([?NOT_FOUND], <<"Not Found">>)
|
||||||
|
, 500 => error_codes([?INTERNAL_ERROR],
|
||||||
|
<<"Ineternal Server Error">>)
|
||||||
|
, 201 => emqx_dashboard_swagger:schema_with_example(
|
||||||
|
ref(emqx_authn_api, response_user),
|
||||||
|
emqx_authn_api:response_user_examples())
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
swagger("/gateway/:name/authentication", post) ->
|
schema("/gateway/:name/authentication/users/:uid") ->
|
||||||
#{ description => <<"Add authentication for the gateway">>
|
#{ 'operationId' => users_insta
|
||||||
, parameters => params_gateway_name_in_path()
|
, get =>
|
||||||
, requestBody => schema_authn()
|
#{ description => <<"Get user info from the gateway "
|
||||||
, responses =>
|
"authentication">>
|
||||||
#{ <<"400">> => schema_bad_request()
|
, parameters => params_gateway_name_in_path() ++
|
||||||
, <<"404">> => schema_not_found()
|
params_userid_in_path()
|
||||||
, <<"500">> => schema_internal_error()
|
, responses =>
|
||||||
, <<"204">> => schema_no_content()
|
#{ 400 => error_codes([?BAD_REQUEST], <<"Bad Request">>)
|
||||||
}
|
, 404 => error_codes([?NOT_FOUND], <<"Not Found">>)
|
||||||
|
, 500 => error_codes([?INTERNAL_ERROR],
|
||||||
|
<<"Ineternal Server Error">>)
|
||||||
|
, 200 => emqx_dashboard_swagger:schema_with_example(
|
||||||
|
ref(emqx_authn_api, response_user),
|
||||||
|
emqx_authn_api:response_user_examples())
|
||||||
|
}
|
||||||
|
},
|
||||||
|
put =>
|
||||||
|
#{ description => <<"Update the user info for the gateway "
|
||||||
|
"authentication">>
|
||||||
|
, parameters => params_gateway_name_in_path() ++
|
||||||
|
params_userid_in_path()
|
||||||
|
, responses =>
|
||||||
|
#{ 400 => error_codes([?BAD_REQUEST], <<"Bad Request">>)
|
||||||
|
, 404 => error_codes([?NOT_FOUND], <<"Not Found">>)
|
||||||
|
, 500 => error_codes([?INTERNAL_ERROR],
|
||||||
|
<<"Ineternal Server Error">>)
|
||||||
|
, 200 => emqx_dashboard_swagger:schema_with_example(
|
||||||
|
ref(emqx_authn_api, response_user),
|
||||||
|
emqx_authn_api:response_user_examples())
|
||||||
|
}
|
||||||
|
},
|
||||||
|
delete =>
|
||||||
|
#{ description => <<"Delete the user for the gateway "
|
||||||
|
"authentication">>
|
||||||
|
, parameters => params_gateway_name_in_path() ++
|
||||||
|
params_userid_in_path() ++
|
||||||
|
params_paging_in_qs()
|
||||||
|
, responses =>
|
||||||
|
#{ 400 => error_codes([?BAD_REQUEST], <<"Bad Request">>)
|
||||||
|
, 404 => error_codes([?NOT_FOUND], <<"Not Found">>)
|
||||||
|
, 500 => error_codes([?INTERNAL_ERROR],
|
||||||
|
<<"Ineternal Server Error">>)
|
||||||
|
, 200 => emqx_dashboard_swagger:schema_with_example(
|
||||||
|
ref(emqx_authn_api, response_user),
|
||||||
|
emqx_authn_api:response_user_examples())
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
swagger("/gateway/:name/authentication", delete) ->
|
schema("/gateway/:name/authentication/import_users") ->
|
||||||
#{ description => <<"Remove the gateway authentication">>
|
#{ 'operationId' => import_users
|
||||||
, parameters => params_gateway_name_in_path()
|
, post =>
|
||||||
, responses =>
|
#{ description => <<"Import users into the gateway authentication">>
|
||||||
#{ <<"400">> => schema_bad_request()
|
, parameters => params_gateway_name_in_path()
|
||||||
, <<"404">> => schema_not_found()
|
, requestBody => emqx_dashboard_swagger:schema_with_examples(
|
||||||
, <<"500">> => schema_internal_error()
|
ref(emqx_authn_api, request_import_users),
|
||||||
, <<"204">> => schema_no_content()
|
emqx_authn_api:request_import_users_examples()
|
||||||
}
|
)
|
||||||
|
, responses =>
|
||||||
|
#{ 400 => error_codes([?BAD_REQUEST], <<"Bad Request">>)
|
||||||
|
, 404 => error_codes([?NOT_FOUND], <<"Not Found">>)
|
||||||
|
, 500 => error_codes([?INTERNAL_ERROR],
|
||||||
|
<<"Ineternal Server Error">>)
|
||||||
|
%% XXX: Put a hint message into 204 return ?
|
||||||
|
, 204 => <<"Imported">>
|
||||||
|
}
|
||||||
|
}
|
||||||
}.
|
}.
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% params defines
|
%% params defines
|
||||||
|
|
||||||
params_gateway_name_in_path() ->
|
params_gateway_name_in_path() ->
|
||||||
[#{ name => name
|
[{name,
|
||||||
, in => path
|
mk(binary(),
|
||||||
, schema => #{type => string}
|
#{ in => path
|
||||||
, required => true
|
, desc => <<"Gateway Name">>
|
||||||
}].
|
})}
|
||||||
|
].
|
||||||
|
|
||||||
|
params_userid_in_path() ->
|
||||||
|
[{uid, mk(binary(),
|
||||||
|
#{ in => path
|
||||||
|
, desc => <<"User ID">>
|
||||||
|
})}
|
||||||
|
].
|
||||||
|
|
||||||
|
params_paging_in_qs() ->
|
||||||
|
[{page, mk(integer(),
|
||||||
|
#{ in => query
|
||||||
|
, desc => <<"Page Number">>
|
||||||
|
})},
|
||||||
|
{limit, mk(integer(),
|
||||||
|
#{ in => query
|
||||||
|
, desc => <<"Page Limit">>
|
||||||
|
})}
|
||||||
|
].
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% schemas
|
%% schemas
|
||||||
|
|
||||||
schema_authn() ->
|
schema_authn() ->
|
||||||
#{ description => <<"OK">>
|
emqx_dashboard_swagger:schema_with_examples(
|
||||||
, content => #{
|
emqx_authn_schema:authenticator_type(),
|
||||||
'application/json' => #{
|
emqx_authn_api:authenticator_examples()
|
||||||
schema => minirest:ref(<<"AuthenticatorInstance">>)
|
).
|
||||||
}}
|
|
||||||
}.
|
|
||||||
|
|
|
@ -18,20 +18,32 @@
|
||||||
|
|
||||||
-behaviour(minirest_api).
|
-behaviour(minirest_api).
|
||||||
|
|
||||||
|
-include_lib("typerefl/include/types.hrl").
|
||||||
|
|
||||||
|
-define(BAD_REQUEST, 'BAD_REQUEST').
|
||||||
|
-define(NOT_FOUND, 'NOT_FOUND').
|
||||||
|
-define(INTERNAL_ERROR, 'INTERNAL_SERVER_ERROR').
|
||||||
|
|
||||||
|
-import(hoconsc, [mk/2, ref/1, ref/2]).
|
||||||
|
-import(emqx_dashboard_swagger, [error_codes/2]).
|
||||||
|
|
||||||
-import(emqx_gateway_http,
|
-import(emqx_gateway_http,
|
||||||
[ return_http_error/2
|
[ return_http_error/2
|
||||||
, schema_bad_request/0
|
|
||||||
, schema_not_found/0
|
|
||||||
, schema_internal_error/0
|
|
||||||
, schema_no_content/0
|
|
||||||
, with_gateway/2
|
, with_gateway/2
|
||||||
, checks/2
|
, checks/2
|
||||||
]).
|
]).
|
||||||
|
|
||||||
-import(emqx_gateway_api_authn, [schema_authn/0]).
|
-import(emqx_gateway_api_authn, [schema_authn/0]).
|
||||||
|
|
||||||
%% minirest behaviour callbacks
|
%% minirest/dashbaord_swagger behaviour callbacks
|
||||||
-export([api_spec/0]).
|
-export([ api_spec/0
|
||||||
|
, paths/0
|
||||||
|
, schema/1
|
||||||
|
]).
|
||||||
|
|
||||||
|
-export([ roots/0
|
||||||
|
, fields/1
|
||||||
|
]).
|
||||||
|
|
||||||
%% http handlers
|
%% http handlers
|
||||||
-export([ listeners/2
|
-export([ listeners/2
|
||||||
|
@ -44,12 +56,12 @@
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
api_spec() ->
|
api_spec() ->
|
||||||
{metadata(apis()), []}.
|
emqx_dashboard_swagger:spec(?MODULE, #{check_schema => true}).
|
||||||
|
|
||||||
apis() ->
|
paths() ->
|
||||||
[ {"/gateway/:name/listeners", listeners}
|
[ "/gateway/:name/listeners"
|
||||||
, {"/gateway/:name/listeners/:id", listeners_insta}
|
, "/gateway/:name/listeners/:id"
|
||||||
, {"/gateway/:name/listeners/:id/authentication", listeners_insta_authn}
|
, "/gateway/:name/listeners/:id/authentication"
|
||||||
].
|
].
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
@ -149,219 +161,228 @@ listeners_insta_authn(delete, #{bindings := #{name := Name0,
|
||||||
%% Swagger defines
|
%% Swagger defines
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
metadata(APIs) ->
|
schema("/gateway/:name/listeners") ->
|
||||||
metadata(APIs, []).
|
#{ 'operationId' => listeners,
|
||||||
metadata([], APIAcc) ->
|
get =>
|
||||||
lists:reverse(APIAcc);
|
#{ description => <<"Get the gateway listeners">>
|
||||||
metadata([{Path, Fun}|More], APIAcc) ->
|
, parameters => params_gateway_name_in_path()
|
||||||
Methods = [get, post, put, delete, patch],
|
, responses =>
|
||||||
Mds = lists:foldl(fun(M, Acc) ->
|
#{ 400 => error_codes([?BAD_REQUEST], <<"Bad Request">>)
|
||||||
try
|
, 404 => error_codes([?NOT_FOUND], <<"Not Found">>)
|
||||||
Acc#{M => swagger(Path, M)}
|
, 500 => error_codes([?INTERNAL_ERROR],
|
||||||
catch
|
<<"Ineternal Server Error">>)
|
||||||
error : function_clause ->
|
, 200 => emqx_dashboard_swagger:schema_with_examples(
|
||||||
Acc
|
hoconsc:array(ref(listener)),
|
||||||
end
|
examples_listener_list())
|
||||||
end, #{}, Methods),
|
}
|
||||||
metadata(More, [{Path, Mds, Fun} | APIAcc]).
|
},
|
||||||
|
post =>
|
||||||
swagger("/gateway/:name/listeners", get) ->
|
#{ description => <<"Create the gateway listener">>
|
||||||
#{ description => <<"Get the gateway listeners">>
|
, parameters => params_gateway_name_in_path()
|
||||||
, parameters => params_gateway_name_in_path()
|
, requestBody => emqx_dashboard_swagger:schema_with_examples(
|
||||||
, responses =>
|
ref(listener),
|
||||||
#{ <<"400">> => schema_bad_request()
|
examples_listener())
|
||||||
, <<"404">> => schema_not_found()
|
, responses =>
|
||||||
, <<"500">> => schema_internal_error()
|
#{ 400 => error_codes([?BAD_REQUEST], <<"Bad Request">>)
|
||||||
, <<"200">> => schema_listener_list()
|
, 404 => error_codes([?NOT_FOUND], <<"Not Found">>)
|
||||||
}
|
, 500 => error_codes([?INTERNAL_ERROR],
|
||||||
|
<<"Ineternal Server Error">>)
|
||||||
|
, 204 => <<"Created">>
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
swagger("/gateway/:name/listeners", post) ->
|
schema("/gateway/:name/listeners/:id") ->
|
||||||
#{ description => <<"Create the gateway listener">>
|
#{ 'operationId' => listeners_insta,
|
||||||
, parameters => params_gateway_name_in_path()
|
get =>
|
||||||
, requestBody => schema_listener()
|
#{ description => <<"Get the gateway listener configurations">>
|
||||||
, responses =>
|
, parameters => params_gateway_name_in_path()
|
||||||
#{ <<"400">> => schema_bad_request()
|
++ params_listener_id_in_path()
|
||||||
, <<"404">> => schema_not_found()
|
, responses =>
|
||||||
, <<"500">> => schema_internal_error()
|
#{ 400 => error_codes([?BAD_REQUEST], <<"Bad Request">>)
|
||||||
, <<"200">> => schema_listener_list()
|
, 404 => error_codes([?NOT_FOUND], <<"Not Found">>)
|
||||||
}
|
, 500 => error_codes([?INTERNAL_ERROR],
|
||||||
|
<<"Ineternal Server Error">>)
|
||||||
|
, 200 => emqx_dashboard_swagger:schema_with_examples(
|
||||||
|
ref(listener),
|
||||||
|
examples_listener())
|
||||||
|
}
|
||||||
|
},
|
||||||
|
delete =>
|
||||||
|
#{ description => <<"Delete the gateway listener">>
|
||||||
|
, parameters => params_gateway_name_in_path()
|
||||||
|
++ params_listener_id_in_path()
|
||||||
|
, responses =>
|
||||||
|
#{ 400 => error_codes([?BAD_REQUEST], <<"Bad Request">>)
|
||||||
|
, 404 => error_codes([?NOT_FOUND], <<"Not Found">>)
|
||||||
|
, 500 => error_codes([?INTERNAL_ERROR],
|
||||||
|
<<"Ineternal Server Error">>)
|
||||||
|
, 204 => <<"Deleted">>
|
||||||
|
}
|
||||||
|
},
|
||||||
|
put =>
|
||||||
|
#{ description => <<"Update the gateway listener">>
|
||||||
|
, parameters => params_gateway_name_in_path()
|
||||||
|
++ params_listener_id_in_path()
|
||||||
|
, requestBody => emqx_dashboard_swagger:schema_with_examples(
|
||||||
|
ref(listener),
|
||||||
|
examples_listener())
|
||||||
|
, responses =>
|
||||||
|
#{ 400 => error_codes([?BAD_REQUEST], <<"Bad Request">>)
|
||||||
|
, 404 => error_codes([?NOT_FOUND], <<"Not Found">>)
|
||||||
|
, 500 => error_codes([?INTERNAL_ERROR],
|
||||||
|
<<"Ineternal Server Error">>)
|
||||||
|
, 200 => <<"Updated">>
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
swagger("/gateway/:name/listeners/:id", get) ->
|
schema("/gateway/:name/listeners/:id/authentication") ->
|
||||||
#{ description => <<"Get the gateway listener configurations">>
|
#{ 'operationId' => listeners_insta_authn,
|
||||||
, parameters => params_gateway_name_in_path()
|
get =>
|
||||||
++ params_listener_id_in_path()
|
#{ description => <<"Get the listener's authentication info">>
|
||||||
, responses =>
|
, parameters => params_gateway_name_in_path()
|
||||||
#{ <<"400">> => schema_bad_request()
|
++ params_listener_id_in_path()
|
||||||
, <<"404">> => schema_not_found()
|
, responses =>
|
||||||
, <<"500">> => schema_internal_error()
|
#{ 400 => error_codes([?BAD_REQUEST], <<"Bad Request">>)
|
||||||
, <<"200">> => schema_listener()
|
, 404 => error_codes([?NOT_FOUND], <<"Not Found">>)
|
||||||
}
|
, 500 => error_codes([?INTERNAL_ERROR],
|
||||||
};
|
<<"Ineternal Server Error">>)
|
||||||
swagger("/gateway/:name/listeners/:id", delete) ->
|
, 200 => schema_authn()
|
||||||
#{ description => <<"Delete the gateway listener">>
|
, 204 => <<"Authentication does not initiated">>
|
||||||
, parameters => params_gateway_name_in_path()
|
}
|
||||||
++ params_listener_id_in_path()
|
},
|
||||||
, responses =>
|
post =>
|
||||||
#{ <<"400">> => schema_bad_request()
|
#{ description => <<"Add authentication for the listener">>
|
||||||
, <<"404">> => schema_not_found()
|
, parameters => params_gateway_name_in_path()
|
||||||
, <<"500">> => schema_internal_error()
|
++ params_listener_id_in_path()
|
||||||
, <<"204">> => schema_no_content()
|
, requestBody => schema_authn()
|
||||||
}
|
, responses =>
|
||||||
};
|
#{ 400 => error_codes([?BAD_REQUEST], <<"Bad Request">>)
|
||||||
swagger("/gateway/:name/listeners/:id", put) ->
|
, 404 => error_codes([?NOT_FOUND], <<"Not Found">>)
|
||||||
#{ description => <<"Update the gateway listener">>
|
, 500 => error_codes([?INTERNAL_ERROR],
|
||||||
, parameters => params_gateway_name_in_path()
|
<<"Ineternal Server Error">>)
|
||||||
++ params_listener_id_in_path()
|
, 204 => <<"Added">>
|
||||||
, requestBody => schema_listener()
|
}
|
||||||
, responses =>
|
},
|
||||||
#{ <<"400">> => schema_bad_request()
|
put =>
|
||||||
, <<"404">> => schema_not_found()
|
#{ description => <<"Update authentication for the listener">>
|
||||||
, <<"500">> => schema_internal_error()
|
, parameters => params_gateway_name_in_path()
|
||||||
, <<"200">> => schema_no_content()
|
++ params_listener_id_in_path()
|
||||||
}
|
, requestBody => schema_authn()
|
||||||
};
|
, responses =>
|
||||||
swagger("/gateway/:name/listeners/:id/authentication", get) ->
|
#{ 400 => error_codes([?BAD_REQUEST], <<"Bad Request">>)
|
||||||
#{ description => <<"Get the listener's authentication info">>
|
, 404 => error_codes([?NOT_FOUND], <<"Not Found">>)
|
||||||
, parameters => params_gateway_name_in_path()
|
, 500 => error_codes([?INTERNAL_ERROR],
|
||||||
++ params_listener_id_in_path()
|
<<"Ineternal Server Error">>)
|
||||||
, responses =>
|
, 204 => <<"Updated">>
|
||||||
#{ <<"400">> => schema_bad_request()
|
}
|
||||||
, <<"404">> => schema_not_found()
|
},
|
||||||
, <<"500">> => schema_internal_error()
|
delete =>
|
||||||
, <<"200">> => schema_authn()
|
#{ description => <<"Remove authentication for the listener">>
|
||||||
, <<"204">> => schema_no_content()
|
, parameters => params_gateway_name_in_path()
|
||||||
}
|
++ params_listener_id_in_path()
|
||||||
};
|
, responses =>
|
||||||
swagger("/gateway/:name/listeners/:id/authentication", post) ->
|
#{ 400 => error_codes([?BAD_REQUEST], <<"Bad Request">>)
|
||||||
#{ description => <<"Add authentication for the listener">>
|
, 404 => error_codes([?NOT_FOUND], <<"Not Found">>)
|
||||||
, parameters => params_gateway_name_in_path()
|
, 500 => error_codes([?INTERNAL_ERROR],
|
||||||
++ params_listener_id_in_path()
|
<<"Ineternal Server Error">>)
|
||||||
, requestBody => schema_authn()
|
, 204 => <<"Deleted">>
|
||||||
, responses =>
|
}
|
||||||
#{ <<"400">> => schema_bad_request()
|
}
|
||||||
, <<"404">> => schema_not_found()
|
|
||||||
, <<"500">> => schema_internal_error()
|
|
||||||
, <<"204">> => schema_no_content()
|
|
||||||
}
|
|
||||||
};
|
|
||||||
swagger("/gateway/:name/listeners/:id/authentication", put) ->
|
|
||||||
#{ description => <<"Update authentication for the listener">>
|
|
||||||
, parameters => params_gateway_name_in_path()
|
|
||||||
++ params_listener_id_in_path()
|
|
||||||
, requestBody => schema_authn()
|
|
||||||
, responses =>
|
|
||||||
#{ <<"400">> => schema_bad_request()
|
|
||||||
, <<"404">> => schema_not_found()
|
|
||||||
, <<"500">> => schema_internal_error()
|
|
||||||
, <<"204">> => schema_no_content()
|
|
||||||
}
|
|
||||||
};
|
|
||||||
swagger("/gateway/:name/listeners/:id/authentication", delete) ->
|
|
||||||
#{ description => <<"Remove authentication for the listener">>
|
|
||||||
, parameters => params_gateway_name_in_path()
|
|
||||||
++ params_listener_id_in_path()
|
|
||||||
, responses =>
|
|
||||||
#{ <<"400">> => schema_bad_request()
|
|
||||||
, <<"404">> => schema_not_found()
|
|
||||||
, <<"500">> => schema_internal_error()
|
|
||||||
, <<"204">> => schema_no_content()
|
|
||||||
}
|
|
||||||
}.
|
}.
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% params defines
|
%% params defines
|
||||||
|
|
||||||
params_gateway_name_in_path() ->
|
params_gateway_name_in_path() ->
|
||||||
[#{ name => name
|
[{name,
|
||||||
, in => path
|
mk(binary(),
|
||||||
, schema => #{type => string}
|
#{ in => path
|
||||||
, required => true
|
, desc => <<"Gateway Name">>
|
||||||
}].
|
})}
|
||||||
|
].
|
||||||
|
|
||||||
params_listener_id_in_path() ->
|
params_listener_id_in_path() ->
|
||||||
[#{ name => id
|
[{id,
|
||||||
, in => path
|
mk(binary(),
|
||||||
, schema => #{type => string}
|
#{ in => path
|
||||||
, required => true
|
, desc => <<"Listener ID">>
|
||||||
}].
|
})}
|
||||||
|
].
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% schemas
|
%% schemas
|
||||||
|
|
||||||
schema_listener_list() ->
|
roots() ->
|
||||||
emqx_mgmt_util:array_schema(
|
[ listener
|
||||||
#{ type => object
|
|
||||||
, properties => properties_listener()
|
|
||||||
},
|
|
||||||
<<"Listener list">>
|
|
||||||
).
|
|
||||||
|
|
||||||
schema_listener() ->
|
|
||||||
emqx_mgmt_util:schema(
|
|
||||||
#{ type => object
|
|
||||||
, properties => properties_listener()
|
|
||||||
}
|
|
||||||
).
|
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
|
||||||
%% properties
|
|
||||||
|
|
||||||
properties_listener() ->
|
|
||||||
emqx_mgmt_util:properties(
|
|
||||||
raw_properties_common_listener() ++
|
|
||||||
[ {tcp, object, raw_properties_tcp_opts()}
|
|
||||||
, {ssl, object, raw_properties_ssl_opts()}
|
|
||||||
, {udp, object, raw_properties_udp_opts()}
|
|
||||||
, {dtls, object, raw_properties_dtls_opts()}
|
|
||||||
]).
|
|
||||||
|
|
||||||
raw_properties_tcp_opts() ->
|
|
||||||
[ {active_n, integer, <<>>}
|
|
||||||
, {backlog, integer, <<>>}
|
|
||||||
, {buffer, string, <<>>}
|
|
||||||
, {recbuf, string, <<>>}
|
|
||||||
, {sndbuf, string, <<>>}
|
|
||||||
, {high_watermark, string, <<>>}
|
|
||||||
, {nodelay, boolean, <<>>}
|
|
||||||
, {reuseaddr, boolean, <<>>}
|
|
||||||
, {send_timeout, string, <<>>}
|
|
||||||
, {send_timeout_close, boolean, <<>>}
|
|
||||||
].
|
].
|
||||||
|
|
||||||
raw_properties_ssl_opts() ->
|
fields(listener) ->
|
||||||
[ {cacertfile, string, <<>>}
|
common_listener_opts() ++
|
||||||
, {certfile, string, <<>>}
|
[ {tcp,
|
||||||
, {keyfile, string, <<>>}
|
mk(ref(tcp_listener_opts),
|
||||||
, {verify, string, <<>>}
|
#{ nullable => {true, recursively}
|
||||||
, {fail_if_no_peer_cert, boolean, <<>>}
|
, desc => <<"The tcp socket options for tcp or ssl listener">>
|
||||||
, {server_name_indication, boolean, <<>>}
|
})}
|
||||||
, {depth, integer, <<>>}
|
, {ssl,
|
||||||
, {password, string, <<>>}
|
mk(ref(ssl_listener_opts),
|
||||||
, {handshake_timeout, string, <<>>}
|
#{ nullable => {true, recursively}
|
||||||
, {versions, {array, string}, <<>>}
|
, desc => <<"The ssl socket options for ssl listener">>
|
||||||
, {ciphers, {array, string}, <<>>}
|
})}
|
||||||
, {user_lookup_fun, string, <<>>}
|
, {udp,
|
||||||
, {reuse_sessions, boolean, <<>>}
|
mk(ref(udp_listener_opts),
|
||||||
, {secure_renegotiate, boolean, <<>>}
|
#{ nullable => {true, recursively}
|
||||||
, {honor_cipher_order, boolean, <<>>}
|
, desc => <<"The udp socket options for udp or dtls listener">>
|
||||||
, {dhfile, string, <<>>}
|
})}
|
||||||
].
|
, {dtls,
|
||||||
|
mk(ref(dtls_listener_opts),
|
||||||
raw_properties_udp_opts() ->
|
#{ nullable => {true, recursively}
|
||||||
[ {active_n, integer, <<>>}
|
, desc => <<"The dtls socket options for dtls listener">>
|
||||||
, {buffer, string, <<>>}
|
})}
|
||||||
, {recbuf, string, <<>>}
|
];
|
||||||
, {sndbuf, string, <<>>}
|
fields(tcp_listener_opts) ->
|
||||||
, {reuseaddr, boolean, <<>>}
|
[ {active_n, mk(integer(), #{})}
|
||||||
].
|
, {backlog, mk(integer(), #{})}
|
||||||
|
, {buffer, mk(binary(), #{})}
|
||||||
raw_properties_dtls_opts() ->
|
, {recbuf, mk(binary(), #{})}
|
||||||
|
, {sndbuf, mk(binary(), #{})}
|
||||||
|
, {high_watermark, mk(binary(), #{})}
|
||||||
|
, {nodelay, mk(boolean(), #{})}
|
||||||
|
, {reuseaddr, boolean()}
|
||||||
|
, {send_timeout, binary()}
|
||||||
|
, {send_timeout_close, boolean()}
|
||||||
|
];
|
||||||
|
fields(ssl_listener_opts) ->
|
||||||
|
[ {cacertfile, binary()}
|
||||||
|
, {certfile, binary()}
|
||||||
|
, {keyfile, binary()}
|
||||||
|
, {verify, binary()}
|
||||||
|
, {fail_if_no_peer_cert, boolean()}
|
||||||
|
, {server_name_indication, boolean()}
|
||||||
|
, {depth, integer()}
|
||||||
|
, {password, binary()}
|
||||||
|
, {handshake_timeout, binary()}
|
||||||
|
, {versions, hoconsc:array(binary())}
|
||||||
|
, {ciphers, hoconsc:array(binary())}
|
||||||
|
, {user_lookup_fun, binary()}
|
||||||
|
, {reuse_sessions, boolean()}
|
||||||
|
, {secure_renegotiate, boolean()}
|
||||||
|
, {honor_cipher_order, boolean()}
|
||||||
|
, {dhfile, binary()}
|
||||||
|
];
|
||||||
|
fields(udp_listener_opts) ->
|
||||||
|
[ {active_n, integer()}
|
||||||
|
, {buffer, binary()}
|
||||||
|
, {recbuf, binary()}
|
||||||
|
, {sndbuf, binary()}
|
||||||
|
, {reuseaddr, boolean()}
|
||||||
|
];
|
||||||
|
fields(dtls_listener_opts) ->
|
||||||
Ls = lists_key_without(
|
Ls = lists_key_without(
|
||||||
[versions,ciphers,handshake_timeout], 1,
|
[versions,ciphers,handshake_timeout], 1,
|
||||||
raw_properties_ssl_opts()
|
fields(ssl_listener_opts)
|
||||||
),
|
),
|
||||||
[ {versions, {array, string}, <<>>}
|
[ {versions, hoconsc:array(binary())}
|
||||||
, {ciphers, {array, string}, <<>>}
|
, {ciphers, hoconsc:array(binary())}
|
||||||
| Ls].
|
| Ls].
|
||||||
|
|
||||||
lists_key_without([], _N, L) ->
|
lists_key_without([], _N, L) ->
|
||||||
|
@ -369,23 +390,67 @@ lists_key_without([], _N, L) ->
|
||||||
lists_key_without([K|Ks], N, L) ->
|
lists_key_without([K|Ks], N, L) ->
|
||||||
lists_key_without(Ks, N, lists:keydelete(K, N, L)).
|
lists_key_without(Ks, N, lists:keydelete(K, N, L)).
|
||||||
|
|
||||||
raw_properties_common_listener() ->
|
common_listener_opts() ->
|
||||||
[ {enable, boolean, <<"Whether to enable this listener">>}
|
[ {enable,
|
||||||
, {id, string, <<"Listener Id">>}
|
mk(boolean(),
|
||||||
, {name, string, <<"Listener name">>}
|
#{ nullable => true
|
||||||
, {type, string,
|
, desc => <<"Whether to enable this listener">>})}
|
||||||
<<"Listener type. Enum: tcp, udp, ssl, dtls">>,
|
, {id,
|
||||||
[<<"tcp">>, <<"ssl">>, <<"udp">>, <<"dtls">>]}
|
mk(binary(),
|
||||||
, {running, boolean, <<"Listener running status">>}
|
#{ nullable => true
|
||||||
, {bind, string, <<"Listener bind address or port">>}
|
, desc => <<"Listener Id">>})}
|
||||||
, {acceptors, integer, <<"Listener acceptors number">>}
|
, {name,
|
||||||
, {access_rules, {array, string}, <<"Listener Access rules for client">>}
|
mk(binary(),
|
||||||
, {max_conn_rate, integer, <<"Max connection rate for the listener">>}
|
#{ nullable => true
|
||||||
, {max_connections, integer, <<"Max connections for the listener">>}
|
, desc => <<"Listener name">>})}
|
||||||
, {mountpoint, string,
|
, {type,
|
||||||
<<"The Mounpoint for clients of the listener. "
|
mk(hoconsc:enum([tcp, ssl, udp, dtls]),
|
||||||
"The gateway-level mountpoint configuration can be overloaded "
|
#{ nullable => true
|
||||||
"when it is not null or empty string">>}
|
, desc => <<"Listener type. Enum: tcp, udp, ssl, dtls">>})}
|
||||||
|
, {running,
|
||||||
|
mk(boolean(),
|
||||||
|
#{ nullable => true
|
||||||
|
, desc => <<"Listener running status">>})}
|
||||||
|
, {bind,
|
||||||
|
mk(binary(),
|
||||||
|
#{ nullable => true
|
||||||
|
, desc => <<"Listener bind address or port">>})}
|
||||||
|
, {acceptors,
|
||||||
|
mk(integer(),
|
||||||
|
#{ nullable => true
|
||||||
|
, desc => <<"Listener acceptors number">>})}
|
||||||
|
, {access_rules,
|
||||||
|
mk(hoconsc:array(binary()),
|
||||||
|
#{ nullable => true
|
||||||
|
, desc => <<"Listener Access rules for client">>})}
|
||||||
|
, {max_conn_rate,
|
||||||
|
mk(integer(),
|
||||||
|
#{ nullable => true
|
||||||
|
, desc => <<"Max connection rate for the listener">>})}
|
||||||
|
, {max_connections,
|
||||||
|
mk(integer(),
|
||||||
|
#{ nullable => true
|
||||||
|
, desc => <<"Max connections for the listener">>})}
|
||||||
|
, {mountpoint,
|
||||||
|
mk(binary(),
|
||||||
|
#{ nullable => true
|
||||||
|
, desc =>
|
||||||
|
<<"The Mounpoint for clients of the listener. "
|
||||||
|
"The gateway-level mountpoint configuration can be overloaded "
|
||||||
|
"when it is not null or empty string">>})}
|
||||||
%% FIXME:
|
%% FIXME:
|
||||||
, {authentication, string, <<"NOT-SUPPORTED-NOW">>}
|
, {authentication,
|
||||||
].
|
mk(emqx_authn_schema:authenticator_type(),
|
||||||
|
#{ nullable => {true, recursively}
|
||||||
|
, desc => <<"The authenticatior for this listener">>
|
||||||
|
})}
|
||||||
|
].
|
||||||
|
|
||||||
|
%%--------------------------------------------------------------------
|
||||||
|
%% examples
|
||||||
|
|
||||||
|
examples_listener_list() ->
|
||||||
|
[examples_listener()].
|
||||||
|
|
||||||
|
examples_listener() ->
|
||||||
|
#{id => true}.
|
||||||
|
|
|
@ -53,6 +53,7 @@
|
||||||
%% Utils for http, swagger, etc.
|
%% Utils for http, swagger, etc.
|
||||||
-export([ return_http_error/2
|
-export([ return_http_error/2
|
||||||
, with_gateway/2
|
, with_gateway/2
|
||||||
|
, with_authn/2
|
||||||
, checks/2
|
, checks/2
|
||||||
, schema_bad_request/0
|
, schema_bad_request/0
|
||||||
, schema_not_found/0
|
, schema_not_found/0
|
||||||
|
@ -159,14 +160,31 @@ remove_listener(ListenerId) ->
|
||||||
|
|
||||||
-spec authn(gateway_name()) -> map().
|
-spec authn(gateway_name()) -> map().
|
||||||
authn(GwName) ->
|
authn(GwName) ->
|
||||||
|
%% XXX: Need append chain-nanme, authenticator-id?
|
||||||
Path = [gateway, GwName, authentication],
|
Path = [gateway, GwName, authentication],
|
||||||
emqx_map_lib:jsonable_map(emqx:get_config(Path)).
|
ChainName = emqx_gateway_utils:global_chain(GwName),
|
||||||
|
wrap_chain_name(
|
||||||
|
ChainName,
|
||||||
|
emqx_map_lib:jsonable_map(emqx:get_config(Path))
|
||||||
|
).
|
||||||
|
|
||||||
-spec authn(gateway_name(), binary()) -> map().
|
-spec authn(gateway_name(), binary()) -> map().
|
||||||
authn(GwName, ListenerId) ->
|
authn(GwName, ListenerId) ->
|
||||||
{_, Type, Name} = emqx_gateway_utils:parse_listener_id(ListenerId),
|
{_, Type, Name} = emqx_gateway_utils:parse_listener_id(ListenerId),
|
||||||
Path = [gateway, GwName, listeners, Type, Name, authentication],
|
Path = [gateway, GwName, listeners, Type, Name, authentication],
|
||||||
emqx_map_lib:jsonable_map(emqx:get_config(Path)).
|
ChainName = emqx_gateway_utils:listener_chain(GwName, Type, Name),
|
||||||
|
wrap_chain_name(
|
||||||
|
ChainName,
|
||||||
|
emqx_map_lib:jsonable_map(emqx:get_config(Path))
|
||||||
|
).
|
||||||
|
|
||||||
|
wrap_chain_name(ChainName, Conf) ->
|
||||||
|
case emqx_authentication:list_authenticators(ChainName) of
|
||||||
|
{ok, [#{id := Id} | _]} ->
|
||||||
|
Conf#{chain_name => ChainName, id => Id};
|
||||||
|
_ ->
|
||||||
|
Conf
|
||||||
|
end.
|
||||||
|
|
||||||
-spec add_authn(gateway_name(), map()) -> ok.
|
-spec add_authn(gateway_name(), map()) -> ok.
|
||||||
add_authn(GwName, AuthConf) ->
|
add_authn(GwName, AuthConf) ->
|
||||||
|
@ -303,6 +321,13 @@ codestr(401) -> 'NOT_SUPPORTED_NOW';
|
||||||
codestr(404) -> 'RESOURCE_NOT_FOUND';
|
codestr(404) -> 'RESOURCE_NOT_FOUND';
|
||||||
codestr(500) -> 'UNKNOW_ERROR'.
|
codestr(500) -> 'UNKNOW_ERROR'.
|
||||||
|
|
||||||
|
-spec with_authn(binary(), function()) -> any().
|
||||||
|
with_authn(GwName0, Fun) ->
|
||||||
|
with_gateway(GwName0, fun(GwName) ->
|
||||||
|
Authn = emqx_gateway_http:authn(GwName),
|
||||||
|
Fun(GwName, Authn)
|
||||||
|
end).
|
||||||
|
|
||||||
-spec with_gateway(binary(), function()) -> any().
|
-spec with_gateway(binary(), function()) -> any().
|
||||||
with_gateway(GwName0, Fun) ->
|
with_gateway(GwName0, Fun) ->
|
||||||
try
|
try
|
||||||
|
|
|
@ -219,23 +219,6 @@ detailed_gateway_info(State) ->
|
||||||
%% Internal funcs
|
%% Internal funcs
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
||||||
%% same with emqx_authentication:global_chain/1
|
|
||||||
global_chain(mqtt) ->
|
|
||||||
'mqtt:global';
|
|
||||||
global_chain('mqtt-sn') ->
|
|
||||||
'mqtt-sn:global';
|
|
||||||
global_chain(coap) ->
|
|
||||||
'coap:global';
|
|
||||||
global_chain(lwm2m) ->
|
|
||||||
'lwm2m:global';
|
|
||||||
global_chain(stomp) ->
|
|
||||||
'stomp:global';
|
|
||||||
global_chain(_) ->
|
|
||||||
'unknown:global'.
|
|
||||||
|
|
||||||
listener_chain(GwName, Type, LisName) ->
|
|
||||||
emqx_gateway_utils:listener_id(GwName, Type, LisName).
|
|
||||||
|
|
||||||
%% There are two layer authentication configs
|
%% There are two layer authentication configs
|
||||||
%% stomp.authn
|
%% stomp.authn
|
||||||
%% / \
|
%% / \
|
||||||
|
@ -266,10 +249,11 @@ do_init_authn([_BadConf|More], Names) ->
|
||||||
authns(GwName, Config) ->
|
authns(GwName, Config) ->
|
||||||
Listeners = maps:to_list(maps:get(listeners, Config, #{})),
|
Listeners = maps:to_list(maps:get(listeners, Config, #{})),
|
||||||
lists:append(
|
lists:append(
|
||||||
[ [{listener_chain(GwName, LisType, LisName), authn_conf(Opts)}
|
[ [{emqx_gateway_utils:listener_chain(GwName, LisType, LisName),
|
||||||
|
authn_conf(Opts)}
|
||||||
|| {LisName, Opts} <- maps:to_list(LisNames) ]
|
|| {LisName, Opts} <- maps:to_list(LisNames) ]
|
||||||
|| {LisType, LisNames} <- Listeners])
|
|| {LisType, LisNames} <- Listeners])
|
||||||
++ [{global_chain(GwName), authn_conf(Config)}].
|
++ [{emqx_gateway_utils:global_chain(GwName), authn_conf(Config)}].
|
||||||
|
|
||||||
authn_conf(Conf) ->
|
authn_conf(Conf) ->
|
||||||
maps:get(authentication, Conf, #{enable => false}).
|
maps:get(authentication, Conf, #{enable => false}).
|
||||||
|
|
|
@ -34,6 +34,8 @@
|
||||||
, listener_id/3
|
, listener_id/3
|
||||||
, parse_listener_id/1
|
, parse_listener_id/1
|
||||||
, is_running/2
|
, is_running/2
|
||||||
|
, global_chain/1
|
||||||
|
, listener_chain/3
|
||||||
]).
|
]).
|
||||||
|
|
||||||
-export([ stringfy/1
|
-export([ stringfy/1
|
||||||
|
@ -159,6 +161,23 @@ is_running(ListenerId, #{<<"bind">> := ListenOn0}) ->
|
||||||
false
|
false
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
%% same with emqx_authentication:global_chain/1
|
||||||
|
global_chain(mqtt) ->
|
||||||
|
'mqtt:global';
|
||||||
|
global_chain('mqtt-sn') ->
|
||||||
|
'mqtt-sn:global';
|
||||||
|
global_chain(coap) ->
|
||||||
|
'coap:global';
|
||||||
|
global_chain(lwm2m) ->
|
||||||
|
'lwm2m:global';
|
||||||
|
global_chain(stomp) ->
|
||||||
|
'stomp:global';
|
||||||
|
global_chain(_) ->
|
||||||
|
'unknown:global'.
|
||||||
|
|
||||||
|
listener_chain(GwName, Type, LisName) ->
|
||||||
|
listener_id(GwName, Type, LisName).
|
||||||
|
|
||||||
bin(A) when is_atom(A) ->
|
bin(A) when is_atom(A) ->
|
||||||
atom_to_binary(A);
|
atom_to_binary(A);
|
||||||
bin(L) when is_list(L); is_binary(L) ->
|
bin(L) when is_list(L); is_binary(L) ->
|
||||||
|
|
|
@ -164,7 +164,7 @@ t_random_test(_) ->
|
||||||
random_test_body() ->
|
random_test_body() ->
|
||||||
Data = generate_random_binary(),
|
Data = generate_random_binary(),
|
||||||
case catch parse(Data) of
|
case catch parse(Data) of
|
||||||
{ok, _Msg} -> ok;
|
Msg when is_record(Msg, mqtt_sn_message) -> ok;
|
||||||
{'EXIT', {Err, _Stack}}
|
{'EXIT', {Err, _Stack}}
|
||||||
when Err =:= unkown_message_type;
|
when Err =:= unkown_message_type;
|
||||||
Err =:= malformed_message_len;
|
Err =:= malformed_message_len;
|
||||||
|
|
Loading…
Reference in New Issue