Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

attestation: improve error message when gh is too old #17727

Merged
merged 2 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions Library/Homebrew/attestation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ module Attestation
# @api private
HOMEBREW_CORE_REPO = "Homebrew/homebrew-core"

# @api private
GH_ATTESTATION_MIN_VERSION = T.let(Version.new("2.49.0").freeze, Version)

# @api private
BACKFILL_REPO = "trailofbits/homebrew-brew-verify"

Expand Down Expand Up @@ -59,10 +62,10 @@ def self.enabled?
# @api private
sig { returns(Pathname) }
def self.gh_executable
# NOTE: We disable HOMEBREW_VERIFY_ATTESTATIONS when installing `gh` itself,
# NOTE: We set HOMEBREW_NO_VERIFY_ATTESTATIONS when installing `gh` itself,
# to prevent a cycle during bootstrapping. This can eventually be resolved
# by vendoring a pure-Ruby Sigstore verifier client.
@gh_executable ||= T.let(with_env("HOMEBREW_VERIFY_ATTESTATIONS" => nil) do
@gh_executable ||= T.let(with_env(HOMEBREW_NO_VERIFY_ATTESTATIONS: "1") do
ensure_executable!("gh")
end, T.nilable(Pathname))
end
Expand Down Expand Up @@ -104,6 +107,13 @@ def self.check_attestation(bottle, signing_repo, signing_workflow = nil, subject
# Even if we have credentials, they may be invalid or malformed.
raise GhAuthNeeded, "invalid credentials" if e.status.exitstatus == 4

gh_version = Version.new(system_command!(gh_executable, args: ["--version"], print_stderr: false)
.stdout.match(/\d+(?:\.\d+)+/i).to_s)
if gh_version < GH_ATTESTATION_MIN_VERSION
raise e,
"#{gh_executable} is too old, you must upgrade it to continue"
end

raise InvalidAttestationError, "attestation verification failed: #{e}"
end

Expand Down
35 changes: 35 additions & 0 deletions Library/Homebrew/test/attestation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

RSpec.describe Homebrew::Attestation do
let(:fake_gh) { Pathname.new("/extremely/fake/gh") }
let(:fake_old_gh) { Pathname.new("/extremely/fake/old/gh") }
let(:fake_gh_creds) { "fake-gh-api-token" }
let(:fake_gh_version) { instance_double(SystemCommand::Result, stdout: "2.49.0") }
let(:fake_old_gh_version) { instance_double(SystemCommand::Result, stdout: "2.48.0") }
let(:fake_error_status) { instance_double(Process::Status, exitstatus: 1, termsig: nil) }
let(:fake_auth_status) { instance_double(Process::Status, exitstatus: 4, termsig: nil) }
let(:cached_download) { "/fake/cached/download" }
Expand Down Expand Up @@ -74,10 +77,42 @@
end
end

describe "::check_attestation fails with old gh" do
before do
allow(described_class).to receive(:gh_executable)
.and_return(fake_old_gh)

allow(described_class).to receive(:system_command!)
.with(fake_old_gh, args: ["--version"], print_stderr: false)
.and_return(fake_old_gh_version)
end

it "raises when gh is too old" do
expect(GitHub::API).to receive(:credentials)
.and_return(fake_gh_creds)

expect(described_class).to receive(:system_command!)
.with(fake_old_gh, args: ["attestation", "verify", cached_download, "--repo",
described_class::HOMEBREW_CORE_REPO, "--format", "json"],
env: { "GH_TOKEN" => fake_gh_creds }, secrets: [fake_gh_creds],
print_stderr: false)
.and_raise(ErrorDuringExecution.new(["foo"], status: fake_error_status))

expect do
described_class.check_attestation fake_bottle,
described_class::HOMEBREW_CORE_REPO
end.to raise_error(ErrorDuringExecution)
end
end

describe "::check_attestation" do
before do
allow(described_class).to receive(:gh_executable)
.and_return(fake_gh)

allow(described_class).to receive(:system_command!)
.with(fake_gh, args: ["--version"], print_stderr: false)
.and_return(fake_gh_version)
end

it "raises without any gh credentials" do
Expand Down
Loading