Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get Repository Name #131

Merged
merged 5 commits into from
Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions src/hex_registry.erl
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,12 @@ encode_names(Names) ->

%% @private
decode_names(Payload, no_verify) ->
#{packages := Packages} = hex_pb_names:decode_msg(Payload, 'Names'),
{ok, Packages};
{ok, hex_pb_names:decode_msg(Payload, 'Names')};

decode_names(Payload, Repository) ->
case hex_pb_names:decode_msg(Payload, 'Names') of
#{repository := Repository, packages := Packages} ->
{ok, Packages};
#{repository := Repository, packages := _Packages} = Result ->
{ok, Result};
_ ->
{error, unverified}
end.
Expand All @@ -80,13 +79,12 @@ encode_versions(Versions) ->

%% @private
decode_versions(Payload, no_verify) ->
#{packages := Packages} = hex_pb_versions:decode_msg(Payload, 'Versions'),
{ok, Packages};
{ok, hex_pb_versions:decode_msg(Payload, 'Versions')};

decode_versions(Payload, Repository) ->
case hex_pb_versions:decode_msg(Payload, 'Versions') of
#{repository := Repository, packages := Packages} ->
{ok, Packages};
#{repository := Repository, packages := _Packages} = Result ->
{ok, Result};
_ ->
{error, unverified}
end.
Expand All @@ -111,13 +109,12 @@ encode_package(Package) ->

%% @private
decode_package(Payload, no_verify, no_verify) ->
#{releases := Releases} = hex_pb_package:decode_msg(Payload, 'Package'),
{ok, Releases};
{ok, hex_pb_package:decode_msg(Payload, 'Package')};

decode_package(Payload, Repository, Package) ->
case hex_pb_package:decode_msg(Payload, 'Package') of
#{repository := Repository, name := Package, releases := Releases} ->
{ok, Releases};
#{repository := Repository, name := Package, releases := _Releases} = Result ->
{ok, Result};
_ ->
{error, unverified}
end.
Expand Down
30 changes: 18 additions & 12 deletions src/hex_repo.erl
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@
get_names(Config) when is_map(Config) ->
Verify = maps:get(repo_verify_origin, Config, true),
Decoder = fun(Data) ->
case Verify of
true -> hex_registry:decode_names(Data, repo_name(Config));
false -> hex_registry:decode_names(Data, no_verify)
end
{ok, #{packages := Packages}} =
case Verify of
true -> hex_registry:decode_names(Data, repo_name(Config));
false -> hex_registry:decode_names(Data, no_verify)
end,
{ok, Packages}
end,
get_protobuf(Config, <<"names">>, Decoder).

Expand All @@ -55,10 +57,12 @@ get_names(Config) when is_map(Config) ->
get_versions(Config) when is_map(Config) ->
Verify = maps:get(repo_verify_origin, Config, true),
Decoder = fun(Data) ->
case Verify of
true -> hex_registry:decode_versions(Data, repo_name(Config));
false -> hex_registry:decode_versions(Data, no_verify)
end
{ok, #{packages := Packages}} =
case Verify of
true -> hex_registry:decode_versions(Data, repo_name(Config));
false -> hex_registry:decode_versions(Data, no_verify)
end,
{ok, Packages}
end,
get_protobuf(Config, <<"versions">>, Decoder).

Expand All @@ -81,10 +85,12 @@ get_versions(Config) when is_map(Config) ->
get_package(Config, Name) when is_binary(Name) and is_map(Config) ->
Verify = maps:get(repo_verify_origin, Config, true),
Decoder = fun(Data) ->
case Verify of
true -> hex_registry:decode_package(Data, repo_name(Config), Name);
false -> hex_registry:decode_package(Data, no_verify, no_verify)
end
{ok, #{releases := Releases}} =
case Verify of
true -> hex_registry:decode_package(Data, repo_name(Config), Name);
false -> hex_registry:decode_package(Data, no_verify, no_verify)
end,
{ok, Releases}
end,
get_protobuf(Config, <<"packages/", Name/binary>>, Decoder).

Expand Down
14 changes: 7 additions & 7 deletions test/hex_registry_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ names_test(_Config) ->
packages => Packages
},
Payload = hex_registry:build_names(Names, TestPrivateKey),
?assertMatch({ok, Packages}, hex_registry:unpack_names(Payload, <<"hexpm">>, TestPublicKey)),
?assertMatch({ok, Packages}, hex_registry:unpack_names(Payload, no_verify, TestPublicKey)),
?assertMatch({ok, Names}, hex_registry:unpack_names(Payload, <<"hexpm">>, TestPublicKey)),
?assertMatch({ok, Names}, hex_registry:unpack_names(Payload, no_verify, TestPublicKey)),
?assertMatch({error, unverified}, hex_registry:unpack_names(Payload, <<"other_repo">>, TestPublicKey)),
ok.

Expand All @@ -49,8 +49,8 @@ versions_test(_Config) ->
packages => Packages
},
Payload = hex_registry:build_versions(Versions, TestPrivateKey),
?assertMatch({ok, Packages}, hex_registry:unpack_versions(Payload, <<"hexpm">>, TestPublicKey)),
?assertMatch({ok, Packages}, hex_registry:unpack_versions(Payload, no_verify, TestPublicKey)),
?assertMatch({ok, Versions}, hex_registry:unpack_versions(Payload, <<"hexpm">>, TestPublicKey)),
?assertMatch({ok, Versions}, hex_registry:unpack_versions(Payload, no_verify, TestPublicKey)),
?assertMatch({error, unverified}, hex_registry:unpack_versions(Payload, <<"other_repo">>, TestPublicKey)),
ok.

Expand Down Expand Up @@ -90,8 +90,8 @@ package_test(_Config) ->
releases => Releases
},
Payload = hex_registry:build_package(Package, TestPrivateKey),
?assertMatch({ok, Releases}, hex_registry:unpack_package(Payload, <<"hexpm">>, <<"foobar">>, TestPublicKey)),
?assertMatch({ok, Releases}, hex_registry:unpack_package(Payload, no_verify, no_verify, TestPublicKey)),
?assertMatch({ok, Package}, hex_registry:unpack_package(Payload, <<"hexpm">>, <<"foobar">>, TestPublicKey)),
?assertMatch({ok, Package}, hex_registry:unpack_package(Payload, no_verify, no_verify, TestPublicKey)),
?assertMatch({error, unverified}, hex_registry:unpack_package(Payload, <<"other_repo">>, <<"foobar">>, TestPublicKey)),
?assertMatch({error, unverified}, hex_registry:unpack_package(Payload, <<"hexpm">>, <<"other_package">>, TestPublicKey)),
ok.
Expand All @@ -107,7 +107,7 @@ signed_test(_Config) ->
Signed = hex_registry:sign_protobuf(Payload, TestPrivateKey),
#{payload := Payload} = hex_registry:decode_signed(Signed),
{ok, Payload} = hex_registry:decode_and_verify_signed(Signed, TestPublicKey),
{ok, []} = hex_registry:decode_names(Payload, <<"hexpm">>),
{ok, #{packages := []}} = hex_registry:decode_names(Payload, <<"hexpm">>),

{error, bad_key} = hex_registry:decode_and_verify_signed(Signed, <<"bad">>),

Expand Down