Skip to content

Commit

Permalink
License Caching and Add license enforcement to all APIs (#3919)
Browse files Browse the repository at this point in the history
License Caching and Add license enforcement to all APIs.

Signed-off-by: jan shahid shaik <[email protected]>
  • Loading branch information
jashaik authored Oct 9, 2024
1 parent 3bbe009 commit 607e4ca
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 22 deletions.
48 changes: 37 additions & 11 deletions src/oc_erchef/apps/chef_license/src/chef_license_worker.erl
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@

-record(state, {
scanned_time,
grace_period,
license_type,
license_cache,
grace_period,
expiration_date,
message
}).

Expand Down Expand Up @@ -51,9 +53,10 @@ init(_Config) ->
erlang:send_after(?DEFAULT_LICENSE_SCAN_INTERVAL, self(), check_license),
{ok, State}.

handle_call(get_license, _From, #state{license_cache = Lic, grace_period = GracePeriod, message = Msg} = State) ->
{reply,{Lic, GracePeriod, Msg}, State};

handle_call(get_license, _From, #state{license_cache = undefined, license_type=Type, expiration_date=ExpDate, grace_period = GracePeriod, message = Msg}=State) ->
{reply, {valid_license, Type, GracePeriod, ExpDate, Msg}, State};
handle_call(get_license, _From, #state{license_cache = Lic, license_type=Type, expiration_date=ExpDate, grace_period = GracePeriod, message = Msg} = State) ->
{reply,{Lic, Type, GracePeriod, ExpDate, Msg}, State};
handle_call(_Message, _From, State) ->
{noreply, State}.

Expand Down Expand Up @@ -84,9 +87,14 @@ check_license(State) ->
{'EXIT', _} -> <<"">>
end,
case process_license(JsonStr) of
{ok, valid_license} -> State#state{license_cache=valid, grace_period=undefined, scanned_time = erlang:timestamp()};
{ok, expired} -> State#state{license_cache=expired, grace_period=undefined, scanned_time = erlang:timestamp()};
{ok, valid_license, grace_period} -> State#state{license_cache=valid, grace_period=true, scanned_time = erlang:timestamp()};
{ok, valid_license, ExpDate} ->
State#state{license_cache=valid_license, grace_period=undefined, scanned_time = erlang:timestamp(), expiration_date=ExpDate};
{ok, commercial_expired, ExpDate, Msg} ->
State#state{license_cache=commercial_expired, license_type = <<"commercial">>, grace_period=undefined, scanned_time = erlang:timestamp(), expiration_date=ExpDate, message=Msg};
{ok, commercial_grace_period, ExpDate, Msg} ->
State#state{license_cache=commercial_grace_period, grace_period=true, scanned_time = erlang:timestamp(), expiration_date=ExpDate, message=Msg};
{ok, trial_expired, ExpDate, Msg} ->
State#state{license_cache=trial_expired_expired, license_type = <<"trial">>, grace_period=undefined, scanned_time = erlang:timestamp(), expiration_date=ExpDate, message=Msg};
{error, _} -> State
end.

Expand All @@ -103,21 +111,23 @@ process_license(LicJson) ->
{LicDetails} ->
case ej:get({<<"expiration_date">>}, LicDetails) of
{[{<<"seconds">>,ExpireInSeconds}]} ->
ExpDate = sec_to_date(ExpireInSeconds),
case os:system_time(second) < ExpireInSeconds of
true -> {ok, valid_license};
true -> {ok, valid_license, ExpDate};
_ ->
case ej:get({<<"license_type">>}, LicDetails) of
<<"commercial">> ->
case ej:get({<<"grace_period">>}, LicDetails) of
true ->
%% WARNING
{ok, valid_license, grace_period};
{ok, commercial_grace_period, ExpDate,
get_alert_message(commercial_grace_period, ExpDate)};
_ ->
%% MSG
{ ok, expired}
{ ok, commercial_expired, ExpDate, get_alert_message(commercial_expired, ExpDate)}
end;
_ ->
{ok, expired}
{ok, trial_expired, ExpDate, get_alert_message(trial_expired, ExpDate)}
end
end;
_ ->
Expand All @@ -127,6 +137,22 @@ process_license(LicJson) ->
{error, invalid_response}
end.

get_alert_message(Type, ExpDate)->
case Type of
trial_expired ->
"Your Progress Chef InfraServer license has expired or does not exist! You no longer have access to Chef Automate. Please contact the Account Team to upgrade to an Enterprise License.";
commercial_expired ->
"Your Progress Chef InfraServer license expired on " ++ ExpDate ++ " and you no longer have access to Chef Automate! To get a new license, please contact the Account Team or email us at [email protected]";
commercial_grace_period ->
"Your Progress Chef InfraServer license expired on " ++ ExpDate ++ " and you are currently on a limited extension period! To get a new license, please contact the Account Team or email us at [email protected]"
end.

sec_to_date(Seconds)->
BaseDate = calendar:datetime_to_gregorian_seconds({{1970,1,1},{0,0,0}}),
Seconds1 = BaseDate + Seconds,
{ {Year,Month,Day},_Time} = calendar:gregorian_seconds_to_datetime(Seconds1),
lists:flatten(io_lib:format("~4..0w-~2..0w-~2..0w",[Year,Month,Day])).

%%% =============================
%%% Sample license response
%%% =============================
Expand Down
20 changes: 10 additions & 10 deletions src/oc_erchef/apps/chef_license/test/chef_license_worker_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -28,36 +28,36 @@ license_test()->
file:write_file(?DEFAULT_FILE_PATH,get_commercial_license()),
refresh_license(),
timer:sleep(100),
Result = chef_license_worker:get_license(),
?assertEqual({valid,undefined, undefined}, Result),
{Result, _, _, _, _} = chef_license_worker:get_license(),
?assertEqual(valid_license, Result),
os:cmd("rm -rf " ++ ?DEFAULT_FILE_PATH),

file:write_file(?DEFAULT_FILE_PATH,get_commercial_license_expired()),
refresh_license(),
timer:sleep(100),
Result1 = chef_license_worker:get_license(),
?assertEqual({expired,undefined, undefined}, Result1),
{Result1, _, _, _, _} = chef_license_worker:get_license(),
?assertEqual(commercial_expired, Result1),
os:cmd("rm -rf " ++ ?DEFAULT_FILE_PATH),

file:write_file(?DEFAULT_FILE_PATH,get_commercial_grace_license()),
refresh_license(),
timer:sleep(100),
Result2 = chef_license_worker:get_license(),
?assertEqual({valid,true,undefined}, Result2),
{Result2, _, _, _, _} = chef_license_worker:get_license(),
?assertEqual(commercial_grace_period, Result2),
os:cmd("rm -rf " ++ ?DEFAULT_FILE_PATH),

file:write_file(?DEFAULT_FILE_PATH,get_trail_license()),
refresh_license(),
timer:sleep(100),
Result3 = chef_license_worker:get_license(),
?assertEqual({valid, undefined, undefined}, Result3),
{Result3, _, _, _, _} = chef_license_worker:get_license(),
?assertEqual(valid_license, Result3),
os:cmd("rm -rf " ++ ?DEFAULT_FILE_PATH),

file:write_file(?DEFAULT_FILE_PATH,get_trail_license_expired()),
refresh_license(),
timer:sleep(100),
Result4 = chef_license_worker:get_license(),
?assertEqual({expired, undefined, undefined}, Result4),
{Result4,_, _, _, _ } = chef_license_worker:get_license(),
?assertEqual(trial_expired_expired, Result4),
os:cmd("rm -rf " ++ ?DEFAULT_FILE_PATH).

refresh_license()->
Expand Down
24 changes: 23 additions & 1 deletion src/oc_erchef/apps/oc_chef_wm/src/oc_chef_wm_base.erl
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,29 @@ service_available(Req, #base_state{reqid_header_name = HeaderName} = State) ->
State1 = State0#base_state{server_api_version = Version},
State2 = set_req_contexts(Req, State1),
State3 = maybe_with_default_org(Req, State2),
{true, Req, State3}
case wrq:path(Req) of
"/_status" ->
{true, Req, State3};
_ ->
case chef_license_worker:get_license() of
{valid_license, _, _, _, _} ->
{true, Req, State3};
{commercial_grace_period, _, _, ExpDate, LicWarnMsg} ->
XOps = binary_to_list(chef_json:encode({[{<<"licenseType">>, <<"commercial">>},{<<"expirationDateTime">>, list_to_binary(ExpDate)},
{<<"warningMessage">>, list_to_binary(LicWarnMsg)}]})),
Req1 = wrq:set_resp_header("X-Ops-License", XOps, Req),
{true, Req1, State3};
{_, Type, _, ExpDate, LicWarnMsg} ->
XOps = binary_to_list(chef_json:encode({[{<<"licenseType">>, Type},{<<"expirationDateTime">>, list_to_binary(ExpDate)},
{<<"warningMessage">>, list_to_binary(LicWarnMsg)}]})),
Req1 = wrq:set_resp_header("X-Ops-License", XOps, Req),
Req2 = chef_wm_util:set_json_body(Req1, {[{<<"error">>, <<"invalid-license">>},
{<<"message">>, list_to_binary(LicWarnMsg)}]}),
{{halt, 402}, Req2, State#base_state{log_msg = invalid_license}};
_ ->
{true, Req, State3}
end
end
end.

%% @doc Validate the requested X-Ops-Server-API-Version value and populate base_state,
Expand Down

0 comments on commit 607e4ca

Please sign in to comment.