esdiag 0.16.4

Elastic Stack diagnostic collector and processor
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
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License 2.0;
// you may not use this file except in compliance with the Elastic License 2.0.

use super::super::processor::{DataSource, DiagnosticManifest, ElasticsearchCluster, ManifestBuilder, SourceContext};
use super::{LONG_RUNNING_REQUEST_TIMEOUT, RawResponse, Receive, ReceiveRaw};
use crate::data::{Auth, KnownHost};
use eyre::{Result, eyre};
use reqwest::header::{ACCEPT, ACCEPT_ENCODING, AUTHORIZATION};
use reqwest::{Client, ClientBuilder, header::HeaderMap};
use serde::de::DeserializeOwned;
use serde_json::Value;
use url::Url;

use std::sync::Arc;
use std::time::Duration;
use std::time::Instant;
use tokio::sync::OnceCell;

const ELASTIC_CLOUD_ADMIN_CONNECT_TIMEOUT: Duration = Duration::from_secs(5);

#[derive(Debug)]
#[non_exhaustive]
pub struct ElasticCloudAdminRequestError {
    pub status: reqwest::StatusCode,
    pub body: String,
    pub response_time_ms: u64,
    pub response_size_bytes: u64,
}

impl std::fmt::Display for ElasticCloudAdminRequestError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "http {} - {}", self.status, self.body)
    }
}

impl std::error::Error for ElasticCloudAdminRequestError {}

#[derive(Clone)]
pub struct ElasticCloudAdminReceiver {
    client: Client,
    url: Url,
    version: Arc<OnceCell<semver::Version>>,
}

impl ElasticCloudAdminReceiver {
    fn format_error_body(body: &str) -> String {
        serde_json::from_str::<Value>(body)
            .map(|value| value.to_string())
            .unwrap_or_else(|_| body.to_string())
    }

    fn proxy_url(&self, path: &str) -> Result<Url> {
        let base_path = self.url.path().trim_end_matches('/');
        let relative_path = path.trim_start_matches('/');
        let proxy_path = if relative_path.is_empty() {
            format!("{base_path}/")
        } else {
            format!("{base_path}/{relative_path}")
        };
        self.url
            .join(&proxy_path)
            .map_err(|err| eyre!("Failed to resolve Elastic Cloud Admin URL: {err}"))
    }

    pub fn new(url: Url, api_key: String) -> Result<Self> {
        let mut default_headers = HeaderMap::new();
        default_headers.append("X-Management-Request", "true".parse().unwrap());
        default_headers.append(ACCEPT, "application/json".parse().unwrap());
        default_headers.append(ACCEPT_ENCODING, "gzip, deflate".parse().unwrap());
        default_headers.append(AUTHORIZATION, format!("ApiKey {}", api_key).parse().unwrap());
        let client = ClientBuilder::new()
            .default_headers(default_headers)
            .connect_timeout(ELASTIC_CLOUD_ADMIN_CONNECT_TIMEOUT)
            // Cloud Admin collection proxies Elasticsearch APIs that can spend
            // minutes building large payloads before response headers arrive.
            .timeout(LONG_RUNNING_REQUEST_TIMEOUT)
            .build()?;
        Ok(Self {
            client,
            url,
            version: Arc::new(OnceCell::new()),
        })
    }

    pub async fn get_version(&self) -> Result<&semver::Version> {
        self.version
            .get_or_try_init(|| async {
                tracing::debug!("Fetching version from {}", self.url);
                let url = self.proxy_url("/")?;
                let started = Instant::now();
                let response = self.client.get(url).send().await?;
                let status = response.status();
                let body = response.text().await?;
                let response_time_ms = started.elapsed().as_millis() as u64;
                let response_size_bytes = body.len() as u64;
                if !status.is_success() {
                    return Err(ElasticCloudAdminRequestError {
                        status,
                        body: Self::format_error_body(&body),
                        response_time_ms,
                        response_size_bytes,
                    }
                    .into());
                }
                let cluster: serde_json::Value = serde_json::from_str(&body)?;
                let version_str = cluster
                    .get("version")
                    .and_then(|v| v.get("number"))
                    .and_then(|v| v.as_str())
                    .ok_or_else(|| eyre::eyre!("No version found in root response"))?;
                semver::Version::parse(version_str).map_err(|e| eyre!("Failed to parse version: {}", e))
            })
            .await
    }

    pub async fn get_raw_response_by_path(&self, path: &str, extension: &str) -> Result<RawResponse> {
        tracing::debug!("Getting raw Elastic Cloud Admin API path: {}", path);
        let started = Instant::now();

        let accept = if extension == ".txt" {
            "text/plain"
        } else {
            "application/json"
        };
        let url = self.proxy_url(path)?;
        let response = self.client.get(url).header(ACCEPT, accept).send().await?;
        let status = response.status();
        let body = response.text().await?;
        let response_time_ms = started.elapsed().as_millis() as u64;
        let response_size_bytes = body.len() as u64;
        if !status.is_success() {
            return Err(ElasticCloudAdminRequestError {
                status,
                body: Self::format_error_body(&body),
                response_time_ms,
                response_size_bytes,
            }
            .into());
        }

        Ok(RawResponse {
            body,
            status: Some(status.as_u16()),
            response_time_ms,
            response_size_bytes,
        })
    }

