Skip to content

Commit 06e3e15

Browse files
Add CREDENTIALS_CODE for explicit profile (#3305)
1 parent 9b96890 commit 06e3e15

File tree

5 files changed

+90
-66
lines changed

5 files changed

+90
-66
lines changed

build_tools/services.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ class ServiceEnumerator
99
MANIFEST_PATH = File.expand_path('../../services.json', __FILE__)
1010

1111
# Minimum `aws-sdk-core` version for new gem builds
12-
MINIMUM_CORE_VERSION = "3.231.0"
12+
MINIMUM_CORE_VERSION = "3.234.0"
1313

1414
# Minimum `aws-sdk-core` version for new S3 gem builds
15-
MINIMUM_CORE_VERSION_S3 = "3.231.0"
15+
MINIMUM_CORE_VERSION_S3 = "3.234.0"
1616

1717
EVENTSTREAM_PLUGIN = "Aws::Plugins::EventStreamConfiguration"
1818

gems/aws-sdk-core/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Unreleased Changes
22
------------------
33

4+
* Issue - Add `CREDENTIALS_CODE` metric for `static_profile_` prefixed methods in default credential chain.
5+
46
3.233.0 (2025-09-23)
57
------------------
68

gems/aws-sdk-core/lib/aws-sdk-core/credential_provider_chain.rb

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def initialize(config = nil)
1111
def resolve
1212
providers.each do |method_name, options|
1313
provider = send(method_name, options.merge(config: @config))
14-
return provider if provider && provider.set?
14+
return provider if provider&.set?
1515
end
1616
nil
1717
end
@@ -54,47 +54,65 @@ def static_credentials(options)
5454
end
5555

5656
def static_profile_assume_role_web_identity_credentials(options)
57-
if Aws.shared_config.config_enabled? && options[:config] && options[:config].profile
58-
Aws.shared_config.assume_role_web_identity_credentials_from_config(
57+
return unless Aws.shared_config.config_enabled? && options[:config]&.profile
58+
59+
with_metrics('CREDENTIALS_CODE') do
60+
creds = Aws.shared_config.assume_role_web_identity_credentials_from_config(
5961
profile: options[:config].profile,
6062
region: options[:config].region
6163
)
64+
return unless creds
65+
66+
creds.metrics << 'CREDENTIALS_CODE'
67+
creds
6268
end
6369
end
6470

6571
def static_profile_sso_credentials(options)
66-
if Aws.shared_config.config_enabled? && options[:config] && options[:config].profile
67-
Aws.shared_config.sso_credentials_from_config(
72+
return unless Aws.shared_config.config_enabled? && options[:config]&.profile
73+
74+
with_metrics('CREDENTIALS_CODE') do
75+
creds = Aws.shared_config.sso_credentials_from_config(
6876
profile: options[:config].profile
6977
)
78+
return unless creds
79+
80+
creds.metrics << 'CREDENTIALS_CODE'
81+
creds
7082
end
7183
end
7284

7385
def static_profile_assume_role_credentials(options)
74-
if Aws.shared_config.config_enabled? && options[:config] && options[:config].profile
75-
assume_role_with_profile(options, options[:config].profile)
86+
return unless Aws.shared_config.config_enabled? && options[:config]&.profile
87+
88+
with_metrics('CREDENTIALS_CODE') do
89+
creds = assume_role_with_profile(options, options[:config].profile)
90+
return unless creds
91+
92+
creds.metrics << 'CREDENTIALS_CODE'
93+
creds
7694
end
7795
end
7896

7997
def static_profile_credentials(options)
80-
if options[:config] && options[:config].profile
81-
creds = SharedCredentials.new(profile_name: options[:config].profile)
82-
creds.metrics = ['CREDENTIALS_PROFILE']
83-
creds
84-
end
98+
return unless options[:config]&.profile
99+
100+
creds = SharedCredentials.new(profile_name: options[:config].profile)
101+
creds.metrics << 'CREDENTIALS_PROFILE'
102+
creds
85103
rescue Errors::NoSuchProfileError
86104
nil
87105
end
88106

89107
def static_profile_process_credentials(options)
90-
if Aws.shared_config.config_enabled? && options[:config] && options[:config].profile
91-
process_provider = Aws.shared_config.credential_process(profile: options[:config].profile)
92-
if process_provider
93-
creds = ProcessCredentials.new([process_provider])
94-
creds.metrics << 'CREDENTIALS_PROFILE_PROCESS'
95-
creds
96-
end
97-
end
108+
return unless Aws.shared_config.config_enabled? && options[:config]&.profile
109+
110+
process_provider = Aws.shared_config.credential_process(profile: options[:config].profile)
111+
return unless process_provider
112+
113+
creds = ProcessCredentials.new([process_provider])
114+
creds.metrics.concat(%w[CREDENTIALS_PROFILE_PROCESS CREDENTIALS_CODE])
115+
creds
98116
rescue Errors::NoSuchProfileError
99117
nil
100118
end
@@ -122,7 +140,7 @@ def envar(keys)
122140
end
123141

124142
def determine_profile_name(options)
125-
(options[:config] && options[:config].profile) || ENV['AWS_PROFILE'] || ENV['AWS_DEFAULT_PROFILE'] || 'default'
143+
(options[:config]&.profile) || ENV['AWS_PROFILE'] || ENV['AWS_DEFAULT_PROFILE'] || 'default'
126144
end
127145

128146
def shared_credentials(options)
@@ -201,10 +219,14 @@ def assume_role_with_profile(options, profile_name)
201219
profile: profile_name,
202220
chain_config: @config
203221
}
204-
if options[:config] && options[:config].region
222+
if options[:config]&.region
205223
assume_opts[:region] = options[:config].region
206224
end
207225
Aws.shared_config.assume_role_credentials_from_config(assume_opts)
208226
end
227+
228+
def with_metrics(metrics, &block)
229+
Aws::Plugins::UserAgent.metric(*metrics, &block)
230+
end
209231
end
210232
end

gems/aws-sdk-core/spec/aws/credential_provider_chain_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def validate_metrics(*expected_metrics)
194194
allow(config).to receive(:profile).and_return(expected_creds[:profile_name])
195195
ENV['AWS_DEFAULT_PROFILE'] = 'BAD_PROFILE'
196196
validate_credentials(expected_creds)
197-
validate_metrics('CREDENTIALS_PROFILE')
197+
validate_metrics('CREDENTIALS_CODE', 'CREDENTIALS_PROFILE')
198198
end
199199
end
200200

@@ -219,7 +219,7 @@ def validate_metrics(*expected_metrics)
219219
allow(config).to receive(:profile).and_return(expected_creds[:profile_name])
220220
with_env_credentials
221221
validate_credentials(expected_creds)
222-
validate_metrics('CREDENTIALS_PROFILE')
222+
validate_metrics('CREDENTIALS_CODE', 'CREDENTIALS_PROFILE')
223223
end
224224
end
225225
end

0 commit comments

Comments
 (0)