Skip to content

Commit 37e6355

Browse files
committed
Serve policies from the repo URL like other resources
get_policy built its URL from a separate repo_organization field that re-inserted the /repos/<org> path segment, even though every other resource builds its URL directly from repo_url. That forced callers whose repo_url already includes /repos/<org> to strip the suffix back off just to have hex_core re-add it. Build the policy URL from repo_url like get_package does, and verify the signed payload against repo_name. The repo_organization config option, its only consumer, is removed.
1 parent 4e787a1 commit 37e6355

3 files changed

Lines changed: 15 additions & 49 deletions

File tree

src/hex_core.erl

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,6 @@
4848
%%
4949
%% * `repo_url' - URL to the repository (default: `https://repo.hex.pm').
5050
%%
51-
%% * `repo_organization' - Name of the organization repository, appends `/repos/:name'
52-
%% to the repository URL and overrides the `repo_name' option.
53-
%%
5451
%% * `repo_verify' - If `true' will verify the repository signature (default: `true').
5552
%%
5653
%% * `repo_verify_origin' - If `true' will verify the repository signature origin,
@@ -114,7 +111,6 @@
114111
repo_name => binary(),
115112
repo_public_key => binary(),
116113
repo_url => binary(),
117-
repo_organization => binary() | undefined,
118114
repo_verify => boolean(),
119115
repo_verify_origin => boolean(),
120116
send_100_continue => boolean(),
@@ -142,7 +138,6 @@ default_config() ->
142138
repo_name => <<"hexpm">>,
143139
repo_public_key => ?HEXPM_PUBLIC_KEY,
144140
repo_url => <<"https://repo.hex.pm">>,
145-
repo_organization => undefined,
146141
repo_verify => true,
147142
repo_verify_origin => true,
148143
send_100_continue => true,

src/hex_repo.erl

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,10 @@ get_package(Config, Name) when is_binary(Name) and is_map(Config) ->
9292
%% @doc
9393
%% Gets policy resource from the repository.
9494
%%
95-
%% Requires `repo_organization' to be set in the config; policies are
96-
%% always served from the per-organization namespace
97-
%% (`/repos/<organization>/policies/<name>'). Returns
98-
%% `{error, missing_repo_organization}' when it is not set.
99-
%%
10095
%% Examples:
10196
%%
10297
%% ```
103-
%% > Config = (hex_core:default_config())#{repo_organization => <<"myorg">>},
98+
%% > Config = (hex_core:default_config())#{repo_url => <<"https://repo.hex.pm/repos/myorg">>, repo_name => <<"myorg">>},
10499
%% > hex_repo:get_policy(Config, <<"strict-prod">>).
105100
%% {ok, {200, ...,
106101
%% #{repository => <<"myorg">>,
@@ -109,19 +104,14 @@ get_package(Config, Name) when is_binary(Name) and is_map(Config) ->
109104
%% '''
110105
%% @end
111106
get_policy(Config, Name) when is_binary(Name) and is_map(Config) ->
112-
case maps:get(repo_organization, Config, undefined) of
113-
undefined ->
114-
{error, missing_repo_organization};
115-
Org when is_binary(Org) ->
116-
Verify = maps:get(repo_verify_origin, Config, true),
117-
Decoder = fun(Data) ->
118-
case Verify of
119-
true -> hex_registry:decode_policy(Data, Org, Name);
120-
false -> hex_registry:decode_policy(Data, no_verify, no_verify)
121-
end
122-
end,
123-
get_protobuf(Config, <<"policies/", Name/binary>>, Decoder)
124-
end.
107+
Verify = maps:get(repo_verify_origin, Config, true),
108+
Decoder = fun(Data) ->
109+
case Verify of
110+
true -> hex_registry:decode_policy(Data, repo_name(Config), Name);
111+
false -> hex_registry:decode_policy(Data, no_verify, no_verify)
112+
end
113+
end,
114+
get_protobuf(Config, <<"policies/", Name/binary>>, Decoder).
125115

126116
%% @doc
127117
%% Gets tarball from the repository.
@@ -358,7 +348,6 @@ verify_repo(Config) ->
358348
end.
359349

360350
%% @private
361-
repo_name(#{repo_organization := Name}) when is_binary(Name) -> Name;
362351
repo_name(#{repo_name := Name}) when is_binary(Name) -> Name.
363352

364353
%% @private
@@ -372,12 +361,8 @@ docs_url(Config, Name, Version) ->
372361
build_url(Config, <<"docs/", Filename/binary>>).
373362

374363
%% @private
375-
build_url(#{repo_url := URI, repo_organization := Org}, Path) when is_binary(Org) ->
376-
<<URI/binary, "/repos/", Org/binary, "/", Path/binary>>;
377-
build_url(#{repo_url := URI, repo_organization := undefined}, Path) ->
378-
<<URI/binary, "/", Path/binary>>;
379-
build_url(Config, Path) ->
380-
build_url(Config#{repo_organization => undefined}, Path).
364+
build_url(#{repo_url := URI}, Path) ->
365+
<<URI/binary, "/", Path/binary>>.
381366

382367
%% @private
383368
tarball_filename(Name, Version) ->

test/hex_repo_SUITE.erl

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,12 @@ all() ->
2424
get_versions_test,
2525
get_package_test,
2626
get_policy_test,
27-
get_policy_missing_org_test,
2827
get_tarball_test,
2928
get_tarball_to_file_test,
3029
get_docs_test,
3130
get_docs_to_file_test,
3231
get_hex_installs_test,
3332
get_public_key_test,
34-
repo_org_not_set,
3533
fingerprint_test,
3634
fingerprint_equal_test
3735
].
@@ -63,7 +61,10 @@ get_package_test(_Config) ->
6361
ok.
6462

6563
get_policy_test(_Config) ->
66-
Config = maps:put(repo_organization, <<"myorg">>, ?CONFIG),
64+
Config = ?CONFIG#{
65+
repo_url => <<"https://repo.test/repos/myorg">>,
66+
repo_name => <<"myorg">>
67+
},
6768

6869
{ok,
6970
{200, _, #{
@@ -96,10 +97,6 @@ get_policy_test(_Config) ->
9697
{ok, {403, _, _}} = hex_repo:get_policy(Config, <<"nonexisting">>),
9798
ok.
9899

99-
get_policy_missing_org_test(_Config) ->
100-
{error, missing_repo_organization} = hex_repo:get_policy(?CONFIG, <<"strict-prod">>),
101-
ok.
102-
103100
get_tarball_test(_Config) ->
104101
{ok, {200, #{<<"etag">> := ETag}, Tarball}} = hex_repo:get_tarball(
105102
?CONFIG, <<"ecto">>, <<"1.0.0">>
@@ -165,17 +162,6 @@ get_public_key_test(_Config) ->
165162
),
166163
ok.
167164

168-
repo_org_not_set(_Config) ->
169-
Config = maps:remove(repo_organization, ?CONFIG),
170-
{ok, {200, _, #{repository := <<"hexpm">>, releases := Releases}}} = hex_repo:get_package(
171-
Config, <<"ecto">>
172-
),
173-
[#{version := <<"1.0.0">>}] =
174-
lists:filter(fun(#{version := Version}) -> Version == <<"1.0.0">> end, Releases),
175-
176-
{ok, {403, _, _}} = hex_repo:get_package(Config, <<"nonexisting">>),
177-
ok.
178-
179165
fingerprint_test(_Config) ->
180166
PublicKeyPem = <<
181167
"-----BEGIN PUBLIC KEY-----\n"

0 commit comments

Comments
 (0)