From 6dc0086d2046506d838b9de41cb12a4aadec58d1 Mon Sep 17 00:00:00 2001 From: Dmitriy Ivliev <3938172+moofkit@users.noreply.github.com> Date: Wed, 10 Jul 2024 17:09:50 +0300 Subject: [PATCH] [rubygems/rubygems] fix s3 source configuration issue https://github.com/rubygems/rubygems/commit/356726bd1a --- lib/rubygems/config_file.rb | 21 +++++---- test/rubygems/test_gem_config_file.rb | 67 +++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 8 deletions(-) diff --git a/lib/rubygems/config_file.rb b/lib/rubygems/config_file.rb index 7874ad0dc9..a2bcb6dfbc 100644 --- a/lib/rubygems/config_file.rb +++ b/lib/rubygems/config_file.rb @@ -522,12 +522,12 @@ if you believe they were disclosed to a third party. # Return the configuration information for +key+. def [](key) - @hash[key.to_s] + @hash[key] || @hash[key.to_s] end # Set configuration option +key+ to +value+. def []=(key, value) - @hash[key.to_s] = value + @hash[key] = value end def ==(other) # :nodoc: @@ -555,8 +555,13 @@ if you believe they were disclosed to a third party. require_relative "yaml_serializer" content = Gem::YAMLSerializer.load(yaml) + deep_transform_config_keys!(content) + end - content.transform_keys! do |k| + private + + def self.deep_transform_config_keys!(config) + config.transform_keys! do |k| if k.match?(/\A:(.*)\Z/) k[1..-1].to_sym elsif k.include?("__") || k.match?(%r{/\Z}) @@ -570,7 +575,7 @@ if you believe they were disclosed to a third party. end end - content.transform_values! do |v| + config.transform_values! do |v| if v.is_a?(String) if v.match?(/\A:(.*)\Z/) v[1..-1].to_sym @@ -583,18 +588,18 @@ if you believe they were disclosed to a third party. else v end - elsif v.is_a?(Hash) && v.empty? + elsif v.empty? nil + elsif v.is_a?(Hash) + deep_transform_config_keys!(v) else v end end - content + config end - private - def set_config_file_name(args) @config_file_name = ENV["GEMRC"] need_config_file_name = false diff --git a/test/rubygems/test_gem_config_file.rb b/test/rubygems/test_gem_config_file.rb index a055f248be..b28dcf503e 100644 --- a/test/rubygems/test_gem_config_file.rb +++ b/test/rubygems/test_gem_config_file.rb @@ -575,6 +575,73 @@ if you believe they were disclosed to a third party. assert_equal("bar", actual[:foo]) end + def test_s3_source + yaml = <<~YAML + --- + :sources: + - s3://bucket1/ + - s3://bucket2/ + - s3://bucket3/path_to_gems_dir/ + - s3://bucket4/ + - https://rubygems.org/ + :s3_source: + :bucket1: + :provider: env + :bucket2: + :provider: instance_profile + :region: us-west-2 + :bucket3: + :id: AOUEAOEU123123AOEUAO + :secret: aodnuhtdao/saeuhto+19283oaehu/asoeu+123h + :region: us-east-2 + :bucket4: + :id: AOUEAOEU123123AOEUAO + :secret: aodnuhtdao/saeuhto+19283oaehu/asoeu+123h + :security_token: AQoDYXdzEJr + :region: us-west-1 + YAML + + File.open @temp_conf, "w" do |fp| + fp.puts yaml + end + util_config_file + + assert_equal(["s3://bucket1/", "s3://bucket2/", "s3://bucket3/path_to_gems_dir/", "s3://bucket4/", "https://rubygems.org/"], @cfg.sources) + expected_config = { + bucket1: { provider: "env" }, + bucket2: { provider: "instance_profile", region: "us-west-2" }, + bucket3: { id: "AOUEAOEU123123AOEUAO", secret: "aodnuhtdao/saeuhto+19283oaehu/asoeu+123h", region: "us-east-2" }, + bucket4: { id: "AOUEAOEU123123AOEUAO", secret: "aodnuhtdao/saeuhto+19283oaehu/asoeu+123h", security_token: "AQoDYXdzEJr", region: "us-west-1" }, + } + assert_equal(expected_config, @cfg[:s3_source]) + assert_equal(expected_config[:bucket1], @cfg[:s3_source][:bucket1]) + assert_equal(expected_config[:bucket2], @cfg[:s3_source][:bucket2]) + assert_equal(expected_config[:bucket3], @cfg[:s3_source][:bucket3]) + assert_equal(expected_config[:bucket4], @cfg[:s3_source][:bucket4]) + end + + def test_s3_source_with_config_without_lookahead + yaml = <<~YAML + :sources: + - s3://bucket1/ + s3_source: + bucket1: + provider: env + YAML + + File.open @temp_conf, "w" do |fp| + fp.puts yaml + end + util_config_file + + assert_equal(["s3://bucket1/"], @cfg.sources) + expected_config = { + "bucket1" => { "provider" => "env" }, + } + assert_equal(expected_config, @cfg[:s3_source]) + assert_equal(expected_config[:bucket1], @cfg[:s3_source][:bucket1]) + end + def util_config_file(args = @cfg_args) @cfg = Gem::ConfigFile.new args end