helr 0.4.0

Generic HTTP API log collector: polls APIs and emits NDJSON to stdout
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
//! OAuth2 token acquisition and caching: refresh_token or client_credentials; optional private_key_jwt and DPoP.

use crate::config::{self, AuthConfig};
use crate::dpop::{DPoPKeyCache, build_dpop_proof, get_or_create_dpop_key};
use anyhow::Context;
use jsonwebtoken::{Algorithm, EncodingKey, Header, encode};
use reqwest::Client;
use std::collections::HashMap;
use std::path::Path;
use std::sync::Arc;
use std::time::{Duration, Instant};
use std::time::{SystemTime, UNIX_EPOCH};
use tokio::sync::RwLock;
use tracing::debug;

/// Per-source cache: (access_token, expires_at). Refreshed when expired or missing.
pub type OAuth2TokenCache = Arc<RwLock<HashMap<String, (String, Instant)>>>;

/// Buffer before expiry to refresh (seconds).
const REFRESH_BUFFER_SECS: u64 = 60;

pub fn new_oauth2_token_cache() -> OAuth2TokenCache {
    Arc::new(RwLock::new(HashMap::new()))
}

/// Invalidate cached token for a source so the next request triggers a refresh (e.g. after 401).
pub async fn invalidate_token(cache: &OAuth2TokenCache, source_id: &str) {
    let mut g = cache.write().await;
    g.remove(source_id);
    debug!(source = %source_id, "oauth2 token invalidated");
}

