Skip to content

Commit e67365b

Browse files
committed
Remove mutual exclusivity between PAT and API+App key auth
1 parent 027cc15 commit e67365b

File tree

3 files changed

+17
-27
lines changed

3 files changed

+17
-27
lines changed

.generator/src/generator/templates/api_client.j2

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -366,15 +366,7 @@ module {{ module_name }}
366366
# @param [Hash] query_params Query parameters
367367
# @param [String] auth_names Authentication scheme name
368368
def update_params_for_auth!(header_params, query_params, auth_names)
369-
auth_names = Array(auth_names)
370-
371-
# When a bearer token (PAT) is configured, use Bearer auth exclusively
372-
# instead of API key + App key.
373-
if @config.access_token
374-
auth_names = [:bearerAuth]
375-
end
376-
377-
auth_names.each do |auth_name|
369+
Array(auth_names).each do |auth_name|
378370
auth_setting = @config.auth_settings[auth_name]
379371
next unless auth_setting
380372
next if auth_setting[:value].nil? || auth_setting[:value].to_s.empty?

lib/datadog_api_client/api_client.rb

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -377,15 +377,7 @@ def build_request_url(path, opts = {})
377377
# @param [Hash] query_params Query parameters
378378
# @param [String] auth_names Authentication scheme name
379379
def update_params_for_auth!(header_params, query_params, auth_names)
380-
auth_names = Array(auth_names)
381-
382-
# When a bearer token (PAT) is configured, use Bearer auth exclusively
383-
# instead of API key + App key.
384-
if @config.access_token
385-
auth_names = [:bearerAuth]
386-
end
387-
388-
auth_names.each do |auth_name|
380+
Array(auth_names).each do |auth_name|
389381
auth_setting = @config.auth_settings[auth_name]
390382
next unless auth_setting
391383
next if auth_setting[:value].nil? || auth_setting[:value].to_s.empty?

spec/api_client_spec.rb

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -211,23 +211,29 @@
211211
let(:config) { DatadogAPIClient::Configuration.new }
212212
let(:api_client) { DatadogAPIClient::APIClient.new(config) }
213213

214-
context 'when bearer token (PAT) is configured' do
214+
context 'when all auth credentials are configured' do
215215
before do
216216
config.api_key = 'test_api_key'
217217
config.application_key = 'test_app_key'
218218
config.access_token = 'ddpat_test_pat'
219219
end
220220

221-
it 'sends only Bearer Authorization header, no API key or app key' do
221+
it 'sends all configured auth headers simultaneously' do
222222
header_params = {}
223223
query_params = {}
224-
api_client.update_params_for_auth!(header_params, query_params, [:apiKeyAuth, :appKeyAuth])
224+
api_client.update_params_for_auth!(header_params, query_params, [:apiKeyAuth, :appKeyAuth, :bearerAuth])
225225
expect(header_params['Authorization']).to eq('Bearer ddpat_test_pat')
226-
expect(header_params).not_to have_key('DD-API-KEY')
227-
expect(header_params).not_to have_key('DD-APPLICATION-KEY')
226+
expect(header_params['DD-API-KEY']).to eq('test_api_key')
227+
expect(header_params['DD-APPLICATION-KEY']).to eq('test_app_key')
228228
end
229+
end
229230

230-
it 'sends only Bearer even when bearerAuth is already in auth_names' do
231+
context 'when only bearer token is configured' do
232+
before do
233+
config.access_token = 'ddpat_test_pat'
234+
end
235+
236+
it 'sends only Bearer header, skips empty API key and app key' do
231237
header_params = {}
232238
query_params = {}
233239
api_client.update_params_for_auth!(header_params, query_params, [:apiKeyAuth, :appKeyAuth, :bearerAuth])
@@ -237,16 +243,16 @@
237243
end
238244
end
239245

240-
context 'when bearer token is not configured' do
246+
context 'when only API key and app key are configured' do
241247
before do
242248
config.api_key = 'test_api_key'
243249
config.application_key = 'test_app_key'
244250
end
245251

246-
it 'uses API key and app key, no Bearer header' do
252+
it 'sends API key and app key, no Bearer header' do
247253
header_params = {}
248254
query_params = {}
249-
api_client.update_params_for_auth!(header_params, query_params, [:apiKeyAuth, :appKeyAuth])
255+
api_client.update_params_for_auth!(header_params, query_params, [:apiKeyAuth, :appKeyAuth, :bearerAuth])
250256
expect(header_params['DD-API-KEY']).to eq('test_api_key')
251257
expect(header_params['DD-APPLICATION-KEY']).to eq('test_app_key')
252258
expect(header_params).not_to have_key('Authorization')

0 commit comments

Comments
 (0)