diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-08-12 21:11:09 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-08-12 21:11:09 +0300 |
commit | 60eaf3d90650086dedb6fd94d6169dc5ab1f8d1e (patch) | |
tree | ff943955f1c424787ba65ff7c1607c522c103115 /lib | |
parent | 1c8734ca5c2981e62b9c1162851ed136de86bbbf (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'lib')
-rw-r--r-- | lib/container_registry/gitlab_api_client.rb | 23 | ||||
-rw-r--r-- | lib/container_registry/tag.rb | 15 | ||||
-rw-r--r-- | lib/gitlab/ci/parsers.rb | 3 | ||||
-rw-r--r-- | lib/gitlab/ci/parsers/sbom/cyclonedx.rb | 10 | ||||
-rw-r--r-- | lib/gitlab/ci/reports/sbom/reports.rb | 21 | ||||
-rw-r--r-- | lib/gitlab/utils/link_header_parser.rb | 46 |
6 files changed, 109 insertions, 9 deletions
diff --git a/lib/container_registry/gitlab_api_client.rb b/lib/container_registry/gitlab_api_client.rb index c68b222af97..be99fa75ffe 100644 --- a/lib/container_registry/gitlab_api_client.rb +++ b/lib/container_registry/gitlab_api_client.rb @@ -21,6 +21,8 @@ module ContainerRegistry REGISTRY_GITLAB_V1_API_FEATURE = 'gitlab_v1_api' + MAX_TAGS_PAGE_SIZE = 1000 + def self.supports_gitlab_api? with_dummy_client(return_value_if_disabled: false) do |client| client.supports_gitlab_api? @@ -86,6 +88,7 @@ module ContainerRegistry end end + # https://gitlab.com/gitlab-org/container-registry/-/blob/master/docs-gitlab/api.md#get-repository-details def repository_details(path, sizing: nil) with_token_faraday do |faraday_client| req = faraday_client.get("/gitlab/v1/repositories/#{path}/") do |req| @@ -98,6 +101,26 @@ module ContainerRegistry end end + # https://gitlab.com/gitlab-org/container-registry/-/blob/master/docs-gitlab/api.md#list-repository-tags + def tags(path, page_size: 100, last: nil) + limited_page_size = [page_size, MAX_TAGS_PAGE_SIZE].min + with_token_faraday do |faraday_client| + response = faraday_client.get("/gitlab/v1/repositories/#{path}/tags/list/") do |req| + req.params['n'] = limited_page_size + req.params['last'] = last if last + end + + break {} unless response.success? + + link_parser = Gitlab::Utils::LinkHeaderParser.new(response.headers['link']) + + { + pagination: link_parser.parse, + response_body: response_body(response) + } + end + end + private def start_import_for(path, pre:) diff --git a/lib/container_registry/tag.rb b/lib/container_registry/tag.rb index 04a8e1d2e8f..76188a937c0 100644 --- a/lib/container_registry/tag.rb +++ b/lib/container_registry/tag.rb @@ -75,15 +75,28 @@ module ContainerRegistry def created_at return @created_at if @created_at - return unless config strong_memoize(:memoized_created_at) do + next unless config + DateTime.rfc3339(config['created']) rescue ArgumentError nil end end + # this function will set and memoize a created_at + # to avoid a #config_blob call. + def force_created_at_from_iso8601(string_value) + date = + begin + DateTime.iso8601(string_value) + rescue ArgumentError + nil + end + instance_variable_set(ivar(:memoized_created_at), date) + end + def layers return unless manifest diff --git a/lib/gitlab/ci/parsers.rb b/lib/gitlab/ci/parsers.rb index 1223d664214..b52e2d8f613 100644 --- a/lib/gitlab/ci/parsers.rb +++ b/lib/gitlab/ci/parsers.rb @@ -13,7 +13,8 @@ module Gitlab accessibility: ::Gitlab::Ci::Parsers::Accessibility::Pa11y, codequality: ::Gitlab::Ci::Parsers::Codequality::CodeClimate, sast: ::Gitlab::Ci::Parsers::Security::Sast, - secret_detection: ::Gitlab::Ci::Parsers::Security::SecretDetection + secret_detection: ::Gitlab::Ci::Parsers::Security::SecretDetection, + cyclonedx: ::Gitlab::Ci::Parsers::Sbom::Cyclonedx } end diff --git a/lib/gitlab/ci/parsers/sbom/cyclonedx.rb b/lib/gitlab/ci/parsers/sbom/cyclonedx.rb index 0e2ca97b9cc..deb20a2138c 100644 --- a/lib/gitlab/ci/parsers/sbom/cyclonedx.rb +++ b/lib/gitlab/ci/parsers/sbom/cyclonedx.rb @@ -8,13 +8,9 @@ module Gitlab SUPPORTED_SPEC_VERSIONS = %w[1.4].freeze COMPONENT_ATTRIBUTES = %w[type name version].freeze - def initialize(json_data, report) - @json_data = json_data - @report = report - end - - def parse! - @data = Gitlab::Json.parse(json_data) + def parse!(blob, sbom_report) + @report = sbom_report + @data = Gitlab::Json.parse(blob) return unless valid? diff --git a/lib/gitlab/ci/reports/sbom/reports.rb b/lib/gitlab/ci/reports/sbom/reports.rb new file mode 100644 index 00000000000..efb772cb818 --- /dev/null +++ b/lib/gitlab/ci/reports/sbom/reports.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +module Gitlab + module Ci + module Reports + module Sbom + class Reports + attr_reader :reports + + def initialize + @reports = [] + end + + def add_report(report) + @reports << report + end + end + end + end + end +end diff --git a/lib/gitlab/utils/link_header_parser.rb b/lib/gitlab/utils/link_header_parser.rb new file mode 100644 index 00000000000..d98c237baf3 --- /dev/null +++ b/lib/gitlab/utils/link_header_parser.rb @@ -0,0 +1,46 @@ +# frozen_string_literal: true + +module Gitlab + module Utils + # Parses Link http headers (as defined in https://www.rfc-editor.org/rfc/rfc5988.txt) + # + # The URI-references with their relation type are extracted and returned as a hash + # Example: + # + # header = '<http://test.org/TheBook/chapter2>; rel="previous", <http://test.org/TheBook/chapter4>; rel="next"' + # + # Gitlab::Utils::LinkHeaderParser.new(header).parse + # { + # previous: { + # uri: #<URI::HTTP http://test.org/TheBook/chapter2> + # }, + # next: { + # uri: #<URI::HTTP http://test.org/TheBook/chapter4> + # } + # } + class LinkHeaderParser + REL_PATTERN = %r{rel="(\w+)"}.freeze + # to avoid parse really long URIs we limit the amount of characters allowed + URI_PATTERN = %r{<(.{1,500})>}.freeze + + def initialize(header) + @header = header + end + + def parse + return {} if @header.blank? + + links = @header.split(',') + result = {} + links.each do |link| + direction = link[REL_PATTERN, 1]&.to_sym + uri = link[URI_PATTERN, 1] + + result[direction] = { uri: URI(uri) } if direction && uri + end + + result + end + end + end +end |