Skip to content

Commit 50e4dc9

Browse files
authored
Merge pull request #307 from mothership/master
First pass at adding AssumeRoleWebIdentity creds
2 parents c6e8576 + 6462967 commit 50e4dc9

File tree

4 files changed

+122
-1
lines changed

4 files changed

+122
-1
lines changed

README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,51 @@ is set to 3600 seconds.
9393
A unique identifier that is used by third parties when assuming roles in
9494
their customers' accounts.
9595

96+
### web_identity_credentials
97+
98+
Similar to the assume_role_credentials, but for usage in EKS.
99+
100+
<match *>
101+
@type s3
102+
103+
<web_identity_credentials>
104+
role_arn ROLE_ARN
105+
role_session_name ROLE_SESSION_NAME
106+
web_identity_token_file AWS_WEB_IDENTITY_TOKEN_FILE
107+
</web_identity_credentials>
108+
</match>
109+
110+
See also:
111+
112+
* [Using IAM Roles - AWS Identity and Access
113+
Management](http://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use.html)
114+
* [IAM Roles For Service Accounts](https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts-technical-overview.html)
115+
* [Aws::STS::Client](http://docs.aws.amazon.com/sdkforruby/api/Aws/STS/Client.html)
116+
* [Aws::AssumeRoleWebIdentityCredentials](https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/AssumeRoleWebIdentityCredentials.html)
117+
118+
**role_arn (required)**
119+
120+
The Amazon Resource Name (ARN) of the role to assume.
121+
122+
**role_session_name (required)**
123+
124+
An identifier for the assumed role session.
125+
126+
**web_identity_token_file (required)**
127+
128+
The absolute path to the file on disk containing the OIDC token
129+
130+
**policy**
131+
132+
An IAM policy in JSON format.
133+
134+
**duration_seconds**
135+
136+
The duration, in seconds, of the role session. The value can range from
137+
900 seconds (15 minutes) to 43200 seconds (12 hours). By default, the value
138+
is set to 3600 seconds.
139+
140+
96141
### instance_profile_credentials
97142

98143
Retrieve temporary security credentials via HTTP request. This is useful on

lib/fluent/plugin/in_s3.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,20 @@ def initialize
4242
desc "A unique identifier that is used by third parties when assuming roles in their customers' accounts."
4343
config_param :external_id, :string, default: nil
4444
end
45+
# See the following link for additional params that could be added:
46+
# https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/STS/Client.html#assume_role_with_web_identity-instance_method
47+
config_section :web_identity_credentials, multi: false do
48+
desc "The Amazon Resource Name (ARN) of the role to assume"
49+
config_param :role_arn, :string # required
50+
desc "An identifier for the assumed role session"
51+
config_param :role_session_name, :string #required
52+
desc "The absolute path to the file on disk containing the OIDC token"
53+
config_param :web_identity_token_file, :string #required
54+
desc "An IAM policy in JSON format"
55+
config_param :policy, :string, default: nil
56+
desc "The duration, in seconds, of the role session (900-43200)"
57+
config_param :duration_seconds, :integer, default: nil
58+
end
4559
config_section :instance_profile_credentials, multi: false do
4660
desc "Number of times to retry when retrieving credentials"
4761
config_param :retries, :integer, default: nil
@@ -202,6 +216,17 @@ def setup_credentials
202216
credentials_options[:client] = Aws::STS::Client.new(:region => @s3_region)
203217
end
204218
options[:credentials] = Aws::AssumeRoleCredentials.new(credentials_options)
219+
when @web_identity_credentials
220+
c = @web_identity_credentials
221+
credentials_options[:role_arn] = c.role_arn
222+
credentials_options[:role_session_name] = c.role_session_name
223+
credentials_options[:web_identity_token_file] = c.web_identity_token_file
224+
credentials_options[:policy] = c.policy if c.policy
225+
credentials_options[:duration_seconds] = c.duration_seconds if c.duration_seconds
226+
if @s3_region
227+
credentials_options[:client] = Aws::STS::Client.new(:region => @s3_region)
228+
end
229+
options[:credentials] = Aws::AssumeRoleWebIdentityCredentials.new(credentials_options)
205230
when @instance_profile_credentials
206231
c = @instance_profile_credentials
207232
credentials_options[:retries] = c.retries if c.retries

lib/fluent/plugin/out_s3.rb

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,20 @@ def initialize
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
4242
end
43+
# See the following link for additional params that could be added:
44+
# https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/STS/Client.html#assume_role_with_web_identity-instance_method
45+
config_section :web_identity_credentials, multi: false do
46+
desc "The Amazon Resource Name (ARN) of the role to assume"
47+
config_param :role_arn, :string # required
48+
desc "An identifier for the assumed role session"
49+
config_param :role_session_name, :string #required
50+
desc "The absolute path to the file on disk containing the OIDC token"
51+
config_param :web_identity_token_file, :string #required
52+
desc "An IAM policy in JSON format"
53+
config_param :policy, :string, default: nil
54+
desc "The duration, in seconds, of the role session (900-43200)"
55+
config_param :duration_seconds, :integer, default: nil
56+
end
4357
config_section :instance_profile_credentials, multi: false do
4458
desc "Number of times to retry when retrieving credentials"
4559
config_param :retries, :integer, default: nil
@@ -175,7 +189,7 @@ def configure(conf)
175189
end
176190

177191
unless @index_format =~ /^%(0\d*)?[dxX]$/
178-
raise Fluent::ConfigError, "index_format parameter should follow `%[flags][width]type`. `0` is the only supported flag, and is mandatory if width is specified. `d`, `x` and `X` are supported types"
192+
raise Fluent::ConfigError, "index_format parameter should follow `%[flags][width]type`. `0` is the only supported flag, and is mandatory if width is specified. `d`, `x` and `X` are supported types"
179193
end
180194

181195
if @reduced_redundancy
@@ -460,6 +474,17 @@ def setup_credentials
460474
credentials_options[:client] = Aws::STS::Client.new(region: @s3_region)
461475
end
462476
options[:credentials] = Aws::AssumeRoleCredentials.new(credentials_options)
477+
when @web_identity_credentials
478+
c = @web_identity_credentials
479+
credentials_options[:role_arn] = c.role_arn
480+
credentials_options[:role_session_name] = c.role_session_name
481+
credentials_options[:web_identity_token_file] = c.web_identity_token_file
482+
credentials_options[:policy] = c.policy if c.policy
483+
credentials_options[:duration_seconds] = c.duration_seconds if c.duration_seconds
484+
if @s3_region
485+
credentials_options[:client] = Aws::STS::Client.new(:region => @s3_region)
486+
end
487+
options[:credentials] = Aws::AssumeRoleWebIdentityCredentials.new(credentials_options)
463488
when @instance_profile_credentials
464489
c = @instance_profile_credentials
465490
credentials_options[:retries] = c.retries if c.retries

test/test_out_s3.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,32 @@ def test_assume_role_credentials_with_region
567567
assert_equal(expected_credentials, credentials)
568568
end
569569

570+
def test_web_identity_credentials
571+
expected_credentials = Aws::Credentials.new("test_key", "test_secret")
572+
mock(Aws::AssumeRoleWebIdentityCredentials).new(
573+
role_arn: "test_arn",
574+
role_session_name: "test_session",
575+
web_identity_token_file: "test_file",
576+
client: anything
577+
){
578+
expected_credentials
579+
}
580+
581+
config = CONFIG_TIME_SLICE.split("\n").reject{|x| x =~ /.+aws_.+/}.join("\n")
582+
config += %[
583+
<web_identity_credentials>
584+
role_arn test_arn
585+
role_session_name test_session
586+
web_identity_token_file test_file
587+
</web_identity_credentials>
588+
]
589+
d = create_time_sliced_driver(config)
590+
assert_nothing_raised { d.run {} }
591+
client = d.instance.instance_variable_get(:@s3).client
592+
credentials = client.config.credentials
593+
assert_equal(expected_credentials, credentials)
594+
end
595+
570596
def test_instance_profile_credentials
571597
expected_credentials = Aws::Credentials.new("test_key", "test_secret")
572598
mock(Aws::InstanceProfileCredentials).new({}).returns(expected_credentials)

0 commit comments

Comments
 (0)