Skip to content

Commit

Permalink
Fix trust lookup for organizations
Browse files Browse the repository at this point in the history
  • Loading branch information
ericmj committed Feb 2, 2023
1 parent 1563370 commit 2f35e1e
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 17 deletions.
4 changes: 2 additions & 2 deletions lib/hex/config.ex
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ defmodule Hex.Config do
term
end

def read_repos(config, repos_key \\ Hex.State.fetch!(:repos_key)) do
hexpm = Hex.Repo.default_hexpm_repo(repos_key)
def read_repos(config, repo_key) do
hexpm = Hex.Repo.nostate_default_hexpm_repo(repo_key)

(config[:"$repos"] || %{})
|> Hex.Repo.merge_hexpm(hexpm)
Expand Down
35 changes: 20 additions & 15 deletions lib/hex/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,14 @@ defmodule Hex.Repo do

case Map.fetch(repos, repo) do
{:ok, config} when repo == "hexpm" ->
trusted_mirror_url = Hex.State.fetch!(:trusted_mirror_url)
mirror_url = Hex.State.fetch!(:mirror_url)

config =
config
|> Map.put(:url, trusted_mirror_url || mirror_url || config.url)
|> Map.put(:trusted, trusted_mirror_url != nil or mirror_url == nil)

{:ok, config}
hexpm = default_hexpm_repo()
{:ok, %{config | url: hexpm.url || config.url, trusted: hexpm.trusted}}

{:ok, config} ->
{:ok, config}

:error ->
fetch_organization_fallback(repo, repos)
fetch_organization_fallback(repo)
end
end

Expand All @@ -51,7 +44,7 @@ defmodule Hex.Repo do
end
end

def default_organization(source, repo, name) do
defp default_organization(source, repo, name) do
url = merge_values(Map.get(repo, :url), source.url <> "/repos/#{name}")
public_key = merge_values(Map.get(repo, :public_key), source.public_key)
auth_key = merge_values(Map.get(repo, :auth_key), source.auth_key)
Expand All @@ -60,10 +53,22 @@ defmodule Hex.Repo do
|> Map.put(:url, url)
|> Map.put(:public_key, public_key)
|> Map.put(:auth_key, auth_key)
|> Map.put(:trusted, auth_key)
|> Map.put(:trusted, Map.has_key?(repo, :auth_key) or source.trusted)
end

def default_hexpm_repo() do
trusted_mirror_url = Hex.State.fetch!(:trusted_mirror_url)
mirror_url = Hex.State.fetch!(:mirror_url)

%{
url: trusted_mirror_url || mirror_url,
public_key: @hexpm_public_key,
auth_key: nil,
trusted: trusted_mirror_url != nil or mirror_url == nil
}
end

def default_hexpm_repo(auth_key \\ Hex.State.fetch!(:repos_key)) do
def nostate_default_hexpm_repo(auth_key) do
%{
url: @hexpm_url,
public_key: @hexpm_public_key,
Expand All @@ -72,10 +77,10 @@ defmodule Hex.Repo do
}
end

defp fetch_organization_fallback(repo, repos) do
defp fetch_organization_fallback(repo) do
case String.split(repo, ":", parts: 2) do
[source, organization] ->
source = Map.fetch!(repos, source)
{:ok, source} = fetch_repo(source)
{:ok, default_organization(source, %{}, organization)}

_ ->
Expand Down
35 changes: 35 additions & 0 deletions test/hex/repo_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,39 @@ defmodule Hex.RepoTest do
config = %{url: "http://localhost:#{bypass.port}/not_found", auth_key: "key", trusted: false}
assert {:ok, {404, "not found", _}} = Hex.Repo.get_public_key(config)
end

test "fetch_repo/1" do
assert {:ok,
%{auth_key: nil, public_key: _, trusted: true, url: "http://localhost:4043/repo"}} =
Hex.Repo.fetch_repo("hexpm")

assert {:ok,
%{
auth_key: nil,
public_key: _,
trusted: true,
url: "http://localhost:4043/repo/repos/acme"
}} = Hex.Repo.fetch_repo("hexpm:acme")

assert Hex.Repo.fetch_repo("foo") == :error

Hex.State.put(:trusted_mirror_url, "http://example.com")

assert {:ok, %{auth_key: nil, public_key: _, trusted: true, url: "http://example.com"}} =
Hex.Repo.fetch_repo("hexpm")

assert {:ok,
%{auth_key: nil, public_key: _, trusted: true, url: "http://example.com/repos/acme"}} =
Hex.Repo.fetch_repo("hexpm:acme")

Hex.State.put(:trusted_mirror_url, nil)
Hex.State.put(:mirror_url, "http://example.com")

assert {:ok, %{auth_key: nil, public_key: _, trusted: false, url: "http://example.com"}} =
Hex.Repo.fetch_repo("hexpm")

assert {:ok,
%{auth_key: nil, public_key: _, trusted: false, url: "http://example.com/repos/acme"}} =
Hex.Repo.fetch_repo("hexpm:acme")
end
end

0 comments on commit 2f35e1e

Please sign in to comment.