drasi-bootstrap-http 0.1.5

HTTP bootstrap plugin for Drasi - fetches initial state from REST APIs
Documentation
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
// Copyright 2025 The Drasi Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Authentication strategies for HTTP bootstrap requests.

use anyhow::{Context, Result};
use reqwest::header::{HeaderMap, HeaderName, HeaderValue};
use reqwest::Client;
use serde::Deserialize;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::RwLock;

use crate::config::{ApiKeyLocation, AuthConfig};

/// Find the largest byte index <= `max` that is a valid UTF-8 char boundary.
fn find_char_boundary(s: &str, max: usize) -> usize {
    if max >= s.len() {
        return s.len();
    }
    let mut end = max;
    while end > 0 && !s.is_char_boundary(end) {
        end -= 1;
    }
    end
}

/// Resolved authentication that can be applied to requests.
pub enum ResolvedAuth {
    Bearer {
        token: String,
    },
    ApiKeyHeader {
        name: String,
        value: String,
    },
    ApiKeyQuery {
        name: String,
        value: String,
    },
    Basic {
        username: String,
        password: String,
    },
    OAuth2 {
        token_provider: Arc<OAuth2TokenProvider>,
    },
}

/// OAuth2 token provider with caching.
pub struct OAuth2TokenProvider {
    token_url: String,
    client_id: String,
    client_secret: String,
    scopes: Vec<String>,
    client: Client,
    cached_token: RwLock<Option<CachedToken>>,
}

#[derive(Clone)]
struct CachedToken {
    access_token: String,
    expires_at: Instant,
}

#[derive(Deserialize)]
struct OAuth2TokenResponse {
    access_token: String,
    #[serde(default)]
    expires_in: Option<u64>,
    #[allow(dead_code)]
    #[serde(default)]
    token_type: Option<String>,
}

impl OAuth2TokenProvider {
    pub fn new(
        token_url: String,
        client_id: String,
        client_secret: String,
        scopes: Vec<String>,
        client: Client,
    ) -> Self {
        Self {
            token_url,
            client_id,
            client_secret,
            scopes,
            client,
            cached_token: RwLock::new(None),
        }
    }

    /// Get a valid access token, refreshing if expired.
    pub async fn get_token(&self) -> Result<String> {
        // Check cache first under read lock
        {
            let cache = self.cached_token.read().await;
            if let Some(ref cached) = *cache {
                if Instant::now() < cached.expires_at {
                    return Ok(cached.access_token.clone());
                }
            }
        }

        // Acquire write lock and re-check to avoid stampede
        let mut cache = self.cached_token.write().await;
        if let Some(ref cached) = *cache {
            if Instant::now() < cached.expires_at {
                return Ok(cached.access_token.clone());
            }
        }

        // Token expired or not cached, fetch new one
        let token = self.fetch_token().await?;
        let access_token = token.access_token.clone();
        *cache = Some(token);

        Ok(access_token)
    }

    async fn fetch_token(&self) -> Result<CachedToken> {
        let mut form = vec![
            ("grant_type", "client_credentials".to_string()),
            ("client_id", self.client_id.clone()),
            ("client_secret", self.client_secret.clone()),
        ];

        if !self.scopes.is_empty() {
            form.push(("scope", self.scopes.join(" ")));
        }

        let response = self
            .client
            .post(&self.token_url)
            .form(&form)
            .send()
            .await
            .context("Failed to request OAuth2 token")?;

        if !response.status().is_success() {
            let status = response.status();
            let body = response
                .text()
                .await
                .unwrap_or_else(|_| "Unable to read response".to_string());
            let truncated = if body.len() > 256 {
                let end = find_char_boundary(&body, 256);
                format!("{}... (truncated)", &body[..end])
            } else {
                body
            };
            return Err(anyhow::anyhow!(
                "OAuth2 token request failed with status {status}: {truncated}"
            ));
        }

        let token_response: OAuth2TokenResponse = response
            .json()
            .await
            .context("Failed to parse OAuth2 token response")?;

        // Default to 1 hour expiry with 60-second safety margin
        let expires_in = token_response.expires_in.unwrap_or(3600);
        let expires_at = Instant::now() + Duration::from_secs(expires_in.saturating_sub(60));

        Ok(CachedToken {
            access_token: token_response.access_token,
            expires_at,
        })
    }
}

