prosa-fetcher 0.4.2

ProSA processor to fetch information from remote systems
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
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
use std::{
    convert::Infallible,
    io,
    time::{Duration, Instant},
};

use base64::{DecodeError, Engine as _, engine::general_purpose::URL_SAFE};
use chrono::{Local, NaiveTime};
use http::Response;
use http_body_util::combinators::BoxBody;
use hyper::{
    Request,
    body::{Bytes, Incoming},
    client::conn::{http1, http2},
};
use hyper_util::rt::{TokioExecutor, TokioIo};
use opentelemetry::KeyValue;
use prosa::{
    core::{
        adaptor::Adaptor,
        error::ProcError,
        msg::{InternalMsg, Msg, RequestMsg},
        proc::{Proc, ProcBusParam as _, proc, proc_settings},
    },
    io::stream::TargetSetting,
};
use serde::{Deserialize, Serialize};
use thiserror::Error;
use tokio::{
    sync::{mpsc, watch},
    time,
};
use tracing::{debug, error, info, warn};

use crate::adaptor::FetcherAdaptor;

#[derive(Debug, Error)]
/// ProSA service error when the service can't respond correctly to a request
pub enum FetcherError<M>
where
    M: std::marker::Send,
{
    /// IO error
    #[error("IO error during the fetch `{0}`")]
    Io(#[from] io::Error),
    /// Hyper error
    #[error("Hyper error during the fetch `{0:?}` from `{1}`")]
    Hyper(hyper::Error, String),
    /// HTTP error
    #[error("HTTP error on object parsing `{0}`")]
    Http(#[from] http::Error),
    /// Queue error
    #[error("Fetcher communication error `{0}`")]
    Queue(#[from] watch::error::SendError<FetchAction<M>>),
    /// HTTP queue error
    #[error("No HTTP task available to process the message `{0}`")]
    HttpQueue(Box<mpsc::error::SendError<http::Request<BoxBody<Bytes, Infallible>>>>),
    /// Base64 decode error
    #[error("Can't decode Base64 data `{0}`")]
    B64Decode(#[from] DecodeError),
    /// Other error
    #[error("Fetcher other error `{0}`")]
    Other(String),
}

impl<M> From<mpsc::error::SendError<http::Request<BoxBody<Bytes, Infallible>>>> for FetcherError<M>
where
    M: std::marker::Send,
{
    fn from(error: mpsc::error::SendError<http::Request<BoxBody<Bytes, Infallible>>>) -> Self {
        FetcherError::<M>::HttpQueue(Box::new(error))
    }
}

impl<M> ProcError for FetcherError<M>
where
    M: 'static + std::fmt::Debug + std::marker::Send,
{
    fn recoverable(&self) -> bool {
        match self {
            FetcherError::Io(error) => error.recoverable(),
            FetcherError::Hyper(_error, _addr) => true,
            FetcherError::Http(_error) => true,
            FetcherError::Queue(_send_error) => false,
            FetcherError::HttpQueue(_send_error) => false,
            FetcherError::B64Decode(_decode_error) => false,
            FetcherError::Other(_) => false,
        }
    }
}

#[derive(Debug, Deserialize, Serialize, Copy, Clone)]
pub struct TimeRange {
    /// Start period hour
    pub start: NaiveTime,
    /// End period hour
    pub end: NaiveTime,
}

impl TimeRange {
    // Méthode pour vérifier si une heure donnée est dans la plage
    pub fn contains(&self, time: &NaiveTime) -> bool {
        if self.start <= self.end {
            time >= &self.start && time <= &self.end
        } else {
            time >= &self.start || time <= &self.end
        }
    }
}

/// Settings for Fetcher processor
#[proc_settings]
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct FetcherSettings {
    /// Target settings to connect to the remote system
    target: Option<TargetSetting>,
    /// Remote service to call in order to fetch information from remote system
    service_name: Option<String>,
    /// Authentication with authorization header with provided user password
    #[serde(default = "FetcherSettings::get_default_authorization")]
    pub authorization: bool,
    /// Period where the remote system need to be fetch
    #[serde(default = "FetcherSettings::get_default_period")]
    period: Duration,
    /// Timeout duration for every fetch
    #[serde(default = "FetcherSettings::get_default_timeout")]
    timeout: Duration,
    /// Maximum number of retry to fetch a resource
    #[serde(default = "FetcherSettings::get_default_max_retry")]
    max_retry: u8,
    /// Hour time range when the fetcher execute
    pub(crate) active_time_range: Option<TimeRange>,
    #[serde(default)]
    title_case_headers: bool,
}

impl FetcherSettings {
    fn get_default_authorization() -> bool {
        true
    }

    fn get_default_period() -> Duration {
        Duration::from_secs(60)
    }

    fn get_default_timeout() -> Duration {
        Duration::from_secs(10)
    }

    fn get_default_max_retry() -> u8 {
        2
    }

    /// Create a new Fetcher settings
    pub fn new(
        target: TargetSetting,
        service_name: String,
        authorization: bool,
        period: Duration,
        timeout: Duration,
    ) -> FetcherSettings {
        FetcherSettings {
            target: Some(target),
            service_name: Some(service_name),
            authorization,
            period,
            timeout,
            ..Default::default()
        }
    }

    /// Get the username for login
    pub fn username(&self) -> Option<&str> {
        self.target.as_ref().map(|t| t.url.username())
    }

    /// Getter of the URL password (decode from Base64Url)
    pub fn password(&self) -> Result<Option<Vec<u8>>, DecodeError> {
        if let Some(password) = self.target.as_ref().and_then(|t| t.url.password()) {
            Ok(Some(URL_SAFE.decode(password.replace("%3D", "="))?))
        } else {
            Ok(None)
        }
    }

    /// Method to get a challenged password to authenticate.
    /// mac is the HMac function to use for your challenge.
    pub fn challenge_password<H, M>(
        &self,
        challenge: &[u8],
    ) -> Result<Option<bytes::Bytes>, FetcherError<M>>
    where
        H: hmac::Mac + hmac::digest::KeyInit,
        M: Send,
    {
        if let Some(password) = self.target.as_ref().and_then(|t| t.url.password()) {
            let binary_password = URL_SAFE.decode(password.replace("%3D", "="))?;
            let mut mac =
                <H as hmac::digest::KeyInit>::new_from_slice(&binary_password).map_err(|e| {
                    FetcherError::Other(format!("Crypto error on password challenge {e}"))
                })?;
            mac.update(challenge);
            return Ok(Some(bytes::Bytes::copy_from_slice(
                &mac.finalize().into_bytes(),
            )));
        }

        Ok(None)
    }

    /// Method to know if the fetcher is active depending of the time of the day.
    /// It only return false if an `active_time_range` is set and the current time is not in range
    pub fn is_active(&self) -> bool {
        if let Some(active_time_range) = self.active_time_range {
            active_time_range.contains(&Local::now().time())
        } else {
            true
        }
    }

    /// Getter of an HTTP1 context
    pub fn get_http1_ctx(&self) -> http1::Builder {
        let mut http1_ctx = http1::Builder::new();

        if self.title_case_headers {
            // Set HTTP1 context for old HTTP server
            http1_ctx.title_case_headers(true);
        }

        http1_ctx
    }
}

#[proc_settings]
impl Default for FetcherSettings {
    fn default() -> Self {
        FetcherSettings {
            target: None,
            service_name: None,
            authorization: Self::get_default_authorization(),
            period: Self::get_default_period(),
            timeout: Self::get_default_timeout(),
            max_retry: Self::get_default_max_retry(),
            active_time_range: None,
            title_case_headers: false,
        }
    }
}

/// Enum that describe what action should be done everytime
#[derive(Debug)]
pub enum FetchAction<M>
where
    M: std::marker::Send,
{
    /// No further action
    None,
    /// Send an HTTP request message
    Http,
    /// Send a service request message
    Srv(String, M),
}

impl<M> FetchAction<M>
where
    M: std::marker::Send,
{
    /// Method to know if there is still action to execute
    pub fn have_action(&self) -> bool {
        !matches!(self, FetchAction::<M>::None)
    }
}

#[proc(settings = FetcherSettings)]
pub struct FetcherProc {}

#[proc]
impl FetcherProc {
    fn spawn_http_fetch(
        settings: &FetcherSettings,
        target: TargetSetting,
        mut req_rx: mpsc::Receiver<Request<BoxBody<Bytes, Infallible>>>,
        resp_tx: mpsc::Sender<Result<Response<Incoming>, FetcherError<M>>>,
    ) {
        let timeout = settings.timeout;
        let have_time_range = settings.active_time_range.is_some();
        let http1_ctx = settings.get_http1_ctx();
        let max_retry = settings.max_retry;
        tokio::spawn(async move {
            let mut msg_to_send = None;
            let mut nb_retry = 0;
            'conn: loop {
                // Wait for a message before openning the socket
                if msg_to_send.is_none() {
                    msg_to_send = req_rx.recv().await;
                    if msg_to_send.is_none() {
                        if let Err(e) = resp_tx.try_send(Err(FetcherError::Other(
                            "Internal HTTP queue is closed".to_string(),
                        ))) {
                            warn!(
                                addr = target.to_string(),
                                "Error during message openning: {e}"
                            );
                        }
                        return;
                    }
                }

                match target.connect().await {
                    Ok(stream) => {
                        let is_http2 = stream.selected_alpn_check(|alpn| alpn == b"h2");
                        let stream = TokioIo::new(stream);

                        if is_http2 {
                            match time::timeout(
                                timeout,
                                http2::handshake(TokioExecutor::new(), stream),
                            )
                            .await
                            {
                                Ok(Ok((mut sender, mut connection))) => loop {
                                    if let Some(msg) = msg_to_send.take() {
                                        tokio::select! {
                                            // Closed the socket
                                            Err(_) = &mut connection => {
                                                debug!(addr = target.to_string(), "Remote close the HTTP2 socket");
                                                continue 'conn;
                                            }
                                            // Send an HTTP request
                                            resp = sender.try_send_request(msg) => {
                                                match resp {
                                                    Ok(r) => {
                                                        if let Err(e) = resp_tx.try_send(Ok(r)) {
                                                            warn!(addr = target.to_string(), "Error during HTTP2 response return: {e}");
                                                        } else {
                                                            nb_retry = 0;
                                                        }
                                                    }
                                                    Err(mut e) => {
                                                        msg_to_send = e.take_message();
                                                        let hyper_err = e.into_error();
                                                        if nb_retry < max_retry {
                                                            // try to send again the message after reconnection
                                                            nb_retry += 1;
                                                            continue 'conn;
                                                        } else {
                                                            error!(addr = target.to_string(), "Failed to fetch HTTP2 `{msg_to_send:?}`, because of `{hyper_err}`, after {nb_retry}/{max_retry} retries");
                                                            if let Err(e) = resp_tx.try_send(Err(FetcherError::Hyper(hyper_err, target.to_string()))) {
                                                                warn!(addr = target.to_string(), "Error during HTTP2 error response return: {e}");
                                                            }
                                                            continue 'conn;
                                                        }
                                                    }
                                                }
                                            }
                                            // Receive a message to send from the queue (unload also the queue if too many messages arrive)
                                            Some(mut msg) = req_rx.recv() => {
                                                *msg.version_mut() = http::Version::HTTP_2;
                                                msg_to_send = Some(msg);
                                            }
                                        }
                                    } else {
                                        tokio::select! {
                                            // Closed the socket
                                            Err(_) = &mut connection => {
                                                continue 'conn;
                                            }
                                            // Receive a message to send from the queue
                                            Some(mut msg) = req_rx.recv() => {
                                                *msg.version_mut() = http::Version::HTTP_2;
                                                msg_to_send = Some(msg);
                                            }
                                        }
                                    }
                                },
                                Ok(Err(handshake_error)) => warn!(
                                    addr = target.to_string(),
                                    "HTTP2 handshake error: {handshake_error}"
                                ),
                                Err(_) => warn!(
                                    addr = target.to_string(),
                                    "HTTP2 handshake timeout after {}ms", target.connect_timeout
                                ),
                            }
                        } else {
                            match time::timeout(timeout, http1_ctx.handshake(stream)).await {
                                Ok(Ok((mut sender, mut connection))) => loop {
                                    if let Some(msg) = msg_to_send.take() {
                                        tokio::select! {
                                            // Closed the socket
                                            Err(_) = &mut connection => {
                                                debug!(addr = target.to_string(), "Remote close the HTTP1 socket");
                                                continue 'conn;
                                            }
                                            // Send an HTTP request
                                            resp = sender.try_send_request(msg) => {
                                                match resp {
                                                    Ok(r) => {
                                                        if let Err(e) = resp_tx.try_send(Ok(r)) {
                                                            warn!(addr = target.to_string(), "Error during HTTP1 response return: {e}");
                                                        } else {
                                                            nb_retry = 0;
                                                        }
                                                    }
                                                    Err(mut e) => {
                                                        msg_to_send = e.take_message();
                                                        let hyper_err = e.into_error();
                                                        if nb_retry < max_retry {
                                                            // try to send again the message after reconnection
                                                            nb_retry += 1;
                                                            continue 'conn;
                                                        } else {
                                                            error!(addr = target.to_string(), "Failed to fetch HTTP1 `{msg_to_send:?}`, because of `{hyper_err}`, after {nb_retry}/{max_retry} retries");
                                                            if let Err(e) = resp_tx.try_send(Err(FetcherError::Hyper(hyper_err, target.to_string()))) {
                                                                warn!(addr = target.to_string(), "Error during HTTP1 error response return: {e}");
                                                            }
                                                            continue 'conn;
                                                        }
                                                    }
                                                }
                                            }
                                            // Receive a message to send from the queue (unload also the queue if too many messages arrive)
                                            Some(mut msg) = req_rx.recv() => {
                                                *msg.version_mut() = http::Version::HTTP_11;
                                                msg_to_send = Some(msg);
                                            }
                                        }
                                    } else {
                                        tokio::select! {
                                            // Closed the socket
                                            Err(_) = &mut connection => {
                                                continue 'conn;
                                            }
                                            // Receive a message to send from the queue
                                            Some(mut msg) = req_rx.recv() => {
                                                *msg.version_mut() = http::Version::HTTP_11;
                                                msg_to_send = Some(msg);
                                            }
                                        }
                                    }
                                },
                                Ok(Err(handshake_error)) => warn!(
                                    addr = target.to_string(),
                                    "HTTP1 handshake error: {handshake_error}"
                                ),
                                Err(_) => warn!(
                                    addr = target.to_string(),
                                    "HTTP1 handshake timeout after {}ms", target.connect_timeout
                                ),
                            }
                        }
                    }
                    Err(e) => {
                        // If the distant have a time range, maybe the distant is not up, so just throw an info log
                        if have_time_range {
                            info!(
                                addr = target.to_string(),
                                "Can't connect to remote: {:?}", e
                            );
                        } else {
                            warn!(
                                addr = target.to_string(),
                                "Can't connect to remote: {:?}", e
                            );
                        }
                    }
                }
            }
        });
    }
}

macro_rules! process_action {
    ($self:ident, $action:ident, $adaptor:ident, $http_req_tx:ident) => {
        match $action {
            FetchAction::Http => {
                let request_builder = if let Some(target) = &$self.settings.target {
                    let mut authority_url = target.url.clone();
                    let _ = authority_url.set_username("");
                    let _ = authority_url.set_password(None);
                    let mut request_builder = Request::builder().header(hyper::header::HOST, authority_url.authority());
                    if $self.settings.authorization
                        && let Some(authorization) = target.get_authentication()
                    {
                        request_builder = request_builder.header(hyper::header::AUTHORIZATION, authorization);
                    }

                    // TOOD add USER agent
                    request_builder
                } else {
                    Request::builder()
                };
                let request = $adaptor.create_http_request(request_builder)?;
                debug!(addr = $self.settings.target.as_ref().map(|t| t.to_string()), "Send: {:?}", request);
                $http_req_tx.send(request).await.map_err(FetcherError::<M>::from)?;
            }
            FetchAction::Srv(service_name, msg) => {
                debug!("Call Service({}) Fetch action", service_name);
                if let Some(service) = $self.service.get_proc_service(&service_name) {
                    let req_msg = RequestMsg::new(service_name.clone(), msg, $self.proc.get_service_queue());
                    debug!(name: "fetcher_proc", target: "prosa_proc_fetcher::proc", parent: req_msg.get_span(), proc_name = $self.proc.name(), service = service_name, request = format!("{:?}", req_msg.get_data()));
                    service.proc_queue.send(InternalMsg::Request(req_msg)).await?;
                }
            },
            FetchAction::None => { /* No further action to do */ }
        }
    };
}

// Fetcher processor to fetch information from remote systems
#[proc]
impl<A> Proc<A> for FetcherProc
where
    A: Adaptor + FetcherAdaptor<M> + std::marker::Send,
{
    async fn internal_run(&mut self) -> Result<(), Box<dyn ProcError + Send + Sync>> {
        // Initiate an adaptor for the fetcher processor
        let mut adaptor = A::new(self)?;

        // TODO wait for external service to become available if needed.

        // Declare the processor
        self.proc.add_proc().await?;

        // Interval between each fetch
        let mut fetch_interval = time::interval(self.settings.period);

        // Spawn HTTP task if needed
        let (http_req_tx, http_req_rx) = mpsc::channel(1);
        let (http_resp_tx, mut http_resp_rx) = mpsc::channel(1);
        if let Some(target) = &self.settings.target {
            Self::spawn_http_fetch(&self.settings, target.clone(), http_req_rx, http_resp_tx);
        }

        let meter = self.proc.meter("fetcher");
        let action_histogram = meter
            .u64_histogram("prosa_fetcher_duration")
            .with_description("Fetcher duration histogram")
            .build();

        let mut is_active = true;
        let mut sent_time = Instant::now();
        loop {
            tokio::select! {
                _interval = fetch_interval.tick() => if self.settings.is_active() {
                    is_active = true;
                    let action = adaptor.fetch()?;
                    process_action!(self, action, adaptor, http_req_tx);
                    sent_time = Instant::now();
                } else if is_active {
                    is_active = false;
                    adaptor.end_active_period();
                },
                Some(http_resp) = http_resp_rx.recv() => {
                    let mut histogram_attributes = vec![
                        KeyValue::new("type", "http"),
                        KeyValue::new("code", http_resp.as_ref().map(|r| r.status().as_u16()).unwrap_or(502) as i64),
                    ];
                    if let Some(target) = &self.settings.target {
                        histogram_attributes.push(KeyValue::new("target", target.to_string()));
                    }
                    action_histogram.record(
                        sent_time.elapsed().as_millis() as u64,
                        &histogram_attributes,
                    );

                    let action = adaptor.process_http_response(http_resp).await?;
                    process_action!(self, action, adaptor, http_req_tx);
                    sent_time = Instant::now();
                }
                Some(msg) = self.internal_rx_queue.recv() => {
                    match msg {
                        InternalMsg::Request(msg) => panic!(
                            "The fetcher processor {} should not receive a request {:?}",
                            self.get_proc_id(),
                            msg
                        ),
                        InternalMsg::Response(msg) => {
                            action_histogram.record(
                                sent_time.elapsed().as_millis() as u64,
                                &[
                                    KeyValue::new("type", "service"),
                                    KeyValue::new("service", msg.get_service().clone()),
                                    KeyValue::new("code", 0),
                                ],
                            );
                            let action = adaptor.process_service_response(msg)?;
                            process_action!(self, action, adaptor, http_req_tx);
                            sent_time = Instant::now();
                        },
                        InternalMsg::Error(err) => {
                            action_histogram.record(
                                sent_time.elapsed().as_millis() as u64,
                                &[
                                    KeyValue::new("type", "service"),
                                    KeyValue::new("service", err.get_service().clone()),
                                    KeyValue::new("code", err.get_err().get_code() as i64),
                                ],
                            );
                            let action = adaptor.process_service_error(err)?;
                            process_action!(self, action, adaptor, http_req_tx);
                            sent_time = Instant::now();
                        },
                        InternalMsg::Command(_) => todo!(),
                        InternalMsg::Config => todo!(),
                        InternalMsg::Service(table) => self.service = table,
                        InternalMsg::Shutdown => {
                            // Stop directly the processor
                            adaptor.terminate();
                            self.proc.remove_proc(None).await?;
                            return Ok(());
                        }
                    }
                }
            }
        }
    }
}