mirror of
https://github.com/processone/ejabberd.git
synced 2024-12-24 17:29:28 +01:00
Support getting certificates for domains not specified in the configuration file
This commit is contained in:
parent
97a4d57f2e
commit
011b7ac3f2
@ -45,5 +45,5 @@
|
|||||||
%% Options
|
%% Options
|
||||||
-type account_opt() :: string().
|
-type account_opt() :: string().
|
||||||
-type verbose_opt() :: string().
|
-type verbose_opt() :: string().
|
||||||
|
-type domains_opt() :: string().
|
||||||
|
|
||||||
|
@ -1,14 +1,15 @@
|
|||||||
-module (ejabberd_acme).
|
-module (ejabberd_acme).
|
||||||
|
|
||||||
-export([%% Ejabberdctl Commands
|
-export([%% Ejabberdctl Commands
|
||||||
get_certificates/2,
|
get_certificates/3,
|
||||||
renew_certificates/1,
|
renew_certificates/1,
|
||||||
list_certificates/1,
|
list_certificates/1,
|
||||||
revoke_certificate/2,
|
revoke_certificate/2,
|
||||||
%% Command Options Validity
|
%% Command Options Validity
|
||||||
is_valid_account_opt/1,
|
is_valid_account_opt/1,
|
||||||
is_valid_verbose_opt/1,
|
is_valid_verbose_opt/1,
|
||||||
%% Misc
|
is_valid_domain_opt/1,
|
||||||
|
%% Key Related
|
||||||
generate_key/0,
|
generate_key/0,
|
||||||
to_public/1
|
to_public/1
|
||||||
]).
|
]).
|
||||||
@ -42,18 +43,27 @@ is_valid_verbose_opt("plain") -> true;
|
|||||||
is_valid_verbose_opt("verbose") -> true;
|
is_valid_verbose_opt("verbose") -> true;
|
||||||
is_valid_verbose_opt(_) -> false.
|
is_valid_verbose_opt(_) -> false.
|
||||||
|
|
||||||
|
%% TODO: Make this check more complicated
|
||||||
|
-spec is_valid_domain_opt(string()) -> boolean().
|
||||||
|
is_valid_domain_opt("all") -> true;
|
||||||
|
is_valid_domain_opt(DomainString) ->
|
||||||
|
case parse_domain_string(DomainString) of
|
||||||
|
[] ->
|
||||||
|
false;
|
||||||
|
SeparatedDomains ->
|
||||||
|
true
|
||||||
|
end.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
%%
|
%%
|
||||||
%% Get Certificate
|
%% Get Certificate
|
||||||
%%
|
%%
|
||||||
|
|
||||||
%% Needs a hell lot of cleaning
|
-spec get_certificates(url(), domains_opt(), account_opt()) -> string() | {'error', _}.
|
||||||
-spec get_certificates(url(), account_opt()) -> string() | {'error', _}.
|
get_certificates(CAUrl, Domains, NewAccountOpt) ->
|
||||||
get_certificates(CAUrl, NewAccountOpt) ->
|
|
||||||
try
|
try
|
||||||
?INFO_MSG("Persistent: ~p~n", [file:read_file_info(persistent_file())]),
|
get_certificates0(CAUrl, Domains, NewAccountOpt)
|
||||||
get_certificates0(CAUrl, NewAccountOpt)
|
|
||||||
catch
|
catch
|
||||||
throw:Throw ->
|
throw:Throw ->
|
||||||
Throw;
|
Throw;
|
||||||
@ -62,24 +72,30 @@ get_certificates(CAUrl, NewAccountOpt) ->
|
|||||||
{error, get_certificates}
|
{error, get_certificates}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec get_certificates0(url(), account_opt()) -> string().
|
-spec get_certificates0(url(), domains_opt(), account_opt()) -> string().
|
||||||
get_certificates0(CAUrl, "old-account") ->
|
get_certificates0(CAUrl, Domains, "old-account") ->
|
||||||
%% Get the current account
|
%% Get the current account
|
||||||
{ok, _AccId, PrivateKey} = ensure_account_exists(),
|
{ok, _AccId, PrivateKey} = ensure_account_exists(),
|
||||||
|
|
||||||
get_certificates1(CAUrl, PrivateKey);
|
get_certificates1(CAUrl, Domains, PrivateKey);
|
||||||
|
|
||||||
get_certificates0(CAUrl, "new-account") ->
|
get_certificates0(CAUrl, Domains, "new-account") ->
|
||||||
%% Create a new account and save it to disk
|
%% Create a new account and save it to disk
|
||||||
{ok, _Id, PrivateKey} = create_save_new_account(CAUrl),
|
{ok, _Id, PrivateKey} = create_save_new_account(CAUrl),
|
||||||
|
|
||||||
get_certificates1(CAUrl, PrivateKey).
|
get_certificates1(CAUrl, Domains, PrivateKey).
|
||||||
|
|
||||||
-spec get_certificates1(url(), jose_jwk:key()) -> string().
|
-spec get_certificates1(url(), domains_opt(), jose_jwk:key()) -> string().
|
||||||
get_certificates1(CAUrl, PrivateKey) ->
|
get_certificates1(CAUrl, "all", PrivateKey) ->
|
||||||
%% Read Config
|
|
||||||
Hosts = get_config_hosts(),
|
Hosts = get_config_hosts(),
|
||||||
|
get_certificates2(CAUrl, PrivateKey, Hosts);
|
||||||
|
get_certificates1(CAUrl, DomainString, PrivateKey) ->
|
||||||
|
Domains = parse_domain_string(DomainString),
|
||||||
|
Hosts = [list_to_bitstring(D) || D <- Domains],
|
||||||
|
get_certificates2(CAUrl, PrivateKey, Hosts).
|
||||||
|
|
||||||
|
-spec get_certificates2(url(), jose_jwk:key(), [bitstring()]) -> string().
|
||||||
|
get_certificates2(CAUrl, PrivateKey, Hosts) ->
|
||||||
%% Get a certificate for each host
|
%% Get a certificate for each host
|
||||||
PemCertKeys = [get_certificate(CAUrl, Host, PrivateKey) || Host <- Hosts],
|
PemCertKeys = [get_certificate(CAUrl, Host, PrivateKey) || Host <- Hosts],
|
||||||
|
|
||||||
@ -87,7 +103,6 @@ get_certificates1(CAUrl, PrivateKey) ->
|
|||||||
SavedCerts = [save_certificate(Cert) || Cert <- PemCertKeys],
|
SavedCerts = [save_certificate(Cert) || Cert <- PemCertKeys],
|
||||||
|
|
||||||
%% Format the result to send back to ejabberdctl
|
%% Format the result to send back to ejabberdctl
|
||||||
%% Result
|
|
||||||
format_get_certificates_result(SavedCerts).
|
format_get_certificates_result(SavedCerts).
|
||||||
|
|
||||||
-spec format_get_certificates_result([{'ok', bitstring(), _} |
|
-spec format_get_certificates_result([{'ok', bitstring(), _} |
|
||||||
@ -704,6 +719,9 @@ utc_string_to_datetime(UtcString) ->
|
|||||||
throw({error, utc_string_to_datetime})
|
throw({error, utc_string_to_datetime})
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
parse_domain_string(DomainString) ->
|
||||||
|
string:tokens(DomainString, ";").
|
||||||
|
|
||||||
-spec is_error(_) -> boolean().
|
-spec is_error(_) -> boolean().
|
||||||
is_error({error, _}) -> true;
|
is_error({error, _}) -> true;
|
||||||
is_error({error, _, _}) -> true;
|
is_error({error, _, _}) -> true;
|
||||||
|
@ -45,7 +45,7 @@
|
|||||||
%% Migration jabberd1.4
|
%% Migration jabberd1.4
|
||||||
import_file/1, import_dir/1,
|
import_file/1, import_dir/1,
|
||||||
%% Acme
|
%% Acme
|
||||||
get_certificate/1,
|
get_certificate/2,
|
||||||
renew_certificate/0,
|
renew_certificate/0,
|
||||||
list_certificates/1,
|
list_certificates/1,
|
||||||
revoke_certificate/1,
|
revoke_certificate/1,
|
||||||
@ -248,11 +248,13 @@ get_commands_spec() ->
|
|||||||
args = [{file, string}],
|
args = [{file, string}],
|
||||||
result = {res, restuple}},
|
result = {res, restuple}},
|
||||||
#ejabberd_commands{name = get_certificate, tags = [acme],
|
#ejabberd_commands{name = get_certificate, tags = [acme],
|
||||||
desc = "Gets a certificate for the specified domain. Can be used with {old-account|new-account}.",
|
desc = "Gets a certificate for all or the specified domains {all|domain1;domain2;...}. Can be used with {old-account|new-account}.",
|
||||||
module = ?MODULE, function = get_certificate,
|
module = ?MODULE, function = get_certificate,
|
||||||
args_desc = ["Whether to create a new account or use the existing one"],
|
args_desc = ["Domains for which to acquire a certificate",
|
||||||
args_example = ["old-account | new-account"],
|
"Whether to create a new account or use the existing one"],
|
||||||
args = [{option, string}],
|
args_example = ["all | www.example.com;www.example1.net",
|
||||||
|
"old-account | new-account"],
|
||||||
|
args = [{domains, string}, {option, string}],
|
||||||
result = {certificates, string}},
|
result = {certificates, string}},
|
||||||
#ejabberd_commands{name = renew_certificate, tags = [acme],
|
#ejabberd_commands{name = renew_certificate, tags = [acme],
|
||||||
desc = "Renews all certificates that are close to expiring",
|
desc = "Renews all certificates that are close to expiring",
|
||||||
@ -575,13 +577,17 @@ import_dir(Path) ->
|
|||||||
%%% Acme
|
%%% Acme
|
||||||
%%%
|
%%%
|
||||||
|
|
||||||
get_certificate(UseNewAccount) ->
|
get_certificate(Domains, UseNewAccount) ->
|
||||||
case ejabberd_acme:is_valid_account_opt(UseNewAccount) of
|
case ejabberd_acme:is_valid_domain_opt(Domains) of
|
||||||
true ->
|
true ->
|
||||||
ejabberd_acme:get_certificates("http://localhost:4000", UseNewAccount);
|
case ejabberd_acme:is_valid_account_opt(UseNewAccount) of
|
||||||
|
true ->
|
||||||
|
ejabberd_acme:get_certificates("http://localhost:4000", Domains, UseNewAccount);
|
||||||
|
false ->
|
||||||
|
io_lib:format("Invalid account option: ~p", [UseNewAccount])
|
||||||
|
end;
|
||||||
false ->
|
false ->
|
||||||
String = io_lib:format("Invalid account option: ~p", [UseNewAccount]),
|
String = io_lib:format("Invalid domains: ~p", [Domains])
|
||||||
{invalid_option, String}
|
|
||||||
end.
|
end.
|
||||||
|
|
||||||
renew_certificate() ->
|
renew_certificate() ->
|
||||||
|
Loading…
Reference in New Issue
Block a user