Просмотр исходного кода

fix(authz api): fix schema error

zhanghongtong 4 лет назад
Родитель
Сommit
c4e0eff772

+ 72 - 44
apps/emqx_authz/src/emqx_authz_api_mnesia.erl

@@ -22,8 +22,7 @@
 -include_lib("emqx/include/logger.hrl").
 -include_lib("emqx/include/logger.hrl").
 -include_lib("stdlib/include/ms_transform.hrl").
 -include_lib("stdlib/include/ms_transform.hrl").
 
 
--define(EXAMPLE_USERNAME, #{type => username,
-                            key => user1,
+-define(EXAMPLE_USERNAME, #{username => user1,
                             rules => [ #{topic => <<"test/toopic/1">>,
                             rules => [ #{topic => <<"test/toopic/1">>,
                                          permission => <<"allow">>,
                                          permission => <<"allow">>,
                                          action => <<"publish">>
                                          action => <<"publish">>
@@ -38,8 +37,7 @@
                                         }
                                         }
                                      ]
                                      ]
                            }).
                            }).
--define(EXAMPLE_CLIENTID, #{type => clientid,
-                            key => client1,
+-define(EXAMPLE_CLIENTID, #{clientid => client1,
                             rules => [ #{topic => <<"test/toopic/1">>,
                             rules => [ #{topic => <<"test/toopic/1">>,
                                          permission => <<"allow">>,
                                          permission => <<"allow">>,
                                          action => <<"publish">>
                                          action => <<"publish">>
@@ -54,8 +52,7 @@
                                         }
                                         }
                                      ]
                                      ]
                            }).
                            }).
--define(EXAMPLE_ALL ,     #{type => all,
-                            rules => [ #{topic => <<"test/toopic/1">>,
+-define(EXAMPLE_ALL ,     #{rules => [ #{topic => <<"test/toopic/1">>,
                                          permission => <<"allow">>,
                                          permission => <<"allow">>,
                                          action => <<"publish">>
                                          action => <<"publish">>
                                         }
                                         }
@@ -106,37 +103,39 @@ definitions() ->
             }
             }
         }
         }
     },
     },
-    Record = #{
-        oneOf => [ #{type => object,
-                     required => [username, rules],
-                     properties => #{
-                        username => #{
-                            type => string,
-                            example => <<"username">>
-                        },
-                        rules => minirest:ref(<<"rules">>)
-                     }
-                   }
-                 , #{type => object,
-                     required => [clientid, rules],
-                     properties => #{
-                        username => #{
-                            type => string,
-                            example => <<"clientid">>
-                        },
-                        rules => minirest:ref(<<"rules">>)
-                     }
-                   }
-                 , #{type => object,
-                     required => [rules],
-                     properties => #{
-                        rules => minirest:ref(<<"rules">>)
-                     }
-                   }
-                 ]
+    Username = #{
+        type => object,
+        required => [username, rules],
+        properties => #{
+           username => #{
+               type => string,
+               example => <<"username">>
+           },
+           rules => minirest:ref(<<"rules">>)
+        }
+    },
+    Clientid = #{
+        type => object,
+        required => [clientid, rules],
+        properties => #{
+           clientid => #{
+               type => string,
+               example => <<"clientid">>
+           },
+           rules => minirest:ref(<<"rules">>)
+        }
+    },
+    ALL = #{
+      type => object,
+      required => [rules],
+      properties => #{
+         rules => minirest:ref(<<"rules">>)
+      }
     },
     },
     [ #{<<"rules">> => Rules}
     [ #{<<"rules">> => Rules}
-    , #{<<"record">> => Record}
+    , #{<<"username">> => Username}
+    , #{<<"clientid">> => Clientid}
+    , #{<<"all">> => ALL}
     ].
     ].
 
 
 purge_api() ->
 purge_api() ->
@@ -187,7 +186,12 @@ records_api() ->
                         'application/json' => #{
                         'application/json' => #{
                             schema => #{
                             schema => #{
                                 type => array,
                                 type => array,
-                                items => minirest:ref(<<"record">>)
+                                items => #{
+                                  oneOf => [ minirest:ref(<<"username">>)
+                                           , minirest:ref(<<"clientid">>)
+                                           , minirest:ref(<<"all">>)
+                                           ]
+                                }
                             },
                             },
                             examples => #{
                             examples => #{
                                 username => #{
                                 username => #{
@@ -226,7 +230,11 @@ records_api() ->
                     'application/json' => #{
                     'application/json' => #{
                         schema => #{
                         schema => #{
                             type => array,
                             type => array,
-                            items => minirest:ref(<<"record">>)
+                            items => #{
+                                oneOf => [ minirest:ref(<<"username">>)
+                                         , minirest:ref(<<"clientid">>)
+                                         ]
+                            }
                         },
                         },
                         examples => #{
                         examples => #{
                             username => #{
                             username => #{
@@ -262,8 +270,24 @@ records_api() ->
             requestBody => #{
             requestBody => #{
                 content => #{
                 content => #{
                     'application/json' => #{
                     'application/json' => #{
-                        schema => minirest:ref(<<"record">>),
+                        schema => #{
+                            type => array,
+                            items => #{
+                              oneOf => [ minirest:ref(<<"username">>)
+                                       , minirest:ref(<<"clientid">>)
+                                       , minirest:ref(<<"all">>)
+                                       ]
+                            }
+                        },
                         examples => #{
                         examples => #{
