refactor(emqx_license): test with pem format input

no need to perform der encode/decode for pub key
This commit is contained in:
Zaiming (Stone) Shi 2022-02-15 10:39:30 +01:00
parent 0f09e7bbd2
commit c8c7ad896e
6 changed files with 40 additions and 32 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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