Commit fe62fb88 authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Merge branch '10io-fix-nuget-upload' into 'master'

Fix nuget uploads

See merge request gitlab-org/gitlab!23599
parents 1a5eb7d6 6a9e9f29
...@@ -3,15 +3,21 @@ ...@@ -3,15 +3,21 @@
module Packages module Packages
module Nuget module Nuget
class CreatePackageService < BaseService class CreatePackageService < BaseService
PACKAGE_NAME = 'NuGet.Package' TEMPORARY_PACKAGE_NAME = 'NuGet.Temporary.Package'
PACKAGE_VERSION = '0.0.0' PACKAGE_VERSION = '0.0.0'
def execute def execute
project.packages.nuget.create!( project.packages.nuget.create!(
name: PACKAGE_NAME, name: TEMPORARY_PACKAGE_NAME,
version: PACKAGE_VERSION version: "#{PACKAGE_VERSION}-#{uuid}"
) )
end end
private
def uuid
SecureRandom.uuid
end
end end
end end
end end
...@@ -250,7 +250,7 @@ module API ...@@ -250,7 +250,7 @@ module API
end end
route_setting :authentication, job_token_allowed: true route_setting :authentication, job_token_allowed: true
put 'authorize' do put 'authorize' do
authorize_workhorse!(project) authorize_workhorse!(subject: project)
end end
end end
...@@ -273,7 +273,7 @@ module API ...@@ -273,7 +273,7 @@ module API
end end
route_setting :authentication, job_token_allowed: true route_setting :authentication, job_token_allowed: true
put 'authorize' do put 'authorize' do
authorize_workhorse!(project) authorize_workhorse!(subject: project)
end end
desc 'Upload package files' do desc 'Upload package files' do
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
module API module API
module Helpers module Helpers
module PackagesHelpers module PackagesHelpers
MAX_PACKAGE_FILE_SIZE = 50.megabytes.freeze
def require_packages_enabled! def require_packages_enabled!
not_found! unless ::Gitlab.config.packages.enabled not_found! unless ::Gitlab.config.packages.enabled
end end
...@@ -29,14 +31,17 @@ module API ...@@ -29,14 +31,17 @@ module API
authorize_read_package!(subject) authorize_read_package!(subject)
end end
def authorize_workhorse!(subject = user_project) def authorize_workhorse!(subject: user_project, has_length: true, maximum_size: MAX_PACKAGE_FILE_SIZE)
authorize_upload!(subject) authorize_upload!(subject)
Gitlab::Workhorse.verify_api_request!(headers) Gitlab::Workhorse.verify_api_request!(headers)
status 200 status 200
content_type Gitlab::Workhorse::INTERNAL_API_CONTENT_TYPE content_type Gitlab::Workhorse::INTERNAL_API_CONTENT_TYPE
::Packages::PackageFileUploader.workhorse_authorize(has_length: true)
params = { has_length: has_length }
params[:maximum_size] = maximum_size unless has_length
::Packages::PackageFileUploader.workhorse_authorize(params)
end end
def authorize_upload!(subject = user_project) def authorize_upload!(subject = user_project)
......
...@@ -36,8 +36,8 @@ module API ...@@ -36,8 +36,8 @@ module API
::Ci::Build.find_by_token(token) ::Ci::Build.find_by_token(token)
end end
def uploaded_package_file def uploaded_package_file(param_name = :file)
uploaded_file = UploadedFile.from_params(params, :file, ::Packages::PackageFileUploader.workhorse_local_upload_path) uploaded_file = UploadedFile.from_params(params, param_name, ::Packages::PackageFileUploader.workhorse_local_upload_path)
bad_request!('Missing package file!') unless uploaded_file bad_request!('Missing package file!') unless uploaded_file
uploaded_file uploaded_file
end end
......
...@@ -96,7 +96,7 @@ module API ...@@ -96,7 +96,7 @@ module API
package = ::Packages::Nuget::CreatePackageService.new(authorized_user_project, current_user).execute package = ::Packages::Nuget::CreatePackageService.new(authorized_user_project, current_user).execute
file_params = params.merge( file_params = params.merge(
file: uploaded_package_file, file: uploaded_package_file(:package),
file_name: PACKAGE_FILENAME, file_name: PACKAGE_FILENAME,
file_type: PACKAGE_FILETYPE file_type: PACKAGE_FILETYPE
) )
...@@ -112,7 +112,7 @@ module API ...@@ -112,7 +112,7 @@ module API
forbidden! forbidden!
end end
put 'authorize' do put 'authorize' do
authorize_workhorse!(authorized_user_project) authorize_workhorse!(subject: authorized_user_project, has_length: false)
end end
end end
end end
......
...@@ -89,7 +89,7 @@ describe API::Helpers::PackagesHelpers do ...@@ -89,7 +89,7 @@ describe API::Helpers::PackagesHelpers do
describe '#authorize_workhorse!' do describe '#authorize_workhorse!' do
let_it_be(:headers) { {} } let_it_be(:headers) { {} }
subject { helper.authorize_workhorse!(project) } subject { helper.authorize_workhorse!(subject: project) }
before do before do
allow(helper).to receive(:headers).and_return(headers) allow(helper).to receive(:headers).and_return(headers)
...@@ -104,6 +104,20 @@ describe API::Helpers::PackagesHelpers do ...@@ -104,6 +104,20 @@ describe API::Helpers::PackagesHelpers do
expect(subject).to eq nil expect(subject).to eq nil
end end
context 'without length' do
subject { helper.authorize_workhorse!(subject: project, has_length: false) }
it 'authorizes workhorse' do
expect(helper).to receive(:authorize_upload!).with(project)
expect(helper).to receive(:status).with(200)
expect(helper).to receive(:content_type).with(Gitlab::Workhorse::INTERNAL_API_CONTENT_TYPE)
expect(Gitlab::Workhorse).to receive(:verify_api_request!).with(headers)
expect(::Packages::PackageFileUploader).to receive(:workhorse_authorize).with(has_length: false, maximum_size: ::API::Helpers::PackagesHelpers::MAX_PACKAGE_FILE_SIZE)
expect(subject).to eq nil
end
end
end end
describe '#authorize_upload!' do describe '#authorize_upload!' do
......
...@@ -177,13 +177,13 @@ describe API::NugetPackages do ...@@ -177,13 +177,13 @@ describe API::NugetPackages do
let_it_be(:file_name) { 'package.nupkg' } let_it_be(:file_name) { 'package.nupkg' }
let(:url) { "/projects/#{project.id}/packages/nuget" } let(:url) { "/projects/#{project.id}/packages/nuget" }
let(:headers) { {} } let(:headers) { {} }
let(:params) { { file: temp_file(file_name) } } let(:params) { { package: temp_file(file_name) } }
subject do subject do
workhorse_finalize( workhorse_finalize(
api(url), api(url),
method: :put, method: :put,
file_key: :file, file_key: :package,
params: params, params: params,
headers: headers headers: headers
) )
......
...@@ -14,8 +14,20 @@ describe Packages::Nuget::CreatePackageService do ...@@ -14,8 +14,20 @@ describe Packages::Nuget::CreatePackageService do
package = Packages::Package.last package = Packages::Package.last
expect(package).to be_valid expect(package).to be_valid
expect(package.name).to eq(Packages::Nuget::CreatePackageService::PACKAGE_NAME) expect(package.name).to eq(Packages::Nuget::CreatePackageService::TEMPORARY_PACKAGE_NAME)
expect(package.version).to eq(Packages::Nuget::CreatePackageService::PACKAGE_VERSION) expect(package.version).to start_with(Packages::Nuget::CreatePackageService::PACKAGE_VERSION)
expect(package.package_type).to eq('nuget')
end
it 'can create two packages in a row' do
expect { subject }.to change { Packages::Package.count }.by(1)
expect { described_class.new(project, user, params).execute }.to change { Packages::Package.count }.by(1)
package = Packages::Package.last
expect(package).to be_valid
expect(package.name).to eq(Packages::Nuget::CreatePackageService::TEMPORARY_PACKAGE_NAME)
expect(package.version).to start_with(Packages::Nuget::CreatePackageService::PACKAGE_VERSION)
expect(package.package_type).to eq('nuget') expect(package.package_type).to eq('nuget')
end end
end end
......
...@@ -98,7 +98,7 @@ RSpec.shared_examples 'process nuget upload' do |user_type, status, add_member = ...@@ -98,7 +98,7 @@ RSpec.shared_examples 'process nuget upload' do |user_type, status, add_member =
context 'with object storage disabled' do context 'with object storage disabled' do
context 'without a file from workhorse' do context 'without a file from workhorse' do
let(:params) { { file: nil } } let(:params) { { package: nil } }
it_behaves_like 'returning response status', :bad_request it_behaves_like 'returning response status', :bad_request
end end
...@@ -121,7 +121,7 @@ RSpec.shared_examples 'process nuget upload' do |user_type, status, add_member = ...@@ -121,7 +121,7 @@ RSpec.shared_examples 'process nuget upload' do |user_type, status, add_member =
) )
end end
let(:fog_file) { fog_to_uploaded_file(tmp_object) } let(:fog_file) { fog_to_uploaded_file(tmp_object) }
let(:params) { { file: fog_file, 'file.remote_id' => file_name } } let(:params) { { package: fog_file, 'package.remote_id' => file_name } }
it_behaves_like 'creates nuget package files' it_behaves_like 'creates nuget package files'
...@@ -129,8 +129,8 @@ RSpec.shared_examples 'process nuget upload' do |user_type, status, add_member = ...@@ -129,8 +129,8 @@ RSpec.shared_examples 'process nuget upload' do |user_type, status, add_member =
context "with invalid remote_id: #{remote_id}" do context "with invalid remote_id: #{remote_id}" do
let(:params) do let(:params) do
{ {
file: fog_file, package: fog_file,
'file.remote_id' => remote_id 'package.remote_id' => remote_id
} }
end end
......
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