+                            username => #{
+                                summary => <<"Username">>,
+                                value => jsx:encode(?EXAMPLE_USERNAME)
+                            },
+                            clientid => #{
+                                summary => <<"Clientid">>,
+                                value => jsx:encode(?EXAMPLE_CLIENTID)
+                            },
                             all => #{
                             all => #{
                                 summary => <<"All">>,
                                 summary => <<"All">>,
                                 value => jsx:encode(?EXAMPLE_ALL)
                                 value => jsx:encode(?EXAMPLE_ALL)
@@ -308,7 +332,11 @@ record_api() ->
                     description => <<"OK">>,
                     description => <<"OK">>,
                     content => #{
                     content => #{
                         'application/json' => #{
                         'application/json' => #{
-                            schema => minirest:ref(<<"record">>),
+                            schema => #{
+                                oneOf => [ minirest:ref(<<"username">>)
+                                         , minirest:ref(<<"clientid">>)
+                                         ]
+                            },
                             examples => #{
                             examples => #{
                                 username => #{
                                 username => #{
                                     summary => <<"Username">>,
                                     summary => <<"Username">>,
@@ -317,10 +345,6 @@ record_api() ->
                                 clientid => #{
                                 clientid => #{
                                     summary => <<"Clientid">>,
                                     summary => <<"Clientid">>,
                                     value => jsx:encode(?EXAMPLE_CLIENTID)
                                     value => jsx:encode(?EXAMPLE_CLIENTID)
-                                },
-                                all => #{
-                                    summary => <<"All">>,
-                                    value => jsx:encode(?EXAMPLE_ALL)
                                 }
                                 }
                             }
                             }
                         }
                         }
@@ -353,7 +377,11 @@ record_api() ->
             requestBody => #{
             requestBody => #{
                 content => #{
                 content => #{
                     'application/json' => #{
                     'application/json' => #{
-                        schema => minirest:ref(<<"record">>),
+                        schema => #{
+                            oneOf => [ minirest:ref(<<"username">>)
+                                     , minirest:ref(<<"clientid">>)
+                                     ]
+                        },
                         examples => #{
                         examples => #{
                             username => #{
                             username => #{
                                 summary => <<"Username">>,
                                 summary => <<"Username">>,

+ 17 - 0
apps/emqx_authz/src/emqx_authz_api_schema.erl

@@ -21,6 +21,7 @@
 definitions() ->
 definitions() ->
     Sources = #{
     Sources = #{
         oneOf => [ minirest:ref(<<"http">>)
         oneOf => [ minirest:ref(<<"http">>)
+                 , minirest:ref(<<"built-in-database">>)
                  , minirest:ref(<<"mongo_single">>)
                  , minirest:ref(<<"mongo_single">>)
                  , minirest:ref(<<"mongo_rs">>)
                  , minirest:ref(<<"mongo_rs">>)
                  , minirest:ref(<<"mongo_sharded">>)
                  , minirest:ref(<<"mongo_sharded">>)
@@ -446,6 +447,21 @@ definitions() ->
             ssl => minirest:ref(<<"ssl">>)
             ssl => minirest:ref(<<"ssl">>)
         }
         }
     },
     },
+    Mnesia = #{
+        type => object,
+        required => [type, enable],
+        properties => #{
+            type => #{
+                type => string,
+                enum => [<<"redis">>],
+                example => <<"redis">>
+            },
+            enable => #{
+                type => boolean,
+                example => true
+            }
+        }
+    },
     File = #{
     File = #{
         type => object,
         type => object,
         required => [type, enable, rules],
         required => [type, enable, rules],
@@ -475,6 +491,7 @@ definitions() ->
     [ #{<<"sources">> => Sources}
     [ #{<<"sources">> => Sources}
     , #{<<"ssl">> => SSL}
     , #{<<"ssl">> => SSL}
     , #{<<"http">> => HTTP}
     , #{<<"http">> => HTTP}
+    , #{<<"built-in-database">> => Mnesia}
     , #{<<"mongo_single">> => MongoSingle}
     , #{<<"mongo_single">> => MongoSingle}
     , #{<<"mongo_rs">> => MongoRs}
     , #{<<"mongo_rs">> => MongoRs}
     , #{<<"mongo_sharded">> => MongoSharded}
     , #{<<"mongo_sharded">> => MongoSharded}

+ 1 - 1
apps/emqx_authz/src/emqx_authz_api_sources.erl

@@ -405,7 +405,7 @@ get_raw_sources() ->
     RawSources = emqx:get_raw_config([authorization, sources]),
     RawSources = emqx:get_raw_config([authorization, sources]),
     Schema = #{roots => emqx_authz_schema:fields("authorization"), fields => #{}},
     Schema = #{roots => emqx_authz_schema:fields("authorization"), fields => #{}},
     Conf = #{<<"sources">> => RawSources},
     Conf = #{<<"sources">> => RawSources},
-    #{sources := Sources} = hocon_schema:check_plain(Schema, Conf, #{atom_key => true, no_conversion => true}),
+    #{sources := Sources} = hocon_schema:check_plain(Schema, Conf, #{atom_key => true, only_fill_defaults => true}),
     Sources.
     Sources.
 
 
 get_raw_source(Type) ->
 get_raw_source(Type) ->