secupdater 0.2.0alpha1 → 0.2.0alpha3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1d028b501eb99bb427b8863f56293be6f04daabae1c87245d9aa22364998846e
4
- data.tar.gz: c8d35b92bcbe9844bc0757f2ec92d054eea94e23df307b060ca3b79b98a5592f
3
+ metadata.gz: 5c31a79bbf3e3c98290cf8348360817d012933c0a98cd08fb8bd5c07bbcf99e2
4
+ data.tar.gz: ea54a58239e29f22f52a2f13419b2103b4e3a099cd33f01c7b0c21ed9f596153
5
5
  SHA512:
6
- metadata.gz: 73464dc3adb67b6ca401ac62fe507b2dc8365daf48b99d862e6843a2bdae5939e1a13e747c8eaac85de9ea2ddea0f9bcc85287359e11c168177c778322f5a69d
7
- data.tar.gz: 3f4d5a0ca4b2c5e0c91c9dd0a34d066daeae1f4b91ca218c981cd81da62b41ea0e6f27d3bebf014d1d098933a60d9fe3651b8d3f3863ea030ef80ad8a367b7c1
6
+ metadata.gz: 25f5e35f6d2c818f6a288d59401d7a8268e796954f94c3e307f6b15362e0134ed2932cded0e4e4d3d4080d50c41f002e42a56630a18be25e26a322cbc77c4570
7
+ data.tar.gz: 1713808b35241ec4b36edf3d0e7290b015ebd61db4ac792f36cd3608fac08c546a99167e5c06dabfae5e967f039ef3a044b898dd01487c1f7061213d0ca1004e
@@ -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
@@ -32,15 +32,15 @@ module Secupdater
32
32
  end
33
33
 
34
34
  def run_update(names, level)
35
- output, status = Open3.capture2e({ "BUNDLE_FROZEN" => "false" }, "bundle", "update", *Array(names), "--conservative", "--#{level}", "--strict")
35
+ output, status = Open3.capture2e({ "BUNDLE_FROZEN" => "false" }, "bundle", "lock", "--update", *Array(names), "--conservative", "--#{level}", "--strict")
36
36
 
37
37
  if status.exitstatus == FROZEN_EXIT_CODE
38
- raise Secupdater::Error, "bundle update can't change Gemfile.lock (is BUNDLE_DEPLOYMENT set?)\n#{output}"
38
+ raise Secupdater::Error, "bundle lock can't change Gemfile.lock (is BUNDLE_DEPLOYMENT set?)\n#{output}"
39
39
  end
40
40
 
41
41
  unless status.success?
42
42
  raise Secupdater::UpdateCommandFailed.new(
43
- "bundle update command returned #{status.exitstatus}",
43
+ "bundle lock command returned #{status.exitstatus}",
44
44
  status.exitstatus,
45
45
  output
46
46
  )
@@ -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
- start_branch: target_branch,
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::Error, "GitLab #{req.method} #{req.path} returned #{res.code}: #{res.body}"
61
+ raise Secupdater::PublishFailed, "GitLab #{req.method} #{req.path} returned #{res.code}: #{res.body}"
61
62
  end
62
63
 
63
64
  res
@@ -42,6 +42,9 @@ module Secupdater
42
42
  end
43
43
 
44
44
  status
45
+ rescue Secupdater::PublishFailed => e
46
+ logger.error "#{dep.name}: #{e.message}"
47
+ :failed
45
48
  end
46
49
 
47
50
  private
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Secupdater
4
- VERSION = "0.2.0alpha1"
4
+ VERSION = "0.2.0alpha3"
5
5
  end
data/lib/secupdater.rb CHANGED
@@ -33,9 +33,12 @@ module Secupdater
33
33
  end
34
34
  end
35
35
 
36
+ class PublishFailed < Error; end
37
+
36
38
  def self.build_logger(log_level: :warn)
37
39
  Dry.Logger(
38
40
  :secupdater,
41
+ stream: $stderr,
39
42
  template: "[%<severity>s] %<message>s",
40
43
  level: log_level
41
44
  )
@@ -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,14 +102,14 @@ 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/).to_stdout
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"))
106
109
  end
107
110
  end
108
111
 
109
- context "real bundle update --local against fixture vendor/cache gems", :integration do
112
+ context "real bundle lock --update --local against fixture vendor/cache gems", :integration do
110
113
  it "bumps the gem in Gemfile.lock"
111
114
  end
112
115
  end
@@ -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
@@ -137,10 +137,10 @@ RSpec.describe Secupdater::Ecosystem::Rubygems do
137
137
  end
138
138
  end
139
139
 
140
- it "runs a strict, conservative, unfrozen bundle update at the given level" do
140
+ it "runs a strict, conservative, unfrozen bundle lock --update at the given level" do
141
141
  rubygems.try_update("css_parser", level: :minor)
142
142
 
143
- expect(Open3).to have_received(:capture2e).with({ "BUNDLE_FROZEN" => "false" }, "bundle", "update", "css_parser", "--conservative", "--minor", "--strict")
143
+ expect(Open3).to have_received(:capture2e).with({ "BUNDLE_FROZEN" => "false" }, "bundle", "lock", "--update", "css_parser", "--conservative", "--minor", "--strict")
144
144
  end
145
145
 
146
146
  it "returns the lockfile change with its new and original content" do
@@ -164,7 +164,7 @@ RSpec.describe Secupdater::Ecosystem::Rubygems do
164
164
  it "passes it as a single argument without going through a shell" do
165
165
  rubygems.try_update("css_parser; touch pwned", level: :patch)
166
166
 
167
- expect(Open3).to have_received(:capture2e).with({ "BUNDLE_FROZEN" => "false" }, "bundle", "update", "css_parser; touch pwned", "--conservative", "--patch", "--strict")
167
+ expect(Open3).to have_received(:capture2e).with({ "BUNDLE_FROZEN" => "false" }, "bundle", "lock", "--update", "css_parser; touch pwned", "--conservative", "--patch", "--strict")
168
168
  end
169
169
  end
170
170
 
@@ -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
- start_branch: "main",
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::Error including the response body" do
86
+ it "raises Secupdater::PublishFailed including the response body" do
87
87
  expect { gitlab.call(ecosystem, bump, advisories, changes) }
88
- .to raise_error(Secupdater::Error, /400.*already exists/)
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
@@ -39,7 +39,7 @@ module UpdateSpec
39
39
 
40
40
  case outcome
41
41
  when :fail
42
- raise Secupdater::UpdateCommandFailed.new("bundle update failed", 1, "could not resolve #{name}")
42
+ raise Secupdater::UpdateCommandFailed.new("bundle lock failed", 1, "could not resolve #{name}")
43
43
  when :abort
44
44
  raise Secupdater::Error, "lockfile is frozen"
45
45
  when :unchanged
@@ -134,7 +134,7 @@ RSpec.describe Secupdater::Update do
134
134
  end
135
135
  end
136
136
 
137
- context "bundle update fails for the first gem" do
137
+ context "bundle lock fails for the first gem" do
138
138
  let(:outcomes) { { "rack" => :fail, "nokogiri" => "2.0" } }
139
139
 
140
140
  it "records the failure and continues to the next gem" do
@@ -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.0alpha1
4
+ version: 0.2.0alpha3
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: