Skip to content

Commit

Permalink
Added code for FQDN masking.
Browse files Browse the repository at this point in the history
Signed-off-by: sreepuramsudheer <ssudheer@progress.com>
  • Loading branch information
sreepuramsudheer committed Aug 19, 2024
1 parent c0f19b7 commit fcc837e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 10 deletions.
48 changes: 42 additions & 6 deletions src/oc_erchef/apps/chef_telemetry/src/chef_telemetry_worker.erl
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@
%% 1) I don't have the server URL.
%% 2) easy for testing.
%% should be changed to actual server URL ASAP.
-define(DEFAULT_REPORTING_URL, "http://127.0.0.1:9001").
-define(DEFAULT_REPORTING_TIME, {12, 00}).
-define(DEFAULT_REPORTING_URL, "https://services.chef.io/usage/v1/payload").
-define(DEFAULT_REPORTING_TIME, {4, 00}).
-define(DEFAULT_IBROWSE_OPTIONS, []).

-define(WINDOW_SECONDS, 300).
Expand All @@ -66,7 +66,7 @@ start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).

init(_Config) ->
ReportingUrl = envy:get(chef_telemetry, reporting_url, ?DEFAULT_REPORTING_URL, string),
ReportingUrl = envy:get(chef_telemetry, reporting_time, ?DEFAULT_REPORTING_TIME, Fun),
Fun = fun({Hour, Min}) ->
Hour >= 0 andalso Hour < 24 andalso Min >= 0 andalso Min < 60
end,
Expand All @@ -88,7 +88,7 @@ handle_call(_Message, _From, State) ->

handle_cast(send_data, State) ->
State6 =
case chef_telemetry:is_enabled() of
try chef_telemetry:is_enabled() of
true ->
State1 = init_req(State),
insert_fqdn(State1),
Expand All @@ -106,6 +106,9 @@ handle_cast(send_data, State) ->
end;
_ ->
State
catch
_:_ ->
State
end,
gen_server:cast(self(), init_timer),
{noreply, State6};
Expand Down Expand Up @@ -158,7 +161,8 @@ get_api_fqdn(State) ->
case sqerl:execute(<<"select property from telemetry where property like 'FQDN:%'">>) of
{ok, Rows} when is_list(Rows) ->
FQDNs = [binary:part(FQDN, 5, size(FQDN) -5) || [{<<"property">>, FQDN}] <- Rows],
State#state{fqdns = FQDNs};
FQDNs1 = mask(FQDNs),
State#state{fqdns = FQDNs1};
_ ->
State
end.
Expand Down Expand Up @@ -319,7 +323,7 @@ to_binary(String) when is_list(String) ->
list_to_binary(String);

to_binary(Element) ->
throw({not_a_binary_or_string, Element}).
throw({not_a_string, Element}).

epoch_to_string(Epoch) ->
calendar:system_time_to_rfc3339(Epoch, [{offset, "Z"}]).
Expand Down Expand Up @@ -377,3 +381,35 @@ insert_fqdn(State) ->
HostName2 = to_binary("FQDN:" ++ HostName1),
sqerl:adhoc_delete(<<"telemetry">>, {<<"property">>, equals, HostName2}),
sqerl:adhoc_insert(<<"telemetry">>, [[{<<"property">>, HostName2}, {<<"event_timestamp">>, Now}, {<<"value_string">>, <<"">>}]]).

mask(FQDNs) ->
Fun = fun(FQDN) ->
case re:run(FQDN,
<<"(?:(.*?):\/\/?)?\/?(?:[^\/\.]+\.)*?([^\/\.]+)\.?([^\/:]*)(?::([^?\/]*)?)?(.*)?">>,
[{capture, all_but_first, binary}]) of
{match, Parts} ->
[Protocall, SubDomain, Domain, Rest1, Rest2] = Parts,
Hash = crypto:hash(md5, SubDomain),
Hash1 = base64:encode(Hash),
Len = binary:longest_common_suffix([Hash1, <<"===">>]),
Hash2 = binary:part(Hash1, {0, size(Hash1) - Len}),
Res1 =
case Protocall /= <<"">> of
true ->
<<Protocall/binary, "://", Hash2/binary, ".", Domain/binary>>;
false ->
<<Hash2/binary, ".", Domain/binary>>
end,
Res2 =
case Rest1 /= <<"">> of
true ->
<<Res1/binary, ":", Rest1/binary, Rest2/binary>>;
_ ->
<<Res1/binary, Rest2/binary>>
end,
Res2;
_ ->
<<"">>
end
end,
lists:map(Fun, FQDNs).
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,19 @@
fqdn = []}).

feild_value_test() ->
State = #state{fqdn_select = {ok, []},
State = #state{fqdn_select = {ok, [{<<"property">>, <<"FQDN:node1.domain1.com">>},
{<<"property">>, <<"FQDN:node2.subdomain2.domain2.com">>},
{<<"property">>, <<"FQDN:node3.subdomain3.domain3.co.uk">>}]},
last_send_timestamp = {{2024, 8, 7}, {0, 0, 1}},
user_emails = [[{<<"email">>, <<"test@testorg.com">>}]],
nodes_count = 10
},
Expected = #expected{company_name = <<"testorg">>,
nodes_count = 10,
active_nodes = 4},
active_nodes = 4,
fqdn = [<<".*\.domain1.com$">>,
<<".*\.subdomain2\.domain2\.com$">>,
<<".*\.subdomain3\.domain3\.co\.uk$">>]},
execute(State, Expected, []).

enable_flag_test() ->
Expand Down Expand Up @@ -145,10 +150,22 @@ validate(Req, Expected) ->
Licence = ej:get({<<"licenseId">>}, Req),
TotalNodes = ej:get({<<"periods">>, 1, <<"summary">>, <<"nodes">>, <<"total">>}, Req),
ActiveNodes = ej:get({<<"periods">>, 1, <<"summary">>, <<"nodes">>, <<"active">>}, Req),
?assertEqual(<<"Infra-Server-license-Id">>, Licence),
FQDNs = ej:get({<<"metadata">>, <<"Infra Server">>, <<"fqdn">>}, Req),
?assertEqual(<<"Infra-Server-license-Id">>, Licence),
?assertEqual(Expected#expected.nodes_count, TotalNodes),
?assertEqual(Expected#expected.active_nodes, ActiveNodes).
?assertEqual(Expected#expected.active_nodes, ActiveNodes),
?assertEqual(true, check_fqdn(FQDNs, Expected#expected.fqdn)).

set_env(ConfigList) ->
ConfigList1 = ?DEFAULT_CONFIG ++ ConfigList,
[ application:set_env(App, Parameter, Value) || {App, Parameter, Value} <- ConfigList1 ].

check_fqdn(ReqFQDNs, Expected) ->
MatchFun =
fun(FQDN) ->
lists:any(
fun(Pattern) ->
match == re:run(FQDN, Pattern, [{capture, none}])
end, Expected)
end,
lists:all(MatchFun, ReqFQDNs).

0 comments on commit fcc837e

Please sign in to comment.