2020-07-28 23:09:34 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module API
|
|
|
|
module Helpers
|
|
|
|
module Packages
|
|
|
|
module DependencyProxyHelpers
|
|
|
|
REGISTRY_BASE_URLS = {
|
2021-10-27 15:23:28 +05:30
|
|
|
npm: 'https://registry.npmjs.org/',
|
2022-10-11 01:57:18 +05:30
|
|
|
pypi: 'https://pypi.org/simple/',
|
|
|
|
maven: 'https://repo.maven.apache.org/maven2/'
|
2021-10-27 15:23:28 +05:30
|
|
|
}.freeze
|
|
|
|
|
|
|
|
APPLICATION_SETTING_NAMES = {
|
|
|
|
npm: 'npm_package_requests_forwarding',
|
2022-10-11 01:57:18 +05:30
|
|
|
pypi: 'pypi_package_requests_forwarding',
|
|
|
|
maven: 'maven_package_requests_forwarding'
|
2020-07-28 23:09:34 +05:30
|
|
|
}.freeze
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
def redirect_registry_request(forward_to_registry: false, package_type: nil, target: nil, **options)
|
|
|
|
if forward_to_registry && redirect_registry_request_available?(package_type, target) && maven_forwarding_ff_enabled?(package_type, target)
|
2021-04-29 21:17:54 +05:30
|
|
|
::Gitlab::Tracking.event(self.options[:for].name, "#{package_type}_request_forward")
|
2023-03-04 22:38:38 +05:30
|
|
|
redirect(registry_url(package_type, options), body: options[:body])
|
|
|
|
# For the requests with POST methods we need to set status 307 in order to keep request's method
|
|
|
|
status :temporary_redirect if options[:method] == 'POST'
|
2020-07-28 23:09:34 +05:30
|
|
|
else
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def registry_url(package_type, options)
|
2023-05-27 22:25:52 +05:30
|
|
|
base_url = registry_base_url(package_type)
|
2020-07-28 23:09:34 +05:30
|
|
|
|
|
|
|
raise ArgumentError, "Can't build registry_url for package_type #{package_type}" unless base_url
|
|
|
|
|
|
|
|
case package_type
|
|
|
|
when :npm
|
2023-03-04 22:38:38 +05:30
|
|
|
"#{base_url}#{[options[:path], options[:package_name]].compact.join('/')}"
|
2021-10-27 15:23:28 +05:30
|
|
|
when :pypi
|
|
|
|
"#{base_url}#{options[:package_name]}/"
|
2022-10-11 01:57:18 +05:30
|
|
|
when :maven
|
|
|
|
"#{base_url}#{options[:path]}/#{options[:file_name]}"
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
def redirect_registry_request_available?(package_type, target)
|
2021-10-27 15:23:28 +05:30
|
|
|
application_setting_name = APPLICATION_SETTING_NAMES[package_type]
|
|
|
|
|
|
|
|
raise ArgumentError, "Can't find application setting for package_type #{package_type}" unless application_setting_name
|
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
if target.present?
|
2022-11-25 23:54:43 +05:30
|
|
|
target.public_send(application_setting_name) # rubocop:disable GitlabSecurity/PublicSend
|
|
|
|
else
|
|
|
|
::Gitlab::CurrentSettings
|
|
|
|
.current_application_settings
|
|
|
|
.attributes
|
|
|
|
.fetch(application_setting_name, false)
|
|
|
|
end
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
2022-10-11 01:57:18 +05:30
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def maven_forwarding_ff_enabled?(package_type, target)
|
|
|
|
return true unless package_type == :maven
|
|
|
|
return true if Feature.enabled?(:maven_central_request_forwarding)
|
|
|
|
return false unless target
|
|
|
|
|
|
|
|
Feature.enabled?(:maven_central_request_forwarding, target.root_ancestor)
|
|
|
|
end
|
2023-05-27 22:25:52 +05:30
|
|
|
|
|
|
|
# Override in JiHu repo
|
|
|
|
def registry_base_url(package_type)
|
|
|
|
REGISTRY_BASE_URLS[package_type]
|
|
|
|
end
|
2020-07-28 23:09:34 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-05-27 22:25:52 +05:30
|
|
|
|
|
|
|
API::Helpers::Packages::DependencyProxyHelpers.prepend_mod
|