restapi 1.1.14

A secure-by-default rest api using hyper, tokio, bb8, kafka-threadpool, postgres, and prometheus for monitoring
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
//! Module for uploading a POST-ed file to s3 and store the tracking data in
//! the postgres db
//!
//! ## Upload a file asynchronously to AWS S3 and store a tracking record in the db
//!
//! Upload a local file on disk to AWS S3 asynchronously and store a
//! tracking record in the ``users_data`` table. The documentation
//! refers to this as a ``user data`` or ``user data file`` record.
//!
//! - URL path: ``/user/data``
//! - Method: ``POST``
//! - Handler: [`upload_user_data`](crate::requests::user::upload_user_data::upload_user_data)
//! - Request: [`ApiReqUserUploadData`](crate::requests::user::upload_user_data::ApiReqUserUploadData)
//! - Response: [`ApiResUserUploadData`](crate::requests::user::upload_user_data::ApiResUserUploadData)
//!

use std::convert::Infallible;

use postgres_native_tls::MakeTlsConnector;

use bb8::Pool;
use bb8_postgres::PostgresConnectionManager;

use hyper::body;
use hyper::header::HeaderValue;
use hyper::Body;
use hyper::HeaderMap;
use hyper::Response;

use serde::Deserialize;
use serde::Serialize;

use kafka_threadpool::kafka_publisher::KafkaPublisher;

use crate::core::core_config::CoreConfig;
use crate::is3::s3_upload_buffer::s3_upload_buffer;
use crate::kafka::publish_msg::publish_msg;
use crate::requests::auth::validate_user_token::validate_user_token;
use crate::utils::get_uuid::get_uuid;

/// ApiReqUserUploadData
///
/// # Request Type For upload_user_data
///
/// Handles creating a `users_data` record in the db and
/// uploading the POST-ed file contents
/// to a remote, db-tracked s3 location (`sloc`).
///
/// This type contains the uploaded file in a `Vec<u8>`
/// from the contents in a local file.
///
/// This type is the deserialized input for:
/// [`upload_user_data`](crate::requests::user::upload_user_data::upload_user_data]
///
/// # Usage
///
/// This type is constructed from the deserialized
/// `bytes` (`&[u8]`) argument
/// on the
/// [`upload_user_data`](crate::requests::user::upload_user_data::upload_user_data)
/// function.
///
/// # Arguments
///
/// * `data` - `Vec<u8>` - contents from the POST-ed file
///   as a vector of bytes
///
#[derive(Serialize, Deserialize, Clone)]
pub struct ApiReqUserUploadData {
    pub data: Vec<u8>,
}

/// ApiResUserUploadData
///
/// # Response type for upload_user_data
///
/// Return the created `users_data` db record
/// including the remote s3 location (`sloc`)
///
/// # Usage
///
/// This type is the serialized output for the function:
/// [`upload_user_data`](crate::requests::user::upload_user_data::upload_user_data]
/// and contained within the
/// hyper [`Body`](hyper::Body)
/// of the
/// hyper [`Response`](hyper::Response)
/// sent back to the client.
///
/// * `user_id` - `i32` - `users.id`
/// * `data_id` - `i32` - `users_data.id`
/// * `filename` - `String` - name of the file
/// * `data_type` - `String` - data type for the file
/// * `size_in_bytes` - `i64` - size of the file
///   (number of bytes in the POST-ed `data`)
/// * `comments` - `String` - notes or description
/// * `encoding` - `String` - encoding
/// * `sloc` - `String` - remote s3 location
/// * `msg` - `String` - help message
///
#[derive(Serialize, Deserialize, Clone)]
pub struct ApiResUserUploadData {
    pub user_id: i32,
    pub data_id: i32,
    pub filename: String,
    pub data_type: String,
    // https://www.google.com/search?q=rust+bigint+postgres
    // postgres size_in_bytes field is a BIGINT type
    pub size_in_bytes: i64,
    pub comments: String,
    pub encoding: String,
    pub sloc: String,
    pub msg: String,
}

