-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Attest by default when using trusted publishing
Fallback to pushing without attestations on any failure Signed-off-by: Samuel Giddins <segiddins@segiddins.me>
- Loading branch information
Showing
2 changed files
with
59 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# frozen_string_literal: true | ||
|
||
return unless defined?(Gem) | ||
|
||
require "rubygems/commands/push_command" | ||
|
||
Gem::Commands::PushCommand.prepend(Module.new do | ||
def send_push_request(name, args) | ||
return super if options[:attestations]&.any? || @host != "https://rubygems.org" | ||
|
||
begin | ||
send_push_request_with_attestation(name, args) | ||
rescue StandardError => e | ||
alert_warning "Failed to push with attestation, retrying without attestation.\n#{e.full_message}" | ||
super | ||
end | ||
end | ||
|
||
def send_push_request_with_attestation(name, args) | ||
attestation = attest!(name) | ||
if options[:attestations] | ||
options[:attestations] << attestation | ||
send_push_request(name, args) | ||
else | ||
rubygems_api_request(*args, scope: get_push_scope) do |request| | ||
request.set_form([ | ||
["gem", Gem.read_binary(name), { filename: name, content_type: "application/octet-stream" }], | ||
["attestations", "[#{Gem.read_binary(attestation)}]", { content_type: "application/json" }] | ||
], "multipart/form-data") | ||
request.add_field "Authorization", api_key | ||
end | ||
end | ||
end | ||
|
||
def attest!(name) | ||
require "open3" | ||
bundle = "#{name}.sigstore.json" | ||
env = defined?(Bundler.unbundled_env) ? Bundler.unbundled_env : ENV.to_h | ||
out, st = Open3.capture2e( | ||
env, | ||
Gem.ruby, "-S", "gem", "exec", | ||
"sigstore-cli:0.2.1", "sign", name, "--bundle", bundle, | ||
unsetenv_others: true | ||
) | ||
raise Gem::Exception, "Failed to sign gem:\n\n#{out}" unless st.success? | ||
|
||
bundle | ||
end | ||
end) |