Skip to content

Commit 7a60e35

Browse files
committed
Add ability to set sts region
Signed-off-by: Chris Solidum <[email protected]>
1 parent dd6a0f2 commit 7a60e35

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

lib/fluent/plugin/out_s3.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ def initialize
3939
config_param :duration_seconds, :integer, default: nil
4040
desc "A unique identifier that is used by third parties when assuming roles in their customers' accounts."
4141
config_param :external_id, :string, default: nil, secret: true
42+
desc "The region of the STS endpoint to use."
43+
config_param :sts_region, :string, default: nil
4244
end
4345
# See the following link for additional params that could be added:
4446
# https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/STS/Client.html#assume_role_with_web_identity-instance_method
@@ -53,6 +55,8 @@ def initialize
5355
config_param :policy, :string, default: nil
5456
desc "The duration, in seconds, of the role session (900-43200)"
5557
config_param :duration_seconds, :integer, default: nil
58+
desc "The region of the STS endpoint to use."
59+
config_param :sts_region, :string, default: nil
5660
end
5761
config_section :instance_profile_credentials, multi: false do
5862
desc "Number of times to retry when retrieving credentials"
@@ -470,7 +474,9 @@ def setup_credentials
470474
credentials_options[:policy] = c.policy if c.policy
471475
credentials_options[:duration_seconds] = c.duration_seconds if c.duration_seconds
472476
credentials_options[:external_id] = c.external_id if c.external_id
473-
if @s3_region
477+
if c.sts_region
478+
credentials_options[:client] = Aws::STS::Client.new(region: c.sts_region)
479+
elsif @s3_region
474480
credentials_options[:client] = Aws::STS::Client.new(region: @s3_region)
475481
end
476482
options[:credentials] = Aws::AssumeRoleCredentials.new(credentials_options)
@@ -481,7 +487,9 @@ def setup_credentials
481487
credentials_options[:web_identity_token_file] = c.web_identity_token_file
482488
credentials_options[:policy] = c.policy if c.policy
483489
credentials_options[:duration_seconds] = c.duration_seconds if c.duration_seconds
484-
if @s3_region
490+
if c.sts_region
491+
credentials_options[:client] = Aws::STS::Client.new(:region => c.sts_region)
492+
elsif @s3_region
485493
credentials_options[:client] = Aws::STS::Client.new(:region => @s3_region)
486494
end
487495
options[:credentials] = Aws::AssumeRoleWebIdentityCredentials.new(credentials_options)

test/test_out_s3.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,36 @@ def test_web_identity_credentials
593593
assert_equal(expected_credentials, credentials)
594594
end
595595

596+
def test_web_identity_credentials_with_sts_region
597+
expected_credentials = Aws::Credentials.new("test_key", "test_secret")
598+
sts_client = Aws::STS::Client.new(region: 'us-east-1')
599+
mock(Aws::STS::Client).new(region: 'us-east-1'){ sts_client }
600+
mock(Aws::AssumeRoleWebIdentityCredentials).new(
601+
role_arn: "test_arn",
602+
role_session_name: "test_session",
603+
web_identity_token_file: "test_file",
604+
client: sts_client
605+
){
606+
expected_credentials
607+
}
608+
609+
config = CONFIG_TIME_SLICE.split("\n").reject{|x| x =~ /.+aws_.+/}.join("\n")
610+
config += %[
611+
s3_region us-west-2
612+
<web_identity_credentials>
613+
role_arn test_arn
614+
role_session_name test_session
615+
web_identity_token_file test_file
616+
sts_region us-east-1
617+
</web_identity_credentials>
618+
]
619+
d = create_time_sliced_driver(config)
620+
assert_nothing_raised { d.run {} }
621+
client = d.instance.instance_variable_get(:@s3).client
622+
credentials = client.config.credentials
623+
assert_equal(expected_credentials, credentials)
624+
end
625+
596626
def test_instance_profile_credentials
597627
expected_credentials = Aws::Credentials.new("test_key", "test_secret")
598628
mock(Aws::InstanceProfileCredentials).new({}).returns(expected_credentials)

0 commit comments

Comments
 (0)