Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dashboard): add SSO feature and integrate with LDAP #11631

Merged
merged 4 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 9 additions & 4 deletions apps/emqx_dashboard/include/emqx_dashboard.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,23 @@
%% a predefined configuration would replace these macros.
-define(ROLE_VIEWER, <<"viewer">>).
-define(ROLE_SUPERUSER, <<"superuser">>).

-define(ROLE_DEFAULT, ?ROLE_SUPERUSER).

-define(SSO_USERNAME(Backend, Name), {Backend, Name}).

-type dashboard_sso_backend() :: atom().
-type dashboard_sso_username() :: {dashboard_sso_backend(), binary()}.
-type dashboard_username() :: binary() | dashboard_sso_username().
-type dashboard_user_role() :: binary().

-record(?ADMIN, {
username :: binary(),
username :: dashboard_username(),
pwdhash :: binary(),
description :: binary(),
role = ?ROLE_DEFAULT :: binary(),
role = ?ROLE_DEFAULT :: dashboard_user_role(),
extra = #{} :: map()
}).

-type dashboard_user_role() :: binary().
-type dashboard_user() :: #?ADMIN{}.

-define(ADMIN_JWT, emqx_admin_jwt).
Expand Down
27 changes: 21 additions & 6 deletions apps/emqx_dashboard/src/emqx_dashboard_admin.erl
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@

-export([backup_tables/0]).

-if(?EMQX_RELEASE_EDITION == ee).
-export([add_sso_user/4, lookup_user/2]).
-endif.

-type emqx_admin() :: #?ADMIN{}.

%%--------------------------------------------------------------------
Expand Down Expand Up @@ -99,10 +103,11 @@ add_default_user() ->
%% API
%%--------------------------------------------------------------------

-spec add_user(binary(), binary(), dashboard_user_role(), binary()) -> {ok, map()} | {error, any()}.
add_user(Username, Password, Role, Desc) when
is_binary(Username), is_binary(Password)
->


-spec add_user(dashboard_username(), binary(), dashboard_user_role(), binary()) ->
{ok, map()} | {error, any()}.
add_user(Username, Password, Role, Desc) when is_binary(Password) ->
case {legal_username(Username), legal_password(Password), legal_role(Role)} of
{ok, ok, ok} -> do_add_user(Username, Password, Role, Desc);
{{error, Reason}, _, _} -> {error, Reason};
Expand All @@ -115,6 +120,8 @@ do_add_user(Username, Password, Role, Desc) ->
return(Res).

%% 0-9 or A-Z or a-z or $_
legal_username(?SSO_USERNAME(_, _)) ->
ok;
legal_username(<<>>) ->
{error, <<"Username cannot be empty">>};
legal_username(UserName) ->
Expand Down Expand Up @@ -312,8 +319,8 @@ update_pwd(Username, Fun) ->
end,
return(mria:transaction(?DASHBOARD_SHARD, Trans)).

-spec lookup_user(binary()) -> [emqx_admin()].
lookup_user(Username) when is_binary(Username) ->
-spec lookup_user(dashboard_username()) -> [emqx_admin()].
lookup_user(Username) ->
Fun = fun() -> mnesia:read(?ADMIN, Username) end,
{atomic, User} = mria:ro_transaction(?DASHBOARD_SHARD, Fun),
User.
Expand Down Expand Up @@ -410,6 +417,14 @@ legal_role(Role) ->
role(Data) ->
emqx_dashboard_rbac:role(Data).

-spec add_sso_user(atom(), binary(), dashboard_user_role(), binary()) ->
{ok, map()} | {error, any()}.
add_sso_user(Backend, Username, Role, Desc) ->
add_user(?SSO_USERNAME(Backend, Username), <<>>, Role, Desc).

-spec lookup_user(dashboard_sso_backend(), binary()) -> [emqx_admin()].
lookup_user(Backend, Username) when is_atom(Backend) ->
lookup_user(?SSO_USERNAME(Backend, Username)).
-else.

-dialyzer({no_match, [add_user/4, update_user/3]}).
Expand Down
94 changes: 94 additions & 0 deletions apps/emqx_dashboard_sso/BSL.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
Business Source License 1.1

Licensor: Hangzhou EMQ Technologies Co., Ltd.
Licensed Work: EMQX Enterprise Edition
The Licensed Work is (c) 2023
Hangzhou EMQ Technologies Co., Ltd.
Additional Use Grant: Students and educators are granted right to copy,
modify, and create derivative work for research
or education.
Change Date: 2027-02-01
Change License: Apache License, Version 2.0

For information about alternative licensing arrangements for the Software,
please contact Licensor: https://www.emqx.com/en/contact

Notice

The Business Source License (this document, or the “License”) is not an Open
Source license. However, the Licensed Work will eventually be made available
under an Open Source License, as stated in this License.

License text copyright (c) 2017 MariaDB Corporation Ab, All Rights Reserved.
“Business Source License” is a trademark of MariaDB Corporation Ab.

-----------------------------------------------------------------------------

Business Source License 1.1

Terms

The Licensor hereby grants you the right to copy, modify, create derivative
works, redistribute, and make non-production use of the Licensed Work. The
Licensor may make an Additional Use Grant, above, permitting limited
production use.

