Merge pull request #7033 from zmstone/feat-emqx-license-allow-comma-in-numbers

feat(emqx_license): allow comma in integer numbers in license body
This commit is contained in:
Zaiming (Stone) Shi 2022-02-16 17:31:34 +01:00 committed by GitHub
commit ea1bf58a2d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 57 additions and 18 deletions

View File

@ -12,6 +12,12 @@
-define(DIGEST_TYPE, sha256). -define(DIGEST_TYPE, sha256).
-define(LICENSE_VERSION, <<"220111">>). -define(LICENSE_VERSION, <<"220111">>).
%% This is the earliest license start date for version 220111
%% in theory it should be the same as ?LICENSE_VERSION (20220111),
%% however, in order to make expiration test easier (before Mar.2022),
%% allow it to start from Nov.2021
-define(MIN_START_DATE, 20211101).
-export([parse/2, -export([parse/2,
dump/1, dump/1,
customer_type/1, customer_type/1,
@ -103,35 +109,48 @@ parse_payload(Payload) ->
end. end.
parse_type(TypeStr) -> parse_type(TypeStr) ->
case string:to_integer(TypeStr) of case parse_int(TypeStr) of
{Type, <<"">>} -> {ok, Type}; {ok, Type} -> {ok, Type};
_ -> {error, invalid_license_type} _ -> {error, invalid_license_type}
end. end.
parse_customer_type(CTypeStr) -> parse_customer_type(CTypeStr) ->
case string:to_integer(CTypeStr) of case parse_int(CTypeStr) of
{CType, <<"">>} -> {ok, CType}; {ok, CType} -> {ok, CType};
_ -> {error, invalid_customer_type} _ -> {error, invalid_customer_type}
end. end.
parse_date_start(<<Y:4/binary, M:2/binary, D:2/binary>>) -> parse_date_start(DateStr) ->
Date = list_to_tuple([N || {N, <<>>} <- [string:to_integer(S) || S <- [Y, M, D]]]), case parse_int(DateStr) of
case calendar:valid_date(Date) of {ok, Num} when Num >= ?MIN_START_DATE ->
true -> {ok, Date}; Y = Num div 1_00_00,
false -> {error, invalid_date} M = (Num rem 1_00_00) div 1_00,
end; D = Num rem 1_00,
parse_date_start(_) -> {error, invalid_date}. case calendar:valid_date({Y, M, D}) of
true -> {ok, {Y, M, D}};
false -> {error, invalid_date}
end;
_ ->
{error, invalid_date}
end.
parse_days(DaysStr) -> parse_days(DaysStr) ->
case string:to_integer(DaysStr) of case parse_int(DaysStr) of
{Days, <<"">>} when Days > 0 -> {ok, Days}; {ok, Days} when Days > 0 -> {ok, Days};
_ -> {error, invalid_int_value} _ -> {error, invalid_int_value}
end. end.
parse_max_connections(MaxConnStr) -> parse_max_connections(MaxConnStr) ->
case string:to_integer(MaxConnStr) of case parse_int(MaxConnStr) of
{MaxConns, <<"">>} when MaxConns > 0 -> {ok, MaxConns}; {ok, MaxConns} when MaxConns > 0 -> {ok, MaxConns};
_ -> {error, invalid_int_value} _ -> {error, invalid_connection_limit}
end.
parse_int(Str0) ->
Str = iolist_to_binary(string:replace(Str0, ",", "")),
case string:to_integer(Str) of
{Num, <<"">>} -> {ok, Num};
_ -> error
end. end.
collect_fields(Fields) -> collect_fields(Fields) ->

View File

@ -141,7 +141,7 @@ t_check_expired(_Config) ->
"0", %% Small customer "0", %% Small customer
"Foo", "Foo",
"contact@foo.com", "contact@foo.com",
"20210101", %% Expired long ago "20211101", %% Expired long ago
"10", "10",
"10"]), "10"]),
#{} = emqx_license_checker:update(License), #{} = emqx_license_checker:update(License),

View File

@ -96,6 +96,26 @@ t_parse(_Config) ->
]), ]),
public_key_pem())), public_key_pem())),
?assertMatch(
{error,
[{emqx_license_parser_v20220101,
[{type,invalid_license_type},
{customer_type,invalid_customer_type},
{date_start,invalid_date},
{days,invalid_int_value}]}]},
emqx_license_parser:parse(
emqx_license_test_lib:make_license(
["220111",
"zero",
"ten",
"Foo",
"contact@foo.com",
"2022-02-1st",
"-10",
"10"
]),
public_key_pem())),
%% invalid signature %% invalid signature
[LicensePart, _] = binary:split( [LicensePart, _] = binary:split(
emqx_license_test_lib:make_license( emqx_license_test_lib:make_license(
@ -189,5 +209,5 @@ sample_license() ->
"Foo", "Foo",
"contact@foo.com", "contact@foo.com",
"20220111", "20220111",
"100000", "100,000",
"10"]). "10"]).