Explorar el Código

Merge pull request #13345 from thalesmg/20240626-r572-fix-validate-schema-reg-name

fix(schema registry api): validate schema name when creating
Thales Macedo Garitezi hace 1 año
padre
commit
2a9c27d206

+ 1 - 1
apps/emqx_schema_registry/src/emqx_schema_registry.app.src

@@ -1,6 +1,6 @@
 {application, emqx_schema_registry, [
     {description, "EMQX Schema Registry"},
-    {vsn, "0.3.1"},
+    {vsn, "0.3.2"},
     {registered, [emqx_schema_registry_sup]},
     {mod, {emqx_schema_registry_app, []}},
     {included_applications, [

+ 20 - 12
apps/emqx_schema_registry/src/emqx_schema_registry_http_api.erl

@@ -142,18 +142,26 @@ schema("/schema_registry/:name") ->
         ),
     ?OK(Response);
 '/schema_registry'(post, #{body := Params0 = #{<<"name">> := Name}}) ->
-    Params = maps:without([<<"name">>], Params0),
-    case emqx_schema_registry:get_schema(Name) of
-        {error, not_found} ->
-            case emqx_schema_registry:add_schema(Name, Params) of
-                ok ->
-                    {ok, Res} = emqx_schema_registry:get_schema(Name),
-                    {201, Res#{name => Name}};
-                {error, Error} ->
-                    ?BAD_REQUEST(Error)
-            end;
-        {ok, _} ->
-            ?BAD_REQUEST('ALREADY_EXISTS', <<"Schema already exists">>)
+    try
+        ok = emqx_resource:validate_name(Name),
+        Params = maps:without([<<"name">>], Params0),
+        case emqx_schema_registry:get_schema(Name) of
+            {error, not_found} ->
+                case emqx_schema_registry:add_schema(Name, Params) of
+                    ok ->
+                        {ok, Res} = emqx_schema_registry:get_schema(Name),
+                        {201, Res#{name => Name}};
+                    {error, Error} ->
+                        ?BAD_REQUEST(Error)
+                end;
+            {ok, _} ->
+                ?BAD_REQUEST('ALREADY_EXISTS', <<"Schema already exists">>)
+        end
+    catch
+        throw:#{kind := Kind, reason := Reason} ->
+            Msg0 = ?ERROR_MSG('BAD_REQUEST', Reason),
+            Msg = Msg0#{kind => Kind},
+            {400, Msg}
     end.
 
 '/schema_registry/:name'(get, #{bindings := #{name := Name}}) ->

+ 23 - 0
apps/emqx_schema_registry/test/emqx_schema_registry_http_api_SUITE.erl

@@ -329,3 +329,26 @@ t_crud(Config) ->
     ),
 
     ok.
+
+%% Tests that we can't create names that are too long and get a decent error message.
+t_name_too_long(Config) ->
+    SerdeType = ?config(serde_type, Config),
+    SourceBin = ?config(schema_source, Config),
+    SerdeTypeBin = atom_to_binary(SerdeType),
+    %% Too long!
+    SchemaName = binary:copy(<<"a">>, 256),
+    Params = #{
+        <<"type">> => SerdeTypeBin,
+        <<"source">> => SourceBin,
+        <<"name">> => SchemaName,
+        <<"description">> => <<"My schema">>
+    },
+    ?assertMatch(
+        {ok, 400, #{
+            <<"code">> := <<"BAD_REQUEST">>,
+            <<"kind">> := <<"validation_error">>,
+            <<"message">> := <<"Name length must be less than 255">>
+        }},
+        request({post, Params})
+    ),
+    ok.

+ 1 - 0
changes/ee/fix-13345.en.md

@@ -0,0 +1 @@
+Improved error message when creating a schema in Schema Registry whose name is too long or has invalid format.