    pub async fn get_raw_by_path(&self, path: &str, extension: &str) -> Result<String> {
        self.get_raw_response_by_path(path, extension)
            .await
            .map(|response| response.body)
    }
}

impl TryFrom<KnownHost> for ElasticCloudAdminReceiver {
    type Error = eyre::Report;

    fn try_from(host: KnownHost) -> Result<Self> {
        let url = host.get_url()?;
        match host.get_auth()? {
            Auth::Apikey(apikey) => Ok(ElasticCloudAdminReceiver::new(url, apikey)?),
            _ => Err(eyre::eyre!("Elastic Cloud Admin requires a URL and ApiKey")),
        }
    }
}

impl Receive for ElasticCloudAdminReceiver {
    async fn collection_date(&self) -> String {
        chrono::Utc::now().to_rfc3339()
    }

    async fn is_connected(&self) -> bool {
        tracing::debug!("Testing Elastic Cloud Admin connection to {}", self.url.as_str());
        // An empty request to `/`
        let response = self.client.get(self.url.as_str()).send().await;

        match response {
            Ok(response) => {
                let status = response.status();
                if status.is_success() {
                    tracing::debug!("Elastic Cloud Admin connection successful: {}", status);
                    true
                } else {
                    tracing::error!(
                        "Elastic Cloud Admin connection to {} failed: {}",
                        self.url.as_str(),
                        status
                    );
                    false
                }
            }
            Err(e) => {
                tracing::error!("Elastic Cloud Admin connection to {} failed: {e}", self.url.as_str());
                if e.is_connect() || e.is_timeout() {
                    tracing::warn!(
                        "Elastic Cloud Admin and Elastic GovCloud Admin hosts require VPN access; verify your VPN connection and retry."
                    );
                }
                false
            }
        }
    }

    fn filename(&self) -> Option<String> {
        None
    }

    async fn get<T>(&self) -> Result<T>
    where
        T: DataSource + DeserializeOwned,
    {
        let ctx = SourceContext::new("elasticsearch", self.get_version().await.ok().cloned());
        let source_path = T::resolve_source_request_path(&ctx)?;
        let url = self.proxy_url(&source_path)?;
        tracing::debug!("Getting API: {}", url);
        let started = Instant::now();
        let response = self.client.get(url).send().await?;
        let status = response.status();
        let body = response.text().await?;
        if !status.is_success() {
            return Err(ElasticCloudAdminRequestError {
                status,
                body: Self::format_error_body(&body),
                response_time_ms: started.elapsed().as_millis() as u64,
                response_size_bytes: body.len() as u64,
            }
            .into());
        }

        serde_json::from_str(&body).map_err(Into::into)
    }

    async fn try_get_manifest(&self) -> Result<DiagnosticManifest> {
        let collection_date = chrono::Utc::now().to_rfc3339();
        tracing::info!("Creating diagnostic manifest with collection date {collection_date}");
        let cluster = self.get::<ElasticsearchCluster>().await?;
        let manifest = ManifestBuilder::from(cluster)
            .runner("esdiag")
            .collection_date(collection_date)
            .build();
        Ok(manifest.into())
    }
}

impl ReceiveRaw for ElasticCloudAdminReceiver {
    async fn get_raw<T>(&self) -> Result<String>
    where
        T: DataSource,
    {
        self.get_raw_response::<T>().await.map(|response| response.body)
    }

    async fn get_raw_response<T>(&self) -> Result<RawResponse>
    where
        T: DataSource,
    {
        let ctx = SourceContext::new("elasticsearch", self.get_version().await.ok().cloned());
        let source_path = T::resolve_source_request_path(&ctx)?;
        let extension = T::resolve_source_extension(&ctx)?;
        self.get_raw_response_by_path(&source_path, &extension).await
    }
}

