Преглед изворни кода

refactor(emqx_license): test with pem format input

no need to perform der encode/decode for pub key
Zaiming (Stone) Shi пре 4 година
родитељ
комит
c8c7ad896e

+ 12 - 7
lib-ee/emqx_license/src/emqx_license_parser.erl

@@ -9,8 +9,12 @@
 -include_lib("emqx/include/logger.hrl").
 -include("emqx_license.hrl").
 
--define(PUBKEY, <<"MEgCQQChzN6lCUdt4sYPQmWBYA3b8Zk87Jfk+1A1zcTd+lCU0Tf
-                  vXhSHgEWz18No4lL2v1n+70CoYpc2fzfhNJitgnV9AgMBAAE=">>).
+-define(PUBKEY, <<"""
+-----BEGIN PUBLIC KEY-----
+MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKHM3qUJR23ixg9CZYFgDdvxmTzsl+T7
+UDXNxN36UJTRN+9eFIeARbPXw2jiUva/Wf7vQKhilzZ/N+E0mK2CdX0CAwEAAQ==
+-----END PUBLIC KEY-----
+""">>).
 
 -define(LICENSE_PARSE_MODULES, [emqx_license_parser_v20220101
                                ]).
@@ -62,10 +66,11 @@
 
 -spec parse(string() | binary()) -> {ok, license()} | {error, term()}.
 parse(Content) ->
-    DecodedKey = base64:decode(?PUBKEY),
-    parse(Content, DecodedKey).
+    parse(Content, ?PUBKEY).
 
-parse(Content, Key) ->
+parse(Content, Pem) ->
+    [PemEntry] = public_key:pem_decode(Pem),
+    Key = public_key:pem_entry_decode(PemEntry),
     do_parse(iolist_to_binary(Content), Key, ?LICENSE_PARSE_MODULES, []).
 
 -spec dump(license()) -> list({atom(), term()}).
@@ -102,6 +107,6 @@ do_parse(Content, Key, [Module | Modules], Errors) ->
         {error, Error} ->
             do_parse(Content, Key, Modules, [{Module, Error} | Errors])
     catch
-        _Class : Error ->
-            do_parse(Content, Key, Modules, [{Module, Error} | Errors])
+        _Class : Error : Stacktrace ->
+            do_parse(Content, Key, Modules, [{Module, {Error, Stacktrace}} | Errors])
     end.

+ 1 - 2
lib-ee/emqx_license/src/emqx_license_parser_v20220101.erl

@@ -81,8 +81,7 @@ do_parse(Content) ->
     end.
 
 verify_signature(Payload, Signature, Key) ->
-    RSAPublicKey = public_key:der_decode('RSAPublicKey', Key),
-    public_key:verify(Payload, ?DIGEST_TYPE, Signature, RSAPublicKey).
+    public_key:verify(Payload, ?DIGEST_TYPE, Signature, Key).
 
 parse_payload(Payload) ->
     Lines = lists:map(

+ 1 - 4
lib-ee/emqx_license/test/emqx_license_SUITE.erl

@@ -164,8 +164,5 @@ mk_license(Fields) ->
     EncodedLicense = emqx_license_test_lib:make_license(Fields),
     {ok, License} = emqx_license_parser:parse(
                       EncodedLicense,
-                      emqx_license_test_lib:public_key_encoded()),
+                      emqx_license_test_lib:public_key_pem()),
     License.
-
-public_key() -> <<"MEgCQQChzN6lCUdt4sYPQmWBYA3b8Zk87Jfk+1A1zcTd+lCU0Tf
-                  vXhSHgEWz18No4lL2v1n+70CoYpc2fzfhNJitgnV9AgMBAAE=">>.

+ 1 - 1
lib-ee/emqx_license/test/emqx_license_checker_SUITE.erl

@@ -198,7 +198,7 @@ mk_license(Fields) ->
     EncodedLicense = emqx_license_test_lib:make_license(Fields),
     {ok, License} = emqx_license_parser:parse(
                       EncodedLicense,
-                      emqx_license_test_lib:public_key_encoded()),
+                      emqx_license_test_lib:public_key_pem()),
     License.
 
 format_date({Year, Month, Day}) ->

+ 14 - 14
lib-ee/emqx_license/test/emqx_license_parser_SUITE.erl

@@ -40,7 +40,7 @@ set_special_configs(_) -> ok.
 %%------------------------------------------------------------------------------
 
 t_parse(_Config) ->
