Skip to content

Commit

Permalink
Fix INI parsing of blank properties
Browse files Browse the repository at this point in the history
  • Loading branch information
alextwoods committed Aug 7, 2023
1 parent e41921f commit c736484
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
2 changes: 2 additions & 0 deletions gems/aws-sdk-core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Unreleased Changes
------------------

* Issue - Fix parsing of ini files with mixes of blank properties and nested configurations.

3.180.1 (2023-07-31)
------------------

Expand Down
9 changes: 8 additions & 1 deletion gems/aws-sdk-core/lib/aws-sdk-core/ini_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class << self
def ini_parse(raw)
current_profile = nil
current_prefix = nil
item = nil
previous_item = nil
raw.lines.inject({}) do |acc, line|
line = line.split(/^|\s;/).first # remove comments
profile = line.match(/^\[([^\[\]]+)\]\s*(#.+)?$/) unless line.nil?
Expand All @@ -17,11 +19,16 @@ def ini_parse(raw)
current_profile = named_profile[1] if named_profile
elsif current_profile
unless line.nil?
item = line.match(/^(.+?)\s*=\s*([^\s].*?)\s*$/)
previous_item = item
item = line.match(/^(.+?)\s*=\s*(.+?)\s*$/)
prefix = line.match(/^(.+?)\s*=\s*$/)
end
if item && item[1].match(/^\s+/)
# Need to add lines to a nested configuration.
if current_prefix.nil? && previous_item[2].strip.empty?
current_prefix = previous_item[1]
acc[current_profile][current_prefix] = {}
end
inner_item = line.match(/^\s*(.+?)\s*=\s*(.+?)\s*$/)
acc[current_profile] ||= {}
acc[current_profile][current_prefix] ||= {}
Expand Down
16 changes: 11 additions & 5 deletions gems/aws-sdk-core/spec/aws/ini_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,34 +34,40 @@ module Aws
s3 =
endpoint_url = https://localhost:8000
[profile blank-property]
aws_session_token =
s3 =
region = ap-southeast-1
FILE
}

let(:parsed) { IniParser.ini_parse(mock_config) }

it 'can parse basic attributes' do
parsed = IniParser.ini_parse(mock_config)
expect(parsed['default']['aws_access_key_id']).to eq("AKIABLAHBLAHBLAH")
expect(parsed['other']['region']).to eq("ap-northeast-1")
end

it 'can parse and strip the "profile" prefix from profile names' do
parsed = IniParser.ini_parse(mock_config)
expect(parsed['third']['region']).to eq("sa-east-1")
end

it 'can parse nested configuration' do
parsed = IniParser.ini_parse(mock_config)
expect(parsed['default']['s3']['region']).to eq("us-west-2")
expect(parsed['other']['s3']['region']).to eq("ap-southeast-1")
end

it 'can parse sso-session sections' do
parsed = IniParser.ini_parse(mock_config)
expect(parsed['sso-session dev']['sso_region']).to eq('us-east-1')
end

it 'can parse services sections' do
parsed = IniParser.ini_parse(mock_config)
expect(parsed['services test-services']['s3']['endpoint_url']).to eq('https://localhost:8000')
end

it 'can parse blank properties mixed with nested configurations with spaces' do
expect(parsed['blank-property']['aws_session_token']).to eq(' ')
expect(parsed['blank-property']['s3']['region']).to eq('ap-southeast-1')
end
end
end

0 comments on commit c736484

Please sign in to comment.