sylvia_iot_coremgr/routes/
middleware.rs

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
//! Provides the operation log middleware by sending requests to the data channel.

use std::{
    collections::HashMap,
    net::SocketAddr,
    task::{Context, Poll},
};

use axum::{
    body::{self, Body},
    extract::{ConnectInfo, Request},
    http::Method,
    response::{IntoResponse, Response},
};
use chrono::Utc;
use futures::future::BoxFuture;
use reqwest;
use serde::{self, Deserialize, Serialize};
use serde_json::{Map, Value};
use tower::{Layer, Service};

use general_mq::{queue::GmqQueue, Queue};
use sylvia_iot_corelib::{err::ErrResp, http as sylvia_http, strings};

#[derive(Clone)]
pub struct GetTokenInfoData {
    pub token: String,
    pub user_id: String,
    pub account: String,
    pub roles: HashMap<String, bool>,
    pub name: String,
    pub client_id: String,
    pub scopes: Vec<String>,
}

#[derive(Clone)]
pub struct LogService {
    auth_uri: String,
    queue: Option<Queue>,
}

#[derive(Clone)]
pub struct LogMiddleware<S> {
    client: reqwest::Client,
    auth_uri: String,
    queue: Option<Queue>,
    service: S,
}

/// The user/client information of the token.
#[derive(Deserialize)]
struct GetTokenInfo {
    data: GetTokenInfoDataInner,
}

#[derive(Deserialize)]
struct GetTokenInfoDataInner {
    #[serde(rename = "userId")]
    user_id: String,
    #[serde(rename = "account")]
    _account: String,
    #[serde(rename = "roles")]
    _roles: HashMap<String, bool>,
    #[serde(rename = "name")]
    _name: String,
    #[serde(rename = "clientId")]
    client_id: String,
    #[serde(rename = "scopes")]
    _scopes: Vec<String>,
}

#[derive(Serialize)]
struct SendDataMsg {
    kind: String,
    data: SendDataKind,
}

#[derive(Serialize)]
#[serde(untagged)]
enum SendDataKind {
    Operation {
        #[serde(rename = "dataId")]
        data_id: String,
        #[serde(rename = "reqTime")]
        req_time: String,
        #[serde(rename = "resTime")]
        res_time: String,
        #[serde(rename = "latencyMs")]
        latency_ms: i64,
        status: isize,
        #[serde(rename = "sourceIp")]
        source_ip: String,
        method: String,
        path: String,
        #[serde(skip_serializing_if = "Option::is_none")]
        body: Option<Map<String, Value>>,
        #[serde(rename = "userId")]
        user_id: String,
        #[serde(rename = "clientId")]
        client_id: String,
        #[serde(rename = "errCode", skip_serializing_if = "Option::is_none")]
        err_code: Option<String>,
        #[serde(rename = "errMessage", skip_serializing_if = "Option::is_none")]
        err_message: Option<String>,
    },
}

struct DataMsgKind;

const DATA_ID_RAND_LEN: usize = 12;

impl DataMsgKind {
    const OPERATION: &'static str = "operation";
}

impl LogService {
    pub fn new(auth_uri: String, queue: Option<Queue>) -> Self {
        LogService { auth_uri, queue }
    }
}

impl<S> Layer<S> for LogService {
    type Service = LogMiddleware<S>;

    fn layer(&self, inner: S) -> Self::Service {
        LogMiddleware {
            client: reqwest::Client::new(),
            auth_uri: self.auth_uri.clone(),
            queue: self.queue.clone(),
            service: inner,
        }
    }
}

