debian-mirror-gitlab/spec/support/helpers/ci/template_helpers.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

68 lines
2.1 KiB
Ruby
Raw Permalink Normal View History

2021-09-30 23:02:18 +05:30
# frozen_string_literal: true
module Ci
module TemplateHelpers
2022-08-27 11:52:29 +05:30
def template_registry_host
'registry.gitlab.com'
end
2023-01-13 00:05:48 +05:30
2023-05-27 22:25:52 +05:30
def auto_build_image_repository
"gitlab-org/cluster-integration/auto-build-image"
end
2023-01-13 00:05:48 +05:30
def public_image_exist?(registry, repository, image)
public_image_manifest(registry, repository, image).present?
end
def public_image_manifest(registry, repository, reference)
token = public_image_repository_token(registry, repository)
2023-04-23 21:23:45 +05:30
headers = {
'Authorization' => "Bearer #{token}",
'Accept' => 'application/vnd.docker.distribution.manifest.list.v2+json, application/vnd.oci.image.index.v1+json'
}
2023-01-13 00:05:48 +05:30
response = with_net_connect_allowed do
2023-04-23 21:23:45 +05:30
Gitlab::HTTP.get(image_manifest_url(registry, repository, reference), headers: headers)
2023-01-13 00:05:48 +05:30
end
2023-04-23 21:23:45 +05:30
if response.success?
Gitlab::Json.parse(response.body)
elsif response.not_found?
nil
else
raise "Could not retrieve manifest: #{response.body}"
end
2023-01-13 00:05:48 +05:30
end
def public_image_repository_token(registry, repository)
@public_image_repository_tokens ||= {}
@public_image_repository_tokens[[registry, repository]] ||=
begin
response = with_net_connect_allowed do
Gitlab::HTTP.get(image_manifest_url(registry, repository, 'latest'))
end
2023-04-23 21:23:45 +05:30
raise 'Unauthorized' unless response.unauthorized?
2023-01-13 00:05:48 +05:30
www_authenticate = response.headers['www-authenticate']
2023-04-23 21:23:45 +05:30
raise 'Missing www-authenticate' unless www_authenticate
2023-01-13 00:05:48 +05:30
realm, service, scope = www_authenticate.split(',').map { |s| s[/\w+="(.*)"/, 1] }
token_response = with_net_connect_allowed do
Gitlab::HTTP.get(realm, query: { service: service, scope: scope })
end
2023-04-23 21:23:45 +05:30
raise "Could not get token: #{token_response.body}" unless token_response.success?
2023-01-13 00:05:48 +05:30
token_response['token']
end
end
def image_manifest_url(registry, repository, reference)
"#{registry}/v2/#{repository}/manifests/#{reference}"
end
2021-09-30 23:02:18 +05:30
end
end
Ci::TemplateHelpers.prepend_mod