/// upload_user_data
///
/// Handles uploading a POST-ed file to s3 and
/// create a `users_data` record to track the s3 utilization.
///
/// # Usage
///
/// ## Environment variables
///
/// ### Change the s3 bucket for file uploads
///
/// ```bash
/// export S3_DATA_BUCKET=BUCKET_NAME
/// ```
///
/// ### Change the s3 bucket prefix path for file uploads
///
/// ```bash
/// export S3_DATA_PREFIX="user/data/file"
/// ```
///
/// The file contents must be passed in the `data` field of the
/// [`ApiReqUserUploadData`](crate::requests::user::upload_user_data::ApiReqUserUploadData)
/// type which is serialized within a POST-ed hyper
/// [`Request`](hyper::Request)'s [`Body`](hyper::Body)
///
/// ## Overview Notes
///
/// This function only creates 1 `users_data` record at a time.
///
/// It also uploads the `data` (file contents) with a user-and-date
/// pathing convention.
///
/// # Arguments
///
/// * `tracking_label` - `&str` - caller logging label
/// * `config` - [`CoreConfig`](crate::core::core_config::CoreConfig)
/// * `db_pool` - [`Pool`](bb8::Pool) - postgres client
///   db threadpool with required tls encryption
/// * `kafka_pool` -
///   [`KafkaPublisher`](kafka_threadpool::kafka_publisher::KafkaPublisher)
///   for asynchronously publishing messages to the connected kafka cluster
/// * `headers` - [`HeaderMap`](hyper::HeaderMap) -
///   hashmap containing headers in key-value pairs
///   [`Request`](hyper::Request)'s [`Body`](hyper::Body)
/// * `body` - `hyper::Body` - the hyper
///   [`Request`](hyper::Request)'s [`Body`](hyper::Body)
///   containing the file's contents to store on s3. The
///   contents must be in the POST-ed Body's `data` key.
///
/// # Returns
///
/// ## upload_user_data on Success Returns
///
/// The newly-uploaded `users_data` record in the db
/// ([`ApiResUserUploadData`](crate::requests::user::upload_user_data::upload_user_data))
///
/// hyper [`Response`](hyper::Response)
/// containing a json-serialized
/// [`ApiResUserUploadData`](crate::requests::user::upload_user_data::ApiResUserUploadData)
/// dictionary within the
/// [`Body`](hyper::Body) and a
/// `200` HTTP status code
///
/// Ok([`Response`](hyper::Response))
///
/// # Errors
///
/// ## upload_user_data on Failure Returns
///
/// All errors return as a
/// hyper [`Response`](hyper::Response)
/// containing a json-serialized
/// [`ApiResUserUploadData`](crate::requests::user::upload_user_data::ApiResUserUploadData)
/// dictionary with a
/// `non-200` HTTP status code
///
/// Err([`Response`](hyper::Response))
///
pub async fn upload_user_data(
    tracking_label: &str,
    config: &CoreConfig,
    db_pool: &Pool<PostgresConnectionManager<MakeTlsConnector>>,
    kafka_pool: &KafkaPublisher,
    headers: &HeaderMap<HeaderValue>,
    body: hyper::Body,
) -> std::result::Result<Response<Body>, Infallible> {
    if !headers.contains_key("user_id") {
        let response = Response::builder()
            .status(400)
            .body(Body::from(
                serde_json::to_string(
                    &ApiResUserUploadData {
                        user_id: -1,
                        data_id: -1,
                        filename: "".to_string(),
                        data_type: "".to_string(),
                        size_in_bytes: 0,
                        comments: "".to_string(),
                        encoding: "".to_string(),
                        sloc: "".to_string(),
                        msg: (
                            "Missing required header 'user_id' key (i.e. curl -H 'user_id: INT'"
                        ).to_string(),
                    }
                ).unwrap()))
            .unwrap();
        return Ok(response);
    }
    let user_id_str = headers.get("user_id").unwrap().to_str().unwrap();
    let user_id: i32 = match user_id_str.parse::<i32>() {
        Ok(user_id) => user_id,
        Err(_) => {
            let response = Response::builder()
                .status(400)
                .body(Body::from(
                    serde_json::to_string(
                        &ApiResUserUploadData {
                            user_id: -1,
                            data_id: -1,
                            filename: "".to_string(),
                            data_type: "".to_string(),
                            size_in_bytes: 0,
                            comments: "".to_string(),
                            encoding: "".to_string(),
                            sloc: "".to_string(),
                            msg: (
                                "user_id must be a postive number that is the actual user_id for the token"
                            ).to_string(),
                        }
                    ).unwrap()))
                .unwrap();
            return Ok(response);
        }
    };
    if !headers.contains_key("filename") {
        let response = Response::builder()
            .status(400)
            .body(Body::from(
                serde_json::to_string(
                    &ApiResUserUploadData {
                        user_id: -1,
                        data_id: -1,
                        filename: "".to_string(),
                        data_type: "".to_string(),
                        size_in_bytes: 0,
                        comments: "".to_string(),
                        encoding: "".to_string(),
                        sloc: "".to_string(),
                        msg: (
                            "Missing required header 'filename' key (i.e. curl -H 'user_id: INT'"
                        ).to_string(),
                    }
                ).unwrap()))
            .unwrap();
        return Ok(response);
    }
    let file_name_str = headers.get("filename").unwrap().to_str().unwrap();
    let file_name_len = file_name_str.len();

    // between 1 and 511 chars
    if !(1..=511).contains(&file_name_len) {
        let response = Response::builder()
            .status(400)
            .body(Body::from(
                serde_json::to_string(
                    &ApiResUserUploadData {
                        user_id: -1,
                        data_id: -1,
                        filename: "".to_string(),
                        data_type: "".to_string(),
                        size_in_bytes: 0,
                        comments: "".to_string(),
                        encoding: "".to_string(),
                        sloc: "".to_string(),
                        msg: (
                            "The header value for 'filename' must be between 1 and 511 characters"
                        ).to_string(),
                    }
                ).unwrap()))
            .unwrap();
        return Ok(response);
    }
    // -H 'filename: testfile.txt' -H 'data_type: file' -H 'encoding: na' -H 'comments: this is a test comment' -H 'sloc: s3://bucket/prefix'
    let encoding = match headers.get("encoding") {
        Some(v) => v.to_str().unwrap().to_string(),
        None => "na".to_string(),
    };
    let comments = match headers.get("comments") {
        Some(v) => v.to_str().unwrap().to_string(),
        None => "file".to_string(),
    };
    let data_type = match headers.get("data_type") {
        Some(v) => v.to_str().unwrap().to_string(),
        None => "file".to_string(),
    };
    let sloc_start = match headers.get("sloc") {
        Some(v) => v.to_str().unwrap().to_string(),
        None => "".to_string(),
    };
    let should_upload_to_s3 = match headers.get("s3_enable") {
        Some(_) => true,
        None => {
            std::env::var("S3_DATA_UPLOAD_TO_S3")
                .unwrap_or_else(|_| "0".to_string())
                == *"0"
        }
    };

    let s3_bucket = std::env::var("S3_DATA_BUCKET")
        .unwrap_or_else(|_| "BUCKET_NAME".to_string());
    let s3_prefix = std::env::var("S3_DATA_PREFIX")
        .unwrap_or_else(|_| "user/data/file".to_string());
    let now = chrono::Utc::now();
    let now_str = now.format("%Y/%m/%d");
    let s3_uuid = get_uuid();
    let s3_key_dst = format!(
        "{s3_prefix}/\
        {user_id}/\
        {now_str}/\
        {s3_uuid}.{file_name_str}"
    );
    let sloc = match sloc_start.len() {
        0 => {
            format!("s3://{s3_bucket}/{s3_key_dst}")
        }
        _ => sloc_start,
    };

    {
        let conn = db_pool.get().await.unwrap();
        let _token = match validate_user_token(
            tracking_label,
            config,
            &conn,
            headers,
            user_id,
        )
        .await
        {
            Ok(_token) => _token,
            Err(_) => {
                let response = Response::builder()
                    .status(400)
                    .body(Body::from(
                        serde_json::to_string(
                            &ApiResUserUploadData {
                                user_id: -1,
                                data_id: -1,
                                filename: "".to_string(),
                                data_type: "".to_string(),
                                size_in_bytes: 0,
                                comments: "".to_string(),
                                encoding: "".to_string(),
                                sloc: "".to_string(),
                                msg: ("
                                    User data upload failed due to invalid token"
                                ).to_string(),
                            }
                        ).unwrap()))
                .unwrap();
                return Ok(response);
            }
        };
    }

    info!("{tracking_label} - receiving user_id={user_id} name={file_name_str} data");
    let bytes = body::to_bytes(body).await.unwrap();
    let file_contents_size: usize = bytes.len() as usize;
    if file_contents_size < 1 {
        let response = Response::builder()
            .status(400)
            .body(Body::from(
                serde_json::to_string(&ApiResUserUploadData {
                    user_id: -1,
                    data_id: -1,
                    filename: "".to_string(),
                    data_type: "".to_string(),
                    size_in_bytes: 0,
                    comments: "".to_string(),
                    encoding: "".to_string(),
                    sloc: "".to_string(),
                    msg: ("No data uploaded in the body").to_string(),
                })
                .unwrap(),
            ))
            .unwrap();
        return Ok(response);
    }

    let file_contents_size_in_mb: f32 =
        file_contents_size as f32 / 1024.0 / 1024.0;

    info!(
        "{tracking_label} - processing data for user_id={user_id} \
        name={file_name_str} \
        size={file_contents_size_in_mb:.2}mb \
        upload_to_s3={should_upload_to_s3} \
        {sloc}"
    );

    if should_upload_to_s3 {
        match s3_upload_buffer(tracking_label, &s3_bucket, &s3_key_dst, &bytes)
            .await
        {
            Ok(good_msg) => {
                info!("{good_msg} - done uploading - {sloc}")
            }
            Err(emsg) => {
                info!("{emsg} - failed uploading {sloc}")
            }
        }
    } else {
        info!("{tracking_label} - not uploading to s3");
    }

    let conn = db_pool.get().await.unwrap();
    let cur_query = format!(
        "INSERT INTO \
        users_data (\
            user_id, \
            filename, \
            data_type, \
            size_in_bytes, \
            comments, \
            encoding, \
            sloc) \
        VALUES (\
            {user_id},
            '{file_name_str}',
            '{data_type}',
            {file_contents_size},
            '{comments}',
            '{encoding}',
            '{sloc}') \
        RETURNING \
            users_data.id,
            users_data.user_id,
            users_data.filename,
            users_data.data_type,
            users_data.size_in_bytes,
            users_data.comments,
            users_data.encoding,
            users_data.sloc;"
    );
    let stmt = conn.prepare(&cur_query).await.unwrap();
    let query_result = match conn.query(&stmt, &[]).await {
        Ok(query_result) => query_result,
        Err(e) => {
            let err_msg = format!("{}", e);
            let response = Response::builder()
                .status(500)
                .body(Body::from(
                    serde_json::to_string(&ApiResUserUploadData {
                        user_id: -1,
                        data_id: -1,
                        filename: "".to_string(),
                        data_type: "".to_string(),
                        size_in_bytes: 0,
                        comments: "".to_string(),
                        encoding: "".to_string(),
                        sloc: "".to_string(),
                        msg: format!(
                            "User data upload failed for user_id={user_id} \
                                with err='{err_msg}'"
                        ),
                    })
                    .unwrap(),
                ))
                .unwrap();
            return Ok(response);
        }
    };
    let mut row_list: Vec<ApiResUserUploadData> = Vec::with_capacity(1);
    for row in query_result.iter() {
        let found_data_id: i32 = row.try_get("id").unwrap();
        let found_user_id: i32 = row.try_get("user_id").unwrap();
        let found_filename: String = row.try_get("filename").unwrap();
        let found_data_type: String = row.try_get("data_type").unwrap();
        let found_size_in_bytes: i64 = row.try_get("size_in_bytes").unwrap();
        let found_comments: String = row.try_get("comments").unwrap();
        let found_encoding: String = row.try_get("encoding").unwrap();
        let found_sloc: String = row.try_get("sloc").unwrap();
        row_list.push(ApiResUserUploadData {
            user_id: found_user_id,
            data_id: found_data_id,
            filename: found_filename,
            data_type: found_data_type,
            size_in_bytes: found_size_in_bytes,
            comments: found_comments,
            encoding: found_encoding,
            sloc: found_sloc,
            msg: "success".to_string(),
        });
    }
    if row_list.is_empty() {
        let response = Response::builder()
            .status(400)
            .body(Body::from(
                serde_json::to_string(&ApiResUserUploadData {
                    user_id: -1,
                    data_id: -1,
                    filename: "".to_string(),
                    data_type: "".to_string(),
                    size_in_bytes: 0,
                    comments: "".to_string(),
                    encoding: "".to_string(),
                    sloc: "".to_string(),
                    msg: ("no upload data found in db").to_string(),
                })
                .unwrap(),
            ))
            .unwrap();
        Ok(response)
    } else {
        // if enabled, publish to kafka
        if config.kafka_publish_events {
            publish_msg(
                kafka_pool,
                // topic
                "user.events",
                // partition key
                &format!("user-{}", user_id),
                // optional headers stored in: Option<HashMap<String, String>>
                None,
                // payload in the message
                &format!("UPLOAD_USER_DATA user={user_id}"),
            )
            .await;
        }
        let response = Response::builder()
            .status(200)
            .body(Body::from(serde_json::to_string(&row_list[0]).unwrap()))
            .unwrap();
        Ok(response)
    }
}