/// Resolve an AuthConfig into a ResolvedAuth by reading environment variables.
pub fn resolve_auth(config: &AuthConfig, client: &Client) -> Result<ResolvedAuth> {
    match config {
        AuthConfig::Bearer { token_env } => {
            let token = std::env::var(token_env)
                .with_context(|| format!("Environment variable '{token_env}' not set"))?;
            Ok(ResolvedAuth::Bearer { token })
        }
        AuthConfig::ApiKey {
            location,
            name,
            value_env,
        } => {
            let value = std::env::var(value_env)
                .with_context(|| format!("Environment variable '{value_env}' not set"))?;
            match location {
                ApiKeyLocation::Header => Ok(ResolvedAuth::ApiKeyHeader {
                    name: name.clone(),
                    value,
                }),
                ApiKeyLocation::Query => Ok(ResolvedAuth::ApiKeyQuery {
                    name: name.clone(),
                    value,
                }),
            }
        }
        AuthConfig::Basic {
            username_env,
            password_env,
        } => {
            let username = std::env::var(username_env)
                .with_context(|| format!("Environment variable '{username_env}' not set"))?;
            let password = match password_env {
                Some(env) => std::env::var(env)
                    .with_context(|| format!("Environment variable '{env}' not set"))?,
                None => String::new(),
            };
            Ok(ResolvedAuth::Basic { username, password })
        }
        AuthConfig::OAuth2ClientCredentials {
            token_url,
            client_id_env,
            client_secret_env,
            scopes,
        } => {
            let client_id = std::env::var(client_id_env)
                .with_context(|| format!("Environment variable '{client_id_env}' not set"))?;
            let client_secret = std::env::var(client_secret_env)
                .with_context(|| format!("Environment variable '{client_secret_env}' not set"))?;

            let provider = OAuth2TokenProvider::new(
                token_url.clone(),
                client_id,
                client_secret,
                scopes.clone(),
                client.clone(),
            );

            Ok(ResolvedAuth::OAuth2 {
                token_provider: Arc::new(provider),
            })
        }
    }
}

