Skip to content

Commit

Permalink
Merge pull request #6683 from Shopify/load-localhost-keys
Browse files Browse the repository at this point in the history
Fix api key loading if RubyGems host is development

(cherry picked from commit 73df193)
  • Loading branch information
indirect authored and deivid-rodriguez committed Oct 16, 2023
1 parent c05ec6b commit 4c0b25f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
4 changes: 2 additions & 2 deletions lib/rubygems/config_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ def load_file(filename)

begin
config = self.class.load_with_rubygems_config_hash(File.read(filename))
if config.keys.any? {|k| k.to_s.gsub(%r{https?:\/\/}, "").include?(":") }
if config.keys.any? {|k| k.to_s.gsub(%r{https?:\/\/}, "").include?(": ") }
warn "Failed to load #{filename} because it doesn't contain valid YAML hash"
return {}
else
Expand Down Expand Up @@ -535,7 +535,7 @@ def self.load_with_rubygems_config_hash(yaml)
content.transform_keys! do |k|
if k.match?(/\A:(.*)\Z/)
k[1..-1].to_sym
elsif k.include?("__")
elsif k.include?("__") || k.match?(%r{/\Z})
if k.is_a?(Symbol)
k.to_s.gsub(/__/,".").gsub(%r{/\Z}, "").to_sym
else
Expand Down
13 changes: 9 additions & 4 deletions test/rubygems/test_gem_config_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -324,14 +324,19 @@ def test_load_api_keys
temp_cred = File.join Gem.user_home, ".gem", "credentials"
FileUtils.mkdir_p File.dirname(temp_cred)
File.open temp_cred, "w", 0o600 do |fp|
fp.puts ":rubygems_api_key: 701229f217cdf23b1344c7b4b54ca97"
fp.puts ":other: a5fdbb6ba150cbb83aad2bb2fede64c"
fp.puts ":rubygems_api_key: rubygems_b9ce70c306b3a2e248679fbbbd66722d408d3c8c4f00566c"
fp.puts ":other: rubygems_9636a120106ea8b81fbc792188251738665711d2ece160c5"
fp.puts "http://localhost:3000: rubygems_be293ad9dd71550a012b17d848893b41960b811ce9312b47"
end

util_config_file

assert_equal({ :rubygems => "701229f217cdf23b1344c7b4b54ca97",
:other => "a5fdbb6ba150cbb83aad2bb2fede64c" }, @cfg.api_keys)
assert_equal(
{ :rubygems => "rubygems_b9ce70c306b3a2e248679fbbbd66722d408d3c8c4f00566c",
:other => "rubygems_9636a120106ea8b81fbc792188251738665711d2ece160c5",
"http://localhost:3000" => "rubygems_be293ad9dd71550a012b17d848893b41960b811ce9312b47" },
@cfg.api_keys
)
end

def test_load_api_keys_bad_permission
Expand Down

0 comments on commit 4c0b25f

Please sign in to comment.