From b1d825ab3a25a12e2602c13555b22809c93bc12e Mon Sep 17 00:00:00 2001 From: bronzdoc Date: Wed, 29 May 2019 20:22:52 -0600 Subject: [PATCH 1/4] Make ruby_code method handle OpenSSL::PKey::RSA objects --- lib/rubygems/specification.rb | 1 + test/rubygems/test_gem_specification.rb | 30 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/lib/rubygems/specification.rb b/lib/rubygems/specification.rb index b3db311cbf30..ff54400c3b54 100644 --- a/lib/rubygems/specification.rb +++ b/lib/rubygems/specification.rb @@ -2303,6 +2303,7 @@ def ruby_code(obj) when Time then obj.strftime('%Y-%m-%d').dump when Numeric then obj.inspect when true, false, nil then obj.inspect + when OpenSSL::PKey::RSA then obj.class when Gem::Platform then "Gem::Platform.new(#{obj.to_a.inspect})" when Gem::Requirement then list = obj.as_list diff --git a/test/rubygems/test_gem_specification.rb b/test/rubygems/test_gem_specification.rb index 7ed502fa0876..c026ea4c3f6a 100644 --- a/test/rubygems/test_gem_specification.rb +++ b/test/rubygems/test_gem_specification.rb @@ -2445,6 +2445,36 @@ def test_to_ruby assert_equal @a2, same_spec end + def test_to_ruby_with_rsa_key + rsa_key = OpenSSL::PKey::RSA.new(2048) + @a2.signing_key = rsa_key + ruby_code = @a2.to_ruby + + expected = <<-SPEC +# -*- encoding: utf-8 -*- +# stub: a 2 ruby lib + +Gem::Specification.new do |s| + s.name = "a".freeze + s.version = "2" + + s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version= + s.require_paths = ["lib".freeze] + s.authors = ["A User".freeze] + s.date = "2019-05-30" + s.description = "This is a test description".freeze + s.email = "example@example.com".freeze + s.files = ["lib/code.rb".freeze] + s.homepage = "http://example.com".freeze + s.rubygems_version = "3.1.0.pre1".freeze + s.signing_key = OpenSSL::PKey::RSA + s.summary = "this is a summary".freeze +end + SPEC + + assert_equal expected, ruby_code + end + def test_to_ruby_for_cache @a2.add_runtime_dependency 'b', '1' @a2.dependencies.first.instance_variable_set :@type, nil From 8ac0647659611e16b5e4883167dcce3d003c34c9 Mon Sep 17 00:00:00 2001 From: bronzdoc Date: Wed, 29 May 2019 20:23:59 -0600 Subject: [PATCH 2/4] Fix indentation in case statement --- lib/rubygems/specification.rb | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/rubygems/specification.rb b/lib/rubygems/specification.rb index ff54400c3b54..a41cbde948df 100644 --- a/lib/rubygems/specification.rb +++ b/lib/rubygems/specification.rb @@ -2293,19 +2293,19 @@ def ri_dir def ruby_code(obj) case obj - when String then obj.dump + ".freeze" - when Array then '[' + obj.map { |x| ruby_code x }.join(", ") + ']' - when Hash then + when String then obj.dump + ".freeze" + when Array then '[' + obj.map { |x| ruby_code x }.join(", ") + ']' + when Hash then seg = obj.keys.sort.map { |k| "#{k.to_s.dump} => #{obj[k].to_s.dump}" } "{ #{seg.join(', ')} }" - when Gem::Version then obj.to_s.dump - when DateLike then obj.strftime('%Y-%m-%d').dump - when Time then obj.strftime('%Y-%m-%d').dump - when Numeric then obj.inspect - when true, false, nil then obj.inspect + when Gem::Version then obj.to_s.dump + when DateLike then obj.strftime('%Y-%m-%d').dump + when Time then obj.strftime('%Y-%m-%d').dump + when Numeric then obj.inspect + when true, false, nil then obj.inspect when OpenSSL::PKey::RSA then obj.class - when Gem::Platform then "Gem::Platform.new(#{obj.to_a.inspect})" - when Gem::Requirement then + when Gem::Platform then "Gem::Platform.new(#{obj.to_a.inspect})" + when Gem::Requirement then list = obj.as_list "Gem::Requirement.new(#{ruby_code(list.size == 1 ? obj.to_s : list)})" else raise Gem::Exception, "ruby_code case not handled: #{obj.class}" From 2e65f7d4ae3c4f727591092daffad0151b8c9588 Mon Sep 17 00:00:00 2001 From: bronzdoc Date: Fri, 21 Jun 2019 19:30:37 -0600 Subject: [PATCH 3/4] Update expectation in test_to_ruby_with_rsa_key --- test/rubygems/test_gem_specification.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/rubygems/test_gem_specification.rb b/test/rubygems/test_gem_specification.rb index c026ea4c3f6a..a447edeeabbc 100644 --- a/test/rubygems/test_gem_specification.rb +++ b/test/rubygems/test_gem_specification.rb @@ -2461,13 +2461,12 @@ def test_to_ruby_with_rsa_key s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version= s.require_paths = ["lib".freeze] s.authors = ["A User".freeze] - s.date = "2019-05-30" + s.date = "#{Gem::Specification::TODAY.strftime "%Y-%m-%d"}" s.description = "This is a test description".freeze s.email = "example@example.com".freeze s.files = ["lib/code.rb".freeze] s.homepage = "http://example.com".freeze s.rubygems_version = "3.1.0.pre1".freeze - s.signing_key = OpenSSL::PKey::RSA s.summary = "this is a summary".freeze end SPEC From ba021fb4be1128603487d9710195e5f13e1faf0b Mon Sep 17 00:00:00 2001 From: bronzdoc Date: Fri, 21 Jun 2019 19:37:09 -0600 Subject: [PATCH 4/4] Avoid adding OpenSSL::PKey::RSA instances --- lib/rubygems/specification.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/rubygems/specification.rb b/lib/rubygems/specification.rb index a41cbde948df..d4290d9ffac2 100644 --- a/lib/rubygems/specification.rb +++ b/lib/rubygems/specification.rb @@ -2303,7 +2303,6 @@ def ruby_code(obj) when Time then obj.strftime('%Y-%m-%d').dump when Numeric then obj.inspect when true, false, nil then obj.inspect - when OpenSSL::PKey::RSA then obj.class when Gem::Platform then "Gem::Platform.new(#{obj.to_a.inspect})" when Gem::Requirement then list = obj.as_list @@ -2462,9 +2461,8 @@ def to_ruby @@attributes.each do |attr_name| next if handled.include? attr_name current_value = self.send(attr_name) - if current_value != default_value(attr_name) or - self.class.required_attribute? attr_name - result << " s.#{attr_name} = #{ruby_code current_value}" + if current_value != default_value(attr_name) || self.class.required_attribute?(attr_name) + result << " s.#{attr_name} = #{ruby_code current_value}" unless current_value.is_a?(OpenSSL::PKey::RSA) end end