Sfoglia il codice sorgente

refactor(authn): make schema doc generation work

Zaiming Shi 4 anni fa
parent
commit
ce5b456872

+ 6 - 5
apps/emqx/src/emqx_schema.erl

@@ -1355,11 +1355,12 @@ str(S) when is_list(S) ->
     S.
 
 authentication(Desc) ->
-    #{ type => hoconsc:union([typerefl:map(), hoconsc:array(typerefl:map())])
-     , desc => [Desc, "<br>", """
+    #{ type => hoconsc:lazy(hoconsc:union([typerefl:map(), hoconsc:array(typerefl:map())]))
+     , desc => iolist_to_binary([Desc, "<br>", """
 Authentication can be one single authenticator instance or a chain of authenticators as an array.
 The when authenticating a login (username, client ID, etc.) the authenticators are checked
-in the configured order.
-"""
-               ]
+in the configured order.<br>
+EMQ X comes with a set of pre-built autenticators, for more details, see
+<code>authenticator_config</code>.
+"""])
      }.

+ 7 - 2
apps/emqx_authn/src/emqx_authn_schema.erl

@@ -23,8 +23,10 @@
         , fields/1
         ]).
 
-%% just a stub, never used at root level
-roots() -> [].
+%% only for doc generation
+roots() -> [{authenticator_config,
+             #{type => hoconsc:union(config_refs([Module || {_AuthnType, Module} <- emqx_authn:providers()]))
+               }}].
 
 fields(_) -> [].
 
@@ -35,3 +37,6 @@ common_fields() ->
 enable(type) -> boolean();
 enable(default) -> true;
 enable(_) -> undefined.
+
+config_refs(Modules) ->
+    lists:append([Module:refs() || Module <- Modules]).

+ 5 - 6
apps/emqx_authn/src/simple_authn/emqx_authn_http.erl

@@ -40,12 +40,11 @@
 %% Hocon Schema
 %%------------------------------------------------------------------------------
 
-namespace() -> "authn-password_based-http_server".
+namespace() -> "authn-http".
 
 roots() ->
-    [ {config, {union, [ hoconsc:ref(?MODULE, get)
-                       , hoconsc:ref(?MODULE, post)
-                       ]}}
+    [ {config, hoconsc:mk(hoconsc:union(refs()),
+                          #{})}
     ].
 
 fields(get) ->
@@ -61,8 +60,8 @@ fields(post) ->
     ] ++ common_fields().
 
 common_fields() ->
-    [ {mechanism,       'password-based'}
-    , {backend,         'http'}
+    [ {mechanism,       hoconsc:enum(['password-based'])}
+    , {backend,         hoconsc:enum(['http'])}
     , {url,             fun url/1}
     , {body,            fun body/1}
     , {request_timeout, fun request_timeout/1}

+ 3 - 4
apps/emqx_authn/src/simple_authn/emqx_authn_jwt.erl

@@ -40,10 +40,9 @@
 namespace() -> "authn-jwt".
 
 roots() ->
-    [ {config, {union, [ hoconsc:mk('hmac-based')
-                       , hoconsc:mk('public-key')
-                       , hoconsc:mk('jwks')
-                       ]}}
+    [ {config, hoconsc:mk(hoconsc:union(refs()),
+                          #{}
+                         )}
     ].
 
 fields('hmac-based') ->

+ 1 - 1
apps/emqx_authn/src/simple_authn/emqx_authn_mnesia.erl

@@ -84,7 +84,7 @@ mnesia(copy) ->
 %% Hocon Schema
 %%------------------------------------------------------------------------------
 
-namespace() -> "authn-password_based-builtin_db".
+namespace() -> "authn-builtin_db".
 
 roots() -> [config].
 

+ 3 - 5
apps/emqx_authn/src/simple_authn/emqx_authn_mongodb.erl

@@ -39,13 +39,11 @@
 %% Hocon Schema
 %%------------------------------------------------------------------------------
 
-namespace() -> "authn-password_based-mongodb".
+namespace() -> "authn-mongodb".
 
 roots() ->
-    [ {config, {union, [ hoconsc:mk(standalone)
-                       , hoconsc:mk('replica-set')
-                       , hoconsc:mk('sharded-cluster')
-                       ]}}
+    [ {config, hoconsc:mk(hoconsc:union(refs()),
+                          #{})}
     ].
 
 fields(standalone) ->

+ 1 - 1
apps/emqx_authn/src/simple_authn/emqx_authn_mysql.erl

@@ -39,7 +39,7 @@
 %% Hocon Schema
 %%------------------------------------------------------------------------------
 
-namespace() -> "authn-password_based-mysql".
+namespace() -> "authn-mysql".
 
 roots() -> [config].
 

+ 1 - 1
apps/emqx_authn/src/simple_authn/emqx_authn_pgsql.erl

@@ -40,7 +40,7 @@
 %% Hocon Schema
 %%------------------------------------------------------------------------------
 
-namespace() -> "authn-password_based-postgresql".
+namespace() -> "authn-postgresql".
 
 roots() -> [config].
 

+ 3 - 5
apps/emqx_authn/src/simple_authn/emqx_authn_redis.erl

@@ -39,13 +39,11 @@
 %% Hocon Schema
 %%------------------------------------------------------------------------------
 
-namespace() -> "authn-password_based-redis".
+namespace() -> "authn-redis".
 
 roots() ->
-    [ {config, {union, [ hoconsc:mk(standalone)
-                       , hoconsc:mk(cluster)
-                       , hoconsc:mk(sentinel)
-                       ]}}
+    [ {config, hoconsc:mk(hoconsc:union(refs()),
+                          #{})}
     ].
 
 fields(standalone) ->

+ 1 - 0
apps/emqx_machine/src/emqx_machine_schema.erl

@@ -45,6 +45,7 @@
         [ emqx_bridge_schema
         , emqx_retainer_schema
         , emqx_statsd_schema
+        , emqx_authn_schema
         , emqx_authz_schema
         , emqx_auto_subscribe_schema
         , emqx_modules_schema