/// Returns valid access_token for the source, refreshing if needed. With dpop: true, dpop_key_cache must be Some.
pub async fn get_oauth_token(
    cache: &OAuth2TokenCache,
    client: &Client,
    source_id: &str,
    auth: &AuthConfig,
    dpop_key_cache: Option<&DPoPKeyCache>,
    audit: Option<&crate::config::AuditConfig>,
) -> anyhow::Result<String> {
    let oauth = match auth {
        AuthConfig::OAuth2 {
            token_url,
            client_id_env,
            client_id_file,
            client_secret_env,
            client_secret_file,
            client_private_key_env,
            client_private_key_file,
            refresh_token_env,
            refresh_token_file,
            scopes,
            dpop,
        } => (
            token_url,
            client_id_env,
            client_id_file.as_deref(),
            client_secret_env.as_deref(),
            client_secret_file.as_deref(),
            client_private_key_env.as_deref().filter(|s| !s.is_empty()),
            client_private_key_file.as_deref(),
            refresh_token_env.as_deref().filter(|s| !s.is_empty()),
            refresh_token_file.as_deref(),
            scopes.as_deref().unwrap_or(&[]),
            *dpop,
        ),
        _ => anyhow::bail!("get_oauth_token requires OAuth2 auth"),
    };

    let (
        token_url,
        client_id_env,
        client_id_file,
        client_secret_env,
        client_secret_file,
        client_private_key_env,
        client_private_key_file,
        refresh_token_env,
        refresh_token_file,
        scopes,
        dpop,
    ) = oauth;

    let use_private_key_jwt =
        client_private_key_env.is_some() || client_private_key_file.is_some_and(|p| !p.is_empty());
    let now = Instant::now();
    let buffer = Duration::from_secs(REFRESH_BUFFER_SECS);

    {
        let g = cache.read().await;
        if let Some((token, expires_at)) = g.get(source_id)
            && now + buffer < *expires_at
        {
            return Ok(token.clone());
        }
    }

    let client_id = config::read_secret(client_id_file, client_id_env)?;
    crate::audit::log_credential_access(audit, source_id, "oauth2_client_id");
    let client_secret = if use_private_key_jwt {
        None
    } else {
        let secret = config::read_secret(client_secret_file, client_secret_env.unwrap_or(""))?;
        crate::audit::log_credential_access(audit, source_id, "oauth2_client_secret");
        Some(secret)
    };

    let mut form: std::collections::HashMap<String, String> = std::collections::HashMap::new();
    let refresh_token = refresh_token_env
        .and_then(|e| config::read_secret(refresh_token_file, e).ok())
        .or_else(|| refresh_token_file.and_then(|p| config::read_secret(Some(p), "").ok()));
    if refresh_token.is_some() {
        crate::audit::log_credential_access(audit, source_id, "oauth2_refresh_token");
    }

    if let Some(rt) = refresh_token {
        form.insert("grant_type".into(), "refresh_token".into());
        form.insert("client_id".into(), client_id.clone());
        if let Some(ref secret) = client_secret {
            form.insert("client_secret".into(), secret.clone());
        }
        form.insert("refresh_token".into(), rt);
    } else {
        form.insert("grant_type".into(), "client_credentials".into());
        form.insert("client_id".into(), client_id.clone());
        if let Some(ref secret) = client_secret {
            form.insert("client_secret".into(), secret.clone());
        }
        if !scopes.is_empty() {
            form.insert("scope".into(), scopes.join(" "));
        }
    }

    if use_private_key_jwt {
        let private_key_pem = config::read_secret(
            client_private_key_file,
            client_private_key_env.unwrap_or(""),
        )?;
        crate::audit::log_credential_access(audit, source_id, "oauth2_client_private_key");
        let client_assertion =
            build_client_assertion(&client_id, token_url.as_str(), &private_key_pem, source_id)?;
        form.insert(
            "client_assertion_type".into(),
            "urn:ietf:params:oauth:client-assertion-type:jwt-bearer".into(),
        );
        form.insert("client_assertion".into(), client_assertion);
    }

    let mut token_req = client.post(token_url.as_str()).form(&form);
    if dpop {
        let key_cache = dpop_key_cache.context("oauth2 dpop: true requires dpop_key_cache")?;
        let key = get_or_create_dpop_key(key_cache, source_id).await?;
        let iat = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .context("system time")?
            .as_secs();
        let jti = format!("{}-{}", source_id, iat);
        let proof = build_dpop_proof("POST", token_url.as_str(), &key, &jti, iat, None, None)?;
        token_req = token_req.header("DPoP", proof);
    }
    let response: reqwest::Response = token_req.send().await.context("oauth2 token request")?;

    let status = response.status();
    let dpop_nonce_header = response
        .headers()
        .get("DPoP-Nonce")
        .and_then(|v| v.to_str().ok())
        .map(|s| s.trim().to_string());
    let body = response
        .text()
        .await
        .context("oauth2 token response body")?;

    if dpop && status.as_u16() == 400 && body.contains("use_dpop_nonce") {
        let nonce = dpop_nonce_header.or_else(|| {
            serde_json::from_str::<serde_json::Value>(&body)
                .ok()
                .and_then(|v| {
                    v.get("nonce")
                        .or(v.get("dpop_nonce"))
                        .and_then(|n| n.as_str())
                        .map(|s| s.to_string())
                })
        });
        if let Some(nonce) = nonce {
            let key_cache = dpop_key_cache.context("oauth2 dpop: true requires dpop_key_cache")?;
            let key = get_or_create_dpop_key(key_cache, source_id).await?;
            tracing::debug!(source = %source_id, "oauth2 retrying token request with DPoP nonce");
            let iat = SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .context("system time")?
                .as_secs();
            let jti = format!("{}-{}", source_id, iat);
            let proof = build_dpop_proof(
                "POST",
                token_url.as_str(),
                &key,
                &jti,
                iat,
                Some(&nonce),
                None,
            )?;
            if use_private_key_jwt {
                let private_key_pem = config::read_secret(
                    client_private_key_file,
                    client_private_key_env.unwrap_or(""),
                )?;
                crate::audit::log_credential_access(audit, source_id, "oauth2_client_private_key");
                let new_assertion = build_client_assertion(
                    &client_id,
                    token_url.as_str(),
                    &private_key_pem,
                    source_id,
                )?;
                form.insert("client_assertion".into(), new_assertion);
            }
            let retry_req = client
                .post(token_url.as_str())
                .form(&form)
                .header("DPoP", proof);
            let retry_response = retry_req
                .send()
                .await
                .context("oauth2 token retry request")?;
            let retry_status = retry_response.status();
            let retry_body = retry_response
                .text()
                .await
                .context("oauth2 token retry response body")?;
            if !retry_status.is_success() {
                anyhow::bail!("oauth2 token error {}: {}", retry_status, retry_body);
            }
            let json: serde_json::Value =
                serde_json::from_str(&retry_body).context("oauth2 token response json")?;
            let access_token = json
                .get("access_token")
                .and_then(|v| v.as_str())
                .context("oauth2 response missing access_token")?
                .to_string();
            let expires_in = json
                .get("expires_in")
                .and_then(|v| v.as_u64())
                .unwrap_or(3600);
            let expires_at = now + Duration::from_secs(expires_in);
            {
                let mut g = cache.write().await;
                g.insert(source_id.to_string(), (access_token.clone(), expires_at));
            }
            debug!(source = %source_id, expires_in, "oauth2 token refreshed (with DPoP nonce)");
            return Ok(access_token);
        }
        tracing::warn!(
            source = %source_id,
            "server returned use_dpop_nonce but no DPoP-Nonce header or nonce in body; \
             ensure the authorization server sends DPoP-Nonce with the 400 response per RFC 9449"
        );
    }

    if !status.is_success() {
        anyhow::bail!("oauth2 token error {}: {}", status, body);
    }

    let json: serde_json::Value =
        serde_json::from_str(&body).context("oauth2 token response json")?;
    let access_token = json
        .get("access_token")
        .and_then(|v| v.as_str())
        .context("oauth2 response missing access_token")?
        .to_string();
    let expires_in = json
        .get("expires_in")
        .and_then(|v| v.as_u64())
        .unwrap_or(3600);
    let expires_at = now + Duration::from_secs(expires_in);

    {
        let mut g = cache.write().await;
        g.insert(source_id.to_string(), (access_token.clone(), expires_at));
    }
    debug!(source = %source_id, expires_in, "oauth2 token refreshed");
    Ok(access_token)
}

