forked from DataDog/pup
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.rs
More file actions
736 lines (685 loc) · 26.7 KB
/
Copy pathclient.rs
File metadata and controls
736 lines (685 loc) · 26.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
use reqwest_middleware::{ClientBuilder, ClientWithMiddleware};
#[cfg(not(target_arch = "wasm32"))]
use async_trait::async_trait;
#[cfg(not(target_arch = "wasm32"))]
use http::Extensions;
#[cfg(not(target_arch = "wasm32"))]
use reqwest_middleware::{Middleware, Next};
use crate::config::Config;
#[cfg(not(target_arch = "wasm32"))]
struct BearerAuthMiddleware {
token: String,
}
#[cfg(not(target_arch = "wasm32"))]
#[async_trait]
impl Middleware for BearerAuthMiddleware {
async fn handle(
&self,
mut req: reqwest_middleware::reqwest::Request,
extensions: &mut Extensions,
next: Next<'_>,
) -> reqwest_middleware::Result<reqwest_middleware::reqwest::Response> {
req.headers_mut().insert(
reqwest_middleware::reqwest::header::AUTHORIZATION,
format!("Bearer {}", self.token).parse().unwrap(),
);
next.run(req, extensions).await
}
}
// The `datadog-api-client` SDK's `Configuration.user_agent` is `pub(crate)`
// with no setter, so the only way to override it from outside the crate is
// via middleware that mutates the header after the SDK builds the request.
#[cfg(not(target_arch = "wasm32"))]
struct UserAgentMiddleware;
#[cfg(not(target_arch = "wasm32"))]
#[async_trait]
impl Middleware for UserAgentMiddleware {
async fn handle(
&self,
mut req: reqwest_middleware::reqwest::Request,
extensions: &mut Extensions,
next: Next<'_>,
) -> reqwest_middleware::Result<reqwest_middleware::reqwest::Response> {
if let Ok(ua) =
reqwest_middleware::reqwest::header::HeaderValue::from_str(&crate::useragent::get())
{
req.headers_mut()
.insert(reqwest_middleware::reqwest::header::USER_AGENT, ua);
}
next.run(req, extensions).await
}
}
// ---------------------------------------------------------------------------
// DD Configuration builder
// ---------------------------------------------------------------------------
/// Creates a DD API Configuration with all unstable ops enabled.
///
/// Explicitly injects `cfg` credentials so API key auth works on targets where
/// `std::env::var` is unavailable (e.g. wasm32-unknown-unknown).
///
/// If PUP_MOCK_SERVER is set, redirects all API calls to the mock server.
pub fn make_dd_config(cfg: &Config) -> datadog_api_client::datadog::Configuration {
let mut dd_cfg = datadog_api_client::datadog::Configuration::new();
// Enable all unstable operations.
for op in UNSTABLE_OPS {
dd_cfg.set_unstable_operation_enabled(op, true);
}
// Inject auth from cfg — supplements env vars and is required on WASM
// targets where std::env::var always returns Err.
if let Some(api_key) = &cfg.api_key {
dd_cfg.set_auth_key(
"apiKeyAuth",
datadog_api_client::datadog::APIKey {
key: api_key.clone(),
prefix: "".to_owned(),
},
);
}
if let Some(app_key) = &cfg.app_key {
dd_cfg.set_auth_key(
"appKeyAuth",
datadog_api_client::datadog::APIKey {
key: app_key.clone(),
prefix: "".to_owned(),
},
);
}
// Route the SDK at the single resolved API host. `api_base_url()` already
// encapsulates every case: the PUP_MOCK_SERVER override, the `api.{site}`
// derivation for canonical Datadog sites, and the verbatim host for
// vanity/gateway hosts. We feed it through the SDK's `{protocol}://{name}`
// template (server index 1) so the host is targeted exactly as resolved —
// the SDK never re-derives or prepends anything from `site`.
let base = cfg.api_base_url();
// A scheme-less value only occurs for a PUP_MOCK_SERVER set without
// `http(s)://`; default it to plain http (mock servers run HTTP locally).
// The non-mock path always yields `https://...`, so it never hits the fallback.
let (protocol, name) = base.split_once("://").unwrap_or(("http", base.as_str()));
dd_cfg.server_index = 1;
dd_cfg
.server_variables
.insert("protocol".into(), protocol.into());
dd_cfg.server_variables.insert("name".into(), name.into());
dd_cfg
}
/// Builds a reqwest middleware client for SDK API calls. Always installs
/// `UserAgentMiddleware` so requests carry pup's branded `User-Agent`
/// instead of the SDK's `datadog-api-client-rust/...` default. When
/// `send_bearer` is true and the config has an access token, also installs
/// `BearerAuthMiddleware`. OAuth-incompatible endpoints (see
/// `raw_client::OAUTH_EXCLUDED_ENDPOINTS`) pass `false` so the SDK falls back
/// to API key headers from the `Configuration`.
///
/// Returns `None` on WASM targets; callers use the SDK default client there.
pub fn make_dd_client(cfg: &Config, send_bearer: bool) -> Option<ClientWithMiddleware> {
#[cfg(not(target_arch = "wasm32"))]
{
let reqwest_client = reqwest_middleware::reqwest::Client::builder()
.build()
.expect("failed to build reqwest client");
let mut builder = ClientBuilder::new(reqwest_client).with(UserAgentMiddleware);
if send_bearer {
if let Some(token) = cfg.access_token.as_ref() {
builder = builder.with(BearerAuthMiddleware {
token: token.clone(),
});
}
}
return Some(builder.build());
}
#[allow(unreachable_code)]
{
let _ = (cfg, send_bearer);
None
}
}
#[macro_export]
macro_rules! make_api {
($api:ty, $cfg:expr) => {{
let cfg = $cfg;
let dd_cfg = $crate::client::make_dd_config(cfg);
match $crate::client::make_dd_client(cfg, true) {
Some(c) => <$api>::with_client_and_config(dd_cfg, c),
None => <$api>::with_config(dd_cfg),
}
}};
}
/// `make_api!` variant that skips bearer auth — for OAuth-incompatible endpoints.
#[macro_export]
macro_rules! make_api_no_auth {
($api:ty, $cfg:expr) => {{
let cfg = $cfg;
let dd_cfg = $crate::client::make_dd_config(cfg);
match $crate::client::make_dd_client(cfg, false) {
Some(c) => <$api>::with_client_and_config(dd_cfg, c),
None => <$api>::with_config(dd_cfg),
}
}};
}
// ---------------------------------------------------------------------------
// Unstable operations table — used by make_dd_config
// ---------------------------------------------------------------------------
/// All unstable operations (snake_case for the Rust DD client).
static UNSTABLE_OPS: &[&str] = &[
// Annotations (5)
"v2.create_annotation",
"v2.delete_annotation",
"v2.get_page_annotations",
"v2.list_annotations",
"v2.update_annotation",
// Incidents (26)
"v2.list_incidents",
"v2.search_incidents",
"v2.get_incident",
"v2.create_incident",
"v2.update_incident",
"v2.delete_incident",
"v2.list_incident_attachments",
"v2.create_global_incident_handle",
"v2.delete_global_incident_handle",
"v2.get_global_incident_settings",
"v2.list_global_incident_handles",
"v2.update_global_incident_handle",
"v2.update_global_incident_settings",
"v2.create_incident_postmortem_template",
"v2.delete_incident_postmortem_template",
"v2.get_incident_postmortem_template",
"v2.list_incident_postmortem_templates",
"v2.update_incident_postmortem_template",
// Fleet Automation (16)
"v2.list_fleet_agents",
"v2.get_fleet_agent_info",
"v2.list_fleet_agent_versions",
"v2.list_fleet_agent_tracers",
"v2.list_fleet_tracers",
"v2.list_fleet_deployments",
"v2.get_fleet_deployment",
"v2.create_fleet_deployment_configure",
"v2.create_fleet_deployment_upgrade",
"v2.cancel_fleet_deployment",
"v2.list_fleet_schedules",
"v2.get_fleet_schedule",
"v2.create_fleet_schedule",
"v2.update_fleet_schedule",
"v2.delete_fleet_schedule",
"v2.trigger_fleet_schedule",
// ServiceNow (9)
"v2.create_service_now_template",
"v2.delete_service_now_template",
"v2.get_service_now_template",
"v2.list_service_now_assignment_groups",
"v2.list_service_now_business_services",
"v2.list_service_now_instances",
"v2.list_service_now_templates",
"v2.list_service_now_users",
"v2.update_service_now_template",
// Jira (7)
"v2.create_jira_issue_template",
"v2.delete_jira_account",
"v2.delete_jira_issue_template",
"v2.get_jira_issue_template",
"v2.list_jira_accounts",
"v2.list_jira_issue_templates",
"v2.update_jira_issue_template",
// Cases (5)
"v2.create_case_jira_issue",
"v2.link_jira_issue_to_case",
"v2.unlink_jira_issue",
"v2.create_case_service_now_ticket",
"v2.move_case_to_project",
// Content Packs (3)
"v2.activate_content_pack",
"v2.deactivate_content_pack",
"v2.get_content_packs_states",
// Indicators of Compromise (2)
"v2.list_indicators_of_compromise",
"v2.get_indicator_of_compromise",
// Security Monitoring Terraform export (3)
"v2.bulk_export_security_monitoring_terraform_resources",
"v2.export_security_monitoring_terraform_resource",
"v2.convert_security_monitoring_terraform_resource",
// Code Coverage (2)
"v2.get_code_coverage_branch_summary",
"v2.get_code_coverage_commit_summary",
// OCI Integration (2)
"v2.create_tenancy_config",
"v2.get_tenancy_configs",
// HAMR (2)
"v2.create_hamr_org_connection",
"v2.get_hamr_org_connection",
// Entity Risk Scores (1)
"v2.list_entity_risk_scores",
// Org Group Policies (11)
"v2.list_org_group_policies",
"v2.get_org_group_policy",
"v2.create_org_group_policy",
"v2.update_org_group_policy",
"v2.delete_org_group_policy",
"v2.list_org_group_policy_overrides",
"v2.get_org_group_policy_override",
"v2.create_org_group_policy_override",
"v2.update_org_group_policy_override",
"v2.delete_org_group_policy_override",
"v2.list_org_group_policy_configs",
// Security Findings (1)
"v2.list_findings",
// SLO Status (1)
"v2.get_slo_status",
// Flaky Tests (4)
"v2.search_flaky_tests",
"v2.update_flaky_tests",
"v2.get_flaky_tests_management_policies",
"v2.update_flaky_tests_management_policies",
// Incidents Import (1)
"v2.import_incident",
// Change Management (6)
"v2.create_change_request",
"v2.create_change_request_branch",
"v2.delete_change_request_decision",
"v2.get_change_request",
"v2.update_change_request",
"v2.update_change_request_decision",
// Cloud Authentication (4)
"v2.create_aws_cloud_auth_persona_mapping",
"v2.delete_aws_cloud_auth_persona_mapping",
"v2.get_aws_cloud_auth_persona_mapping",
"v2.list_aws_cloud_auth_persona_mappings",
// LLM Observability (25)
"v2.create_llm_obs_project",
"v2.list_llm_obs_projects",
"v2.create_llm_obs_experiment",
"v2.list_llm_obs_experiments",
"v2.update_llm_obs_experiment",
"v2.delete_llm_obs_experiments",
"v2.create_llm_obs_dataset",
"v2.list_llm_obs_datasets",
"v2.batch_update_llm_obs_dataset",
"v2.clone_llm_obs_dataset",
"v2.restore_llm_obs_dataset_version",
"v2.create_llm_obs_annotation_queue",
"v2.list_llm_obs_annotation_queues",
"v2.update_llm_obs_annotation_queue",
"v2.delete_llm_obs_annotation_queue",
"v2.create_llm_obs_annotation_queue_interactions",
"v2.delete_llm_obs_annotation_queue_interactions",
"v2.get_llm_obs_annotated_interactions",
"v2.get_llm_obs_annotation_queue_label_schema",
"v2.update_llm_obs_annotation_queue_label_schema",
"v2.upsert_llm_obs_annotations",
"v2.delete_llm_obs_annotations",
"v2.get_llm_obs_custom_eval_config",
"v2.update_llm_obs_custom_eval_config",
"v2.delete_llm_obs_custom_eval_config",
// Logs Restriction Queries (9)
"v2.list_restriction_queries",
"v2.get_restriction_query",
"v2.create_restriction_query",
"v2.update_restriction_query",
"v2.delete_restriction_query",
"v2.list_restriction_query_roles",
"v2.add_role_to_restriction_query",
"v2.remove_role_from_restriction_query",
"v2.get_role_restriction_query",
// Datasets (5)
"v2.create_dataset",
"v2.delete_dataset",
"v2.get_all_datasets",
"v2.get_dataset",
"v2.update_dataset",
// Data Deletion (3)
"v2.cancel_data_deletion_request",
"v2.create_data_deletion_request",
"v2.get_data_deletion_requests",
// Service Scorecards (7)
"v2.create_scorecard_outcomes_batch",
"v2.create_scorecard_rule",
"v2.delete_scorecard_rule",
"v2.list_scorecard_outcomes",
"v2.list_scorecard_rules",
"v2.update_scorecard_outcomes_async",
"v2.update_scorecard_rule",
// Static Analysis (10)
"v2.create_custom_rule",
"v2.create_custom_rule_revision",
"v2.create_sca_resolve_vulnerable_symbols",
"v2.create_sca_result",
"v2.delete_custom_rule",
"v2.delete_custom_ruleset",
"v2.get_custom_rule",
"v2.get_custom_rule_revision",
"v2.get_custom_ruleset",
"v2.list_custom_rule_revisions",
"v2.revert_custom_rule_revision",
"v2.update_custom_ruleset",
// Bits AI Investigations (3)
"v2.get_investigation",
"v2.list_investigations",
"v2.trigger_investigation",
// Cloud Cost Management — Anomalies (1)
"v2.list_cost_anomalies",
// Tag Policies (6)
"v2.create_tag_policy",
"v2.delete_tag_policy",
"v2.get_tag_policy",
"v2.get_tag_policy_score",
"v2.list_tag_policies",
"v2.update_tag_policy",
// Model Lab (16)
"v2.delete_model_lab_run",
"v2.get_model_lab_artifact_content",
"v2.get_model_lab_project",
"v2.get_model_lab_run",
"v2.list_model_lab_project_artifacts",
"v2.list_model_lab_project_facet_keys",
"v2.list_model_lab_project_facet_values",
"v2.list_model_lab_projects",
"v2.list_model_lab_run_artifacts",
"v2.list_model_lab_run_facet_keys",
"v2.list_model_lab_run_facet_values",
"v2.list_model_lab_runs",
"v2.pin_model_lab_run",
"v2.star_model_lab_project",
"v2.unpin_model_lab_run",
"v2.unstar_model_lab_project",
];
#[cfg(test)]
mod tests {
use crate::test_support::*;
use super::*;
use crate::config::Config;
use crate::test_utils::ENV_LOCK;
fn test_cfg() -> Config {
Config {
api_key: Some("test".into()),
app_key: Some("test".into()),
access_token: None,
site: "datadoghq.com".into(),
site_explicit: false,
org: None,
output_format: crate::config::OutputFormat::Json,
auto_approve: false,
agent_mode: false,
read_only: false,
jq: None,
}
}
/// Asserts the SDK is routed at the host produced by `name`/`protocol`,
/// the single `{protocol}://{name}` template at server index 1.
fn assert_dd_host(
dd_cfg: &datadog_api_client::datadog::Configuration,
protocol: &str,
host: &str,
) {
assert_eq!(dd_cfg.server_index, 1);
assert_eq!(
dd_cfg.server_variables.get("protocol").map(String::as_str),
Some(protocol)
);
assert_eq!(
dd_cfg.server_variables.get("name").map(String::as_str),
Some(host)
);
}
/// Canonical Datadog sites derive `api.{site}` — including non-default ones
/// (staging datad0g.com, datadoghq.eu) resolved programmatically via `--org`
/// rather than DD_SITE.
#[test]
fn test_make_dd_config_canonical_site_derives_api_host() {
let _guard = ENV_LOCK.blocking_lock();
std::env::remove_var("PUP_MOCK_SERVER");
std::env::remove_var("DD_SITE");
let mut cfg = test_cfg();
cfg.site = "datad0g.com".into();
assert_dd_host(&make_dd_config(&cfg), "https", "api.datad0g.com");
cfg.site = "datadoghq.eu".into();
assert_dd_host(&make_dd_config(&cfg), "https", "api.datadoghq.eu");
}
#[test]
fn test_make_dd_config_literal_host_used_verbatim() {
let _guard = ENV_LOCK.blocking_lock();
std::env::remove_var("PUP_MOCK_SERVER");
std::env::remove_var("DD_SITE");
let mut cfg = test_cfg();
cfg.site = "mygateway.example.com".into(); // literal, not in KNOWN_SITES
assert_dd_host(&make_dd_config(&cfg), "https", "mygateway.example.com");
}
#[test]
fn test_make_dd_config_vanity_subdomain_used_verbatim() {
let _guard = ENV_LOCK.blocking_lock();
std::env::remove_var("PUP_MOCK_SERVER");
std::env::remove_var("DD_SITE");
let mut cfg = test_cfg();
cfg.site = "mycompany.datadoghq.com".into(); // vanity, not in KNOWN_SITES
assert_dd_host(&make_dd_config(&cfg), "https", "mycompany.datadoghq.com");
}
/// A literal host must be targeted verbatim even when the user has DD_SITE
/// set in their shell — `cfg.site` is the single source of truth and the SDK
/// never re-derives the host from the `DD_SITE`-populated `site` variable.
#[test]
fn test_make_dd_config_literal_host_ignores_env_dd_site() {
let _guard = ENV_LOCK.blocking_lock();
std::env::remove_var("PUP_MOCK_SERVER");
std::env::set_var("DD_SITE", "datadoghq.com");
let mut cfg = test_cfg();
cfg.site = "mygateway.example.com".into(); // literal, not in KNOWN_SITES
let dd_cfg = make_dd_config(&cfg);
std::env::remove_var("DD_SITE");
assert_dd_host(&dd_cfg, "https", "mygateway.example.com");
}
/// `cfg.site` (e.g. resolved from a saved org session) must override any
/// stale `DD_SITE` env var the user happens to have set in their shell —
/// otherwise `pup --org staging-org` would silently route to the env's
/// site instead of the org's saved site.
#[test]
fn test_make_dd_config_cfg_site_overrides_env_dd_site() {
let _guard = ENV_LOCK.blocking_lock();
std::env::remove_var("PUP_MOCK_SERVER");
std::env::set_var("DD_SITE", "datadoghq.com");
let mut cfg = test_cfg();
cfg.site = "datad0g.com".into();
let dd_cfg = make_dd_config(&cfg);
std::env::remove_var("DD_SITE");
assert_dd_host(&dd_cfg, "https", "api.datad0g.com");
}
#[test]
fn test_unstable_ops_count() {
assert_eq!(UNSTABLE_OPS.len(), 190);
}
#[test]
fn test_make_dd_client_some_without_token() {
// UA middleware is always installed, so the client is always Some on native.
let cfg = test_cfg();
assert!(make_dd_client(&cfg, true).is_some());
assert!(make_dd_client(&cfg, false).is_some());
}
#[test]
fn test_make_dd_client_some_with_token() {
let mut cfg = test_cfg();
cfg.access_token = Some("test-token".into());
assert!(make_dd_client(&cfg, true).is_some());
assert!(make_dd_client(&cfg, false).is_some());
}
#[test]
fn test_make_api_macro_without_bearer_token() {
use datadog_api_client::datadogV1::api_monitors::MonitorsAPI;
let _guard = ENV_LOCK.blocking_lock();
std::env::remove_var("PUP_MOCK_SERVER");
let cfg = test_cfg();
let _api: MonitorsAPI = crate::make_api!(MonitorsAPI, &cfg);
}
#[test]
fn test_make_api_macro_with_bearer_token() {
use datadog_api_client::datadogV1::api_monitors::MonitorsAPI;
let _guard = ENV_LOCK.blocking_lock();
std::env::remove_var("PUP_MOCK_SERVER");
let mut cfg = test_cfg();
cfg.access_token = Some("test-token".into());
let _api: MonitorsAPI = crate::make_api!(MonitorsAPI, &cfg);
}
#[test]
fn test_make_dd_config_returns_valid() {
let _guard = ENV_LOCK.blocking_lock();
let cfg = test_cfg();
// Ensure env vars are set for DD client
std::env::set_var("DD_API_KEY", "test-key");
std::env::set_var("DD_APP_KEY", "test-app-key");
std::env::remove_var("PUP_MOCK_SERVER");
std::env::remove_var("DD_SITE");
let dd_cfg = make_dd_config(&cfg);
// Default canonical site datadoghq.com derives api.datadoghq.com.
assert_dd_host(&dd_cfg, "https", "api.datadoghq.com");
std::env::remove_var("DD_API_KEY");
std::env::remove_var("DD_APP_KEY");
}
#[test]
fn test_make_dd_config_with_mock_server() {
let _guard = ENV_LOCK.blocking_lock();
let cfg = test_cfg();
std::env::set_var("DD_API_KEY", "test-key");
std::env::set_var("DD_APP_KEY", "test-app-key");
std::env::set_var("PUP_MOCK_SERVER", "http://127.0.0.1:9999");
let dd_cfg = make_dd_config(&cfg);
assert_eq!(dd_cfg.server_index, 1);
assert_eq!(dd_cfg.server_variables.get("protocol").unwrap(), "http");
assert_eq!(
dd_cfg.server_variables.get("name").unwrap(),
"127.0.0.1:9999"
);
std::env::remove_var("PUP_MOCK_SERVER");
std::env::remove_var("DD_API_KEY");
std::env::remove_var("DD_APP_KEY");
}
#[test]
fn test_make_dd_config_https_mock() {
let _guard = ENV_LOCK.blocking_lock();
let cfg = test_cfg();
std::env::set_var("DD_API_KEY", "test-key");
std::env::set_var("DD_APP_KEY", "test-app-key");
std::env::set_var("PUP_MOCK_SERVER", "https://mock.example.com");
let dd_cfg = make_dd_config(&cfg);
assert_eq!(dd_cfg.server_variables.get("protocol").unwrap(), "https");
assert_eq!(
dd_cfg.server_variables.get("name").unwrap(),
"mock.example.com"
);
std::env::remove_var("PUP_MOCK_SERVER");
std::env::remove_var("DD_API_KEY");
std::env::remove_var("DD_APP_KEY");
}
/// A scheme-less PUP_MOCK_SERVER (no `http(s)://`) defaults to plain http —
/// mock servers run HTTP locally. Exercises the `split_once` fallback branch.
#[test]
fn test_make_dd_config_scheme_less_mock_defaults_to_http() {
let _guard = ENV_LOCK.blocking_lock();
let cfg = test_cfg();
std::env::set_var("DD_API_KEY", "test-key");
std::env::set_var("DD_APP_KEY", "test-app-key");
std::env::set_var("PUP_MOCK_SERVER", "127.0.0.1:9999");
let dd_cfg = make_dd_config(&cfg);
assert_dd_host(&dd_cfg, "http", "127.0.0.1:9999");
std::env::remove_var("PUP_MOCK_SERVER");
std::env::remove_var("DD_API_KEY");
std::env::remove_var("DD_APP_KEY");
}
/// Verifies that requests built via `make_api!` carry pup's branded
/// `User-Agent` rather than the SDK's default `datadog-api-client-rust/...`.
/// The mock only matches when the header starts with `pup/`; if the
/// middleware fails to override, mockito returns 501 and the SDK call fails.
#[tokio::test]
async fn test_make_api_sends_pup_user_agent() {
use datadog_api_client::datadogV1::api_monitors::{
ListMonitorsOptionalParams, MonitorsAPI,
};
let _lock = lock_env().await;
let mut server = mockito::Server::new_async().await;
let mock = server
.mock("GET", mockito::Matcher::Any)
.match_header("User-Agent", mockito::Matcher::Regex("^pup/".into()))
.with_status(200)
.with_header("content-type", "application/json")
.with_body("[]")
.expect(1)
.create_async()
.await;
let cfg = test_config(&server.url());
let api: MonitorsAPI = crate::make_api!(MonitorsAPI, &cfg);
let resp = api
.list_monitors(ListMonitorsOptionalParams::default())
.await;
assert!(
resp.is_ok(),
"make_api! request did not carry pup/ User-Agent: {:?}",
resp.err()
);
mock.assert_async().await;
cleanup_env();
}
/// Like `test_make_api_sends_pup_user_agent`, but with an OAuth bearer
/// token configured — verifies that the UA middleware coexists with the
/// bearer middleware (both headers land on the same request).
#[tokio::test]
async fn test_make_api_sends_pup_user_agent_with_bearer() {
use datadog_api_client::datadogV1::api_monitors::{
ListMonitorsOptionalParams, MonitorsAPI,
};
let _lock = lock_env().await;
let mut server = mockito::Server::new_async().await;
let mock = server
.mock("GET", mockito::Matcher::Any)
.match_header("User-Agent", mockito::Matcher::Regex("^pup/".into()))
.match_header("Authorization", "Bearer test-bearer-token")
.with_status(200)
.with_header("content-type", "application/json")
.with_body("[]")
.expect(1)
.create_async()
.await;
let mut cfg = test_config(&server.url());
cfg.access_token = Some("test-bearer-token".into());
let api: MonitorsAPI = crate::make_api!(MonitorsAPI, &cfg);
let resp = api
.list_monitors(ListMonitorsOptionalParams::default())
.await;
assert!(
resp.is_ok(),
"make_api! with bearer didn't carry both UA and Authorization: {:?}",
resp.err()
);
mock.assert_async().await;
cleanup_env();
}
/// Same coverage for the no-auth variant. Asserts the UA is overridden
/// AND that no `Authorization` header leaks through, even when a bearer
/// token exists in the config — that's the contract of `make_api_no_auth!`.
/// Uses `ApplicationSecurityAPI` (ASM WAF custom rules), which is still a
/// genuinely no-auth production call site as of this test.
#[tokio::test]
async fn test_make_api_no_auth_sends_pup_user_agent() {
use datadog_api_client::datadogV2::api_application_security::ApplicationSecurityAPI;
let _lock = lock_env().await;
let mut server = mockito::Server::new_async().await;
let mock = server
.mock("GET", mockito::Matcher::Any)
.match_header("User-Agent", mockito::Matcher::Regex("^pup/".into()))
.match_header("Authorization", mockito::Matcher::Missing)
.with_status(200)
.with_header("content-type", "application/json")
.with_body(r#"{"data":[]}"#)
.expect(1)
.create_async()
.await;
let mut cfg = test_config(&server.url());
// Set a token so the Authorization-absent assertion meaningfully
// exercises that `make_api_no_auth!` actively suppresses bearer.
cfg.access_token = Some("test-bearer-token".into());
let api: ApplicationSecurityAPI = crate::make_api_no_auth!(ApplicationSecurityAPI, &cfg);
let resp = api.list_application_security_waf_custom_rules().await;
assert!(
resp.is_ok(),
"make_api_no_auth! request leaked Authorization or wrong UA: {:?}",
resp.err()
);
mock.assert_async().await;
cleanup_env();
}
}