summaryrefslogtreecommitdiff
path: root/app/services/import/gitlab_projects/file_acquisition_strategies/remote_file.rb
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2022-03-18 20:02:30 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2022-03-18 20:02:30 +0000
commit41fe97390ceddf945f3d967b8fdb3de4c66b7dea (patch)
tree9c8d89a8624828992f06d892cd2f43818ff5dcc8 /app/services/import/gitlab_projects/file_acquisition_strategies/remote_file.rb
parent0804d2dc31052fb45a1efecedc8e06ce9bc32862 (diff)
downloadgitlab-ce-41fe97390ceddf945f3d967b8fdb3de4c66b7dea.tar.gz
Add latest changes from gitlab-org/gitlab@14-9-stable-eev14.9.0-rc42
Diffstat (limited to 'app/services/import/gitlab_projects/file_acquisition_strategies/remote_file.rb')
-rw-r--r--app/services/import/gitlab_projects/file_acquisition_strategies/remote_file.rb76
1 files changed, 76 insertions, 0 deletions
diff --git a/app/services/import/gitlab_projects/file_acquisition_strategies/remote_file.rb b/app/services/import/gitlab_projects/file_acquisition_strategies/remote_file.rb
new file mode 100644
index 00000000000..ae9a450660c
--- /dev/null
+++ b/app/services/import/gitlab_projects/file_acquisition_strategies/remote_file.rb
@@ -0,0 +1,76 @@
+# frozen_string_literal: true
+
+module Import
+ module GitlabProjects
+ module FileAcquisitionStrategies
+ class RemoteFile
+ include ActiveModel::Validations
+
+ def self.allow_local_requests?
+ ::Gitlab::CurrentSettings.allow_local_requests_from_web_hooks_and_services?
+ end
+
+ validates :file_url, addressable_url: {
+ schemes: %w(https),
+ allow_localhost: allow_local_requests?,
+ allow_local_network: allow_local_requests?,
+ dns_rebind_protection: true
+ }
+ validate :aws_s3, if: :validate_aws_s3?
+ # When removing the import_project_from_remote_file_s3 remove the
+ # whole condition of this validation:
+ validates_with RemoteFileValidator, if: -> { validate_aws_s3? || !s3_request? }
+
+ def initialize(current_user: nil, params:)
+ @params = params
+ end
+
+ def project_params
+ @project_parms ||= {
+ import_export_upload: ::ImportExportUpload.new(remote_import_url: file_url)
+ }
+ end
+
+ def file_url
+ @file_url ||= params[:remote_import_url]
+ end
+
+ def content_type
+ @content_type ||= headers['content-type']
+ end
+
+ def content_length
+ @content_length ||= headers['content-length'].to_i
+ end
+
+ private
+
+ attr_reader :params
+
+ def aws_s3
+ if s3_request?
+ errors.add(:base, 'To import from AWS S3 use `projects/remote-import-s3`')
+ end
+ end
+
+ def s3_request?
+ headers['Server'] == 'AmazonS3' && headers['x-amz-request-id'].present?
+ end
+
+ def validate_aws_s3?
+ ::Feature.enabled?(:import_project_from_remote_file_s3, default_enabled: :yaml)
+ end
+
+ def headers
+ return {} if file_url.blank?
+
+ @headers ||= Gitlab::HTTP.head(file_url, timeout: 1.second).headers
+ rescue StandardError => e
+ errors.add(:base, "Failed to retrive headers: #{e.message}")
+
+ {}
+ end
+ end
+ end
+ end
+end