Commit b4e8bca6 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Add frozen literals and permission checks

Signed-off-by: default avatarDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
parent 540dface
...@@ -64,6 +64,7 @@ class License < ActiveRecord::Base ...@@ -64,6 +64,7 @@ class License < ActiveRecord::Base
protected_environments protected_environments
system_header_footer system_header_footer
custom_project_templates custom_project_templates
packages
].freeze ].freeze
EEU_FEATURES = EEP_FEATURES + %i[ EEU_FEATURES = EEP_FEATURES + %i[
......
# frozen_string_literal: true
module Packages module Packages
def self.table_name_prefix def self.table_name_prefix
'packages_' 'packages_'
......
# frozen_string_literal: true
class Packages::MavenMetadatum < ActiveRecord::Base class Packages::MavenMetadatum < ActiveRecord::Base
belongs_to :package belongs_to :package
......
# frozen_string_literal: true
class Packages::Package < ActiveRecord::Base class Packages::Package < ActiveRecord::Base
belongs_to :project belongs_to :project
has_many :package_files has_many :package_files
......
# frozen_string_literal: true
class Packages::PackageFile < ActiveRecord::Base class Packages::PackageFile < ActiveRecord::Base
belongs_to :package belongs_to :package
......
...@@ -84,6 +84,10 @@ module EE ...@@ -84,6 +84,10 @@ module EE
rule { can?(:read_issue) }.enable :read_issue_link rule { can?(:read_issue) }.enable :read_issue_link
rule { can?(:public_access) }.policy do
enable :read_packages
end
rule { can?(:reporter_access) }.policy do rule { can?(:reporter_access) }.policy do
enable :admin_board enable :admin_board
enable :read_deploy_board enable :read_deploy_board
...@@ -95,6 +99,7 @@ module EE ...@@ -95,6 +99,7 @@ module EE
rule { can?(:developer_access) }.policy do rule { can?(:developer_access) }.policy do
enable :admin_board enable :admin_board
enable :admin_vulnerability_feedback enable :admin_vulnerability_feedback
enable :write_packages
end end
rule { can?(:developer_access) & security_reports_feature_available }.enable :read_project_security_dashboard rule { can?(:developer_access) & security_reports_feature_available }.enable :read_project_security_dashboard
......
# frozen_string_literal: true
module Packages module Packages
class CreateMavenPackageService < BaseService class CreateMavenPackageService < BaseService
def execute def execute
......
# frozen_string_literal: true
class Packages::PackageFileUploader < GitlabUploader class Packages::PackageFileUploader < GitlabUploader
extend Workhorse::UploadPath extend Workhorse::UploadPath
include ObjectStorage::Concern include ObjectStorage::Concern
......
# frozen_string_literal: true
class CreatePackagesPackages < ActiveRecord::Migration class CreatePackagesPackages < ActiveRecord::Migration
DOWNTIME = false DOWNTIME = false
......
# frozen_string_literal: true
class CreatePackagesPackageFiles < ActiveRecord::Migration class CreatePackagesPackageFiles < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers include Gitlab::Database::MigrationHelpers
......
# frozen_string_literal: true
class CreatePackagesMavenMetadata < ActiveRecord::Migration class CreatePackagesMavenMetadata < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers include Gitlab::Database::MigrationHelpers
......
# frozen_string_literal: true
module API module API
class MavenPackages < Grape::API class MavenPackages < Grape::API
MAVEN_ENDPOINT_REQUIREMENTS = { MAVEN_ENDPOINT_REQUIREMENTS = {
...@@ -48,6 +49,8 @@ module API ...@@ -48,6 +49,8 @@ module API
requires :file_name, type: String, desc: 'Package file name' requires :file_name, type: String, desc: 'Package file name'
end end
get ':id/packages/maven/*app_group/:app_name/:app_version/:file_name', requirements: MAVEN_ENDPOINT_REQUIREMENTS do get ':id/packages/maven/*app_group/:app_name/:app_version/:file_name', requirements: MAVEN_ENDPOINT_REQUIREMENTS do
unauthorized! unless can?(current_user, :read_package, user_project)
file_name, format = extract_format(params[:file_name]) file_name, format = extract_format(params[:file_name])
metadata = ::Packages::MavenMetadatum.find_by!(app_group: params[:app_group], metadata = ::Packages::MavenMetadatum.find_by!(app_group: params[:app_group],
...@@ -77,6 +80,8 @@ module API ...@@ -77,6 +80,8 @@ module API
end end
put ':id/packages/maven/*app_group/:app_name/:app_version/:file_name/authorize', requirements: MAVEN_ENDPOINT_REQUIREMENTS do put ':id/packages/maven/*app_group/:app_name/:app_version/:file_name/authorize', requirements: MAVEN_ENDPOINT_REQUIREMENTS do
not_allowed! unless Gitlab.config.packages.enabled not_allowed! unless Gitlab.config.packages.enabled
unauthorized! unless can?(current_user, :write_package, user_project)
require_gitlab_workhorse! require_gitlab_workhorse!
Gitlab::Workhorse.verify_api_request!(headers) Gitlab::Workhorse.verify_api_request!(headers)
...@@ -101,6 +106,8 @@ module API ...@@ -101,6 +106,8 @@ module API
end end
put ':id/packages/maven/*app_group/:app_name/:app_version/:file_name', requirements: MAVEN_ENDPOINT_REQUIREMENTS do put ':id/packages/maven/*app_group/:app_name/:app_version/:file_name', requirements: MAVEN_ENDPOINT_REQUIREMENTS do
not_allowed! unless Gitlab.config.packages.enabled not_allowed! unless Gitlab.config.packages.enabled
unauthorized! unless can?(current_user, :write_package, user_project)
require_gitlab_workhorse! require_gitlab_workhorse!
file_name, format = extract_format(params[:file_name]) file_name, format = extract_format(params[:file_name])
......
# frozen_string_literal: true
FactoryBot.define do FactoryBot.define do
factory :package, class: Packages::Package do factory :package, class: Packages::Package do
project project
......
# frozen_string_literal: true
require 'rails_helper' require 'rails_helper'
RSpec.describe Packages::MavenMetadatum, type: :model do RSpec.describe Packages::MavenMetadatum, type: :model do
......
# frozen_string_literal: true
require 'rails_helper' require 'rails_helper'
RSpec.describe Packages::PackageFile, type: :model do RSpec.describe Packages::PackageFile, type: :model do
......
# frozen_string_literal: true
require 'rails_helper' require 'rails_helper'
RSpec.describe Packages::Package, type: :model do RSpec.describe Packages::Package, type: :model do
......
# frozen_string_literal: true
require 'spec_helper' require 'spec_helper'
describe API::MavenPackages do describe API::MavenPackages do
let(:user) { create(:user) } let(:user) { create(:user) }
let(:project) { create(:project) } let(:project) { create(:project, :public) }
let(:personal_access_token) { create(:personal_access_token, user: user) } let(:personal_access_token) { create(:personal_access_token, user: user) }
let(:jwt_token) { JWT.encode({ 'iss' => 'gitlab-workhorse' }, Gitlab::Workhorse.secret, 'HS256') } let(:jwt_token) { JWT.encode({ 'iss' => 'gitlab-workhorse' }, Gitlab::Workhorse.secret, 'HS256') }
let(:headers) { { 'GitLab-Workhorse' => '1.0', Gitlab::Workhorse::INTERNAL_API_REQUEST_HEADER => jwt_token } } let(:headers) { { 'GitLab-Workhorse' => '1.0', Gitlab::Workhorse::INTERNAL_API_REQUEST_HEADER => jwt_token } }
...@@ -35,7 +36,32 @@ describe API::MavenPackages do ...@@ -35,7 +36,32 @@ describe API::MavenPackages do
end end
context 'private project' do context 'private project' do
# Auth required, read permissions required before do
project.update!(visibility_level: Gitlab::VisibilityLevel::PRIVATE)
end
it 'returns the file' do
download_file_with_token(package_file_xml.file_name)
expect(response).to have_gitlab_http_status(200)
expect(response.content_type.to_s).to eq('application/octet-stream')
end
it 'denies download when not enough permissions' do
project.add_guest(user)
download_file_with_token(package_file_xml.file_name)
expect(response).to have_gitlab_http_status(400)
end
it 'denies download when no private token' do
project.add_guest(user)
download_file(package_file_xml.file_name)
expect(response).to have_gitlab_http_status(400)
end
end end
def download_file(file_name, params = {}, request_headers = headers) def download_file(file_name, params = {}, request_headers = headers)
...@@ -92,7 +118,7 @@ describe API::MavenPackages do ...@@ -92,7 +118,7 @@ describe API::MavenPackages do
end end
describe 'PUT /api/v4/projects/:id/packages/maven/*app_group/:app_name/:app_version/:file_name' do describe 'PUT /api/v4/projects/:id/packages/maven/*app_group/:app_name/:app_version/:file_name' do
let(:file_upload) { fixture_file_upload('spec/fixtures/maven/maven-metadata.xml') } let(:file_upload) { fixture_file_upload('ee/spec/fixtures/maven/maven-metadata.xml') }
before do before do
# by configuring this path we allow to pass temp file from any path # by configuring this path we allow to pass temp file from any path
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment