Skip to content
This repository was archived by the owner on Apr 17, 2023. It is now read-only.

Commit 0c0d46b

Browse files
author
Fabian Baumanis
committed
Implement a environment variable in config.yml, so that only admins can push images.
Signed-off-by: Fabian Baumanis <[email protected]>
1 parent c2ed54e commit 0c0d46b

File tree

8 files changed

+119
-5
lines changed

8 files changed

+119
-5
lines changed

app/policies/namespace_policy.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,14 @@ def pull?
3030
def push?
3131
raise Pundit::NotAuthorizedError, "must be logged in" unless user
3232

33-
# Only owners and contributors have WRITE access
34-
user.admin? ||
35-
namespace.team.owners.exists?(user.id) ||
36-
namespace.team.contributors.exists?(user.id)
33+
if APP_CONFIG.enabled?("user_permission.push_images")
34+
# Only owner and contributors have WRITE access
35+
user.admin? ||
36+
namespace.team.owners.exists?(user.id) ||
37+
namespace.team.contributors.exists?(user.id)
38+
else
39+
user.admin?
40+
end
3741
end
3842

3943
def index?

config/config.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,11 @@ user_permission:
248248
manage_namespace:
249249
enabled: true
250250

251+
# Allow users to push images to namespaces, where they are owner/contributor of.
252+
# If this is disabled, only admins can push images. This defaults to true.
253+
push_images:
254+
enabled: true
255+
251256
# Security scanner support. Add the server location for each driver in order to
252257
# enable it. If no drivers have been enabled, then this feature is skipped
253258
# altogether. Enabling multiple drivers will simply aggregate the information

spec/controllers/namespaces_controller_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,19 @@
276276
expect(response.status).to eq(401)
277277
expect(namespace.reload.team.id).to eq team.id
278278
end
279+
280+
context "when option user_permission.push_images" do
281+
before do
282+
APP_CONFIG["user_permission"]["push_images"]["enabled"] = false
283+
end
284+
285+
it "raises an authorization error when trying to change to a non-existing team" do
286+
sign_in owner
287+
patch :update, id: namespace.id, namespace: { team: "unknown" }, format: :json
288+
expect(response.status). to eq(401)
289+
expect(namespace.reload.team.id).to eq team.id
290+
end
291+
end
279292
end
280293

281294
it "does not allow to change the team to viewers" do

spec/features/namespaces_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,26 @@
311311
expect(page).to have_content("Pull Viewer")
312312
end
313313

314+
context "when user_permission.push_images is disabled" do
315+
before do
316+
APP_CONFIG["user_permission"]["push_images"]["enabled"] = false
317+
end
318+
319+
it "shows the proper visual aid for each role" do
320+
login_as user
321+
visit namespace_path(namespace.id)
322+
expect(page).to have_content("Push Pull Owner")
323+
324+
login_as user2, scope: :user
325+
visit namespace_path(namespace.id)
326+
expect(page).not_to have_content("Push Pull Contr.")
327+
328+
login_as user3, scope: :user
329+
visit namespace_path(namespace.id)
330+
expect(page).to have_content("Pull Viewer")
331+
end
332+
end
333+
314334
it "An user sees dropdown for 'Show webhooks'", js: true do
315335
visit namespace_path(namespace.id)
316336

spec/features/repositories_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,32 @@ def find_tag_checkbox(name)
131131
expect(info).not_to have_content("You are a contributor in this repository")
132132
end
133133

134+
context "when user_permission.push_images is disabled" do
135+
before do
136+
APP_CONFIG["user_permission"]["push_images"]["enabled"] = false
137+
end
138+
139+
it "Visual aid for each role is shown properly" do
140+
login_as user
141+
visit repository_path(repository)
142+
info = page.find(".repository-information-icon")["data-content"]
143+
expect(info).to have_content("You can push images")
144+
expect(info).to have_content("You can pull images")
145+
expect(info).to have_content("You are an owner of this repository")
146+
expect(info).not_to have_content("You are a contributor in this repository")
147+
expect(info).not_to have_content("You are a viewer in this repository")
148+
149+
login_as user2, scope: :user
150+
visit repository_path(repository)
151+
info = page.find(".repository-information-icon")["data-content"]
152+
expect(info).not_to have_content("You can push images")
153+
expect(info).to have_content("You can pull images")
154+
expect(info).not_to have_content("You are an owner of this repository")
155+
expect(info).to have_content("You are a contributor in this repository")
156+
expect(info).not_to have_content("You are a viewer in this repository")
157+
end
158+
end
159+
134160
it "A user can star a repository", js: true do
135161
visit repository_path(repository)
136162
expect(page).to have_css("#toggle_star")

spec/helpers/repositories_helper_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ def update_registry!(catalog)
3333
expect(message).to include("You can push images")
3434
end
3535

36+
context "when the user push permission is disabled" do
37+
before do
38+
APP_CONFIG["user_permission"]["push_images"]["enabled"] = false
39+
end
40+
41+
it "shows you can push images only for admins" do
42+
sign_in owner
43+
message = helper.render_repository_information(repo)
44+
expect(message).not_to include("You can push images")
45+
end
46+
end
47+
3648
it "shows you can pull images" do
3749
sign_in owner
3850
message = helper.render_repository_information(repo)

spec/policies/namespace_policy_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,38 @@
118118
expect(subject).not_to permit(user, registry.global_namespace)
119119
end
120120
end
121+
122+
context "when user_permission.push_images is disabled" do
123+
before do
124+
APP_CONFIG["user_permission"]["push_images"]["enabled"] = false
125+
end
126+
127+
it "disallows access to user with viewer role" do
128+
expect(subject).not_to permit(viewer, namespace)
129+
end
130+
131+
it "disallows access to user with owner role" do
132+
expect(subject).not_to permit(owner, namespace)
133+
end
134+
135+
it "disallows access to user who is not part of the team" do
136+
expect(subject).not_to permit(user, namespace)
137+
end
138+
139+
it "disallows access to user who is not part of the team" do
140+
expect(subject).not_to permit(user, namespace)
141+
end
142+
143+
it "disallows access to user who is not logged in" do
144+
expect do
145+
subject.new(nil, namespace).push?
146+
end.to raise_error(Pundit::NotAuthorizedError, /must be logged in/)
147+
end
148+
149+
it "allows access to admin" do
150+
expect(subject).to permit(@admin, namespace)
151+
end
152+
end
121153
end
122154

123155
permissions :all? do

spec/spec_helper.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@
7171
# This allows non-admins to modify teams
7272
"manage_team" => { "enabled" => true },
7373
# This allows non-admins to create teams
74-
"create_team" => { "enabled" => true }
74+
"create_team" => { "enabled" => true },
75+
# This allows non-admins to push images
76+
"push_images" => { "enabled" => true }
7577
}
7678

7779
APP_CONFIG["security"] = {

0 commit comments

Comments
 (0)