Skip to content

Commit 6ef9fe5

Browse files
Copilothsbt
authored andcommitted
[ruby/rubygems] Use Tempfile for auto-attestation bundles and clean up after use
ruby/rubygems@498401c010 Co-authored-by: hsbt <12301+hsbt@users.noreply.github.com>
1 parent c4eeb67 commit 6ef9fe5

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

lib/rubygems/commands/push_command.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,12 @@ def send_push_request_with_attestation(name, args)
121121
Gem.read_binary(attestation)
122122
end
123123
else
124-
[Gem.read_binary(attest!(name))]
124+
bundle_path = attest!(name)
125+
begin
126+
[Gem.read_binary(bundle_path)]
127+
ensure
128+
File.unlink(bundle_path) if bundle_path && File.exist?(bundle_path)
129+
end
125130
end
126131
bundles = "[" + attestations.join(",") + "]"
127132

@@ -136,8 +141,12 @@ def send_push_request_with_attestation(name, args)
136141

137142
def attest!(name)
138143
require "open3"
144+
require "tempfile"
145+
146+
tempfile = Tempfile.new([File.basename(name, ".*"), ".sigstore.json"])
147+
bundle = tempfile.path
148+
tempfile.close(false)
139149

140-
bundle = "#{name}.sigstore.json"
141150
env = defined?(Bundler.unbundled_env) ? Bundler.unbundled_env : ENV.to_h
142151
out, st = Open3.capture2e(
143152
env,

test/rubygems/test_gem_commands_push_command.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ def test_execute_attestation_auto
123123
@fetcher.data["#{Gem.host}/api/v1/gems"] = HTTPResponseFactory.create(body: @response, code: 200, msg: "OK")
124124

125125
attestation_path = "#{@path}.sigstore.json"
126-
File.write(attestation_path, "auto-attestation")
126+
attestation_content = "auto-attestation"
127+
File.write(attestation_path, attestation_content)
127128
@cmd.options[:args] = [@path]
128129

129130
@cmd.stub(:attest!, attestation_path) do
@@ -133,7 +134,7 @@ def test_execute_attestation_auto
133134
assert_equal Gem::Net::HTTP::Post, @fetcher.last_request.class
134135
content_length = @fetcher.last_request["Content-Length"].to_i
135136
assert_equal content_length, @fetcher.last_request.body.length
136-
assert_attestation_multipart Gem.read_binary(attestation_path)
137+
assert_attestation_multipart attestation_content
137138
end
138139

139140
def test_execute_attestation_fallback

0 commit comments

Comments
 (0)