/// Build client_assertion JWT for private_key_jwt (RFC 7523): iss/sub=client_id, aud=token_url, RS256.
fn build_client_assertion(
    client_id: &str,
    token_url: &str,
    private_key_pem: &str,
    source_id: &str,
) -> anyhow::Result<String> {
    let now = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .context("system time")?;
    let iat = now.as_secs();
    let exp = iat + 300;
    let jti = format!("{}-{}-{}", source_id, iat, now.as_nanos());
    #[derive(serde::Serialize)]
    struct ClientAssertionClaims {
        iss: String,
        sub: String,
        aud: String,
        iat: u64,
        exp: u64,
        jti: String,
    }
    let claims = ClientAssertionClaims {
        iss: client_id.to_string(),
        sub: client_id.to_string(),
        aud: token_url.to_string(),
        iat,
        exp,
        jti,
    };
    let pem = private_key_pem.replace("\\n", "\n");
    let key = EncodingKey::from_rsa_pem(pem.as_bytes()).context("parse client private_key PEM")?;
    let token = encode(&Header::new(Algorithm::RS256), &claims, &key)
        .context("sign client_assertion JWT")?;
    Ok(token)
}

const GOOGLE_TOKEN_URL: &str = "https://oauth2.googleapis.com/token";

/// Returns current valid access_token for the source when auth is Google Service Account; cached.
pub async fn get_google_sa_token(
    cache: &OAuth2TokenCache,
    client: &Client,
    source_id: &str,
    auth: &AuthConfig,
) -> anyhow::Result<String> {
    let sa = match auth {
        AuthConfig::GoogleServiceAccount {
            credentials_file,
            credentials_env,
            subject_env,
            subject_file,
            scopes,
        } => (
            credentials_file.as_deref(),
            credentials_env.as_deref(),
            subject_env.as_deref(),
            subject_file.as_deref(),
            scopes,
        ),
        _ => anyhow::bail!("get_google_sa_token requires GoogleServiceAccount auth"),
    };
    let (creds_path, creds_env, subject_env, subject_file, scopes) = sa;
    let now = Instant::now();
    let buffer = Duration::from_secs(REFRESH_BUFFER_SECS);
    {
        let g = cache.read().await;
        if let Some((token, expires_at)) = g.get(source_id)
            && now + buffer < *expires_at
        {
            return Ok(token.clone());
        }
    }
    let json_str = if let Some(p) = creds_path {
        if p.is_empty() {
            None
        } else {
            Some(std::fs::read_to_string(Path::new(p)).context("read credentials file")?)
        }
    } else {
        None
    };
    let json_str = json_str.or_else(|| creds_env.and_then(|e| std::env::var(e).ok()));
    let json_str = json_str.context(
        "google_service_account credentials not set (credentials_file or credentials_env)",
    )?;
    let creds: serde_json::Value = serde_json::from_str(&json_str).context("credentials JSON")?;
    let client_email = creds
        .get("client_email")
        .and_then(|v| v.as_str())
        .context("credentials missing client_email")?;
    let private_key_str = creds
        .get("private_key")
        .and_then(|v| v.as_str())
        .context("credentials missing private_key")?;
    let private_key = private_key_str.replace("\\n", "\n");
    let subject = subject_env.and_then(|e| std::env::var(e).ok()).or_else(|| {
        subject_file.and_then(|p| {
            if p.is_empty() {
                None
            } else {
                std::fs::read_to_string(Path::new(p))
                    .ok()
                    .map(|s| s.trim().to_string())
            }
        })
    });
    let scope = scopes.join(" ");
    let now_secs = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap()
        .as_secs();
    #[derive(serde::Serialize)]
    struct GoogleJwtClaims {
        iss: String,
        scope: String,
        aud: String,
        iat: u64,
        exp: u64,
        #[serde(skip_serializing_if = "Option::is_none")]
        sub: Option<String>,
    }
    let claims = GoogleJwtClaims {
        iss: client_email.to_string(),
        scope,
        aud: GOOGLE_TOKEN_URL.to_string(),
        iat: now_secs,
        exp: now_secs + 3600,
        sub: subject,
    };
    let key = EncodingKey::from_rsa_pem(private_key.as_bytes()).context("parse private_key PEM")?;
    let token = encode(&Header::new(Algorithm::RS256), &claims, &key).context("sign JWT")?;
    let mut form = std::collections::HashMap::new();
    form.insert("grant_type", "urn:ietf:params:oauth:grant-type:jwt-bearer");
    form.insert("assertion", token.as_str());
    let response = client
        .post(GOOGLE_TOKEN_URL)
        .form(&form)
        .send()
        .await
        .context("google token request")?;
    let status = response.status();
    let body = response
        .text()
        .await
        .context("google token response body")?;
    if !status.is_success() {
        anyhow::bail!("google token error {}: {}", status, body);
    }
    let json: serde_json::Value =
        serde_json::from_str(&body).context("google token response json")?;
    let access_token = json
        .get("access_token")
        .and_then(|v| v.as_str())
        .context("response missing access_token")?
        .to_string();
    let expires_in = json
        .get("expires_in")
        .and_then(|v| v.as_u64())
        .unwrap_or(3600);
    let expires_at = now + Duration::from_secs(expires_in);
    {
        let mut g = cache.write().await;
        g.insert(source_id.to_string(), (access_token.clone(), expires_at));
    }
    debug!(source = %source_id, expires_in, "google service account token obtained");
    Ok(access_token)
}