Effective on the Change Date, or the fourth anniversary of the first publicly
available distribution of a specific version of the Licensed Work under this
License, whichever comes first, the Licensor hereby grants you rights under
the terms of the Change License, and the rights granted in the paragraph
above terminate.

If your use of the Licensed Work does not comply with the requirements
currently in effect as described in this License, you must purchase a
commercial license from the Licensor, its affiliated entities, or authorized
resellers, or you must refrain from using the Licensed Work.

All copies of the original and modified Licensed Work, and derivative works
of the Licensed Work, are subject to this License. This License applies
separately for each version of the Licensed Work and the Change Date may vary
for each version of the Licensed Work released by Licensor.

You must conspicuously display this License on each original or modified copy
of the Licensed Work. If you receive the Licensed Work in original or
modified form from a third party, the terms and conditions set forth in this
License apply to your use of that work.

Any use of the Licensed Work in violation of this License will automatically
terminate your rights under this License for the current and all other
versions of the Licensed Work.

This License does not grant you any right in any trademark or logo of
Licensor or its affiliates (provided that you may use a trademark or logo of
Licensor as expressly required by this License).

TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
TITLE.

MariaDB hereby grants you permission to use this License’s text to license
your works, and to refer to it using the trademark “Business Source License”,
as long as you comply with the Covenants of Licensor below.

Covenants of Licensor

In consideration of the right to use this License’s text and the “Business
Source License” name and trademark, Licensor covenants to MariaDB, and to all
other recipients of the licensed work to be provided by Licensor:

1. To specify as the Change License the GPL Version 2.0 or any later version,
or a license that is compatible with GPL Version 2.0 or a later version,
where “compatible” means that software provided under the Change License can
be included in a program with software provided under GPL Version 2.0 or a
later version. Licensor may specify additional Change Licenses without
limitation.

2. To either: (a) specify an additional grant of rights to use that does not
impose any additional restriction on the right granted in this License, as
the Additional Use Grant; or (b) insert the text “None”.

3. To specify a Change Date.

4. Not to modify this License in any other way.
11 changes: 11 additions & 0 deletions apps/emqx_dashboard_sso/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Dashboard Single sign-on

Single Sign-On is a mechanism that allows a user to automatically sign in to multiple applications after signing in to one. This improves convenience and security.
lafirest marked this conversation as resolved.
Show resolved Hide resolved

## Contributing

Please see our [contributing.md](../../CONTRIBUTING.md).

## License

See [APL](../../APL.txt).
lafirest marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions apps/emqx_dashboard_sso/docker-ct
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

7 changes: 7 additions & 0 deletions apps/emqx_dashboard_sso/rebar.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
%% -*- mode: erlang; -*-

{erl_opts, [debug_info]}.
{deps, [
{emqx_ldap, {path, "../../apps/emqx_ldap"}},
{emqx_dashboard, {path, "../../apps/emqx_dashboard"}}
]}.
19 changes: 19 additions & 0 deletions apps/emqx_dashboard_sso/src/emqx_dashboard_sso.app.src
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{application, emqx_dashboard_sso, [
{description, "EMQX Dashboard Single Sign-On"},
{vsn, "0.1.0"},
{registered, [emqx_dashboard_sso_sup]},
{applications, [
kernel,
stdlib,
emqx_dashboard,
emqx_ldap
]},
{mod, {emqx_dashboard_sso_app, []}},
{env, []},
{modules, []},
{maintainers, ["EMQX Team <contact@emqx.io>"]},
{links, [
{"Homepage", "https://emqx.io/"},
{"Github", "https://github.com/emqx/emqx-dashboard5"}
]}
]}.
45 changes: 45 additions & 0 deletions apps/emqx_dashboard_sso/src/emqx_dashboard_sso.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
%%--------------------------------------------------------------------
%% Copyright (c) 2023 EMQ Technologies Co., Ltd. All Rights Reserved.
%%--------------------------------------------------------------------

-module(emqx_dashboard_sso).

-include_lib("hocon/include/hoconsc.hrl").

-export([types/0, modules/0, provider/1, backends/0]).
%%------------------------------------------------------------------------------
%% Callbacks
%%------------------------------------------------------------------------------
-type request() :: map().
-type parsed_config() :: #{
backend => atom(),
atom() => term()
}.
-type state() :: #{atom() => term()}.
-type raw_config() :: #{binary() => term()}.
-type config() :: parsed_config() | raw_config().

-callback hocon_ref() -> ?R_REF(Module :: atom(), Name :: atom() | binary()).
-callback login_ref() -> ?R_REF(Module :: atom(), Name :: atom() | binary()).
-callback create(Config :: config()) ->
{ok, State :: state()} | {error, Reason :: term()}.
-callback update(Config :: config(), State :: state()) ->
{ok, NewState :: state()} | {error, Reason :: term()}.
-callback destroy(State :: state()) -> ok.
-callback sign(request(), State :: state()) ->
{ok, Token :: binary()} | {error, Reason :: term()}.

%%------------------------------------------------------------------------------
%% API
%%------------------------------------------------------------------------------
types() ->
maps:keys(backends()).

modules() ->
maps:values(backends()).

provider(Backend) ->
maps:get(Backend, backends()).

backends() ->
#{ldap => emqx_dashboard_sso_ldap}.