|
206 | 206 | expect(api_client.sanitize_filename('.\sun.gif')).to eq('sun.gif') |
207 | 207 | end |
208 | 208 | end |
| 209 | + |
| 210 | + describe '#update_params_for_auth!' do |
| 211 | + let(:config) { DatadogAPIClient::Configuration.new } |
| 212 | + let(:api_client) { DatadogAPIClient::APIClient.new(config) } |
| 213 | + |
| 214 | + context 'when bearer token (PAT) is configured' do |
| 215 | + before do |
| 216 | + config.api_key = 'test_api_key' |
| 217 | + config.application_key = 'test_app_key' |
| 218 | + config.access_token = 'ddpat_test_pat' |
| 219 | + end |
| 220 | + |
| 221 | + it 'sends only Bearer Authorization header, no API key or app key' do |
| 222 | + header_params = {} |
| 223 | + query_params = {} |
| 224 | + api_client.update_params_for_auth!(header_params, query_params, [:apiKeyAuth, :appKeyAuth]) |
| 225 | + 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') |
| 228 | + end |
| 229 | + |
| 230 | + it 'sends only Bearer even when bearerAuth is already in auth_names' do |
| 231 | + header_params = {} |
| 232 | + query_params = {} |
| 233 | + api_client.update_params_for_auth!(header_params, query_params, [:apiKeyAuth, :appKeyAuth, :bearerAuth]) |
| 234 | + expect(header_params['Authorization']).to eq('Bearer ddpat_test_pat') |
| 235 | + expect(header_params).not_to have_key('DD-API-KEY') |
| 236 | + expect(header_params).not_to have_key('DD-APPLICATION-KEY') |
| 237 | + end |
| 238 | + end |
| 239 | + |
| 240 | + context 'when bearer token is not configured' do |
| 241 | + before do |
| 242 | + config.api_key = 'test_api_key' |
| 243 | + config.application_key = 'test_app_key' |
| 244 | + end |
| 245 | + |
| 246 | + it 'uses API key and app key, no Bearer header' do |
| 247 | + header_params = {} |
| 248 | + query_params = {} |
| 249 | + api_client.update_params_for_auth!(header_params, query_params, [:apiKeyAuth, :appKeyAuth]) |
| 250 | + expect(header_params['DD-API-KEY']).to eq('test_api_key') |
| 251 | + expect(header_params['DD-APPLICATION-KEY']).to eq('test_app_key') |
| 252 | + expect(header_params).not_to have_key('Authorization') |
| 253 | + end |
| 254 | + end |
| 255 | + end |
| 256 | + |
| 257 | + describe '#sanitize_request_header' do |
| 258 | + let(:api_client) { DatadogAPIClient::APIClient.new } |
| 259 | + |
| 260 | + it 'redacts sensitive headers including Authorization' do |
| 261 | + headers = { |
| 262 | + 'DD-API-KEY' => 'secret_api_key', |
| 263 | + 'DD-APPLICATION-KEY' => 'secret_app_key', |
| 264 | + 'Authorization' => 'Bearer ddapp_secret_pat', |
| 265 | + 'Content-Type' => 'application/json' |
| 266 | + } |
| 267 | + sanitized = api_client.sanitize_request_header(headers) |
| 268 | + expect(sanitized['DD-API-KEY']).to eq('REDACTED') |
| 269 | + expect(sanitized['DD-APPLICATION-KEY']).to eq('REDACTED') |
| 270 | + expect(sanitized['Authorization']).to eq('REDACTED') |
| 271 | + expect(sanitized['Content-Type']).to eq('application/json') |
| 272 | + end |
| 273 | + end |
209 | 274 | end |
0 commit comments