secupdater 0.2.0alpha3 → 0.3.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/lib/secupdater/ecosystem/rubygems.rb +43 -2
- data/lib/secupdater/publisher/gitlab.rb +129 -27
- data/lib/secupdater/publisher.rb +10 -0
- data/lib/secupdater/update.rb +13 -3
- data/lib/secupdater/version.rb +1 -1
- data/lib/secupdater.rb +3 -0
- data/spec/e2e/secupdater_spec.rb +16 -1
- data/spec/secupdater/ecosystem/rubygems_spec.rb +125 -8
- data/spec/secupdater/publisher/gitlab_spec.rb +243 -31
- data/spec/secupdater/update_spec.rb +78 -2
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: f3e2a3d2fd687a912cd17a2dab8216d5c7187693c5239ccfdeac56ce87a825ac
|
|
4
|
+
data.tar.gz: 6db3dd9e3f87b76f96e75b3849c9d447ac24a1f0ea31972d24a37cdc20437ee6
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 66aabee38119a94edf9f609ba05d6d8c59581a7994f3f8c3a089f836c8460af124a5a34b08f44418dd9588439b590204864970fad3faf492e05a885f605ae329
|
|
7
|
+
data.tar.gz: 3f8607eca5779819b70d6346365dfa70ec158af70bc9f5c8a3d6c2c3ea9bef3a53839708c9eee388ff942369cc5303c03af958db07013dfb9d85ec04d162155a
|
|
@@ -6,6 +6,9 @@ module Secupdater
|
|
|
6
6
|
class Ecosystem
|
|
7
7
|
class Rubygems < Secupdater::Ecosystem
|
|
8
8
|
FROZEN_EXIT_CODE = 16
|
|
9
|
+
# Bundler failures that will happen the same way at every update level:
|
|
10
|
+
# GemfileNotFound, GitError, PathError, GemspecError, HTTPError, RubyVersionMismatch
|
|
11
|
+
UNRETRYABLE_EXIT_CODES = [10, 11, 13, 14, 17, 18].freeze
|
|
9
12
|
|
|
10
13
|
def self.ecosystem_name = "RubyGems"
|
|
11
14
|
|
|
@@ -32,14 +35,16 @@ module Secupdater
|
|
|
32
35
|
end
|
|
33
36
|
|
|
34
37
|
def run_update(names, level)
|
|
35
|
-
output, status = Open3.capture2e(
|
|
38
|
+
output, status = Open3.capture2e(bundle_env, "bundle", *bundler_version_arg, "lock", "--update", *Array(names), "--conservative", "--#{level}", "--strict")
|
|
36
39
|
|
|
37
40
|
if status.exitstatus == FROZEN_EXIT_CODE
|
|
38
41
|
raise Secupdater::Error, "bundle lock can't change Gemfile.lock (is BUNDLE_DEPLOYMENT set?)\n#{output}"
|
|
39
42
|
end
|
|
40
43
|
|
|
41
44
|
unless status.success?
|
|
42
|
-
|
|
45
|
+
error_class = UNRETRYABLE_EXIT_CODES.include?(status.exitstatus) ? Secupdater::UnretryableUpdateFailed : Secupdater::UpdateCommandFailed
|
|
46
|
+
|
|
47
|
+
raise error_class.new(
|
|
43
48
|
"bundle lock command returned #{status.exitstatus}",
|
|
44
49
|
status.exitstatus,
|
|
45
50
|
output
|
|
@@ -71,6 +76,42 @@ module Secupdater
|
|
|
71
76
|
ver = Gem::Version.create(version)
|
|
72
77
|
req.satisfied_by?(ver)
|
|
73
78
|
end
|
|
79
|
+
|
|
80
|
+
private
|
|
81
|
+
|
|
82
|
+
def bundler_version_arg
|
|
83
|
+
version = Bundler::LockfileParser.new(Bundler.read_file("Gemfile.lock")).bundler_version
|
|
84
|
+
return [] unless version
|
|
85
|
+
|
|
86
|
+
install_bundler(version)
|
|
87
|
+
["_#{version}_"]
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def install_bundler(version)
|
|
91
|
+
@installed_bundlers ||= Set.new
|
|
92
|
+
return if @installed_bundlers.include?(version)
|
|
93
|
+
|
|
94
|
+
output, status = Open3.capture2e(bundle_env, "gem", "install", "bundler", "--version", version.to_s, "--conservative", "--no-document")
|
|
95
|
+
raise Secupdater::Error, "Couldn't install Bundler #{version}, which Gemfile.lock was bundled with\n#{output}" unless status.success?
|
|
96
|
+
|
|
97
|
+
@installed_bundlers << version
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# When secupdater itself runs under `bundle exec` (e.g. an image whose
|
|
101
|
+
# entrypoint is `bundle exec`), undo what that set up so bundle lock works on
|
|
102
|
+
# the Gemfile and Gemfile.lock here. PATH is left alone, as callers may have
|
|
103
|
+
# changed it since.
|
|
104
|
+
def bundle_env
|
|
105
|
+
original = Bundler.original_env
|
|
106
|
+
rubyopt = original["RUBYOPT"]&.gsub(%r{-r\S*bundler/setup\S*}, "")&.strip
|
|
107
|
+
|
|
108
|
+
(Bundler::EnvironmentPreserver::BUNDLER_KEYS - %w[PATH MANPATH]).to_h { [it, original[it]] }.merge(
|
|
109
|
+
"BUNDLE_FROZEN" => "false",
|
|
110
|
+
"BUNDLE_GEMFILE" => File.expand_path("Gemfile"),
|
|
111
|
+
"BUNDLE_LOCKFILE" => nil,
|
|
112
|
+
"RUBYOPT" => rubyopt.nil? || rubyopt.empty? ? nil : rubyopt
|
|
113
|
+
)
|
|
114
|
+
end
|
|
74
115
|
end
|
|
75
116
|
end
|
|
76
117
|
end
|
|
@@ -11,45 +11,145 @@ module Secupdater
|
|
|
11
11
|
@project_id = project_id
|
|
12
12
|
@target_branch = target_branch
|
|
13
13
|
@start_sha = start_sha
|
|
14
|
+
@current_merge_requests = {}
|
|
14
15
|
end
|
|
15
16
|
|
|
17
|
+
# Added to every MR secupdater opens. Closing one declines that version of
|
|
18
|
+
# that dependency; reopening it or removing the label undoes that.
|
|
19
|
+
LABEL = "secupdater"
|
|
20
|
+
|
|
16
21
|
def run
|
|
17
22
|
Net::HTTP.start("gitlab.com", 443, use_ssl: true) do |http|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
23
|
+
merge_requests = find_merge_requests(http)
|
|
24
|
+
open_merge_request = merge_requests.find { it["state"] == "opened" }
|
|
25
|
+
|
|
26
|
+
if !open_merge_request && (previous = merge_requests.find { it["labels"].include?(LABEL) })
|
|
27
|
+
return [:declined, "#{reference} was #{previous["state"]} in !#{previous["iid"]}, reopen it or remove its #{LABEL} label to have it updated again"]
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
if branch_exists?(http)
|
|
31
|
+
others = other_authors(http)
|
|
32
|
+
return [:skipped, "#{reference} has commits from #{others.join(", ")}, so it won't be updated"] if others.any?
|
|
33
|
+
|
|
34
|
+
up_to_date = branch_has_changes?(http)
|
|
35
|
+
commit(http, force: true) unless up_to_date
|
|
36
|
+
else
|
|
37
|
+
commit(http)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
if open_merge_request
|
|
41
|
+
remember(open_merge_request)
|
|
42
|
+
return [:up_to_date, nil] if up_to_date
|
|
43
|
+
else
|
|
44
|
+
remember(JSON.parse(create_merge_request(http).body))
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
[:published, nil]
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Closes open secupdater MRs for a dependency that this run opened or kept a
|
|
52
|
+
# different MR for, or that no longer has any advisories. MRs for
|
|
53
|
+
# dependencies this run couldn't update are left alone.
|
|
54
|
+
def finish(vulnerable_names)
|
|
55
|
+
Net::HTTP.start("gitlab.com", 443, use_ssl: true) do |http|
|
|
56
|
+
find_open_secupdater_merge_requests(http).filter_map do |merge_request|
|
|
57
|
+
name = merge_request["source_branch"][REFERENCE_PATTERN, :name]
|
|
58
|
+
current = @current_merge_requests[name]
|
|
59
|
+
|
|
60
|
+
reason = if current && current != merge_request["iid"]
|
|
61
|
+
"Superseded by !#{current}."
|
|
62
|
+
elsif name && !vulnerable_names.include?(name)
|
|
63
|
+
"#{name} no longer has any security advisories on #{target_branch}, so this is no longer needed."
|
|
64
|
+
end
|
|
65
|
+
next unless reason
|
|
66
|
+
|
|
67
|
+
close_merge_request(http, merge_request["iid"], reason)
|
|
68
|
+
"closed !#{merge_request["iid"]} (#{merge_request["source_branch"]}): #{reason}"
|
|
69
|
+
end
|
|
39
70
|
end
|
|
40
71
|
end
|
|
41
72
|
|
|
42
73
|
private
|
|
43
74
|
|
|
44
|
-
def
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
75
|
+
def commit(http, force: false)
|
|
76
|
+
data = {
|
|
77
|
+
branch: reference,
|
|
78
|
+
start_sha: start_sha,
|
|
79
|
+
commit_message: title,
|
|
80
|
+
actions: changes.map { { action: it.type, file_path: it.filename, content: it.content }.compact }
|
|
81
|
+
}
|
|
82
|
+
data[:force] = true if force
|
|
83
|
+
|
|
84
|
+
request(http, Net::HTTP::Post, "repository/commits", data)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
def remember(merge_request)
|
|
88
|
+
@current_merge_requests[bump.dependency.name] = merge_request["iid"]
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def find_open_secupdater_merge_requests(http)
|
|
92
|
+
query = URI.encode_www_form(state: "opened", labels: LABEL, target_branch: target_branch, per_page: 100)
|
|
93
|
+
JSON.parse(request(http, Net::HTTP::Get, "merge_requests?#{query}").body)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
def close_merge_request(http, iid, reason)
|
|
97
|
+
request(http, Net::HTTP::Post, "merge_requests/#{iid}/notes", { body: reason })
|
|
98
|
+
request(http, Net::HTTP::Put, "merge_requests/#{iid}", { state_event: "close" })
|
|
48
99
|
end
|
|
49
100
|
|
|
50
|
-
def
|
|
101
|
+
def create_merge_request(http)
|
|
102
|
+
data = {
|
|
103
|
+
source_branch: reference,
|
|
104
|
+
target_branch: target_branch,
|
|
105
|
+
title: title,
|
|
106
|
+
description: description,
|
|
107
|
+
remove_source_branch: true,
|
|
108
|
+
labels: [LABEL]
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
request(http, Net::HTTP::Post, "merge_requests", data)
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def branch_exists?(http)
|
|
115
|
+
!request(http, Net::HTTP::Get, "repository/branches/#{URI.encode_www_form_component(reference)}", allow_missing: true).nil?
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def find_merge_requests(http)
|
|
119
|
+
query = URI.encode_www_form(source_branch: reference, target_branch: target_branch)
|
|
120
|
+
JSON.parse(request(http, Net::HTTP::Get, "merge_requests?#{query}").body)
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# Authors of commits on the branch, other than whoever owns the access token
|
|
124
|
+
def other_authors(http)
|
|
125
|
+
query = URI.encode_www_form(from: target_branch, to: reference)
|
|
126
|
+
commits = JSON.parse(request(http, Net::HTTP::Get, "repository/compare?#{query}").body)["commits"]
|
|
127
|
+
|
|
128
|
+
commits.reject { own_emails(http).include?(it["author_email"]) }.map { it["author_name"] }.uniq
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def own_emails(http)
|
|
132
|
+
@own_emails ||= begin
|
|
133
|
+
user = JSON.parse(api_request(http, Net::HTTP::Get, "user").body)
|
|
134
|
+
user.values_at("email", "commit_email", "public_email").reject { it.nil? || it.empty? }
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def branch_has_changes?(http)
|
|
139
|
+
changes.all? do |change|
|
|
140
|
+
query = URI.encode_www_form(ref: reference)
|
|
141
|
+
res = request(http, Net::HTTP::Get, "repository/files/#{URI.encode_www_form_component(change.filename)}/raw?#{query}", allow_missing: true)
|
|
142
|
+
res&.body == change.content
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def request(http, type, path, data = nil, allow_missing: false)
|
|
147
|
+
api_request(http, type, "projects/#{URI.encode_www_form_component(project_id)}/#{path}", data, allow_missing:)
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
def api_request(http, type, path, data = nil, allow_missing: false)
|
|
51
151
|
req = type.new(
|
|
52
|
-
"/api/v4
|
|
152
|
+
"/api/v4/#{path}",
|
|
53
153
|
"PRIVATE-TOKEN" => access_token,
|
|
54
154
|
"Content-Type" => "application/json"
|
|
55
155
|
)
|
|
@@ -57,6 +157,8 @@ module Secupdater
|
|
|
57
157
|
|
|
58
158
|
res = http.request(req)
|
|
59
159
|
|
|
160
|
+
return if allow_missing && res.is_a?(Net::HTTPNotFound)
|
|
161
|
+
|
|
60
162
|
unless res.is_a?(Net::HTTPSuccess)
|
|
61
163
|
raise Secupdater::PublishFailed, "GitLab #{req.method} #{req.path} returned #{res.code}: #{res.body}"
|
|
62
164
|
end
|
data/lib/secupdater/publisher.rb
CHANGED
|
@@ -3,6 +3,9 @@ module Secupdater
|
|
|
3
3
|
attr_reader :ecosystem, :bump, :advisories, :changes
|
|
4
4
|
|
|
5
5
|
def initialize = raise NotImplementedError
|
|
6
|
+
|
|
7
|
+
# Returns [status, message], with a message explaining any status other
|
|
8
|
+
# than :published (e.g. :declined, :skipped or :up_to_date)
|
|
6
9
|
def run = raise NotImplementedError
|
|
7
10
|
|
|
8
11
|
def call(ecosystem, dependency_bump, advisories, changes)
|
|
@@ -14,6 +17,13 @@ module Secupdater
|
|
|
14
17
|
run
|
|
15
18
|
end
|
|
16
19
|
|
|
20
|
+
# Called once every dependency has been through #call, with the names of all
|
|
21
|
+
# the dependencies that have advisories. Returns messages about anything it
|
|
22
|
+
# tidied up.
|
|
23
|
+
def finish(vulnerable_names) = []
|
|
24
|
+
|
|
25
|
+
REFERENCE_PATTERN = %r{\Asecurity/(?<name>.+)-(?<version>[^-]+)\z}
|
|
26
|
+
|
|
17
27
|
def reference = "security/#{bump.dependency.name}-#{bump.to}"
|
|
18
28
|
|
|
19
29
|
def title = "Upgrade #{ecosystem.ecosystem_name} dependency #{bump.dependency.name} to version #{bump.to}"
|
data/lib/secupdater/update.rb
CHANGED
|
@@ -16,9 +16,13 @@ module Secupdater
|
|
|
16
16
|
missing = deps_needing_checking.keys - original.keys
|
|
17
17
|
raise Secupdater::Error, "Advisories for dependencies not in the lockfile: #{missing.join(', ')}" if missing.any?
|
|
18
18
|
|
|
19
|
-
deps_needing_checking.to_h do |name, dep_advisories|
|
|
19
|
+
results = deps_needing_checking.to_h do |name, dep_advisories|
|
|
20
20
|
[name, update_dependency(original[name], dep_advisories)]
|
|
21
21
|
end
|
|
22
|
+
|
|
23
|
+
publisher&.finish(deps_needing_checking.keys)&.each { logger.info it }
|
|
24
|
+
|
|
25
|
+
results
|
|
22
26
|
end
|
|
23
27
|
|
|
24
28
|
def update_dependency(dep, advisories)
|
|
@@ -42,6 +46,9 @@ module Secupdater
|
|
|
42
46
|
end
|
|
43
47
|
|
|
44
48
|
status
|
|
49
|
+
rescue Secupdater::UnretryableUpdateFailed => e
|
|
50
|
+
logger.error "#{dep.name}: #{e.message}\n#{e.output}"
|
|
51
|
+
:failed
|
|
45
52
|
rescue Secupdater::PublishFailed => e
|
|
46
53
|
logger.error "#{dep.name}: #{e.message}"
|
|
47
54
|
:failed
|
|
@@ -69,8 +76,11 @@ module Secupdater
|
|
|
69
76
|
|
|
70
77
|
return [:resolved, nil] unless publisher
|
|
71
78
|
|
|
72
|
-
publisher.call(ecosystem, bump, advisories, file_changes)
|
|
73
|
-
|
|
79
|
+
status, message = publisher.call(ecosystem, bump, advisories, file_changes)
|
|
80
|
+
logger.warn message if message
|
|
81
|
+
[status, nil]
|
|
82
|
+
rescue Secupdater::UnretryableUpdateFailed
|
|
83
|
+
raise
|
|
74
84
|
rescue Secupdater::UpdateCommandFailed => e
|
|
75
85
|
[:failed, "#{dep.name}: #{e.message}\n#{e.output}"]
|
|
76
86
|
ensure
|
data/lib/secupdater/version.rb
CHANGED
data/lib/secupdater.rb
CHANGED
data/spec/e2e/secupdater_spec.rb
CHANGED
|
@@ -54,6 +54,8 @@ RSpec.describe "secupdater end to end" do
|
|
|
54
54
|
FileUtils.mkdir("bin")
|
|
55
55
|
File.write("bin/bundle", "#!/bin/sh\ncp Gemfile.lock.after Gemfile.lock\n")
|
|
56
56
|
File.chmod(0o755, "bin/bundle")
|
|
57
|
+
File.write("bin/gem", "#!/bin/sh\n")
|
|
58
|
+
File.chmod(0o755, "bin/gem")
|
|
57
59
|
|
|
58
60
|
system("git init -q && git add Gemfile.lock && git -c user.name=test -c user.email=test@example.com commit -qm init", exception: true)
|
|
59
61
|
|
|
@@ -70,10 +72,22 @@ RSpec.describe "secupdater end to end" do
|
|
|
70
72
|
let(:env) { { "SECUPDATER_GITLAB_TOKEN" => "token", "CI_PROJECT_ID" => "123", "CI_DEFAULT_BRANCH" => "main" } }
|
|
71
73
|
|
|
72
74
|
let!(:find_mr_stub) do
|
|
73
|
-
stub_request(:get,
|
|
75
|
+
stub_request(:get, "https://gitlab.com/api/v4/projects/123/merge_requests")
|
|
76
|
+
.with(query: hash_including(source_branch: "security/css_parser-1.1.0"))
|
|
74
77
|
.to_return(status: 200, body: "[]")
|
|
75
78
|
end
|
|
76
79
|
|
|
80
|
+
let!(:find_open_secupdater_mrs_stub) do
|
|
81
|
+
stub_request(:get, "https://gitlab.com/api/v4/projects/123/merge_requests")
|
|
82
|
+
.with(query: hash_including(state: "opened", labels: "secupdater"))
|
|
83
|
+
.to_return(status: 200, body: "[]")
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
let!(:find_branch_stub) do
|
|
87
|
+
stub_request(:get, "https://gitlab.com/api/v4/projects/123/repository/branches/security%2Fcss_parser-1.1.0")
|
|
88
|
+
.to_return(status: 404, body: "{}")
|
|
89
|
+
end
|
|
90
|
+
|
|
77
91
|
let!(:create_commit_stub) do
|
|
78
92
|
stub_request(:post, "https://gitlab.com/api/v4/projects/123/repository/commits")
|
|
79
93
|
.with(body: hash_including(
|
|
@@ -94,6 +108,7 @@ RSpec.describe "secupdater end to end" do
|
|
|
94
108
|
expect { run_cli }.not_to raise_error
|
|
95
109
|
|
|
96
110
|
expect(find_mr_stub).to have_been_requested.once
|
|
111
|
+
expect(find_open_secupdater_mrs_stub).to have_been_requested.once
|
|
97
112
|
expect(create_commit_stub).to have_been_requested.once
|
|
98
113
|
expect(create_mr_stub).to have_been_requested.once
|
|
99
114
|
expect(File.read("Gemfile.lock")).to eq(lockfile("1.0.0"))
|
|
@@ -128,10 +128,82 @@ RSpec.describe Secupdater::Ecosystem::Rubygems do
|
|
|
128
128
|
|
|
129
129
|
describe "#try_update" do
|
|
130
130
|
let(:status) { instance_double(Process::Status, success?: true, exitstatus: 0) }
|
|
131
|
+
let(:original_env) { {} }
|
|
132
|
+
let(:bundle_env) do
|
|
133
|
+
{
|
|
134
|
+
"BUNDLE_BIN_PATH" => nil,
|
|
135
|
+
"BUNDLE_FROZEN" => "false",
|
|
136
|
+
"BUNDLE_GEMFILE" => File.expand_path("Gemfile"),
|
|
137
|
+
"BUNDLE_LOCKFILE" => nil,
|
|
138
|
+
"BUNDLER_SETUP" => nil,
|
|
139
|
+
"BUNDLER_VERSION" => nil,
|
|
140
|
+
"GEM_HOME" => nil,
|
|
141
|
+
"GEM_PATH" => nil,
|
|
142
|
+
"RB_USER_INSTALL" => nil,
|
|
143
|
+
"RUBYLIB" => nil,
|
|
144
|
+
"RUBYOPT" => nil
|
|
145
|
+
}
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
let(:install_status) { instance_double(Process::Status, success?: true, exitstatus: 0) }
|
|
149
|
+
|
|
150
|
+
before do
|
|
151
|
+
allow(Bundler).to receive(:original_env).and_return(original_env)
|
|
152
|
+
allow(Open3).to receive(:capture2e).with(anything, "gem", any_args).and_return(["", install_status])
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
context "lockfile bundled with a Bundler other than the one running" do
|
|
156
|
+
before { allow(Open3).to receive(:capture2e).with(anything, "bundle", any_args).and_return(["", status]) }
|
|
157
|
+
|
|
158
|
+
it "installs that Bundler if it's missing, then runs bundle lock with it" do
|
|
159
|
+
rubygems.try_update("css_parser", level: :patch)
|
|
160
|
+
|
|
161
|
+
expect(Open3).to have_received(:capture2e).with(bundle_env, "gem", "install", "bundler", "--version", "2.5.0", "--conservative", "--no-document").ordered
|
|
162
|
+
expect(Open3).to have_received(:capture2e).with(bundle_env, "bundle", "_2.5.0_", "lock", any_args).ordered
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
it "only installs it once per run" do
|
|
166
|
+
rubygems.try_update("css_parser", level: :patch)
|
|
167
|
+
rubygems.try_update("css_parser", level: :minor)
|
|
168
|
+
|
|
169
|
+
expect(Open3).to have_received(:capture2e).with(anything, "gem", any_args).once
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
context "lockfile without BUNDLED WITH" do
|
|
174
|
+
before do
|
|
175
|
+
File.write("Gemfile.lock", lockfile("1.0.0").sub(/\nBUNDLED WITH\n.*\n\z/, ""))
|
|
176
|
+
allow(Open3).to receive(:capture2e).with(anything, "bundle", any_args).and_return(["", status])
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
it "runs bundle lock with whichever Bundler RubyGems picks, without installing one" do
|
|
180
|
+
rubygems.try_update("css_parser", level: :patch)
|
|
181
|
+
|
|
182
|
+
expect(Open3).not_to have_received(:capture2e).with(anything, "gem", any_args)
|
|
183
|
+
expect(Open3).to have_received(:capture2e).with(bundle_env, "bundle", "lock", "--update", "css_parser", "--conservative", "--patch", "--strict")
|
|
184
|
+
end
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
context "locked Bundler can't be installed" do
|
|
188
|
+
let(:install_status) { instance_double(Process::Status, success?: false, exitstatus: 2) }
|
|
189
|
+
|
|
190
|
+
before do
|
|
191
|
+
allow(Open3).to receive(:capture2e).with(anything, "gem", any_args).and_return(["Could not find a valid gem 'bundler' (= 2.5.0)", install_status])
|
|
192
|
+
allow(Open3).to receive(:capture2e).with(anything, "bundle", any_args)
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
it "raises Secupdater::Error rather than UpdateCommandFailed, so the run stops, and doesn't run bundle lock" do
|
|
196
|
+
expect { rubygems.try_update("css_parser", level: :patch) }.to raise_error(Secupdater::Error) do
|
|
197
|
+
expect(it).not_to be_a(Secupdater::UpdateCommandFailed)
|
|
198
|
+
expect(it.message).to include("Bundler 2.5.0", "Could not find a valid gem")
|
|
199
|
+
end
|
|
200
|
+
expect(Open3).not_to have_received(:capture2e).with(anything, "bundle", any_args)
|
|
201
|
+
end
|
|
202
|
+
end
|
|
131
203
|
|
|
132
204
|
context "command succeeds" do
|
|
133
205
|
before do
|
|
134
|
-
allow(Open3).to receive(:capture2e) do
|
|
206
|
+
allow(Open3).to receive(:capture2e).with(anything, "bundle", any_args) do
|
|
135
207
|
File.write("Gemfile.lock", lockfile("1.1.0"))
|
|
136
208
|
["Bundle updated!", status]
|
|
137
209
|
end
|
|
@@ -140,7 +212,7 @@ RSpec.describe Secupdater::Ecosystem::Rubygems do
|
|
|
140
212
|
it "runs a strict, conservative, unfrozen bundle lock --update at the given level" do
|
|
141
213
|
rubygems.try_update("css_parser", level: :minor)
|
|
142
214
|
|
|
143
|
-
expect(Open3).to have_received(:capture2e).with(
|
|
215
|
+
expect(Open3).to have_received(:capture2e).with(bundle_env, "bundle", "_2.5.0_", "lock", "--update", "css_parser", "--conservative", "--minor", "--strict")
|
|
144
216
|
end
|
|
145
217
|
|
|
146
218
|
it "returns the lockfile change with its new and original content" do
|
|
@@ -151,7 +223,7 @@ RSpec.describe Secupdater::Ecosystem::Rubygems do
|
|
|
151
223
|
end
|
|
152
224
|
|
|
153
225
|
context "command succeeds without changing anything" do
|
|
154
|
-
before { allow(Open3).to receive(:capture2e).and_return(["Bundle updated!", status]) }
|
|
226
|
+
before { allow(Open3).to receive(:capture2e).with(anything, "bundle", any_args).and_return(["Bundle updated!", status]) }
|
|
155
227
|
|
|
156
228
|
it "returns no file changes" do
|
|
157
229
|
expect(rubygems.try_update("css_parser", level: :patch)).to eq([])
|
|
@@ -159,32 +231,77 @@ RSpec.describe Secupdater::Ecosystem::Rubygems do
|
|
|
159
231
|
end
|
|
160
232
|
|
|
161
233
|
context "gem name containing shell metacharacters" do
|
|
162
|
-
before { allow(Open3).to receive(:capture2e).and_return(["", status]) }
|
|
234
|
+
before { allow(Open3).to receive(:capture2e).with(anything, "bundle", any_args).and_return(["", status]) }
|
|
163
235
|
|
|
164
236
|
it "passes it as a single argument without going through a shell" do
|
|
165
237
|
rubygems.try_update("css_parser; touch pwned", level: :patch)
|
|
166
238
|
|
|
167
|
-
expect(Open3).to have_received(:capture2e).with(
|
|
239
|
+
expect(Open3).to have_received(:capture2e).with(bundle_env, "bundle", "_2.5.0_", "lock", "--update", "css_parser; touch pwned", "--conservative", "--patch", "--strict")
|
|
240
|
+
end
|
|
241
|
+
end
|
|
242
|
+
|
|
243
|
+
context "secupdater itself running under bundle exec for another bundle" do
|
|
244
|
+
let(:original_env) do
|
|
245
|
+
{
|
|
246
|
+
"GEM_HOME" => "/usr/local/bundle",
|
|
247
|
+
"BUNDLE_GEMFILE" => "/elsewhere/Gemfile",
|
|
248
|
+
"BUNDLE_LOCKFILE" => "/elsewhere/Gemfile.lock",
|
|
249
|
+
"RUBYOPT" => "-W0 -rbundler/setup"
|
|
250
|
+
}
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
before { allow(Open3).to receive(:capture2e).with(anything, "bundle", any_args).and_return(["", status]) }
|
|
254
|
+
|
|
255
|
+
it "runs bundle lock with Bundler's settings from before bundle exec, on the Gemfile in the working directory" do
|
|
256
|
+
rubygems.try_update("css_parser", level: :patch)
|
|
257
|
+
|
|
258
|
+
expect(Open3).to have_received(:capture2e).with(
|
|
259
|
+
bundle_env.merge("GEM_HOME" => "/usr/local/bundle", "RUBYOPT" => "-W0"),
|
|
260
|
+
"bundle", "_2.5.0_", "lock", "--update", "css_parser", "--conservative", "--patch", "--strict"
|
|
261
|
+
)
|
|
168
262
|
end
|
|
169
263
|
end
|
|
170
264
|
|
|
171
265
|
context "command exits 1" do
|
|
172
266
|
let(:status) { instance_double(Process::Status, success?: false, exitstatus: 1) }
|
|
173
267
|
|
|
174
|
-
before { allow(Open3).to receive(:capture2e).and_return(["Could not find gem 'css_parser'", status]) }
|
|
268
|
+
before { allow(Open3).to receive(:capture2e).with(anything, "bundle", any_args).and_return(["Could not find gem 'css_parser'", status]) }
|
|
175
269
|
|
|
176
|
-
it "raises UpdateCommandFailed with the integer exit code and combined output" do
|
|
270
|
+
it "raises a retryable UpdateCommandFailed with the integer exit code and combined output" do
|
|
177
271
|
expect { rubygems.try_update("css_parser", level: :patch) }.to raise_error(Secupdater::UpdateCommandFailed) do
|
|
272
|
+
expect(it).not_to be_a(Secupdater::UnretryableUpdateFailed)
|
|
178
273
|
expect(it.exit_code).to eq(1)
|
|
179
274
|
expect(it.output).to eq("Could not find gem 'css_parser'")
|
|
180
275
|
end
|
|
181
276
|
end
|
|
182
277
|
end
|
|
183
278
|
|
|
279
|
+
{
|
|
280
|
+
10 => "Gemfile not found",
|
|
281
|
+
11 => "git source couldn't be fetched",
|
|
282
|
+
13 => "path source not found",
|
|
283
|
+
14 => "gemspec invalid",
|
|
284
|
+
17 => "HTTP error",
|
|
285
|
+
18 => "Ruby version mismatch"
|
|
286
|
+
}.each do |exit_code, reason|
|
|
287
|
+
context "command exits #{exit_code} (#{reason})" do
|
|
288
|
+
let(:status) { instance_double(Process::Status, success?: false, exitstatus: exit_code) }
|
|
289
|
+
|
|
290
|
+
before { allow(Open3).to receive(:capture2e).with(anything, "bundle", any_args).and_return([reason, status]) }
|
|
291
|
+
|
|
292
|
+
it "raises UnretryableUpdateFailed with the exit code and output" do
|
|
293
|
+
expect { rubygems.try_update("css_parser", level: :patch) }.to raise_error(Secupdater::UnretryableUpdateFailed) do
|
|
294
|
+
expect(it.exit_code).to eq(exit_code)
|
|
295
|
+
expect(it.output).to eq(reason)
|
|
296
|
+
end
|
|
297
|
+
end
|
|
298
|
+
end
|
|
299
|
+
end
|
|
300
|
+
|
|
184
301
|
context "command exits 16 (frozen lockfile)" do
|
|
185
302
|
let(:status) { instance_double(Process::Status, success?: false, exitstatus: 16) }
|
|
186
303
|
|
|
187
|
-
before { allow(Open3).to receive(:capture2e).and_return(["frozen mode is set", status]) }
|
|
304
|
+
before { allow(Open3).to receive(:capture2e).with(anything, "bundle", any_args).and_return(["frozen mode is set", status]) }
|
|
188
305
|
|
|
189
306
|
it "raises Secupdater::Error rather than UpdateCommandFailed, so the run stops" do
|
|
190
307
|
expect { rubygems.try_update("css_parser", level: :patch) }.to raise_error(Secupdater::Error) do
|
|
@@ -11,6 +11,10 @@ RSpec.describe Secupdater::Publisher::Gitlab do
|
|
|
11
11
|
let(:bump) { Secupdater::DependencySpecDiff.new(dep, "1.0.0", "1.1.0") }
|
|
12
12
|
let(:changes) { [Secupdater::FileChange.new("Gemfile.lock", "update", "abc", "xyz")] }
|
|
13
13
|
|
|
14
|
+
let(:api) { "https://gitlab.com/api/v4" }
|
|
15
|
+
let(:project_api) { "#{api}/projects/123" }
|
|
16
|
+
let(:bot_email) { "group_1_bot_abc@noreply.gitlab.com" }
|
|
17
|
+
|
|
14
18
|
let(:headers) do
|
|
15
19
|
{
|
|
16
20
|
"PRIVATE-TOKEN" => "access_token",
|
|
@@ -19,59 +23,95 @@ RSpec.describe Secupdater::Publisher::Gitlab do
|
|
|
19
23
|
end
|
|
20
24
|
|
|
21
25
|
let(:existing_merge_requests) { [] }
|
|
26
|
+
let(:branch_exists) { false }
|
|
27
|
+
let(:branch_commits) { [{ author_name: "release-utilities", author_email: bot_email }] }
|
|
28
|
+
let(:branch_lockfile) { "old" }
|
|
29
|
+
|
|
30
|
+
let(:commit_body) do
|
|
31
|
+
{
|
|
32
|
+
branch: "security/css_parser-1.1.0",
|
|
33
|
+
start_sha: "abc123",
|
|
34
|
+
commit_message: "Upgrade RubyGems dependency css_parser to version 1.1.0",
|
|
35
|
+
actions: [{ action: "update", file_path: "Gemfile.lock", content: "abc" }]
|
|
36
|
+
}
|
|
37
|
+
end
|
|
22
38
|
|
|
23
39
|
let!(:find_mr_stub) do
|
|
24
|
-
stub_request(:get, "
|
|
25
|
-
.with(query: { source_branch: "security/css_parser-1.1.0",
|
|
40
|
+
stub_request(:get, "#{project_api}/merge_requests")
|
|
41
|
+
.with(query: { source_branch: "security/css_parser-1.1.0", target_branch: "main" }, headers: headers)
|
|
26
42
|
.to_return(status: 200, body: existing_merge_requests.to_json)
|
|
27
43
|
end
|
|
28
44
|
|
|
45
|
+
let!(:find_branch_stub) do
|
|
46
|
+
stub_request(:get, "#{project_api}/repository/branches/security%2Fcss_parser-1.1.0")
|
|
47
|
+
.to_return(status: branch_exists ? 200 : 404, body: "{}")
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
let!(:compare_stub) do
|
|
51
|
+
stub_request(:get, "#{project_api}/repository/compare")
|
|
52
|
+
.with(query: { from: "main", to: "security/css_parser-1.1.0" })
|
|
53
|
+
.to_return(status: 200, body: { commits: branch_commits }.to_json)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
let!(:user_stub) do
|
|
57
|
+
stub_request(:get, "#{api}/user")
|
|
58
|
+
.to_return(status: 200, body: { email: bot_email, commit_email: bot_email, public_email: "" }.to_json)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
let!(:branch_file_stub) do
|
|
62
|
+
stub_request(:get, "#{project_api}/repository/files/Gemfile.lock/raw")
|
|
63
|
+
.with(query: { ref: "security/css_parser-1.1.0" })
|
|
64
|
+
.to_return(status: 200, body: branch_lockfile)
|
|
65
|
+
end
|
|
66
|
+
|
|
29
67
|
let!(:create_commit_stub) do
|
|
30
|
-
stub_request(:post, "
|
|
31
|
-
.with(
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
headers: headers
|
|
39
|
-
)
|
|
68
|
+
stub_request(:post, "#{project_api}/repository/commits")
|
|
69
|
+
.with(body: commit_body, headers: headers)
|
|
70
|
+
.to_return(status: 201, body: "{}")
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
let!(:force_commit_stub) do
|
|
74
|
+
stub_request(:post, "#{project_api}/repository/commits")
|
|
75
|
+
.with(body: commit_body.merge(force: true), headers: headers)
|
|
40
76
|
.to_return(status: 201, body: "{}")
|
|
41
77
|
end
|
|
42
78
|
|
|
43
79
|
let!(:create_mr_stub) do
|
|
44
|
-
stub_request(:post, "
|
|
80
|
+
stub_request(:post, "#{project_api}/merge_requests")
|
|
45
81
|
.with(body: {
|
|
46
82
|
source_branch: "security/css_parser-1.1.0",
|
|
47
83
|
target_branch: "main",
|
|
48
84
|
remove_source_branch: true,
|
|
49
|
-
labels: [],
|
|
85
|
+
labels: ["secupdater"],
|
|
50
86
|
title: "Upgrade RubyGems dependency css_parser to version 1.1.0",
|
|
51
87
|
description: "Upgrade RubyGems dependency css_parser to version 1.1.0 to address the following security advisories:\n\n* cve/2024-0001\n",
|
|
52
88
|
}, headers: headers)
|
|
53
|
-
.to_return(status: 201, body:
|
|
89
|
+
.to_return(status: 201, body: { iid: 9 }.to_json)
|
|
54
90
|
end
|
|
55
91
|
|
|
92
|
+
def publish = gitlab.call(ecosystem, bump, advisories, changes)
|
|
93
|
+
|
|
56
94
|
describe "#call(ecosystem, bump, advisories, changes)" do
|
|
57
|
-
|
|
58
|
-
|
|
95
|
+
context "no branch or MR yet" do
|
|
96
|
+
it "creates the branch from start_sha and a labelled merge request" do
|
|
97
|
+
expect(publish).to eq([:published, nil])
|
|
59
98
|
|
|
60
|
-
|
|
61
|
-
|
|
99
|
+
expect(create_commit_stub).to have_been_requested
|
|
100
|
+
expect(create_mr_stub).to have_been_requested
|
|
101
|
+
end
|
|
62
102
|
end
|
|
63
103
|
|
|
64
104
|
context "deleting a file" do
|
|
65
105
|
let(:changes) { [Secupdater::FileChange.new("Gemfile.lock", "delete", nil, "xyz")] }
|
|
66
106
|
|
|
67
107
|
let!(:create_commit_stub) do
|
|
68
|
-
stub_request(:post, "
|
|
108
|
+
stub_request(:post, "#{project_api}/repository/commits")
|
|
69
109
|
.with(body: hash_including(actions: [{ action: "delete", file_path: "Gemfile.lock" }]))
|
|
70
110
|
.to_return(status: 201, body: "{}")
|
|
71
111
|
end
|
|
72
112
|
|
|
73
113
|
it "doesn't send content for the action" do
|
|
74
|
-
|
|
114
|
+
publish
|
|
75
115
|
|
|
76
116
|
expect(create_commit_stub).to have_been_requested
|
|
77
117
|
end
|
|
@@ -79,27 +119,199 @@ RSpec.describe Secupdater::Publisher::Gitlab do
|
|
|
79
119
|
|
|
80
120
|
context "API returns 4xx" do
|
|
81
121
|
let!(:create_commit_stub) do
|
|
82
|
-
stub_request(:post, "
|
|
122
|
+
stub_request(:post, "#{project_api}/repository/commits")
|
|
83
123
|
.to_return(status: 400, body: '{"message":"A branch called security/css_parser-1.1.0 already exists"}')
|
|
84
124
|
end
|
|
85
125
|
|
|
86
126
|
it "raises Secupdater::PublishFailed including the response body" do
|
|
87
|
-
expect {
|
|
88
|
-
.to raise_error(Secupdater::PublishFailed, /400.*already exists/)
|
|
127
|
+
expect { publish }.to raise_error(Secupdater::PublishFailed, /400.*already exists/)
|
|
89
128
|
|
|
90
129
|
expect(create_mr_stub).not_to have_been_requested
|
|
91
130
|
end
|
|
92
131
|
end
|
|
93
132
|
|
|
94
|
-
context "
|
|
95
|
-
let(:existing_merge_requests) { [{ iid:
|
|
133
|
+
context "a closed MR for this version" do
|
|
134
|
+
let(:existing_merge_requests) { [{ iid: 7, state: "closed", labels: ["secupdater"] }] }
|
|
135
|
+
let(:branch_exists) { true }
|
|
96
136
|
|
|
97
|
-
it "
|
|
98
|
-
|
|
137
|
+
it "treats the update as declined, without touching the branch" do
|
|
138
|
+
expect(publish).to eq([:declined, "security/css_parser-1.1.0 was closed in !7, reopen it or remove its secupdater label to have it updated again"])
|
|
99
139
|
|
|
100
|
-
expect(
|
|
101
|
-
|
|
102
|
-
|
|
140
|
+
expect(a_request(:post, //)).not_to have_been_made
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
context "whose branch has since been deleted" do
|
|
144
|
+
let(:branch_exists) { false }
|
|
145
|
+
|
|
146
|
+
it "still treats the update as declined" do
|
|
147
|
+
expect(publish.first).to eq(:declined)
|
|
148
|
+
|
|
149
|
+
expect(a_request(:post, //)).not_to have_been_made
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
end
|
|
153
|
+
|
|
154
|
+
context "a closed MR without the secupdater label (e.g. from before labels)" do
|
|
155
|
+
let(:existing_merge_requests) { [{ iid: 7, state: "closed", labels: [] }] }
|
|
156
|
+
|
|
157
|
+
it "doesn't count as declined, so opens a new MR" do
|
|
158
|
+
expect(publish).to eq([:published, nil])
|
|
159
|
+
|
|
160
|
+
expect(create_mr_stub).to have_been_requested
|
|
161
|
+
end
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
context "a merged MR for this version" do
|
|
165
|
+
let(:existing_merge_requests) { [{ iid: 7, state: "merged", labels: ["secupdater"] }] }
|
|
166
|
+
|
|
167
|
+
it "doesn't open another" do
|
|
168
|
+
expect(publish).to eq([:declined, "security/css_parser-1.1.0 was merged in !7, reopen it or remove its secupdater label to have it updated again"])
|
|
169
|
+
|
|
170
|
+
expect(a_request(:post, //)).not_to have_been_made
|
|
171
|
+
end
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
context "an open MR, labelled or not" do
|
|
175
|
+
let(:existing_merge_requests) { [{ iid: 7, state: "opened", labels: [] }, { iid: 3, state: "closed", labels: ["secupdater"] }] }
|
|
176
|
+
let(:branch_exists) { true }
|
|
177
|
+
|
|
178
|
+
context "whose branch already has these changes" do
|
|
179
|
+
let(:branch_lockfile) { "abc" }
|
|
180
|
+
|
|
181
|
+
it "leaves it alone" do
|
|
182
|
+
expect(publish).to eq([:up_to_date, nil])
|
|
183
|
+
|
|
184
|
+
expect(a_request(:post, //)).not_to have_been_made
|
|
185
|
+
end
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
context "whose branch is out of date" do
|
|
189
|
+
it "replaces the branch's commit, from start_sha, without opening another MR" do
|
|
190
|
+
expect(publish).to eq([:published, nil])
|
|
191
|
+
|
|
192
|
+
expect(force_commit_stub).to have_been_requested
|
|
193
|
+
expect(create_mr_stub).not_to have_been_requested
|
|
194
|
+
end
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
context "whose branch has a commit from someone else" do
|
|
198
|
+
let(:branch_commits) do
|
|
199
|
+
[
|
|
200
|
+
{ author_name: "release-utilities", author_email: bot_email },
|
|
201
|
+
{ author_name: "Jo Bloggs", author_email: "jo@example.com" }
|
|
202
|
+
]
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
it "leaves the branch alone and says why" do
|
|
206
|
+
expect(publish).to eq([:skipped, "security/css_parser-1.1.0 has commits from Jo Bloggs, so it won't be updated"])
|
|
207
|
+
|
|
208
|
+
expect(a_request(:post, //)).not_to have_been_made
|
|
209
|
+
end
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
context "whose branch has been deleted" do
|
|
213
|
+
let(:branch_exists) { false }
|
|
214
|
+
|
|
215
|
+
it "recreates the branch without opening another MR" do
|
|
216
|
+
expect(publish).to eq([:published, nil])
|
|
217
|
+
|
|
218
|
+
expect(create_commit_stub).to have_been_requested
|
|
219
|
+
expect(create_mr_stub).not_to have_been_requested
|
|
220
|
+
end
|
|
221
|
+
end
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
context "a branch left without an MR (e.g. MR creation failed, or an MR from before labels)" do
|
|
225
|
+
let(:branch_exists) { true }
|
|
226
|
+
|
|
227
|
+
it "replaces the branch's commit and opens an MR" do
|
|
228
|
+
expect(publish).to eq([:published, nil])
|
|
229
|
+
|
|
230
|
+
expect(force_commit_stub).to have_been_requested
|
|
231
|
+
expect(create_mr_stub).to have_been_requested
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
context "that already has these changes" do
|
|
235
|
+
let(:branch_lockfile) { "abc" }
|
|
236
|
+
|
|
237
|
+
it "opens an MR without committing" do
|
|
238
|
+
expect(publish).to eq([:published, nil])
|
|
239
|
+
|
|
240
|
+
expect(a_request(:post, "#{project_api}/repository/commits")).not_to have_been_made
|
|
241
|
+
expect(create_mr_stub).to have_been_requested
|
|
242
|
+
end
|
|
243
|
+
end
|
|
244
|
+
end
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
describe "#finish(vulnerable_names)" do
|
|
248
|
+
let(:open_secupdater_merge_requests) { [] }
|
|
249
|
+
|
|
250
|
+
let!(:find_open_stub) do
|
|
251
|
+
stub_request(:get, "#{project_api}/merge_requests")
|
|
252
|
+
.with(query: { state: "opened", labels: "secupdater", target_branch: "main", per_page: "100" }, headers: headers)
|
|
253
|
+
.to_return(status: 200, body: open_secupdater_merge_requests.to_json)
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
let!(:note_stub) { stub_request(:post, %r{\A#{project_api}/merge_requests/\d+/notes\z}).to_return(status: 201, body: "{}") }
|
|
257
|
+
let!(:close_stub) do
|
|
258
|
+
stub_request(:put, %r{\A#{project_api}/merge_requests/\d+\z})
|
|
259
|
+
.with(body: { state_event: "close" })
|
|
260
|
+
.to_return(status: 200, body: "{}")
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
context "an older MR for a dependency this run opened a new MR for" do
|
|
264
|
+
let(:open_secupdater_merge_requests) do
|
|
265
|
+
[
|
|
266
|
+
{ iid: 5, source_branch: "security/css_parser-1.0.5" },
|
|
267
|
+
{ iid: 9, source_branch: "security/css_parser-1.1.0" }
|
|
268
|
+
]
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
before { publish }
|
|
272
|
+
|
|
273
|
+
it "closes it, saying which MR superseded it" do
|
|
274
|
+
expect(gitlab.finish(["css_parser"])).to eq(["closed !5 (security/css_parser-1.0.5): Superseded by !9."])
|
|
275
|
+
|
|
276
|
+
expect(a_request(:post, "#{project_api}/merge_requests/5/notes").with(body: { body: "Superseded by !9." })).to have_been_made
|
|
277
|
+
expect(a_request(:put, "#{project_api}/merge_requests/5")).to have_been_made
|
|
278
|
+
expect(a_request(:put, //)).to have_been_made.once
|
|
279
|
+
end
|
|
280
|
+
end
|
|
281
|
+
|
|
282
|
+
context "an MR for a dependency that no longer has advisories" do
|
|
283
|
+
let(:open_secupdater_merge_requests) { [{ iid: 4, source_branch: "security/net-ssh-7.3.8" }] }
|
|
284
|
+
|
|
285
|
+
it "closes it, saying it's no longer needed" do
|
|
286
|
+
expect(gitlab.finish(["css_parser"])).to eq(
|
|
287
|
+
["closed !4 (security/net-ssh-7.3.8): net-ssh no longer has any security advisories on main, so this is no longer needed."]
|
|
288
|
+
)
|
|
289
|
+
|
|
290
|
+
expect(a_request(:put, "#{project_api}/merge_requests/4")).to have_been_made
|
|
291
|
+
expect(a_request(:put, //)).to have_been_made.once
|
|
292
|
+
end
|
|
293
|
+
end
|
|
294
|
+
|
|
295
|
+
context "an MR for a dependency that still has advisories, but that this run didn't publish" do
|
|
296
|
+
let(:open_secupdater_merge_requests) { [{ iid: 4, source_branch: "security/net-ssh-7.3.8" }] }
|
|
297
|
+
|
|
298
|
+
it "leaves it alone" do
|
|
299
|
+
expect(gitlab.finish(["net-ssh"])).to eq([])
|
|
300
|
+
|
|
301
|
+
expect(a_request(:put, //)).not_to have_been_made
|
|
302
|
+
expect(a_request(:post, //)).not_to have_been_made
|
|
303
|
+
end
|
|
304
|
+
end
|
|
305
|
+
|
|
306
|
+
context "the MR this run opened or kept" do
|
|
307
|
+
let(:open_secupdater_merge_requests) { [{ iid: 9, source_branch: "security/css_parser-1.1.0" }] }
|
|
308
|
+
|
|
309
|
+
before { publish }
|
|
310
|
+
|
|
311
|
+
it "leaves it alone" do
|
|
312
|
+
expect(gitlab.finish(["css_parser"])).to eq([])
|
|
313
|
+
|
|
314
|
+
expect(a_request(:put, //)).not_to have_been_made
|
|
103
315
|
end
|
|
104
316
|
end
|
|
105
317
|
end
|
|
@@ -5,7 +5,7 @@ require "stringio"
|
|
|
5
5
|
require "tmpdir"
|
|
6
6
|
|
|
7
7
|
module UpdateSpec
|
|
8
|
-
# outcomes: gem name => version try_update moves it to, :unchanged, :fail or :abort, or a hash of those by level
|
|
8
|
+
# outcomes: gem name => version try_update moves it to, :unchanged, :fail, :fail_unretryable or :abort, or a hash of those by level
|
|
9
9
|
class FakeEcosystem
|
|
10
10
|
LOCKFILE = "Gemfile.lock"
|
|
11
11
|
|
|
@@ -40,6 +40,8 @@ module UpdateSpec
|
|
|
40
40
|
case outcome
|
|
41
41
|
when :fail
|
|
42
42
|
raise Secupdater::UpdateCommandFailed.new("bundle lock failed", 1, "could not resolve #{name}")
|
|
43
|
+
when :fail_unretryable
|
|
44
|
+
raise Secupdater::UnretryableUpdateFailed.new("bundle lock failed", 11, "could not fetch git source for #{name}")
|
|
43
45
|
when :abort
|
|
44
46
|
raise Secupdater::Error, "lockfile is frozen"
|
|
45
47
|
when :unchanged
|
|
@@ -82,7 +84,8 @@ RSpec.describe Secupdater::Update do
|
|
|
82
84
|
end
|
|
83
85
|
|
|
84
86
|
before do
|
|
85
|
-
allow(publisher).to receive(:call) { |*args| published << args } if publisher
|
|
87
|
+
allow(publisher).to receive(:call) { |*args| published << args; [:published, nil] } if publisher
|
|
88
|
+
allow(publisher).to receive(:finish).and_return([]) if publisher
|
|
86
89
|
end
|
|
87
90
|
|
|
88
91
|
describe "#call(advisories)" do
|
|
@@ -252,6 +255,50 @@ RSpec.describe Secupdater::Update do
|
|
|
252
255
|
end
|
|
253
256
|
end
|
|
254
257
|
|
|
258
|
+
context "the publisher doesn't publish (e.g. the update was declined)" do
|
|
259
|
+
let(:outcomes) { { "rack" => { patch: "2.0.1", minor: "2.1" } } }
|
|
260
|
+
|
|
261
|
+
before do
|
|
262
|
+
allow(publisher).to receive(:call).and_return([:declined, "security/rack-2.0.1 was closed in !7"])
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
it "returns the publisher's status and warns with its message" do
|
|
266
|
+
expect(update.call([advisory("rack", "cve/1")])).to eq("rack" => :declined)
|
|
267
|
+
|
|
268
|
+
expect(log.string).to match(%r{^WARN security/rack-2\.0\.1 was closed in !7})
|
|
269
|
+
end
|
|
270
|
+
|
|
271
|
+
it "doesn't try the other levels" do
|
|
272
|
+
update.call([advisory("rack", "cve/1")])
|
|
273
|
+
|
|
274
|
+
expect(ecosystem.attempts).to eq([["rack", :patch]])
|
|
275
|
+
end
|
|
276
|
+
end
|
|
277
|
+
|
|
278
|
+
context "once every gem has been through the publisher" do
|
|
279
|
+
let(:outcomes) { { "rack" => "2.1", "nokogiri" => :unchanged } }
|
|
280
|
+
|
|
281
|
+
before { allow(publisher).to receive(:finish).and_return(["closed !5 (security/rack-2.0): Superseded by !9."]) }
|
|
282
|
+
|
|
283
|
+
it "lets the publisher tidy up, with the names of every gem that has advisories" do
|
|
284
|
+
update.call([advisory("rack", "cve/1"), advisory("nokogiri", "cve/2")])
|
|
285
|
+
|
|
286
|
+
expect(publisher).to have_received(:finish).with(["rack", "nokogiri"])
|
|
287
|
+
end
|
|
288
|
+
|
|
289
|
+
it "logs what it tidied up" do
|
|
290
|
+
update.call([advisory("rack", "cve/1")])
|
|
291
|
+
|
|
292
|
+
expect(log.string).to match(%r{^INFO closed !5 \(security/rack-2\.0\): Superseded by !9\.})
|
|
293
|
+
end
|
|
294
|
+
|
|
295
|
+
it "does so even when there are no advisories, so MRs whose fixes have landed get closed" do
|
|
296
|
+
expect(update.call([])).to eq({})
|
|
297
|
+
|
|
298
|
+
expect(publisher).to have_received(:finish).with([])
|
|
299
|
+
end
|
|
300
|
+
end
|
|
301
|
+
|
|
255
302
|
context "publishing fails for the first gem" do
|
|
256
303
|
let(:outcomes) { { "rack" => "2.1", "nokogiri" => "2.0" } }
|
|
257
304
|
|
|
@@ -260,6 +307,7 @@ RSpec.describe Secupdater::Update do
|
|
|
260
307
|
raise Secupdater::PublishFailed, "branch already exists" if bump.dependency.name == "rack"
|
|
261
308
|
|
|
262
309
|
published << bump
|
|
310
|
+
[:published, nil]
|
|
263
311
|
end
|
|
264
312
|
end
|
|
265
313
|
|
|
@@ -289,6 +337,34 @@ RSpec.describe Secupdater::Update do
|
|
|
289
337
|
end
|
|
290
338
|
end
|
|
291
339
|
|
|
340
|
+
context "an update failure that another level won't fix, for the first gem" do
|
|
341
|
+
let(:outcomes) { { "rack" => :fail_unretryable, "nokogiri" => "2.0" } }
|
|
342
|
+
|
|
343
|
+
it "returns :failed for that gem without trying the other levels" do
|
|
344
|
+
expect(update.call([advisory("rack", "cve/1")])).to eq("rack" => :failed)
|
|
345
|
+
expect(ecosystem.attempts).to eq([["rack", :patch]])
|
|
346
|
+
end
|
|
347
|
+
|
|
348
|
+
it "logs the failure and its output once, as an error" do
|
|
349
|
+
update.call([advisory("rack", "cve/1")])
|
|
350
|
+
|
|
351
|
+
expect(log.string).to match(/^ERROR rack: bundle lock failed\ncould not fetch git source for rack/)
|
|
352
|
+
expect(log.string.scan(/could not fetch git source/).length).to eq(1)
|
|
353
|
+
end
|
|
354
|
+
|
|
355
|
+
it "continues to the next gem" do
|
|
356
|
+
update.call([advisory("rack", "cve/1"), advisory("nokogiri", "cve/2")])
|
|
357
|
+
|
|
358
|
+
expect(published.map { it[1].dependency.name }).to eq(["nokogiri"])
|
|
359
|
+
end
|
|
360
|
+
|
|
361
|
+
it "leaves the lockfile as it found it" do
|
|
362
|
+
update.call([advisory("rack", "cve/1")])
|
|
363
|
+
|
|
364
|
+
expect(lockfile_on_disk).to eq(lockfile)
|
|
365
|
+
end
|
|
366
|
+
end
|
|
367
|
+
|
|
292
368
|
context "an error that isn't an update failure" do
|
|
293
369
|
let(:outcomes) { { "rack" => :abort, "nokogiri" => "2.0" } }
|
|
294
370
|
|