secupdater 0.3.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/examples/central-project/.gitlab-ci.yml +95 -0
- data/lib/secupdater/cli.rb +32 -1
- data/lib/secupdater/discovery/gitlab.rb +136 -0
- data/lib/secupdater/ecosystem/rubygems.rb +7 -2
- data/lib/secupdater/publisher/gitlab.rb +85 -13
- data/lib/secupdater/publisher.rb +4 -1
- data/lib/secupdater/update.rb +120 -40
- data/lib/secupdater/version.rb +1 -1
- data/lib/secupdater.rb +1 -0
- data/spec/secupdater/cli_spec.rb +74 -0
- data/spec/secupdater/discovery/gitlab_spec.rb +205 -0
- data/spec/secupdater/ecosystem/rubygems_spec.rb +17 -1
- data/spec/secupdater/publisher/gitlab_spec.rb +281 -3
- data/spec/secupdater/update_spec.rb +126 -3
- metadata +4 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 5ed9ba8653acb0daf90dc07f3b0deb0e7be9c7ed8620bd876f86f1c378f82454
|
|
4
|
+
data.tar.gz: 1fb043a7cbaf67de3c0acda6fe4f30526da35604158b6f44587dfb4b3b9e554d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d02a837690cb83ce6f043f7e30fd65bb37185fa0ffc1295520e973352a329b4139d0860c98807c6c9c0a008028afbd64d6e5fee2232d5885a1f2179274c1bfc6
|
|
7
|
+
data.tar.gz: e679ccffe1c7bbdd76efa4238954dfb753baa36715535b121e30ec24316a6c3f751dd41df8760bfe8e48808535e9c87da5b7453b51b359ab49b1138c5e7b2a70
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# Runs secupdater from one central GitLab project against every project in a
|
|
2
|
+
# group that has opted in by adding the `secupdater` topic (Settings > General >
|
|
3
|
+
# Topics).
|
|
4
|
+
#
|
|
5
|
+
# Setup:
|
|
6
|
+
# 1. Create a group access token on the group with the Developer role and the
|
|
7
|
+
# `api` scope, and add it to this project as a masked CI/CD variable called
|
|
8
|
+
# SECUPDATER_GITLAB_TOKEN.
|
|
9
|
+
# 2. Set SECUPDATER_GROUP and SECUPDATER_VERSION below.
|
|
10
|
+
# 3. Add a pipeline schedule (e.g. hourly) to this project.
|
|
11
|
+
#
|
|
12
|
+
# The discover job audits every opted-in project's Gemfile.lock. The update job
|
|
13
|
+
# then runs secupdater, in a container on that project's own Ruby, for each one
|
|
14
|
+
# with advisories or with open secupdater MRs (so it can close the ones that are
|
|
15
|
+
# no longer needed).
|
|
16
|
+
#
|
|
17
|
+
# Anyone who can push to an opted-in project's default branch can change the
|
|
18
|
+
# Gemfile that `bundle lock` evaluates here. secupdater keeps the token out of
|
|
19
|
+
# that process's environment and out of the clone's git config, but treat
|
|
20
|
+
# opting in as trusting that project with the token.
|
|
21
|
+
|
|
22
|
+
variables:
|
|
23
|
+
SECUPDATER_GROUP: delivereasy
|
|
24
|
+
SECUPDATER_TOPIC: secupdater
|
|
25
|
+
SECUPDATER_VERSION: "0.5.0" # the first release with `secupdater discover`
|
|
26
|
+
|
|
27
|
+
workflow:
|
|
28
|
+
rules:
|
|
29
|
+
- if: $CI_PIPELINE_SOURCE == "schedule" || $CI_PIPELINE_SOURCE == "web"
|
|
30
|
+
|
|
31
|
+
discover:
|
|
32
|
+
image: ruby:3.4
|
|
33
|
+
script:
|
|
34
|
+
- gem install secupdater -v "$SECUPDATER_VERSION" --no-document
|
|
35
|
+
- gem install bundler-audit --no-document
|
|
36
|
+
- bundle-audit update
|
|
37
|
+
# Writes projects/<id>/Gemfile.lock (and .bundler-audit.yml) and projects/projects.txt.
|
|
38
|
+
# Exits non-zero if any project couldn't be checked, after listing the rest,
|
|
39
|
+
# so carry on and fail at the end.
|
|
40
|
+
- secupdater discover --verbose --group="$SECUPDATER_GROUP" --topic="$SECUPDATER_TOPIC" projects || failed=1
|
|
41
|
+
- |
|
|
42
|
+
: > projects/run.txt
|
|
43
|
+
while read -r id path branch ruby open_mrs url; do
|
|
44
|
+
# Bundler won't parse a lockfile with path: gems without a Gemfile to
|
|
45
|
+
# resolve them from, so give it an empty one. bundle-audit exits 1 when it
|
|
46
|
+
# finds advisories, so check it wrote a report instead.
|
|
47
|
+
touch "projects/$id/Gemfile"
|
|
48
|
+
BUNDLE_GEMFILE="$PWD/projects/$id/Gemfile" \
|
|
49
|
+
bundle-audit check "projects/$id" --format json --output "projects/$id/audit.json" || true
|
|
50
|
+
if [ ! -s "projects/$id/audit.json" ]; then
|
|
51
|
+
echo "$path: bundle-audit failed"
|
|
52
|
+
failed=1
|
|
53
|
+
continue
|
|
54
|
+
fi
|
|
55
|
+
|
|
56
|
+
findings=$(ruby -rjson -e 'puts JSON.parse(File.read(ARGV[0]))["results"].size' "projects/$id/audit.json")
|
|
57
|
+
echo "$path: $findings advisories, $open_mrs open secupdater MRs"
|
|
58
|
+
if [ "$findings" -gt 0 ] || [ "$open_mrs" -gt 0 ]; then
|
|
59
|
+
echo "$id $path $branch $ruby $url" >> projects/run.txt
|
|
60
|
+
fi
|
|
61
|
+
done < projects/projects.txt
|
|
62
|
+
- exit "${failed:-0}"
|
|
63
|
+
artifacts:
|
|
64
|
+
when: always
|
|
65
|
+
paths: [projects/]
|
|
66
|
+
expire_in: 1 day
|
|
67
|
+
|
|
68
|
+
update:
|
|
69
|
+
image: docker:27
|
|
70
|
+
services: [docker:27-dind]
|
|
71
|
+
variables:
|
|
72
|
+
DOCKER_TLS_CERTDIR: "/certs"
|
|
73
|
+
needs: [discover]
|
|
74
|
+
# Still update the projects discover could check when it couldn't check them all
|
|
75
|
+
when: always
|
|
76
|
+
script:
|
|
77
|
+
- |
|
|
78
|
+
failed=0
|
|
79
|
+
while read -r id path branch ruby url; do
|
|
80
|
+
echo "== $path (Ruby $ruby)"
|
|
81
|
+
# The report goes in on stdin, as the dind daemon can't see this job's files
|
|
82
|
+
docker run --rm -i \
|
|
83
|
+
-e SECUPDATER_GITLAB_TOKEN -e SECUPDATER_VERSION \
|
|
84
|
+
-e SECUPDATER_PROJECT_ID="$id" -e SECUPDATER_TARGET_BRANCH="$branch" -e CLONE_URL="$url" \
|
|
85
|
+
"ruby:$ruby" sh -euc '
|
|
86
|
+
cat > /tmp/audit.json
|
|
87
|
+
git clone --quiet --depth 1 --branch "$SECUPDATER_TARGET_BRANCH" \
|
|
88
|
+
"https://oauth2:${SECUPDATER_GITLAB_TOKEN}@${CLONE_URL#https://}" /repo
|
|
89
|
+
cd /repo
|
|
90
|
+
git remote remove origin # takes the token out of .git/config
|
|
91
|
+
gem install secupdater -v "$SECUPDATER_VERSION" --no-document --silent
|
|
92
|
+
secupdater run --ecosystem=rubygems --verbose /tmp/audit.json
|
|
93
|
+
' < "projects/$id/audit.json" || failed=1
|
|
94
|
+
done < projects/run.txt
|
|
95
|
+
exit $failed
|
data/lib/secupdater/cli.rb
CHANGED
|
@@ -33,7 +33,11 @@ module Secupdater
|
|
|
33
33
|
def publisher
|
|
34
34
|
return unless ENV["SECUPDATER_GITLAB_TOKEN"]
|
|
35
35
|
|
|
36
|
-
|
|
36
|
+
# SECUPDATER_* for when it runs from another project's pipeline (see secupdater discover)
|
|
37
|
+
project_id = ENV["SECUPDATER_PROJECT_ID"] || ENV.fetch("CI_PROJECT_ID")
|
|
38
|
+
target_branch = ENV["SECUPDATER_TARGET_BRANCH"] || ENV.fetch("CI_DEFAULT_BRANCH")
|
|
39
|
+
|
|
40
|
+
Publisher::Gitlab.new(ENV["SECUPDATER_GITLAB_TOKEN"], project_id, target_branch, head_commit)
|
|
37
41
|
rescue KeyError => e
|
|
38
42
|
raise Secupdater::Error, "SECUPDATER_GITLAB_TOKEN is set but #{e.key} isn't"
|
|
39
43
|
end
|
|
@@ -48,7 +52,34 @@ module Secupdater
|
|
|
48
52
|
end
|
|
49
53
|
end
|
|
50
54
|
|
|
55
|
+
class Discover < Dry::CLI::Command
|
|
56
|
+
desc "Find the GitLab projects in a group that have opted in with a topic, for running secupdater from a central project"
|
|
57
|
+
|
|
58
|
+
option :group, required: true, desc: "GitLab group (ID or path) to look in, including its subgroups"
|
|
59
|
+
option :topic, default: "secupdater", desc: "Topic a project needs to be included"
|
|
60
|
+
option :verbose, type: :boolean, default: false
|
|
61
|
+
|
|
62
|
+
argument :output, required: true, desc: "Directory to write each project's files and projects.txt to"
|
|
63
|
+
|
|
64
|
+
def call(output:, topic:, verbose:, group: nil, **)
|
|
65
|
+
# dry-cli doesn't enforce required options
|
|
66
|
+
raise Secupdater::Error, "--group is required, e.g. --group=my-group" unless group
|
|
67
|
+
|
|
68
|
+
token = ENV.fetch("SECUPDATER_GITLAB_TOKEN") { raise Secupdater::Error, "SECUPDATER_GITLAB_TOKEN isn't set" }
|
|
69
|
+
logger = Secupdater.build_logger(log_level: verbose ? :info : :warn)
|
|
70
|
+
|
|
71
|
+
discovery = Discovery::Gitlab.new(token, group:, topic:, logger:)
|
|
72
|
+
discovery.call(output)
|
|
73
|
+
|
|
74
|
+
exit(1) if discovery.failures.any?
|
|
75
|
+
rescue Secupdater::Error => e
|
|
76
|
+
err.puts e.message
|
|
77
|
+
exit(1)
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
51
81
|
register "run", Run
|
|
82
|
+
register "discover", Discover
|
|
52
83
|
end
|
|
53
84
|
end
|
|
54
85
|
end
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
require "fileutils"
|
|
2
|
+
require "net/http"
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
5
|
+
module Secupdater
|
|
6
|
+
module Discovery
|
|
7
|
+
# Finds the projects in a GitLab group that have opted in to secupdater with a
|
|
8
|
+
# topic, so one central pipeline can audit and update them all. For each one
|
|
9
|
+
# it writes, to <output>/<project id>/, the files bundle-audit needs:
|
|
10
|
+
# Gemfile.lock, and .bundler-audit.yml if the project has one (so its ignored
|
|
11
|
+
# advisories stay ignored). Then <output>/projects.txt lists them, a line each:
|
|
12
|
+
#
|
|
13
|
+
# <id> <path> <default branch> <Ruby version> <open secupdater MRs> <clone URL>
|
|
14
|
+
#
|
|
15
|
+
# None of those fields can contain spaces.
|
|
16
|
+
class Gitlab
|
|
17
|
+
Project = Struct.new(:id, :path, :default_branch, :ruby_version, :open_merge_requests, :clone_url, keyword_init: true) do
|
|
18
|
+
def to_line = [id, path, default_branch, ruby_version, open_merge_requests, clone_url].join(" ")
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# secupdater uses `it` block parameters
|
|
22
|
+
MINIMUM_RUBY = Gem::Version.new("3.4")
|
|
23
|
+
|
|
24
|
+
# As Publisher::Gitlab::LABEL
|
|
25
|
+
LABEL = "secupdater"
|
|
26
|
+
|
|
27
|
+
attr_reader :access_token, :group, :topic, :logger, :failures
|
|
28
|
+
|
|
29
|
+
def initialize(access_token, group:, topic:, logger:)
|
|
30
|
+
@access_token = access_token
|
|
31
|
+
@group = group
|
|
32
|
+
@topic = topic
|
|
33
|
+
@logger = logger
|
|
34
|
+
@failures = []
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Returns the projects it listed. Projects it couldn't check are logged and
|
|
38
|
+
# left out, and recorded in #failures.
|
|
39
|
+
def call(output)
|
|
40
|
+
FileUtils.mkdir_p(output)
|
|
41
|
+
|
|
42
|
+
projects = Net::HTTP.start("gitlab.com", 443, use_ssl: true) do |http|
|
|
43
|
+
group_projects(http).filter_map do |project|
|
|
44
|
+
discover(http, project, output)
|
|
45
|
+
rescue Secupdater::Error => e
|
|
46
|
+
logger.error "#{project["path_with_namespace"]}: #{e.message}"
|
|
47
|
+
failures << project["path_with_namespace"]
|
|
48
|
+
nil
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
File.write(File.join(output, "projects.txt"), projects.map { "#{it.to_line}\n" }.join)
|
|
53
|
+
projects
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
private
|
|
57
|
+
|
|
58
|
+
def discover(http, project, output)
|
|
59
|
+
path = project["path_with_namespace"]
|
|
60
|
+
branch = project["default_branch"]
|
|
61
|
+
return skip(:info, "#{path}: skipped, it has no default branch (empty repository?)") unless branch
|
|
62
|
+
|
|
63
|
+
lockfile = file(http, project, "Gemfile.lock")
|
|
64
|
+
return skip(:info, "#{path}: skipped, it has no Gemfile.lock") unless lockfile
|
|
65
|
+
|
|
66
|
+
ruby_version = ruby_version(http, project)
|
|
67
|
+
unless ruby_version
|
|
68
|
+
return skip(:warn, "#{path}: skipped, its Ruby version can't be found in .tool-versions or .ruby-version")
|
|
69
|
+
end
|
|
70
|
+
if Gem::Version.new(ruby_version) < MINIMUM_RUBY
|
|
71
|
+
return skip(:warn, "#{path}: skipped, secupdater needs Ruby #{MINIMUM_RUBY} or later, and it uses #{ruby_version}")
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
dir = File.join(output, project["id"].to_s)
|
|
75
|
+
FileUtils.mkdir_p(dir)
|
|
76
|
+
File.write(File.join(dir, "Gemfile.lock"), lockfile)
|
|
77
|
+
audit_config = file(http, project, ".bundler-audit.yml")
|
|
78
|
+
File.write(File.join(dir, ".bundler-audit.yml"), audit_config) if audit_config
|
|
79
|
+
|
|
80
|
+
Project.new(
|
|
81
|
+
id: project["id"],
|
|
82
|
+
path:,
|
|
83
|
+
default_branch: branch,
|
|
84
|
+
ruby_version:,
|
|
85
|
+
open_merge_requests: open_merge_requests(http, project),
|
|
86
|
+
clone_url: project["http_url_to_repo"]
|
|
87
|
+
).tap { logger.info "#{path}: found, Ruby #{ruby_version}, #{it.open_merge_requests} open secupdater MRs" }
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def skip(level, message)
|
|
91
|
+
logger.public_send(level, message)
|
|
92
|
+
nil
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# The group's own projects (not ones shared into it) with the topic. The
|
|
96
|
+
# topic is checked here as well as in the query, so a GitLab that ignores
|
|
97
|
+
# the query parameter can't widen it.
|
|
98
|
+
def group_projects(http)
|
|
99
|
+
(1..).lazy.map do |page|
|
|
100
|
+
query = URI.encode_www_form(include_subgroups: true, with_shared: false, archived: false, topic:, per_page: 100, page:)
|
|
101
|
+
JSON.parse(get(http, "groups/#{URI.encode_www_form_component(group)}/projects?#{query}").body)
|
|
102
|
+
end.take_while(&:any?).flat_map { it }.select { it["topics"].to_a.include?(topic) }.to_a
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def file(http, project, name)
|
|
106
|
+
query = URI.encode_www_form(ref: project["default_branch"])
|
|
107
|
+
get(http, "projects/#{project["id"]}/repository/files/#{URI.encode_www_form_component(name)}/raw?#{query}", allow_missing: true)&.body
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
VERSION_PATTERN = /\A\d+\.\d+(\.\d+)?\z/
|
|
111
|
+
|
|
112
|
+
def ruby_version(http, project)
|
|
113
|
+
version = file(http, project, ".tool-versions")&.then { it[/^ruby\s+(\S+)/, 1] }
|
|
114
|
+
version ||= file(http, project, ".ruby-version")&.strip&.delete_prefix("ruby-")
|
|
115
|
+
version if version&.match?(VERSION_PATTERN)
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
# Up to 100, which is plenty to know whether it has any
|
|
119
|
+
def open_merge_requests(http, project)
|
|
120
|
+
query = URI.encode_www_form(state: "opened", labels: LABEL, target_branch: project["default_branch"], per_page: 100)
|
|
121
|
+
JSON.parse(get(http, "projects/#{project["id"]}/merge_requests?#{query}").body).size
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def get(http, path, allow_missing: false)
|
|
125
|
+
req = Net::HTTP::Get.new("/api/v4/#{path}", "PRIVATE-TOKEN" => access_token)
|
|
126
|
+
res = http.request(req)
|
|
127
|
+
|
|
128
|
+
return if allow_missing && res.is_a?(Net::HTTPNotFound)
|
|
129
|
+
|
|
130
|
+
raise Secupdater::Error, "GitLab GET #{req.path} returned #{res.code}: #{res.body}" unless res.is_a?(Net::HTTPSuccess)
|
|
131
|
+
|
|
132
|
+
res
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
end
|
|
@@ -68,7 +68,7 @@ module Secupdater
|
|
|
68
68
|
advisory_id = advisory["id"] if advisory_id.empty?
|
|
69
69
|
|
|
70
70
|
SecurityAdvisory.new(result["gem"]["name"], advisory_id, advisory["patched_versions"])
|
|
71
|
-
end
|
|
71
|
+
end.uniq # bundle-audit reports an advisory once per platform the gem is locked for
|
|
72
72
|
end
|
|
73
73
|
|
|
74
74
|
def satisfies_requirement?(version, requirement)
|
|
@@ -101,6 +101,10 @@ module Secupdater
|
|
|
101
101
|
# entrypoint is `bundle exec`), undo what that set up so bundle lock works on
|
|
102
102
|
# the Gemfile and Gemfile.lock here. PATH is left alone, as callers may have
|
|
103
103
|
# changed it since.
|
|
104
|
+
#
|
|
105
|
+
# bundle lock evaluates the Gemfile, which is Ruby from the repo being
|
|
106
|
+
# updated, so it doesn't get the GitLab token (which, run centrally, can
|
|
107
|
+
# reach every opted-in project).
|
|
104
108
|
def bundle_env
|
|
105
109
|
original = Bundler.original_env
|
|
106
110
|
rubyopt = original["RUBYOPT"]&.gsub(%r{-r\S*bundler/setup\S*}, "")&.strip
|
|
@@ -109,7 +113,8 @@ module Secupdater
|
|
|
109
113
|
"BUNDLE_FROZEN" => "false",
|
|
110
114
|
"BUNDLE_GEMFILE" => File.expand_path("Gemfile"),
|
|
111
115
|
"BUNDLE_LOCKFILE" => nil,
|
|
112
|
-
"RUBYOPT" => rubyopt.nil? || rubyopt.empty? ? nil : rubyopt
|
|
116
|
+
"RUBYOPT" => rubyopt.nil? || rubyopt.empty? ? nil : rubyopt,
|
|
117
|
+
"SECUPDATER_GITLAB_TOKEN" => nil
|
|
113
118
|
)
|
|
114
119
|
end
|
|
115
120
|
end
|
|
@@ -29,22 +29,36 @@ module Secupdater
|
|
|
29
29
|
|
|
30
30
|
if branch_exists?(http)
|
|
31
31
|
others = other_authors(http)
|
|
32
|
-
|
|
32
|
+
if others.any?
|
|
33
|
+
explain_skipping(http, open_merge_request, others) if open_merge_request
|
|
34
|
+
return [:skipped, "#{reference} has commits from #{others.join(", ")}, so it won't be updated"]
|
|
35
|
+
end
|
|
33
36
|
|
|
34
|
-
up_to_date = branch_has_changes?(http)
|
|
37
|
+
up_to_date = branch_has_changes?(http) && !target_manifests_moved_on?(http)
|
|
35
38
|
commit(http, force: true) unless up_to_date
|
|
36
39
|
else
|
|
37
40
|
commit(http)
|
|
38
41
|
end
|
|
42
|
+
committed = !up_to_date
|
|
39
43
|
|
|
40
44
|
if open_merge_request
|
|
41
45
|
remember(open_merge_request)
|
|
42
|
-
|
|
46
|
+
|
|
47
|
+
# e.g. when this update has since absorbed another dependency's advisories
|
|
48
|
+
described = open_merge_request["description"] == description
|
|
49
|
+
request(http, Net::HTTP::Put, "merge_requests/#{open_merge_request["iid"]}", { description: description }) unless described
|
|
43
50
|
else
|
|
44
|
-
|
|
51
|
+
open_merge_request = JSON.parse(create_merge_request(http).body)
|
|
52
|
+
remember(open_merge_request)
|
|
45
53
|
end
|
|
46
54
|
|
|
47
|
-
|
|
55
|
+
# A commit cancels auto-merge, and its pipeline won't have started
|
|
56
|
+
# yet, so a run that commits leaves auto-merge to the next run
|
|
57
|
+
message = enable_auto_merge(http, open_merge_request["iid"]) unless committed
|
|
58
|
+
|
|
59
|
+
return [:up_to_date, message] if up_to_date && described
|
|
60
|
+
|
|
61
|
+
[:published, message]
|
|
48
62
|
end
|
|
49
63
|
end
|
|
50
64
|
|
|
@@ -84,8 +98,12 @@ module Secupdater
|
|
|
84
98
|
request(http, Net::HTTP::Post, "repository/commits", data)
|
|
85
99
|
end
|
|
86
100
|
|
|
101
|
+
# Against every dependency the MR fixes advisories for, so finish closes
|
|
102
|
+
# their own MRs once another update has absorbed them
|
|
87
103
|
def remember(merge_request)
|
|
88
|
-
|
|
104
|
+
[bump.dependency.name, *advisories.map(&:name)].uniq.each do
|
|
105
|
+
@current_merge_requests[it] = merge_request["iid"]
|
|
106
|
+
end
|
|
89
107
|
end
|
|
90
108
|
|
|
91
109
|
def find_open_secupdater_merge_requests(http)
|
|
@@ -93,6 +111,45 @@ module Secupdater
|
|
|
93
111
|
JSON.parse(request(http, Net::HTTP::Get, "merge_requests?#{query}").body)
|
|
94
112
|
end
|
|
95
113
|
|
|
114
|
+
# Hidden in the rendered comment; marks the comment so it's only posted once
|
|
115
|
+
SKIPPING_MARKER = "<!-- secupdater:skipping -->"
|
|
116
|
+
|
|
117
|
+
# Says on the MR, once, that secupdater has stopped updating it, so whoever
|
|
118
|
+
# committed knows rebasing and refreshing it is now up to them
|
|
119
|
+
def explain_skipping(http, merge_request, others)
|
|
120
|
+
notes_path = "merge_requests/#{merge_request["iid"]}/notes"
|
|
121
|
+
notes = JSON.parse(request(http, Net::HTTP::Get, "#{notes_path}?#{URI.encode_www_form(per_page: 100)}").body)
|
|
122
|
+
return if notes.any? { it.dig("author", "id") == own_user(http)["id"] && it["body"].to_s.include?(SKIPPING_MARKER) }
|
|
123
|
+
|
|
124
|
+
body = <<~BODY.chomp
|
|
125
|
+
#{SKIPPING_MARKER}
|
|
126
|
+
This branch has commits from #{others.join(", ")}, so secupdater won't update it any more, \
|
|
127
|
+
including rebasing it when `#{target_branch}` changes.
|
|
128
|
+
|
|
129
|
+
To hand it back, delete the branch, or force-push it without those commits, and secupdater will take it over on its next run.
|
|
130
|
+
BODY
|
|
131
|
+
request(http, Net::HTTP::Post, notes_path, { body: })
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
# GitLab doesn't document what auto-merge does without a pipeline, so it
|
|
135
|
+
# is only set once the branch's head commit has a pipeline that can still
|
|
136
|
+
# pass. Returns a message if GitLab won't set it (e.g. the MR can't merge).
|
|
137
|
+
def enable_auto_merge(http, iid)
|
|
138
|
+
merge_request = JSON.parse(request(http, Net::HTTP::Get, "merge_requests/#{iid}").body)
|
|
139
|
+
return if merge_request["merge_when_pipeline_succeeds"] || merge_request["draft"]
|
|
140
|
+
|
|
141
|
+
pipeline = merge_request["head_pipeline"]
|
|
142
|
+
return unless pipeline && pipeline["sha"] == merge_request["sha"]
|
|
143
|
+
return if %w[failed canceled skipped].include?(pipeline["status"])
|
|
144
|
+
|
|
145
|
+
# sha makes GitLab refuse (409) if the branch has moved on since
|
|
146
|
+
data = { auto_merge: true, sha: merge_request["sha"], should_remove_source_branch: true }
|
|
147
|
+
res = request(http, Net::HTTP::Put, "merge_requests/#{iid}/merge", data, tolerate: [405, 406, 409, 422])
|
|
148
|
+
return if res.is_a?(Net::HTTPSuccess)
|
|
149
|
+
|
|
150
|
+
"#{reference}: couldn't set !#{iid} to auto-merge, GitLab returned #{res.code}: #{res.body}"
|
|
151
|
+
end
|
|
152
|
+
|
|
96
153
|
def close_merge_request(http, iid, reason)
|
|
97
154
|
request(http, Net::HTTP::Post, "merge_requests/#{iid}/notes", { body: reason })
|
|
98
155
|
request(http, Net::HTTP::Put, "merge_requests/#{iid}", { state_event: "close" })
|
|
@@ -129,10 +186,12 @@ module Secupdater
|
|
|
129
186
|
end
|
|
130
187
|
|
|
131
188
|
def own_emails(http)
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
189
|
+
own_user(http).values_at("email", "commit_email", "public_email").reject { it.nil? || it.empty? }
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
# Whoever owns the access token
|
|
193
|
+
def own_user(http)
|
|
194
|
+
@own_user ||= JSON.parse(api_request(http, Net::HTTP::Get, "user").body)
|
|
136
195
|
end
|
|
137
196
|
|
|
138
197
|
def branch_has_changes?(http)
|
|
@@ -143,11 +202,23 @@ module Secupdater
|
|
|
143
202
|
end
|
|
144
203
|
end
|
|
145
204
|
|
|
146
|
-
|
|
147
|
-
|
|
205
|
+
# Whether a manifest has changed on the target branch since the branch was
|
|
206
|
+
# cut from it. The update can come out the same even then (e.g. when another
|
|
207
|
+
# MR made some of the same bumps), but the branch would show changes the
|
|
208
|
+
# target already has, and may conflict.
|
|
209
|
+
def target_manifests_moved_on?(http)
|
|
210
|
+
query = URI.encode_www_form(from: reference, to: start_sha)
|
|
211
|
+
diffs = JSON.parse(request(http, Net::HTTP::Get, "repository/compare?#{query}").body)["diffs"]
|
|
212
|
+
|
|
213
|
+
diffs.any? { ecosystem.manifest_paths.intersect?(it.values_at("old_path", "new_path")) }
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
def request(http, type, path, data = nil, allow_missing: false, tolerate: [])
|
|
217
|
+
api_request(http, type, "projects/#{URI.encode_www_form_component(project_id)}/#{path}", data, allow_missing:, tolerate:)
|
|
148
218
|
end
|
|
149
219
|
|
|
150
|
-
|
|
220
|
+
# tolerate: status codes to return the response for instead of raising
|
|
221
|
+
def api_request(http, type, path, data = nil, allow_missing: false, tolerate: [])
|
|
151
222
|
req = type.new(
|
|
152
223
|
"/api/v4/#{path}",
|
|
153
224
|
"PRIVATE-TOKEN" => access_token,
|
|
@@ -158,6 +229,7 @@ module Secupdater
|
|
|
158
229
|
res = http.request(req)
|
|
159
230
|
|
|
160
231
|
return if allow_missing && res.is_a?(Net::HTTPNotFound)
|
|
232
|
+
return res if tolerate.include?(res.code.to_i)
|
|
161
233
|
|
|
162
234
|
unless res.is_a?(Net::HTTPSuccess)
|
|
163
235
|
raise Secupdater::PublishFailed, "GitLab #{req.method} #{req.path} returned #{res.code}: #{res.body}"
|
data/lib/secupdater/publisher.rb
CHANGED
|
@@ -30,7 +30,10 @@ module Secupdater
|
|
|
30
30
|
|
|
31
31
|
def description
|
|
32
32
|
heading = "Upgrade #{ecosystem.ecosystem_name} dependency #{bump.dependency.name} to version #{bump.to} to address the following security advisories:"
|
|
33
|
-
|
|
33
|
+
# Advisories for other dependencies this update also fixes say which one
|
|
34
|
+
items = advisories.map do |advisory|
|
|
35
|
+
advisory.name == bump.dependency.name ? "* #{advisory.report_id}" : "* #{advisory.report_id} (#{advisory.name})"
|
|
36
|
+
end.join("\n")
|
|
34
37
|
"#{heading}\n\n#{items}\n"
|
|
35
38
|
end
|
|
36
39
|
|