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("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.

View File

@ -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(

View File

@ -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=">>.

View File

@ -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}) ->

View File

@ -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(

View File

@ -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),
case Format of
pem -> KeyData;
decoded ->
[PemEntry] = public_key:pem_decode(KeyData),
public_key:pem_entry_decode(PemEntry).
public_key:pem_entry_decode(PemEntry)
end.
make_license(Values) ->
Key = private_key(),