impl std::fmt::Display for ElasticCloudAdminReceiver {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "Elastic Cloud {}", self.url)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use axum::{
        Router,
        extract::Request,
        extract::State,
        http::StatusCode,
        middleware::{self, Next},
        routing::get,
    };
    use reqwest::header::ACCEPT;
    use std::sync::{Arc, Mutex};
    use tokio::sync::oneshot;
    use tokio::task::JoinHandle;

    async fn status_handler(State(status): State<StatusCode>) -> (StatusCode, &'static str) {
        (status, "test response")
    }

    async fn spawn_status_server(status: StatusCode) -> (Url, JoinHandle<()>, oneshot::Sender<()>) {
        let app = Router::new().route("/", get(status_handler)).with_state(status);
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
            .await
            .expect("bind test server");
        let addr = listener.local_addr().expect("listener addr");
        let (shutdown_tx, shutdown_rx) = oneshot::channel::<()>();
        let server = tokio::spawn(async move {
            axum::serve(listener, app)
                .with_graceful_shutdown(async {
                    let _ = shutdown_rx.await;
                })
                .await
                .expect("serve test app");
        });
        let url = Url::parse(&format!("http://{addr}/")).expect("parse test url");
        (url, server, shutdown_tx)
    }

    async fn stop_status_server(server: JoinHandle<()>, shutdown_tx: oneshot::Sender<()>) {
        let _ = shutdown_tx.send(());
        server.await.expect("test server should exit cleanly");
    }

    async fn stop_status_server_with_shutdown(server: JoinHandle<()>, shutdown_tx: oneshot::Sender<()>) {
        let _ = shutdown_tx.send(());
        server.await.expect("test server should exit cleanly");
    }

    async fn capture_accept_header(
        State(last_accept): State<Arc<Mutex<Option<String>>>>,
        request: Request,
        next: Next,
    ) -> impl axum::response::IntoResponse {
        let header = request
            .headers()
            .get(ACCEPT)
            .and_then(|value| value.to_str().ok())
            .map(str::to_string);
        *last_accept.lock().expect("accept lock") = header;
        next.run(request).await
    }

    #[tokio::test]
    async fn is_connected_returns_true_for_success_status() {
        let (url, server, shutdown_tx) = spawn_status_server(StatusCode::OK).await;
        let receiver = ElasticCloudAdminReceiver::new(url, "test-api-key".to_string()).expect("receiver");

        assert!(receiver.is_connected().await);

        stop_status_server(server, shutdown_tx).await;
    }

    #[tokio::test]
    async fn is_connected_returns_false_for_unauthorized_status() {
        let (url, server, shutdown_tx) = spawn_status_server(StatusCode::UNAUTHORIZED).await;
        let receiver = ElasticCloudAdminReceiver::new(url, "test-api-key".to_string()).expect("receiver");

        assert!(!receiver.is_connected().await);

        stop_status_server(server, shutdown_tx).await;
    }

    #[tokio::test]
    async fn get_raw_by_path_uses_proxy_base_path_and_accept_header() {
        async fn text_handler() -> &'static str {
            "hot threads"
        }

        let last_accept = Arc::new(Mutex::new(None));
        let app = Router::new()
            .route(
                "/api/v1/deployments/test/elasticsearch/main-elasticsearch/proxy/_nodes/hot_threads",
                get(text_handler),
            )
            .layer(middleware::from_fn_with_state(
                last_accept.clone(),
                capture_accept_header,
            ));
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
            .await
            .expect("bind test server");
        let addr = listener.local_addr().expect("listener addr");
        let (shutdown_tx, shutdown_rx) = oneshot::channel::<()>();
        let server = tokio::spawn(async move {
            axum::serve(listener, app)
                .with_graceful_shutdown(async {
                    let _ = shutdown_rx.await;
                })
                .await
                .expect("serve test app");
        });
        let url = Url::parse(&format!(
            "http://{addr}/api/v1/deployments/test/elasticsearch/main-elasticsearch/proxy/"
        ))
        .expect("parse test url");
        let receiver = ElasticCloudAdminReceiver::new(url, "test-api-key".to_string()).expect("receiver");

        let body = receiver
            .get_raw_by_path("/_nodes/hot_threads", ".txt")
            .await
            .expect("raw response");

        assert_eq!(body, "hot threads");
        assert_eq!(last_accept.lock().expect("accept lock").as_deref(), Some("text/plain"));

        stop_status_server_with_shutdown(server, shutdown_tx).await;
    }

    #[tokio::test]
    async fn get_raw_by_path_returns_request_error_for_http_failures() {
        async fn not_found_handler() -> (StatusCode, &'static str) {
            (StatusCode::NOT_FOUND, r#"{"error":"missing"}"#)
        }

        let app = Router::new().route(
            "/api/v1/deployments/test/elasticsearch/main-elasticsearch/proxy/_cluster/missing",
            get(not_found_handler),
        );
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
            .await
            .expect("bind test server");
        let addr = listener.local_addr().expect("listener addr");
        let (shutdown_tx, shutdown_rx) = oneshot::channel::<()>();
        let server = tokio::spawn(async move {
            axum::serve(listener, app)
                .with_graceful_shutdown(async {
                    let _ = shutdown_rx.await;
                })
                .await
                .expect("serve test app");
        });
        let url = Url::parse(&format!(
            "http://{addr}/api/v1/deployments/test/elasticsearch/main-elasticsearch/proxy/"
        ))
        .expect("parse test url");
        let receiver = ElasticCloudAdminReceiver::new(url, "test-api-key".to_string()).expect("receiver");

        let err = receiver
            .get_raw_by_path("/_cluster/missing", ".json")
            .await
            .expect_err("should fail");
        let request_error = err
            .downcast_ref::<ElasticCloudAdminRequestError>()
            .expect("request error");

        assert_eq!(request_error.status, StatusCode::NOT_FOUND);
        assert_eq!(request_error.body, r#"{"error":"missing"}"#);

        stop_status_server_with_shutdown(server, shutdown_tx).await;
    }
}