<commit_before>-module(ns_server_cert).

-include("ns_common.hrl").

-export([cluster_cert_and_pkey_pem/0,
         generate_cert_and_pkey/0,
         validate_cert/1,
         generate_and_set_cert_and_pkey/0]).

cluster_cert_and_pkey_pem() ->
    RV = ns_config:run_txn(
           fun (Config, SetFn) ->
                   case ns_config:search(Config, cert_and_pkey) of
                       {value, Pair} ->
                           {abort, Pair};
                       false ->
                           CP = generate_cert_and_pkey(),
                           {commit, SetFn(cert_and_pkey, CP, Config)}
                   end
           end),
    case RV of
        {abort, Pair} ->
            Pair;
        _ ->
            {value, Pair} = ns_config:search(cert_and_pkey),
            Pair
    end.

generate_and_set_cert_and_pkey() ->
    Pair = generate_cert_and_pkey(),
    ns_config:set(cert_and_pkey, Pair).

generate_cert_and_pkey() ->
    StartTS = os:timestamp(),
    RV = do_generate_cert_and_pkey(),
    EndTS = os:timestamp(),

    Diff = timer:now_diff(EndTS, StartTS),
    ?log_debug("Generated certificate and private key in ~p us", [Diff]),

    RV.

do_generate_cert_and_pkey() ->
    {Status, Output} = misc:run_external_tool(path_config:component_path(bin, "generate_cert"), []),
    case Status of
        0 ->
            extract_cert_and_pkey(Output);
        _ ->
            erlang:exit({bad_generate_cert_exit, Status, Output})
    end.


validate_cert(CertPemBin) ->
    PemEntries = public_key:pem_decode(CertPemBin),<commit_msg>MB-9834 Move certificate generation out of config transaction.<commit_after>-module(ns_server_cert).

-include("ns_common.hrl").

-export([cluster_cert_and_pkey_pem/0,
         generate_cert_and_pkey/0,
         validate_cert/1,
         generate_and_set_cert_and_pkey/0]).

cluster_cert_and_pkey_pem() ->
    case ns_config:search(cert_and_pkey) of
        {value, Pair} ->
            Pair;
        false ->
            Pair = generate_cert_and_pkey(),
            RV = ns_config:run_txn(
                   fun (Config, SetFn) ->
                           case ns_config:search(Config, cert_and_pkey) of
                               {value, OtherPair} ->
                                   {abort, OtherPair};
                               false ->
                                   {commit, SetFn(cert_and_pkey, Pair, Config)}
                           end
                   end),

            case RV of
                {abort, OtherPair} ->
                    OtherPair;
                _ ->
                    Pair
            end
    end.

generate_and_set_cert_and_pkey() ->
    Pair = generate_cert_and_pkey(),
    ns_config:set(cert_and_pkey, Pair).

generate_cert_and_pkey() ->
    StartTS = os:timestamp(),
    RV = do_generate_cert_and_pkey(),
    EndTS = os:timestamp(),

    Diff = timer:now_diff(EndTS, StartTS),
    ?log_debug("Generated certificate and private key in ~p us", [Diff]),

    RV.

do_generate_cert_and_pkey() ->
    {Status, Output} = misc:run_external_tool(path_config:component_path(bin, "generate_cert"), []),
    case Status of
        0 ->
            extract_cert_and_pkey(Output);
        _ ->
            erlang:exit({bad_generate_cert_exit, Status, Output})
    end.


validate_cert(CertPemBin) ->
    PemEntries = public_key:pem_decode(CertPemBin),
{'fraction_non_alphanumeric': 0.10652818991097923, 'fraction_numerical': 0.004154302670623145, 'mean_word_length': 1.8910806174957118, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4001464', 'n_tokens_mistral': 1103, 'n_tokens_neox': 1058, 'n_words': 175}