2018-10-06 19:10:08 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-05-09 15:14:46 -04:00
|
|
|
module ContainerRegistry
|
2022-01-25 13:11:55 -05:00
|
|
|
class Client < BaseClient
|
2020-02-12 10:09:37 -05:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2016-05-04 08:22:54 -04:00
|
|
|
attr_accessor :uri
|
|
|
|
|
2020-05-15 08:08:28 -04:00
|
|
|
REGISTRY_VERSION_HEADER = 'gitlab-container-registry-version'
|
|
|
|
REGISTRY_FEATURES_HEADER = 'gitlab-container-registry-features'
|
2020-11-09 16:08:48 -05:00
|
|
|
REGISTRY_TAG_DELETE_FEATURE = 'tag_delete'
|
2019-09-19 07:50:12 -04:00
|
|
|
|
2022-03-04 13:20:01 -05:00
|
|
|
ALLOWED_REDIRECT_SCHEMES = %w[http https].freeze
|
|
|
|
REDIRECT_OPTIONS = {
|
|
|
|
clear_authorization_header: true,
|
|
|
|
limit: 3,
|
|
|
|
cookies: [],
|
|
|
|
callback: -> (response_env, request_env) do
|
|
|
|
request_env.request_headers.delete(::FaradayMiddleware::FollowRedirects::AUTH_HEADER)
|
|
|
|
|
|
|
|
redirect_to = request_env.url
|
|
|
|
unless redirect_to.scheme.in?(ALLOWED_REDIRECT_SCHEMES)
|
|
|
|
raise ArgumentError, "Invalid scheme for #{redirect_to}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
}.freeze
|
|
|
|
|
2020-09-02 05:10:23 -04:00
|
|
|
def self.supports_tag_delete?
|
2022-01-25 13:11:55 -05:00
|
|
|
with_dummy_client(return_value_if_disabled: false) do |client|
|
|
|
|
client.supports_tag_delete?
|
|
|
|
end
|
2020-09-02 05:10:23 -04:00
|
|
|
end
|
|
|
|
|
2021-10-11 14:09:46 -04:00
|
|
|
def self.registry_info
|
2022-01-25 13:11:55 -05:00
|
|
|
with_dummy_client do |client|
|
|
|
|
client.registry_info
|
|
|
|
end
|
2016-05-04 08:22:54 -04:00
|
|
|
end
|
|
|
|
|
2020-05-15 08:08:28 -04:00
|
|
|
def registry_info
|
|
|
|
response = faraday.get("/v2/")
|
|
|
|
|
|
|
|
return {} unless response&.success?
|
|
|
|
|
|
|
|
version = response.headers[REGISTRY_VERSION_HEADER]
|
|
|
|
features = response.headers.fetch(REGISTRY_FEATURES_HEADER, '')
|
|
|
|
|
|
|
|
{
|
|
|
|
version: version,
|
|
|
|
features: features.split(',').map(&:strip),
|
|
|
|
vendor: version ? 'gitlab' : 'other'
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2016-05-04 08:22:54 -04:00
|
|
|
def repository_tags(name)
|
2016-06-30 08:49:58 -04:00
|
|
|
response_body faraday.get("/v2/#{name}/tags/list")
|
2016-05-04 08:22:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def repository_manifest(name, reference)
|
2016-06-30 08:49:58 -04:00
|
|
|
response_body faraday.get("/v2/#{name}/manifests/#{reference}")
|
2016-05-04 08:22:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def repository_tag_digest(name, reference)
|
2016-06-30 08:49:58 -04:00
|
|
|
response = faraday.head("/v2/#{name}/manifests/#{reference}")
|
2021-11-09 10:12:42 -05:00
|
|
|
response.headers[DependencyProxy::Manifest::DIGEST_HEADER] if response.success?
|
2016-05-04 08:22:54 -04:00
|
|
|
end
|
|
|
|
|
2020-02-12 10:09:37 -05:00
|
|
|
def delete_repository_tag_by_digest(name, reference)
|
|
|
|
delete_if_exists("/v2/#{name}/manifests/#{reference}")
|
|
|
|
end
|
2019-10-16 08:06:32 -04:00
|
|
|
|
2020-02-12 10:09:37 -05:00
|
|
|
def delete_repository_tag_by_name(name, reference)
|
|
|
|
delete_if_exists("/v2/#{name}/tags/reference/#{reference}")
|
|
|
|
end
|
|
|
|
|
|
|
|
# Check if the registry supports tag deletion. This is only supported by the
|
|
|
|
# GitLab registry fork. The fastest and safest way to check this is to send
|
|
|
|
# an OPTIONS request to /v2/<name>/tags/reference/<tag>, using a random
|
|
|
|
# repository name and tag (the registry won't check if they exist).
|
|
|
|
# Registries that support tag deletion will reply with a 200 OK and include
|
|
|
|
# the DELETE method in the Allow header. Others reply with an 404 Not Found.
|
|
|
|
def supports_tag_delete?
|
|
|
|
strong_memoize(:supports_tag_delete) do
|
2020-11-09 16:08:48 -05:00
|
|
|
registry_features = Gitlab::CurrentSettings.container_registry_features || []
|
|
|
|
next true if ::Gitlab.com? && registry_features.include?(REGISTRY_TAG_DELETE_FEATURE)
|
|
|
|
|
2020-02-12 10:09:37 -05:00
|
|
|
response = faraday.run_request(:options, '/v2/name/tags/reference/tag', '', {})
|
|
|
|
response.success? && response.headers['allow']&.include?('DELETE')
|
|
|
|
end
|
2016-05-04 08:22:54 -04:00
|
|
|
end
|
|
|
|
|
2019-09-19 07:50:12 -04:00
|
|
|
def upload_raw_blob(path, blob)
|
|
|
|
digest = "sha256:#{Digest::SHA256.hexdigest(blob)}"
|
|
|
|
|
|
|
|
if upload_blob(path, blob, digest).success?
|
|
|
|
[blob, digest]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def upload_blob(name, content, digest)
|
2021-01-07 22:10:42 -05:00
|
|
|
upload = faraday(timeout_enabled: false).post("/v2/#{name}/blobs/uploads/")
|
2019-11-11 04:06:38 -05:00
|
|
|
return upload unless upload.success?
|
2019-09-19 07:50:12 -04:00
|
|
|
|
|
|
|
location = URI(upload.headers['location'])
|
|
|
|
|
2021-01-07 22:10:42 -05:00
|
|
|
faraday(timeout_enabled: false).put("#{location.path}?#{location.query}") do |req|
|
2019-09-19 07:50:12 -04:00
|
|
|
req.params['digest'] = digest
|
|
|
|
req.headers['Content-Type'] = 'application/octet-stream'
|
|
|
|
req.body = content
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_empty_manifest(path)
|
|
|
|
image = {
|
|
|
|
config: {}
|
|
|
|
}
|
2020-04-30 20:09:59 -04:00
|
|
|
image, image_digest = upload_raw_blob(path, Gitlab::Json.pretty_generate(image))
|
2019-09-19 07:50:12 -04:00
|
|
|
return unless image
|
|
|
|
|
|
|
|
{
|
|
|
|
schemaVersion: 2,
|
|
|
|
mediaType: DOCKER_DISTRIBUTION_MANIFEST_V2_TYPE,
|
|
|
|
config: {
|
|
|
|
mediaType: CONTAINER_IMAGE_V1_TYPE,
|
|
|
|
size: image.size,
|
|
|
|
digest: image_digest
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2016-05-04 08:22:54 -04:00
|
|
|
def blob(name, digest, type = nil)
|
2016-06-30 08:49:58 -04:00
|
|
|
type ||= 'application/octet-stream'
|
2022-04-26 11:10:32 -04:00
|
|
|
response_body faraday_blob.get("/v2/#{name}/blobs/#{digest}", nil, 'Accept' => type)
|
2016-05-04 08:22:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def delete_blob(name, digest)
|
2020-02-12 10:09:37 -05:00
|
|
|
delete_if_exists("/v2/#{name}/blobs/#{digest}")
|
2016-05-04 08:22:54 -04:00
|
|
|
end
|
2016-06-30 08:49:58 -04:00
|
|
|
|
2019-09-19 07:50:12 -04:00
|
|
|
def put_tag(name, reference, manifest)
|
2021-01-07 22:10:42 -05:00
|
|
|
response = faraday(timeout_enabled: false).put("/v2/#{name}/manifests/#{reference}") do |req|
|
2019-09-19 07:50:12 -04:00
|
|
|
req.headers['Content-Type'] = DOCKER_DISTRIBUTION_MANIFEST_V2_TYPE
|
2020-04-30 20:09:59 -04:00
|
|
|
req.body = Gitlab::Json.pretty_generate(manifest)
|
2019-09-19 07:50:12 -04:00
|
|
|
end
|
|
|
|
|
2021-11-09 10:12:42 -05:00
|
|
|
response.headers[DependencyProxy::Manifest::DIGEST_HEADER] if response.success?
|
2019-09-19 07:50:12 -04:00
|
|
|
end
|
|
|
|
|
2016-05-09 15:34:24 -04:00
|
|
|
private
|
2016-06-30 08:49:58 -04:00
|
|
|
|
|
|
|
def faraday_blob
|
2020-04-15 05:09:46 -04:00
|
|
|
@faraday_blob ||= faraday_base do |conn|
|
2016-06-30 08:49:58 -04:00
|
|
|
initialize_connection(conn, @options)
|
2022-03-04 13:20:01 -05:00
|
|
|
|
2022-04-26 11:10:32 -04:00
|
|
|
conn.use ::FaradayMiddleware::FollowRedirects, REDIRECT_OPTIONS
|
2016-06-30 08:49:58 -04:00
|
|
|
end
|
2016-06-17 11:33:03 -04:00
|
|
|
end
|
2016-05-04 08:22:54 -04:00
|
|
|
end
|
|
|
|
end
|
2019-09-13 09:26:31 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
ContainerRegistry::Client.prepend_mod_with('ContainerRegistry::Client')
|