Repository navigation
Expand file tree
/
Copy pathlocal.ex
More file actions
315 lines (257 loc) · 7.92 KB
/
Copy pathlocal.ex
File metadata and controls
315 lines (257 loc) · 7.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
defmodule Mix.Local do
@moduledoc false
@public_keys_html "https://builds.hex.pm/installs/public_keys.html"
@in_memory_key """
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAslPz1mAfyAvRv8W8xOdv
HQMbDJkDKfRhsL4JBGwGH7qw0xh+TbaUlNaM3pF+i8VUjS/4FeXjT/OAUEAHu5Y2
rBVlx00QcH8Dpbyf+H73XiCs0MXnTSecqDgzx6i6NMi8knklHT7yHySHtuuPmPuN
Po8QTKolCKftwPE/iNDeyZfwufd+hTCoCQdoTVcB01SElfNtvKRtoKbx35q80IPr
rOcGsALmr58+bWqCTY/51kFeRxzrPJ5LdcLU/AebyWddD4IUfPDxk16jTiCagMWA
JPSwo8NUrWDIBbD+rEUp06y0ek276rG5Tzm/3Bma56RN/u6nAqBTBE8F2Hu2QBKj
lQIDAQAB
-----END PUBLIC KEY-----
"""
@doc """
Returns the name for an archive or an escript, based on the project config.
## Examples
iex> Mix.Local.name_for(:archives, app: "foo", version: "0.1.0")
"foo-0.1.0.ez"
iex> Mix.Local.name_for(:escripts, escript: [name: "foo"])
"foo"
"""
@spec name_for(:archives | :escripts, keyword) :: String.t()
def name_for(:archives, project) do
version = if version = project[:version], do: "-#{version}"
"#{project[:app]}#{version}.ez"
end
def name_for(:escripts, project) do
case get_in(project, [:escript, :name]) do
nil -> project[:app]
name -> name
end
|> to_string()
end
@deprecated "Use Mix.path_for/1 instead"
def path_for(:archive), do: Mix.path_for(:archives)
def path_for(:escript), do: Mix.path_for(:escripts)
@doc """
Appends archive paths to the Erlang code path.
"""
def append_archives do
for archive <- archives_ebins() do
check_elixir_version_in_ebin(archive)
Code.append_path(archive, cache: true)
end
:ok
end
@doc """
Removes archive paths from Erlang code path.
"""
def remove_archives do
for archive <- archives_ebins() do
Code.delete_path(archive)
end
:ok
end
@doc """
Appends Mix paths to the Erlang code path.
We don't cache them as they are rarely used and
they may be development paths.
"""
def append_paths do
Enum.each(mix_paths(), &Code.append_path/1)
end
@doc """
Removes Mix paths from the Erlang code path.
"""
def remove_paths do
Enum.each(mix_paths(), &Code.delete_path/1)
end
defp mix_paths do
if path = System.get_env("MIX_PATH") do
String.split(path, path_separator())
else
[]
end
end
defp path_separator do
case :os.type() do
{:win32, _} -> ";"
{:unix, _} -> ":"
end
end
@doc """
Returns all tasks in local archives.
"""
def archives_tasks do
Mix.Task.load_tasks(archives_ebins())
end
@doc """
Returns the name of an archive given a path.
"""
def archive_name(path) do
path
|> Path.basename()
|&g
8000
t; Path.rootname(".ez")
end
@doc """
Returns the ebin path of an archive.
"""
def archive_ebin(path) do
Path.join([path, archive_name(path), "ebin"])
end
defp archives_ebins do
path = Mix.path_for(:archives)
case File.ls(path) do
{:ok, entries} -> Enum.map(entries, &archive_ebin(Path.join(path, &1)))
{:error, _} -> []
end
end
@doc """
Checks Elixir version requirement stored in the ebin directory
and print a warning if it is not satisfied.
"""
def check_elixir_version_in_ebin(ebin) do
elixir = ebin |> Path.dirname() |> Path.join(".elixir") |> String.to_charlist()
case File.read(elixir) do
{:ok, req} ->
unless Version.match?(System.version(), req) do
archive = ebin |> Path.dirname() |> Path.basename()
Mix.shell().error(
"warning: the archive #{archive} requires Elixir #{inspect(req)} " <>
"but you are running on v#{System.version()}"
)
end
:ok
{:error, _} ->
:ok
end
end
@doc """
Fetches the given signed CSV files, verifies and returns the matching
Elixir version, artifact version and artifact's checksum.
Used to install both Rebar and Hex from S3.
"""
def find_matching_versions_from_signed_csv!(name, version, path) do
ensure_applications!()
csv = read_unsafe_path!(name, path)
signature =
read_unsafe_path!(name, path <> ".signed")
|> String.replace("\n", "")
|> Base.decode64!()
verified? =
Enum.any?(public_keys(), fn {id, key} ->
:public_key.verify(csv, :sha512, signature, decode_pk!(id, key))
end)
if verified? do
result =
csv
|> parse_csv()
|> find_latest_eligible_version(version)
result || Mix.raise("Could not find a version of #{name} matching: #{version}")
else
Mix.raise(
"Could not install #{name} because Mix could not verify authenticity " <>
"of metadata file at #{inspect(path)}. This may happen because a proxy or some " <>
"entity is interfering with the download or because you don't have a " <>
"public key to verify the download.\n\nYou may try again later or check " <>
"if a new public key has been released in our public keys page: #{@public_keys_html}"
)
end
end
defp read_unsafe_path!(name, path) do
case Mix.Utils.read_path(path, unsafe_uri: true) do
{:ok, contents} ->
contents
{:remote, message} ->
Mix.raise(
"""
#{message}
Could not install #{name} because Mix could not download metadata at #{path}.
""" <> suggestions(name)
)
end
end
defp suggestions("Hex") do
"""
Alternatively, you can compile and install Hex directly with this command:
$ mix archive.install github hexpm/hex branch latest
"""
end
defp suggestions(_) do
""
end
defp parse_csv(body) do
body
|> :binary.split("\n", [:global, :trim])
|> Enum.map(&:binary.split(&1, ",", [:global, :trim]))
end
defp find_latest_eligible_version(entries, artifact_version) do
elixir_version = Version.parse!(System.version())
entries
|> Enum.reverse()
|> find_version(artifact_version, elixir_version)
end
defp find_version(entries, _artifact_version = nil, elixir_version) do
Enum.find_value(entries, &find_by_elixir_version(&1, elixir_version))
end
defp find_version(entries, artifact_version, elixir_version) do
Enum.find_value(entries, &find_by_artifact_version(&1, artifact_version, elixir_version))
end
defp find_by_elixir_version([artifact_version, digest | versions], elixir_version) do
if version = Enum.find(versions, &(Version.compare(&1, elixir_version) != :gt)) do
{version, artifact_version, digest}
end
end
defp find_by_artifact_version(
[artifact_version, digest | versions],
artifact_version,
elixir_version
) do
if version = Enum.find(versions, &(Version.compare(&1, elixir_version) != :gt)) do
{version, artifact_version, digest}
end
end
defp find_by_artifact_version(_entry, _artifact_version, _elixir_version) do
nil
end
## Public keys
@doc """
Returns the file system path for public keys.
"""
def public_keys_path, do: Path.join(Mix.Utils.mix_home(), "public_keys")
@doc """
Returns all public keys as a list.
"""
def public_keys do
path = public_keys_path()
[{"in-memory public key for Elixir v#{System.version()}", @in_memory_key}] ++
case File.ls(path) do
{:ok, keys} -> Enum.map(keys, &{&1, File.read!(Path.join(path, &1))})
{:error, _} -> []
end
end
@doc """
Decodes a public key and raises if the key is invalid.
"""
def decode_pk!(id, key) do
ensure_applications!()
try do
[rsa_public_key] = :public_key.pem_decode(key)
:public_key.pem_entry_decode(rsa_public_key)
rescue
_ ->
Mix.raise("""
Could not decode public key: #{id}. The public key contents are shown below.
#{key}
Public keys must be valid and be in the PEM format
""")
end
end
defp ensure_applications! do
Mix.ensure_application!(:crypto)
Mix.ensure_application!(:asn1)
Mix.ensure_application!(:public_key)
end
end