summaryrefslogtreecommitdiff
path: root/src/rebar_utils.erl
diff options
context:
space:
mode:
authorTristan Sloughter <t@crashfast.com>2018-09-13 19:36:00 -0600
committerGitHub <noreply@github.com>2018-09-13 19:36:00 -0600
commit56b7d88975aa8da6446857cfd92de0825024bf63 (patch)
tree7e5b5de9b64b4b709775925c935bd20e4576457d /src/rebar_utils.erl
parent5e678754d7951a1700e2fcca4e1eb91ecc862e1a (diff)
support for hex v2, multiple repository fetching, private organizations (#1884)
* update to hex_core for hex-v2 repo support (#1865) * update to hex_core for hex-v2 repo support This patch adds only single repo hex-v2 support through hex_core. Packages no longer filtered out by buildtool metadata and the package index is updated per-package instead of fetched as one large ets dump. * tell travis to also build hex_core branch * support list of repos for hex packages (#1866) * support list of repos for hex packages repos are defined under the hex key in rebar configs. They can be defined at the top level of a project or globally, but not in profiles and the repos configured in dependencies are also ignored. Searching for packages involves first checking for a match in the local repo index cache, in the order repos are defined. If not found each repo is checked through the hex api for any known versions of the package and the first repo with a version that fits the constraint is used. * add {repos, replace, []} for overriding the global & default repos * add hex auth handling for repos (#1874) auth token are kept in a hex.config file that is modified by the rebar3 hex plugin. Repo names that have a : separating a parent and child are considered organizations. The parent repo's auth will be included with the child. So an organization named hexpm:rebar3_test will include any hexpm auth tokens found in the rebar3_test organization's configuration. * move packages to top level of of hexpm cache dir (#1876) * move packages to top level of of hexpm cache dir * append organization name to parent's repo_url when parsing repos * only eval config scripts and apply overrides once per app (#1879) * only eval config scripts and apply overrides once per app * move new resource behaviour to rebar_resource_v2 and keep v1 * cleanup use of rebar_resource module and unused functions * cleanup error messages and unused code * when discovering apps support mix packages as unbuilt apps (#1882) * use hex_core tarball unpacking support in pkg resource (#1883) * use hex_core tarball unpacking support in pkg resource * ignore etag if package doesn't exist and delete if checksum fails * add back tests for bad package checksums * improve bad registry checksum error message
Diffstat (limited to 'src/rebar_utils.erl')
-rw-r--r--src/rebar_utils.erl157
1 files changed, 127 insertions, 30 deletions
diff --git a/src/rebar_utils.erl b/src/rebar_utils.erl
index 7e57d01..995d212 100644
--- a/src/rebar_utils.erl
+++ b/src/rebar_utils.erl
@@ -74,13 +74,15 @@
user_agent/0,
reread_config/1, reread_config/2,
get_proxy_auth/0,
- is_list_of_strings/1]).
+ is_list_of_strings/1,
+ ssl_opts/1]).
%% for internal use only
-export([otp_release/0]).
-include("rebar.hrl").
+-include_lib("public_key/include/OTP-PUB-KEY.hrl").
-define(ONE_LEVEL_INDENT, " ").
-define(APP_NAME_INDEX, 2).
@@ -710,12 +712,12 @@ escript_foldl(Fun, Acc, File) ->
Error
end.
-vcs_vsn(Vcs, Dir, Resources) ->
- case vcs_vsn_cmd(Vcs, Dir, Resources) of
+vcs_vsn(AppInfo, Vcs, State) ->
+ case vcs_vsn_cmd(AppInfo, Vcs, State) of
{plain, VsnString} ->
VsnString;
{cmd, CmdString} ->
- vcs_vsn_invoke(CmdString, Dir);
+ vcs_vsn_invoke(CmdString, rebar_app_info:dir(AppInfo));
unknown ->
?ABORT("vcs_vsn: Unknown vsn format: ~p", [Vcs]);
{error, Reason} ->
@@ -723,23 +725,18 @@ vcs_vsn(Vcs, Dir, Resources) ->
end.
%% Temp work around for repos like relx that use "semver"
-vcs_vsn_cmd(Vsn, _, _) when is_binary(Vsn) ->
+vcs_vsn_cmd(_AppInfo, Vsn, _) when is_binary(Vsn) ->
{plain, Vsn};
-vcs_vsn_cmd(VCS, Dir, Resources) when VCS =:= semver ; VCS =:= "semver" ->
- vcs_vsn_cmd(git, Dir, Resources);
-vcs_vsn_cmd({cmd, _Cmd}=Custom, _, _) ->
+vcs_vsn_cmd(AppInfo, VCS, State) when VCS =:= semver ; VCS =:= "semver" ->
+ vcs_vsn_cmd(AppInfo, git, State);
+vcs_vsn_cmd(_AppInfo, {cmd, _Cmd}=Custom, _) ->
Custom;
-vcs_vsn_cmd(VCS, Dir, Resources) when is_atom(VCS) ->
- case find_resource_module(VCS, Resources) of
- {ok, Module} ->
- Module:make_vsn(Dir);
- {error, _} ->
- unknown
- end;
-vcs_vsn_cmd(VCS, Dir, Resources) when is_list(VCS) ->
+vcs_vsn_cmd(AppInfo, VCS, State) when is_atom(VCS) ->
+ rebar_resource_v2:make_vsn(AppInfo, VCS, State);
+vcs_vsn_cmd(AppInfo, VCS, State) when is_list(VCS) ->
try list_to_existing_atom(VCS) of
AVCS ->
- case vcs_vsn_cmd(AVCS, Dir, Resources) of
+ case vcs_vsn_cmd(AppInfo, AVCS, State) of
unknown -> {plain, VCS};
Other -> Other
end
@@ -754,19 +751,6 @@ vcs_vsn_invoke(Cmd, Dir) ->
{ok, VsnString} = rebar_utils:sh(Cmd, [{cd, Dir}, {use_stdout, false}]),
rebar_string:trim(VsnString, trailing, "\n").
-find_resource_module(Type, Resources) ->
- case lists:keyfind(Type, 1, Resources) of
- false ->
- case code:which(Type) of
- non_existing ->
- {error, unknown};
- _ ->
- {ok, Type}
- end;
- {Type, Module} ->
- {ok, Module}
- end.
-
%% @doc ident to the level specified
-spec indent(non_neg_integer()) -> iolist().
indent(Amount) when erlang:is_integer(Amount) ->
@@ -977,3 +961,116 @@ is_list_of_strings(List) when is_list(hd(List)) ->
true;
is_list_of_strings(List) when is_list(List) ->
true.
+
+%%------------------------------------------------------------------------------
+%% @doc
+%% Return the SSL options adequate for the project based on
+%% its configuration, including for validation of certs.
+%% @end
+%%------------------------------------------------------------------------------
+-spec ssl_opts(Url) -> Res when
+ Url :: string(),
+ Res :: proplists:proplist().
+ssl_opts(Url) ->
+ case get_ssl_config() of
+ ssl_verify_enabled ->
+ ssl_opts(ssl_verify_enabled, Url);
+ ssl_verify_disabled ->
+ [{verify, verify_none}]
+ end.
+
+%%------------------------------------------------------------------------------
+%% @doc
+%% Return the SSL options adequate for the project based on
+%% its configuration, including for validation of certs.
+%% @end
+%%------------------------------------------------------------------------------
+-spec ssl_opts(Enabled, Url) -> Res when
+ Enabled :: atom(),
+ Url :: string(),
+ Res :: proplists:proplist().
+ssl_opts(ssl_verify_enabled, Url) ->
+ case check_ssl_version() of
+ true ->
+ {ok, {_, _, Hostname, _, _, _}} =
+ http_uri:parse(rebar_utils:to_list(Url)),
+ VerifyFun = {fun ssl_verify_hostname:verify_fun/3,
+ [{check_hostname, Hostname}]},
+ CACerts = certifi:cacerts(),
+ [{verify, verify_peer}, {depth, 2}, {cacerts, CACerts},
+ {partial_chain, fun partial_chain/1}, {verify_fun, VerifyFun}];
+ false ->
+ ?WARN("Insecure HTTPS request (peer verification disabled), "
+ "please update to OTP 17.4 or later", []),
+ [{verify, verify_none}]
+ end.
+
+-spec partial_chain(Certs) -> Res when
+ Certs :: list(any()),
+ Res :: unknown_ca | {trusted_ca, any()}.
+partial_chain(Certs) ->
+ Certs1 = [{Cert, public_key:pkix_decode_cert(Cert, otp)} || Cert <- Certs],
+ CACerts = certifi:cacerts(),
+ CACerts1 = [public_key:pkix_decode_cert(Cert, otp) || Cert <- CACerts],
+ case ec_lists:find(fun({_, Cert}) ->
+ check_cert(CACerts1, Cert)
+ end, Certs1) of
+ {ok, Trusted} ->
+ {trusted_ca, element(1, Trusted)};
+ _ ->
+ unknown_ca
+ end.
+
+-spec extract_public_key_info(Cert) -> Res when
+ Cert :: #'OTPCertificate'{tbsCertificate::#'OTPTBSCertificate'{}},
+ Res :: any().
+extract_public_key_info(Cert) ->
+ ((Cert#'OTPCertificate'.tbsCertificate)#'OTPTBSCertificate'.subjectPublicKeyInfo).
+
+-spec check_cert(CACerts, Cert) -> Res when
+ CACerts :: list(any()),
+ Cert :: any(),
+ Res :: boolean().
+check_cert(CACerts, Cert) ->
+ lists:any(fun(CACert) ->
+ extract_public_key_info(CACert) == extract_public_key_info(Cert)
+ end, CACerts).
+
+-spec check_ssl_version() ->
+ boolean().
+check_ssl_version() ->
+ case application:get_key(ssl, vsn) of
+ {ok, Vsn} ->
+ parse_vsn(Vsn) >= {5, 3, 6};
+ _ ->
+ false
+ end.
+
+-spec get_ssl_config() ->
+ ssl_verify_disabled | ssl_verify_enabled.
+get_ssl_config() ->
+ GlobalConfigFile = rebar_dir:global_config(),
+ Config = rebar_config:consult_file(GlobalConfigFile),
+ case proplists:get_value(ssl_verify, Config, []) of
+ false ->
+ ssl_verify_disabled;
+ _ ->
+ ssl_verify_enabled
+ end.
+
+-spec parse_vsn(Vsn) -> Res when
+ Vsn :: string(),
+ Res :: {integer(), integer(), integer()}.
+parse_vsn(Vsn) ->
+ version_pad(rebar_string:lexemes(Vsn, ".-")).
+
+-spec version_pad(list(nonempty_string())) -> Res when
+ Res :: {integer(), integer(), integer()}.
+version_pad([Major]) ->
+ {list_to_integer(Major), 0, 0};
+version_pad([Major, Minor]) ->
+ {list_to_integer(Major), list_to_integer(Minor), 0};
+version_pad([Major, Minor, Patch]) ->
+ {list_to_integer(Major), list_to_integer(Minor), list_to_integer(Patch)};
+version_pad([Major, Minor, Patch | _]) ->
+ {list_to_integer(Major), list_to_integer(Minor), list_to_integer(Patch)}.