Skip to content

Commit 5605e45

Browse files
committed
Remove mutual exclusivity between PAT and API+App key auth
1 parent 8013ba4 commit 5605e45

File tree

2 files changed

+13
-26
lines changed

2 files changed

+13
-26
lines changed

.generator/src/generator/templates/configuration.j2

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,26 +112,21 @@ impl Configuration {
112112
{%- if "bearerAuth" in openapi.components.securitySchemes %}
113113

114114
/// Set a bearer token for authentication.
115-
/// When a bearer token is configured, the client sends only an `Authorization: Bearer <token>`
116-
/// header and does NOT send DD-API-KEY or DD-APPLICATION-KEY headers.
117115
pub fn set_pat(&mut self, pat: String) {
118116
self.pat = Some(pat);
119117
}
120118

121119
{%- endif %}
122120

123121
/// Build authentication headers for an API request.
124-
{%- if "bearerAuth" in openapi.components.securitySchemes %}
125-
/// If a bearer token is configured, returns a single `Authorization: Bearer` header.
126-
/// Otherwise, returns API key headers.
127-
{%- endif %}
122+
/// All configured auth credentials are sent; the server decides which to use.
128123
pub fn auth_headers(&self) -> Vec<(String, String)> {
124+
let mut headers = Vec::new();
129125
{%- if "bearerAuth" in openapi.components.securitySchemes %}
130126
if let Some(ref pat) = self.pat {
131-
return vec![("Authorization".to_string(), format!("Bearer {}", pat))];
127+
headers.push(("Authorization".to_string(), format!("Bearer {}", pat)));
132128
}
133129
{%- endif %}
134-
let mut headers = Vec::new();
135130
{%- set authMethods = openapi.security %}
136131
{%- if authMethods %}
137132
{%- for authMethod in authMethods %}

src/datadog/configuration.rs

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -111,20 +111,17 @@ impl Configuration {
111111
}
112112

113113
/// Set a bearer token for authentication.
114-
/// When a bearer token is configured, the client sends only an `Authorization: Bearer <token>`
115-
/// header and does NOT send DD-API-KEY or DD-APPLICATION-KEY headers.
116114
pub fn set_pat(&mut self, pat: String) {
117115
self.pat = Some(pat);
118116
}
119117

120118
/// Build authentication headers for an API request.
121-
/// If a bearer token is configured, returns a single `Authorization: Bearer` header.
122-
/// Otherwise, returns API key headers.
119+
/// All configured auth credentials are sent; the server decides which to use.
123120
pub fn auth_headers(&self) -> Vec<(String, String)> {
121+
let mut headers = Vec::new();
124122
if let Some(ref pat) = self.pat {
125-
return vec![("Authorization".to_string(), format!("Bearer {}", pat))];
123+
headers.push(("Authorization".to_string(), format!("Bearer {}", pat)));
126124
}
127-
let mut headers = Vec::new();
128125
if let Some(key) = self.auth_keys.get("apiKeyAuth") {
129126
headers.push(("DD-API-KEY".to_string(), key.key.clone()));
130127
}
@@ -1179,13 +1176,11 @@ mod tests {
11791176
config.set_pat("my-pat-token".to_string());
11801177

11811178
let headers = config.auth_headers();
1182-
assert_eq!(headers.len(), 1);
1183-
assert_eq!(headers[0].0, "Authorization");
1184-
assert_eq!(headers[0].1, "Bearer my-pat-token");
1179+
assert!(headers.iter().any(|(k, v)| k == "Authorization" && v == "Bearer my-pat-token"));
11851180
}
11861181

11871182
#[test]
1188-
fn test_auth_headers_with_pat_excludes_api_keys() {
1183+
fn test_auth_headers_with_pat_and_api_keys_sends_all() {
11891184
let mut config = Configuration::new();
11901185
config.set_auth_key(
11911186
"apiKeyAuth",
@@ -1201,13 +1196,12 @@ mod tests {
12011196
prefix: "".to_string(),
12021197
},
12031198
);
1204-
// PAT overrides all key-based auth
12051199
config.set_pat("my-pat-token".to_string());
12061200

12071201
let headers = config.auth_headers();
1208-
assert_eq!(headers.len(), 1);
1209-
assert_eq!(headers[0].0, "Authorization");
1210-
assert_eq!(headers[0].1, "Bearer my-pat-token");
1202+
assert!(headers.iter().any(|(k, v)| k == "Authorization" && v == "Bearer my-pat-token"));
1203+
assert!(headers.iter().any(|(k, v)| k == "DD-API-KEY" && v == "my-api-key"));
1204+
assert!(headers.iter().any(|(k, v)| k == "DD-APPLICATION-KEY" && v == "my-app-key"));
12111205
}
12121206

12131207
#[test]
@@ -1245,7 +1239,7 @@ mod tests {
12451239
}
12461240

12471241
#[test]
1248-
fn test_dd_pat_env_var() {
1242+
fn test_dd_bearer_token_env_var() {
12491243
let _lock = ENV_MUTEX.lock().unwrap();
12501244
let old_pat = env::var("DD_BEARER_TOKEN").ok();
12511245

@@ -1255,9 +1249,7 @@ mod tests {
12551249
assert_eq!(config.pat.as_deref(), Some("env-pat-token"));
12561250

12571251
let headers = config.auth_headers();
1258-
assert_eq!(headers.len(), 1);
1259-
assert_eq!(headers[0].0, "Authorization");
1260-
assert_eq!(headers[0].1, "Bearer env-pat-token");
1252+
assert!(headers.iter().any(|(k, v)| k == "Authorization" && v == "Bearer env-pat-token"));
12611253

12621254
// Restore env
12631255
match old_pat {

0 commit comments

Comments
 (0)