-    ?assertMatch({ok, _}, emqx_license_parser:parse(sample_license(), public_key_encoded())),
+    ?assertMatch({ok, _}, emqx_license_parser:parse(sample_license(), public_key_pem())),
 
     %% invalid version
     ?assertMatch(
@@ -57,7 +57,7 @@ t_parse(_Config) ->
             "100000",
             "10"
            ]),
-         public_key_encoded())),
+         public_key_pem())),
 
     %% invalid field number
     ?assertMatch(
@@ -74,7 +74,7 @@ t_parse(_Config) ->
             "100000",
             "10"
            ]),
-         public_key_encoded())),
+         public_key_pem())),
 
     ?assertMatch(
        {error,
@@ -94,7 +94,7 @@ t_parse(_Config) ->
             "-10",
             "10"
            ]),
-         public_key_encoded())),
+         public_key_pem())),
 
     %% invalid signature
     [LicensePart, _] = binary:split(
@@ -125,23 +125,23 @@ t_parse(_Config) ->
          [{emqx_license_parser_v20220101,invalid_signature}]},
        emqx_license_parser:parse(
          iolist_to_binary([LicensePart, <<".">>, SignaturePart]),
-         public_key_encoded())),
+         public_key_pem())),
 
     %% totally invalid strings as license
     ?assertMatch(
        {error, [_ | _]},
        emqx_license_parser:parse(
          <<"badlicense">>,
-         public_key_encoded())),
+         public_key_pem())),
 
     ?assertMatch(
        {error, [_ | _]},
        emqx_license_parser:parse(
          <<"bad.license">>,
-         public_key_encoded())).
+         public_key_pem())).
 
 t_dump(_Config) ->
-    {ok, License} = emqx_license_parser:parse(sample_license(), public_key_encoded()),
+    {ok, License} = emqx_license_parser:parse(sample_license(), public_key_pem()),
 
     ?assertEqual(
        [{customer,<<"Foo">>},
@@ -155,22 +155,22 @@ t_dump(_Config) ->
        emqx_license_parser:dump(License)).
 
 t_customer_type(_Config) ->
-    {ok, License} = emqx_license_parser:parse(sample_license(), public_key_encoded()),
+    {ok, License} = emqx_license_parser:parse(sample_license(), public_key_pem()),
 
     ?assertEqual(10, emqx_license_parser:customer_type(License)).
 
 t_license_type(_Config) ->
-    {ok, License} = emqx_license_parser:parse(sample_license(), public_key_encoded()),
+    {ok, License} = emqx_license_parser:parse(sample_license(), public_key_pem()),
 
     ?assertEqual(0, emqx_license_parser:license_type(License)).
 
 t_max_connections(_Config) ->
-    {ok, License} = emqx_license_parser:parse(sample_license(), public_key_encoded()),
+    {ok, License} = emqx_license_parser:parse(sample_license(), public_key_pem()),
 
     ?assertEqual(10, emqx_license_parser:max_connections(License)).
 
 t_expiry_date(_Config) ->
-    {ok, License} = emqx_license_parser:parse(sample_license(), public_key_encoded()),
+    {ok, License} = emqx_license_parser:parse(sample_license(), public_key_pem()),
 
     ?assertEqual({2295,10,27}, emqx_license_parser:expiry_date(License)).
 
@@ -178,8 +178,8 @@ t_expiry_date(_Config) ->
 %% Helpers
 %%------------------------------------------------------------------------------
 
-public_key_encoded() ->
-    emqx_license_test_lib:public_key_encoded().
+public_key_pem() ->
+    emqx_license_test_lib:public_key_pem().
 
 sample_license() ->
     emqx_license_test_lib:make_license(

+ 11 - 4
lib-ee/emqx_license/test/emqx_license_test_lib.erl

@@ -25,15 +25,22 @@ private_key() ->
 public_key() ->
     test_key("pub.pem").
 
-public_key_encoded() ->
-    public_key:der_encode('RSAPublicKey', public_key()).
+public_key_pem() ->
+    test_key("pub.pem", pem).
 
 test_key(Filename) ->
+    test_key(Filename, decoded).
+
+test_key(Filename, Format) ->
     Dir = code:lib_dir(emqx_license, test),
     Path = filename:join([Dir, "data", Filename]),
     {ok, KeyData} = file:read_file(Path),
-    [PemEntry] = public_key:pem_decode(KeyData),
-    public_key:pem_entry_decode(PemEntry).
+    case Format of
+        pem -> KeyData;
+        decoded ->
+            [PemEntry] = public_key:pem_decode(KeyData),
+            public_key:pem_entry_decode(PemEntry)
+    end.
 
 make_license(Values) ->
     Key = private_key(),