/// Apply resolved authentication to a request builder.
pub async fn apply_auth(
    builder: reqwest::RequestBuilder,
    auth: &ResolvedAuth,
) -> Result<reqwest::RequestBuilder> {
    match auth {
        ResolvedAuth::Bearer { token } => Ok(builder.bearer_auth(token)),
        ResolvedAuth::ApiKeyHeader { name, value } => {
            let mut headers = HeaderMap::new();
            let header_name = HeaderName::try_from(name.as_str())
                .with_context(|| format!("Invalid header name: {name}"))?;
            let header_value = HeaderValue::from_str(value)
                .with_context(|| format!("Invalid header value for {name}"))?;
            headers.insert(header_name, header_value);
            Ok(builder.headers(headers))
        }
        ResolvedAuth::ApiKeyQuery { name, value } => Ok(builder.query(&[(name, value)])),
        ResolvedAuth::Basic { username, password } => {
            Ok(builder.basic_auth(username, Some(password)))
        }
        ResolvedAuth::OAuth2 { token_provider } => {
            let token = token_provider
                .get_token()
                .await
                .context("Failed to get OAuth2 token")?;
            Ok(builder.bearer_auth(token))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use axum::response::IntoResponse;

    #[tokio::test]
    async fn test_oauth2_token_caching() {
        // Start a mock token server that counts requests
        let request_count = Arc::new(std::sync::atomic::AtomicU64::new(0));

        let app = {
            let request_count = request_count.clone();
            axum::Router::new().route(
                "/token",
                axum::routing::post(move || {
                    let request_count = request_count.clone();
                    async move {
                        request_count.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
                        axum::Json(serde_json::json!({
                            "access_token": "test-token-123",
                            "expires_in": 3600,
                            "token_type": "Bearer"
                        }))
                    }
                }),
            )
        };

        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); // DevSkim: ignore DS137138
        let addr = listener.local_addr().unwrap();
        tokio::spawn(async move {
            axum::serve(listener, app).await.unwrap();
        });

        let token_url = format!("http://127.0.0.1:{}/token", addr.port()); // DevSkim: ignore DS137138
        let client = Client::new();

        let provider = OAuth2TokenProvider::new(
            token_url,
            "client-id".to_string(),
            "client-secret".to_string(),
            vec!["read".to_string()],
            client,
        );

        // First call fetches from server
        let token1 = provider.get_token().await.unwrap();
        assert_eq!(token1, "test-token-123");
        assert_eq!(
            request_count.load(std::sync::atomic::Ordering::SeqCst),
            1,
            "First call should hit the server"
        );

        // Second call should return cached token (no additional request)
        let token2 = provider.get_token().await.unwrap();
        assert_eq!(token2, "test-token-123");
        assert_eq!(
            request_count.load(std::sync::atomic::Ordering::SeqCst),
            1,
            "Second call should use cache, not hit server"
        );
    }

    #[tokio::test]
    async fn test_oauth2_token_refresh_on_expiry() {
        let request_count = Arc::new(std::sync::atomic::AtomicU64::new(0));

        let app = {
            let request_count = request_count.clone();
            axum::Router::new().route(
                "/token",
                axum::routing::post(move || {
                    let request_count = request_count.clone();
                    async move {
                        let count = request_count.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
                        axum::Json(serde_json::json!({
                            "access_token": format!("token-{}", count + 1),
                            "expires_in": 1,
                            "token_type": "Bearer"
                        }))
                    }
                }),
            )
        };

        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); // DevSkim: ignore DS137138
        let addr = listener.local_addr().unwrap();
        tokio::spawn(async move {
            axum::serve(listener, app).await.unwrap();
        });

        let token_url = format!("http://127.0.0.1:{}/token", addr.port()); // DevSkim: ignore DS137138
        let client = Client::new();

        let provider = OAuth2TokenProvider::new(
            token_url,
            "client-id".to_string(),
            "client-secret".to_string(),
            vec![],
            client,
        );

        // First call — token expires immediately (1s - 60s safety = already expired)
        let token1 = provider.get_token().await.unwrap();
        assert_eq!(token1, "token-1");

        // Second call should refresh since token is already expired
        let token2 = provider.get_token().await.unwrap();
        assert_eq!(token2, "token-2");
        assert_eq!(
            request_count.load(std::sync::atomic::Ordering::SeqCst),
            2,
            "Expired token should trigger refresh"
        );
    }

    #[tokio::test]
    async fn test_oauth2_error_is_truncated() {
        let app = axum::Router::new().route(
            "/token",
            axum::routing::post(|| async {
                let body = "x".repeat(500);
                (axum::http::StatusCode::BAD_REQUEST, body).into_response()
            }),
        );

        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); // DevSkim: ignore DS137138
        let addr = listener.local_addr().unwrap();
        tokio::spawn(async move {
            axum::serve(listener, app).await.unwrap();
        });

        let token_url = format!("http://127.0.0.1:{}/token", addr.port()); // DevSkim: ignore DS137138
        let client = Client::new();

        let provider = OAuth2TokenProvider::new(
            token_url,
            "client-id".to_string(),
            "client-secret".to_string(),
            vec![],
            client,
        );

        let err = provider.get_token().await.unwrap_err();
        let err_msg = format!("{err}");
        assert!(
            err_msg.contains("truncated"),
            "Error should be truncated: {err_msg}"
        );
        assert!(err_msg.len() < 400, "Error message should be bounded");
    }
}