impl<S> Service<Request> for LogMiddleware<S>
where
    S: Service<Request, Response = Response> + Clone + Send + 'static,
    S::Future: Send + 'static,
{
    type Response = S::Response;
    type Error = S::Error;
    type Future = BoxFuture<'static, Result<Self::Response, Self::Error>>;

    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        self.service.poll_ready(cx)
    }

    fn call(&mut self, req: Request) -> Self::Future {
        let mut svc = self.service.clone();
        let client = self.client.clone();
        let auth_uri = self.auth_uri.clone();
        let method = req.method().clone();
        let queue = match method {
            Method::DELETE | Method::PATCH | Method::POST | Method::PUT => self.queue.clone(),
            _ => None,
        };

        Box::pin(async move {
            // Only log for DELETE/PATCH/POST/PUT methods.
            let q = match queue.as_ref() {
                None => {
                    let res = svc.call(req).await?;
                    return Ok(res);
                }
                Some(q) => q,
            };

            let req_time = Utc::now();

            // Collect body (and generate a new stream) and information for logging the operation.
            let source_ip = match req.extensions().get::<ConnectInfo<SocketAddr>>() {
                None => "".to_string(),
                Some(info) => info.0.to_string(),
            };
            let method = req.method().to_string();
            let path = req.uri().to_string();
            let auth_token = match sylvia_http::parse_header_auth(&req) {
                Err(_) => None,
                Ok(token) => match token {
                    None => None,
                    Some(token) => Some(token),
                },
            };
            let (parts, body) = req.into_parts();
            let body_bytes = match body::to_bytes(body, usize::MAX).await {
                Err(e) => {
                    let e = format!("convert body error: {}", e);
                    return Ok(ErrResp::ErrParam(Some(e)).into_response());
                }
                Ok(body_bytes) => body_bytes,
            };
            let log_body = match serde_json::from_slice::<Map<String, Value>>(&body_bytes.to_vec())
            {
                Err(_) => None,
                Ok(mut body) => {
                    // Remove secret contents.
                    if let Some(data) = body.get_mut("data") {
                        if let Some(data) = data.as_object_mut() {
                            if data.contains_key("password") {
                                data.insert("password".to_string(), Value::String("".to_string()));
                            }
                        }
                    }
                    Some(body)
                }
            };
            let req = Request::from_parts(parts, Body::from(body_bytes));

            // Do the request.
            let res = svc.call(req).await?;
            let (err_code, err_message) = match res.status().is_success() {
                false => {
                    // TODO: extract (code, message) pair of response body.
                    (None, None)
                }
                true => (None, None),
            };

            // Send log.
            let auth_token = match auth_token.as_ref() {
                None => return Ok(res),
                Some(auth_token) => auth_token,
            };
            let token_info = match get_token(client, auth_token.as_str(), auth_uri.as_str()).await {
                Err(_) => return Ok(res),
                Ok(token_info) => token_info,
            };
            let res_time = Utc::now();
            let msg = SendDataMsg {
                kind: DataMsgKind::OPERATION.to_string(),
                data: SendDataKind::Operation {
                    data_id: strings::random_id(&req_time, DATA_ID_RAND_LEN),
                    req_time: strings::time_str(&req_time),
                    res_time: strings::time_str(&res_time),
                    latency_ms: res_time.timestamp_millis() - req_time.timestamp_millis(),
                    status: res.status().as_u16() as isize,
                    source_ip,
                    method,
                    path,
                    body: log_body,
                    user_id: token_info.data.user_id,
                    client_id: token_info.data.client_id,
                    err_code,
                    err_message,
                },
            };
            if let Ok(payload) = serde_json::to_vec(&msg) {
                let _ = q.send_msg(payload).await;
            }
            Ok(res)
        })
    }
}

async fn get_token(
    client: reqwest::Client,
    auth_token: &str,
    auth_uri: &str,
) -> Result<GetTokenInfo, String> {
    let token_req = match client
        .request(reqwest::Method::GET, auth_uri)
        .header(reqwest::header::AUTHORIZATION, auth_token)
        .build()
    {
        Err(e) => return Err(format!("request auth error: {}", e)),
        Ok(req) => req,
    };
    let resp = match client.execute(token_req).await {
        Err(e) => return Err(format!("auth error: {}", e)),
        Ok(resp) => match resp.status() {
            reqwest::StatusCode::UNAUTHORIZED => return Err("unauthorized".to_string()),
            reqwest::StatusCode::OK => resp,
            _ => return Err(format!("auth error with status code: {}", resp.status())),
        },
    };
    match resp.json::<GetTokenInfo>().await {
        Err(e) => Err(format!("read auth body error: {}", e)),
        Ok(info) => Ok(info),
    }
}