secupdater 0.2.0alpha1 → 0.2.0alpha2
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/cli.rb +11 -1
- data/lib/secupdater/publisher/gitlab.rb +5 -4
- data/lib/secupdater/update.rb +3 -0
- data/lib/secupdater/version.rb +1 -1
- data/lib/secupdater.rb +3 -0
- data/spec/e2e/secupdater_spec.rb +4 -1
- data/spec/secupdater/cli_spec.rb +15 -2
- data/spec/secupdater/publisher/gitlab_spec.rb +4 -4
- data/spec/secupdater/update_spec.rb +37 -0
- data/spec/secupdater_spec.rb +9 -0
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 9370b2774586510cf198499d82567825da267bb6c616ebcbfe5a97b0f2df17b6
|
|
4
|
+
data.tar.gz: 17b9536a5abdb0f73f18e76a3d91dca0d0921560e14468d98cb6c7b611808ab2
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 06345b62279096ca6cf5226a16b67da136ea4a300dba9d9b573f6500dca9d3201cc427738be31ba20bb6f1a756c97affc40e22fe2a3194051f752f92914a3d01
|
|
7
|
+
data.tar.gz: 326e8beccaa4ea2fbb74fb3d388a94e0dd8ec61d765c0a14701440b12faa6671ecdde8ec7c4bad5e2db452d529880877d60fc241ffb97faa5e3e48f3102b6ec2
|
data/lib/secupdater/cli.rb
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
require "dry/cli"
|
|
2
|
+
require "open3"
|
|
2
3
|
|
|
3
4
|
module Secupdater
|
|
4
5
|
module CLI
|
|
@@ -32,10 +33,19 @@ module Secupdater
|
|
|
32
33
|
def publisher
|
|
33
34
|
return unless ENV["SECUPDATER_GITLAB_TOKEN"]
|
|
34
35
|
|
|
35
|
-
Publisher::Gitlab.new(ENV["SECUPDATER_GITLAB_TOKEN"], ENV.fetch("CI_PROJECT_ID"), ENV.fetch("CI_DEFAULT_BRANCH"))
|
|
36
|
+
Publisher::Gitlab.new(ENV["SECUPDATER_GITLAB_TOKEN"], ENV.fetch("CI_PROJECT_ID"), ENV.fetch("CI_DEFAULT_BRANCH"), head_commit)
|
|
36
37
|
rescue KeyError => e
|
|
37
38
|
raise Secupdater::Error, "SECUPDATER_GITLAB_TOKEN is set but #{e.key} isn't"
|
|
38
39
|
end
|
|
40
|
+
|
|
41
|
+
# Update branches start from the commit the lockfile was read from, so
|
|
42
|
+
# their diff is only the bump, even if the target branch has moved on
|
|
43
|
+
def head_commit
|
|
44
|
+
output, status = Open3.capture2e("git", "rev-parse", "--verify", "HEAD")
|
|
45
|
+
raise Secupdater::Error, "Can't find the current git commit to base updates on: #{output}" unless status.success?
|
|
46
|
+
|
|
47
|
+
output.strip
|
|
48
|
+
end
|
|
39
49
|
end
|
|
40
50
|
|
|
41
51
|
register "run", Run
|
|
@@ -4,12 +4,13 @@ require "json"
|
|
|
4
4
|
module Secupdater
|
|
5
5
|
class Publisher
|
|
6
6
|
class Gitlab < Secupdater::Publisher
|
|
7
|
-
attr_reader :access_token, :project_id, :target_branch
|
|
7
|
+
attr_reader :access_token, :project_id, :target_branch, :start_sha
|
|
8
8
|
|
|
9
|
-
def initialize(access_token, project_id, target_branch)
|
|
9
|
+
def initialize(access_token, project_id, target_branch, start_sha)
|
|
10
10
|
@access_token = access_token
|
|
11
11
|
@project_id = project_id
|
|
12
12
|
@target_branch = target_branch
|
|
13
|
+
@start_sha = start_sha
|
|
13
14
|
end
|
|
14
15
|
|
|
15
16
|
def run
|
|
@@ -18,7 +19,7 @@ module Secupdater
|
|
|
18
19
|
|
|
19
20
|
data = {
|
|
20
21
|
branch: reference,
|
|
21
|
-
|
|
22
|
+
start_sha: start_sha,
|
|
22
23
|
commit_message: title,
|
|
23
24
|
actions: changes.map { { action: it.type, file_path: it.filename, content: it.content }.compact }
|
|
24
25
|
}
|
|
@@ -57,7 +58,7 @@ module Secupdater
|
|
|
57
58
|
res = http.request(req)
|
|
58
59
|
|
|
59
60
|
unless res.is_a?(Net::HTTPSuccess)
|
|
60
|
-
raise Secupdater::
|
|
61
|
+
raise Secupdater::PublishFailed, "GitLab #{req.method} #{req.path} returned #{res.code}: #{res.body}"
|
|
61
62
|
end
|
|
62
63
|
|
|
63
64
|
res
|
data/lib/secupdater/update.rb
CHANGED
data/lib/secupdater/version.rb
CHANGED
data/lib/secupdater.rb
CHANGED
data/spec/e2e/secupdater_spec.rb
CHANGED
|
@@ -55,6 +55,8 @@ RSpec.describe "secupdater end to end" do
|
|
|
55
55
|
File.write("bin/bundle", "#!/bin/sh\ncp Gemfile.lock.after Gemfile.lock\n")
|
|
56
56
|
File.chmod(0o755, "bin/bundle")
|
|
57
57
|
|
|
58
|
+
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
|
+
|
|
58
60
|
ENV.delete("SECUPDATER_GITLAB_TOKEN")
|
|
59
61
|
ENV.update(env.merge("PATH" => "#{dir}/bin:#{ENV['PATH']}"))
|
|
60
62
|
example.run
|
|
@@ -76,6 +78,7 @@ RSpec.describe "secupdater end to end" do
|
|
|
76
78
|
stub_request(:post, "https://gitlab.com/api/v4/projects/123/repository/commits")
|
|
77
79
|
.with(body: hash_including(
|
|
78
80
|
branch: "security/css_parser-1.1.0",
|
|
81
|
+
start_sha: `git rev-parse HEAD`.strip,
|
|
79
82
|
actions: [{ action: "update", file_path: "Gemfile.lock", content: lockfile("1.1.0") }]
|
|
80
83
|
))
|
|
81
84
|
.to_return(status: 201, body: "{}")
|
|
@@ -99,7 +102,7 @@ RSpec.describe "secupdater end to end" do
|
|
|
99
102
|
|
|
100
103
|
context "same fixture project without GitLab configuration (dry run)" do
|
|
101
104
|
it "exits 0, reports what would be updated and makes no HTTP requests" do
|
|
102
|
-
expect { run_cli("--verbose") }.to output(/css_parser from 1\.0\.0 to 1\.1\.0 \(patch\) would resolve/).
|
|
105
|
+
expect { run_cli("--verbose") }.to output(/css_parser from 1\.0\.0 to 1\.1\.0 \(patch\) would resolve/).to_stderr
|
|
103
106
|
|
|
104
107
|
expect(a_request(:any, //)).not_to have_been_made
|
|
105
108
|
expect(File.read("Gemfile.lock")).to eq(lockfile("1.0.0"))
|
data/spec/secupdater/cli_spec.rb
CHANGED
|
@@ -6,6 +6,7 @@ RSpec.describe Secupdater::CLI::Commands::Run do
|
|
|
6
6
|
let(:err) { StringIO.new }
|
|
7
7
|
let(:runner) { instance_double(Secupdater::Runner, call: { "rack" => :published }) }
|
|
8
8
|
let(:env) { {} }
|
|
9
|
+
let(:git_head) { ["abc123\n", instance_double(Process::Status, success?: true)] }
|
|
9
10
|
|
|
10
11
|
def run_cli(*arguments)
|
|
11
12
|
Dry::CLI.new(Secupdater::CLI::Commands).call(arguments: ["run", *arguments], out: out, err: err)
|
|
@@ -18,6 +19,7 @@ RSpec.describe Secupdater::CLI::Commands::Run do
|
|
|
18
19
|
before do
|
|
19
20
|
stub_const("ENV", env)
|
|
20
21
|
allow(Secupdater::Runner).to receive(:new).and_return(runner)
|
|
22
|
+
allow(Open3).to receive(:capture2e).with("git", "rev-parse", "--verify", "HEAD").and_return(git_head)
|
|
21
23
|
end
|
|
22
24
|
|
|
23
25
|
describe "#call" do
|
|
@@ -49,15 +51,26 @@ RSpec.describe Secupdater::CLI::Commands::Run do
|
|
|
49
51
|
context "with SECUPDATER_GITLAB_TOKEN set" do
|
|
50
52
|
let(:env) { { "SECUPDATER_GITLAB_TOKEN" => "token", "CI_PROJECT_ID" => "123", "CI_DEFAULT_BRANCH" => "main" } }
|
|
51
53
|
|
|
52
|
-
it "publishes to GitLab using the CI project and default branch" do
|
|
54
|
+
it "publishes to GitLab using the CI project and default branch, starting from the checked out commit" do
|
|
53
55
|
run_cli("--ecosystem=rubygems", "audit.json")
|
|
54
56
|
|
|
55
57
|
expect(Secupdater::Runner).to have_received(:new).with(hash_including(
|
|
56
58
|
publisher: an_instance_of(Secupdater::Publisher::Gitlab).and(
|
|
57
|
-
have_attributes(access_token: "token", project_id: "123", target_branch: "main")
|
|
59
|
+
have_attributes(access_token: "token", project_id: "123", target_branch: "main", start_sha: "abc123")
|
|
58
60
|
)
|
|
59
61
|
))
|
|
60
62
|
end
|
|
63
|
+
|
|
64
|
+
context "outside a git checkout" do
|
|
65
|
+
let(:git_head) { ["fatal: not a git repository\n", instance_double(Process::Status, success?: false)] }
|
|
66
|
+
|
|
67
|
+
it "prints why and exits non-zero" do
|
|
68
|
+
expect_exit(1) { run_cli("--ecosystem=rubygems", "audit.json") }
|
|
69
|
+
|
|
70
|
+
expect(err.string).to include("Can't find the current git commit", "not a git repository")
|
|
71
|
+
expect(Secupdater::Runner).not_to have_received(:new)
|
|
72
|
+
end
|
|
73
|
+
end
|
|
61
74
|
end
|
|
62
75
|
|
|
63
76
|
context "with SECUPDATER_GITLAB_TOKEN set but no CI_PROJECT_ID" do
|
|
@@ -3,7 +3,7 @@ require "secupdater/publisher"
|
|
|
3
3
|
require "secupdater/publisher/gitlab"
|
|
4
4
|
|
|
5
5
|
RSpec.describe Secupdater::Publisher::Gitlab do
|
|
6
|
-
subject(:gitlab) { described_class.new("access_token", "123", "main") }
|
|
6
|
+
subject(:gitlab) { described_class.new("access_token", "123", "main", "abc123") }
|
|
7
7
|
|
|
8
8
|
let(:ecosystem) { Secupdater::Ecosystem::Rubygems.new }
|
|
9
9
|
let(:dep) { Secupdater::DependencySpec.new("css_parser", "ruby", Gem::Version.new("1.0.0")) }
|
|
@@ -31,7 +31,7 @@ RSpec.describe Secupdater::Publisher::Gitlab do
|
|
|
31
31
|
.with(
|
|
32
32
|
body: {
|
|
33
33
|
branch: "security/css_parser-1.1.0",
|
|
34
|
-
|
|
34
|
+
start_sha: "abc123",
|
|
35
35
|
commit_message: "Upgrade RubyGems dependency css_parser to version 1.1.0",
|
|
36
36
|
actions: [{ action: "update", file_path: "Gemfile.lock", content: "abc" }]
|
|
37
37
|
},
|
|
@@ -83,9 +83,9 @@ RSpec.describe Secupdater::Publisher::Gitlab do
|
|
|
83
83
|
.to_return(status: 400, body: '{"message":"A branch called security/css_parser-1.1.0 already exists"}')
|
|
84
84
|
end
|
|
85
85
|
|
|
86
|
-
it "raises Secupdater::
|
|
86
|
+
it "raises Secupdater::PublishFailed including the response body" do
|
|
87
87
|
expect { gitlab.call(ecosystem, bump, advisories, changes) }
|
|
88
|
-
.to raise_error(Secupdater::
|
|
88
|
+
.to raise_error(Secupdater::PublishFailed, /400.*already exists/)
|
|
89
89
|
|
|
90
90
|
expect(create_mr_stub).not_to have_been_requested
|
|
91
91
|
end
|
|
@@ -252,6 +252,43 @@ RSpec.describe Secupdater::Update do
|
|
|
252
252
|
end
|
|
253
253
|
end
|
|
254
254
|
|
|
255
|
+
context "publishing fails for the first gem" do
|
|
256
|
+
let(:outcomes) { { "rack" => "2.1", "nokogiri" => "2.0" } }
|
|
257
|
+
|
|
258
|
+
before do
|
|
259
|
+
allow(publisher).to receive(:call) do |_, bump, *|
|
|
260
|
+
raise Secupdater::PublishFailed, "branch already exists" if bump.dependency.name == "rack"
|
|
261
|
+
|
|
262
|
+
published << bump
|
|
263
|
+
end
|
|
264
|
+
end
|
|
265
|
+
|
|
266
|
+
it "returns :failed for that gem and continues to the next gem" do
|
|
267
|
+
results = update.call([advisory("rack", "cve/1"), advisory("nokogiri", "cve/2")])
|
|
268
|
+
|
|
269
|
+
expect(results).to eq("rack" => :failed, "nokogiri" => :published)
|
|
270
|
+
expect(published.map { it.dependency.name }).to eq(["nokogiri"])
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
it "doesn't try the other levels" do
|
|
274
|
+
update.call([advisory("rack", "cve/1")])
|
|
275
|
+
|
|
276
|
+
expect(ecosystem.attempts).to eq([["rack", :patch]])
|
|
277
|
+
end
|
|
278
|
+
|
|
279
|
+
it "logs the failure as an error" do
|
|
280
|
+
update.call([advisory("rack", "cve/1")])
|
|
281
|
+
|
|
282
|
+
expect(log.string).to match(/^ERROR rack: branch already exists/)
|
|
283
|
+
end
|
|
284
|
+
|
|
285
|
+
it "leaves the lockfile as it found it" do
|
|
286
|
+
update.call([advisory("rack", "cve/1")])
|
|
287
|
+
|
|
288
|
+
expect(lockfile_on_disk).to eq(lockfile)
|
|
289
|
+
end
|
|
290
|
+
end
|
|
291
|
+
|
|
255
292
|
context "an error that isn't an update failure" do
|
|
256
293
|
let(:outcomes) { { "rack" => :abort, "nokogiri" => "2.0" } }
|
|
257
294
|
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
require "secupdater"
|
|
2
|
+
|
|
3
|
+
RSpec.describe Secupdater do
|
|
4
|
+
describe ".build_logger" do
|
|
5
|
+
it "logs to stderr, so log lines stay in order with errors when stdout is buffered" do
|
|
6
|
+
expect { Secupdater.build_logger.warn("hello") }.to output("[WARN] hello\n").to_stderr
|
|
7
|
+
end
|
|
8
|
+
end
|
|
9
|
+
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: secupdater
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.2.
|
|
4
|
+
version: 0.2.0alpha2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Ben Anderson
|
|
@@ -97,6 +97,7 @@ files:
|
|
|
97
97
|
- spec/secupdater/publisher_spec.rb
|
|
98
98
|
- spec/secupdater/runner_spec.rb
|
|
99
99
|
- spec/secupdater/update_spec.rb
|
|
100
|
+
- spec/secupdater_spec.rb
|
|
100
101
|
- spec/spec_helper.rb
|
|
101
102
|
homepage: https://github.com/bagedevimo/secupdater
|
|
102
103
|
licenses:
|