Skip to content

Commit

Permalink
refactor: switch from /dev/shm to /ramdrive
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjackwills committed Aug 14, 2024
1 parent e89e39a commit 24160e5
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 78 deletions.
4 changes: 2 additions & 2 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
"seccomp=unconfined"
],
"mounts": [
"source=/run/shm,target=/dev/shm,type=bind",
"source=/ramdrive,target=/ramdrive,type=bind",
"source=/etc/localtime,target=/etc/localtime,type=bind,readonly"
],
"postCreateCommand": "cargo install cargo-watch cross typos-cli",
"postStartCommand": "sudo mkdir -p /dev/shm/staticpi/backups /dev/shm/staticpi/pg_data /dev/shm/staticpi/redis_data /dev/shm/staticpi/logs /dev/shm/staticpi/target && sudo chown -R vscode:vscode /dev/shm/staticpi",
"postStartCommand": "sudo mkdir -p /ramdrive/staticpi/backups /ramdrive/staticpi/pg_data /ramdrive/staticpi/redis_data /ramdrive/staticpi/logs /ramdrive/staticpi/target && sudo chown -R vscode:vscode /ramdrive/staticpi",
"customizations": {
"vscode": {
// Add the IDs of extensions you want installed when the container is created.
Expand Down
12 changes: 6 additions & 6 deletions docker/dev.docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ services:
restart: always
shm_size: 256MB
volumes:
- /dev/shm/staticpi/pg_data:/pg_data
- /ramdrive/staticpi/pg_data:/pg_data
ports:
- 127.0.0.1:5432:5432
networks:
Expand All @@ -45,7 +45,7 @@ services:
dockerfile: ./docker/dockerfile/redis.Dockerfile
env_file: ./env/.redis.env
volumes:
- /dev/shm/staticpi/redis_data:/redis_data
- /ramdrive/staticpi/redis_data:/redis_data
cap_drop:
- all
security_opt:
Expand Down Expand Up @@ -81,9 +81,9 @@ services:
- no-new-privileges
ipc: private
volumes:
- /dev/shm/staticpi/backups:/backups
- /dev/shm/staticpi/redis_data:/redis_data:ro
- /dev/shm/staticpi/logs:/logs:ro
- /ramdrive/staticpi/backups:/backups
- /ramdrive/staticpi/redis_data:/redis_data:ro
- /ramdrive/staticpi/logs:/logs:ro
networks:
- staticpi-net
logging:
Expand All @@ -106,7 +106,7 @@ services:
context: /workspaces/staticpi
dockerfile: docker/dockerfile/api.Dockerfile
volumes:
- /dev/shm:/logs
- /ramdrive/staticpi/:/logs
- /workspaces/staticpi/.env:/app_env/.api.env:ro
ports:
- "127.0.0.1:8008:8008"
Expand Down
24 changes: 13 additions & 11 deletions src/emailer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ impl Emailer {
// Handle all errors in this function, just trace on any issues
#[cfg(test)]
async fn _send(emailer: Self, postgres: PgPool, email_log: ModelEmailLog) {
use crate::servers::api::api_tests::{EMAIL_BODY_LOCATION, EMAIL_HEADERS_LOCATION};

let to_box = format!("{} <{}>", emailer.name, emailer.email_address).parse::<Mailbox>();
if let (Ok(from), Ok(to)) = (emailer.env.get_from_mailbox(), to_box) {
let subject = emailer.template.get_subject();
Expand All @@ -137,9 +139,9 @@ impl Emailer {
);

if let Ok(message) = message_builder {
std::fs::write("/dev/shm/email_headers.txt", message.headers().to_string())
std::fs::write(EMAIL_HEADERS_LOCATION, message.headers().to_string())
.ok();
std::fs::write("/dev/shm/email_body.txt", html_string).ok();
std::fs::write(EMAIL_BODY_LOCATION, html_string).ok();
info!("Would be sending email if on production");
} else {
email_log.update_sent_false(&postgres).await;
Expand Down Expand Up @@ -205,9 +207,9 @@ impl Emailer {
}
}
} else {
std::fs::write("/dev/shm/email_headers.txt", message.headers().to_string())
std::fs::write("/ramdrive/staticpi/email_headers.txt", message.headers().to_string())
.ok();
std::fs::write("/dev/shm/email_body.txt", html_string).ok();
std::fs::write("/ramdrive/staticpi/email_body.txt", html_string).ok();
info!("Would be sending email if on production");
}
} else {
Expand All @@ -230,7 +232,7 @@ mod tests {
use super::*;
use crate::{
parse_env,
servers::test_setup::{setup, TestSetup, TEST_EMAIL},
servers::{api::api_tests::{EMAIL_BODY_LOCATION, EMAIL_HEADERS_LOCATION}, test_setup::{setup, TestSetup, TEST_EMAIL}},
sleep,
};

Expand Down Expand Up @@ -268,17 +270,17 @@ mod tests {
1
);

let result = std::fs::read_to_string("/dev/shm/email_body.txt").unwrap();
let result = std::fs::read_to_string(EMAIL_BODY_LOCATION).unwrap();
assert!(result.starts_with("<!doctype html><html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\"><head><title>"));
assert!(result.contains("john smith"));

let result = std::fs::read_to_string("/dev/shm/email_headers.txt").unwrap();
let result = std::fs::read_to_string(EMAIL_HEADERS_LOCATION).unwrap();
assert!(result.contains("From: staticPi <noreply@staticpi.com>"));
assert!(result.contains("To: \"john smith\" <email@example.com>"));
assert!(result.contains("Subject: Password Changed"));

std::fs::remove_file("/dev/shm/email_headers.txt").unwrap();
std::fs::remove_file("/dev/shm/email_body.txt").unwrap();
std::fs::remove_file(EMAIL_HEADERS_LOCATION).unwrap();
std::fs::remove_file(EMAIL_BODY_LOCATION).unwrap();
}

/// Emails not send if more than 275 have been sent in the previous hour
Expand Down Expand Up @@ -320,7 +322,7 @@ mod tests {
.unwrap();

assert_eq!(pre_count.count, post_count.count);
assert!(std::fs::read_to_string("/dev/shm/email_headers.txt").is_err());
assert!(std::fs::read_to_string("/dev/shm/email_body.txt").is_err());
assert!(std::fs::read_to_string(EMAIL_HEADERS_LOCATION).is_err());
assert!(std::fs::read_to_string(EMAIL_BODY_LOCATION).is_err());
}
}
3 changes: 3 additions & 0 deletions src/servers/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ pub mod api_tests {
use crate::servers::test_setup::TestSetup;
use crate::servers::{get_api_version, test_setup::start_servers};

pub const EMAIL_BODY_LOCATION: &str = "/ramdrive/staticpi/email_body.txt";
pub const EMAIL_HEADERS_LOCATION: &str = "/ramdrive/staticpi/email_headers.txt";

#[test]
fn http_mod_get_api_version() {
assert_eq!(get_api_version(), "/v0".to_owned());
Expand Down
45 changes: 23 additions & 22 deletions src/servers/api/routers/incognito.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,8 @@ impl IncognitoRouter {
mod tests {

use super::IncognitoRoutes;
use crate::database::contact_message::ModelContactMessage;
use crate::servers::api::api_tests::EMAIL_BODY_LOCATION;
use crate::{database::contact_message::ModelContactMessage, servers::api::api_tests::EMAIL_HEADERS_LOCATION};
use crate::database::email_address::ModelEmailAddress;
use crate::database::email_log::ModelEmailLog;
use crate::database::invite::ModelInvite;
Expand Down Expand Up @@ -718,10 +719,10 @@ mod tests {
client.post(&url).json(&body).send().await.unwrap();
}

let result = std::fs::read_to_string("/dev/shm/email_headers.txt");
let result = std::fs::read_to_string(EMAIL_HEADERS_LOCATION);
assert!(result.is_ok());
assert!(result.unwrap().contains("Subject: Security Alert"));
let result = std::fs::read_to_string("/dev/shm/email_body.txt");
let result = std::fs::read_to_string(EMAIL_BODY_LOCATION);
assert!(result.is_ok());
assert!(result
.unwrap()
Expand Down Expand Up @@ -1345,9 +1346,9 @@ mod tests {
let result = RedisNewUser::exists(&test_setup.redis, "email@mrjackwills.com").await;
assert!(result.is_ok());
assert!(!result.unwrap());
let result = std::fs::metadata("/dev/shm/email_headers.txt");
let result = std::fs::metadata(EMAIL_HEADERS_LOCATION);
assert!(result.is_err());
let result = std::fs::metadata("/dev/shm/email_body.txt");
let result = std::fs::metadata(EMAIL_BODY_LOCATION);
assert!(result.is_err());
assert_eq!(
ModelEmailLog::get_count_total(&test_setup.postgres)
Expand Down Expand Up @@ -1415,9 +1416,9 @@ mod tests {
assert!(result.unwrap());

// check email sent - well written to disk & inserted into db
let result = std::fs::metadata("/dev/shm/email_headers.txt");
let result = std::fs::metadata(EMAIL_HEADERS_LOCATION);
assert!(result.is_ok());
let result = std::fs::metadata("/dev/shm/email_body.txt");
let result = std::fs::metadata(EMAIL_BODY_LOCATION);
assert!(result.is_ok());
let link = format!(
"href=\"https://www.{}/user/verify/",
Expand All @@ -1430,7 +1431,7 @@ mod tests {
.count,
1
);
assert!(std::fs::read_to_string("/dev/shm/email_body.txt")
assert!(std::fs::read_to_string(EMAIL_BODY_LOCATION)
.unwrap()
.contains(&link));
}
Expand Down Expand Up @@ -1464,15 +1465,15 @@ mod tests {
assert!(result.unwrap());

// check email sent - well written to disk & inserted into db
let result = std::fs::metadata("/dev/shm/email_headers.txt");
let result = std::fs::metadata(EMAIL_HEADERS_LOCATION);
assert!(result.is_ok());
let result = std::fs::metadata("/dev/shm/email_body.txt");
let result = std::fs::metadata(EMAIL_BODY_LOCATION);
assert!(result.is_ok());
let link = format!(
"href=\"https://www.{}/user/verify/",
test_setup.app_env.domain
);
assert!(std::fs::read_to_string("/dev/shm/email_body.txt")
assert!(std::fs::read_to_string(EMAIL_BODY_LOCATION)
.unwrap()
.contains(&link));
assert_eq!(
Expand Down Expand Up @@ -1515,9 +1516,9 @@ mod tests {
.count,
0
);
let result = std::fs::metadata("/dev/shm/email_headers.txt");
let result = std::fs::metadata(EMAIL_HEADERS_LOCATION);
assert!(result.is_err());
let result = std::fs::metadata("/dev/shm/email_body.txt");
let result = std::fs::metadata(EMAIL_BODY_LOCATION);
assert!(result.is_err());

let second_secret = get_keys(&test_setup.redis, "verify::secret::*").await;
Expand Down Expand Up @@ -1569,15 +1570,15 @@ mod tests {
assert!(result.unwrap());

// check email sent - well written to disk & inserted into db
let result = std::fs::metadata("/dev/shm/email_headers.txt");
let result = std::fs::metadata(EMAIL_HEADERS_LOCATION);
assert!(result.is_ok());
let result = std::fs::metadata("/dev/shm/email_body.txt");
let result = std::fs::metadata(EMAIL_BODY_LOCATION);
assert!(result.is_ok());
let link = format!(
"href=\"https://www.{}/user/verify/",
test_setup.app_env.domain
);
assert!(std::fs::read_to_string("/dev/shm/email_body.txt")
assert!(std::fs::read_to_string(EMAIL_BODY_LOCATION)
.unwrap()
.contains(&link));

Expand Down Expand Up @@ -1741,9 +1742,9 @@ mod tests {
0
);
// check email NOT sent - well written to disk
let result = std::fs::metadata("/dev/shm/email_headers.txt");
let result = std::fs::metadata(EMAIL_HEADERS_LOCATION);
assert!(result.is_err());
let result = std::fs::metadata("/dev/shm/email_body.txt");
let result = std::fs::metadata(EMAIL_BODY_LOCATION);
assert!(result.is_err());
}

Expand Down Expand Up @@ -1777,7 +1778,7 @@ mod tests {
let password_reset = password_reset.unwrap();

// check email has been sent - well written to disk, and contain secret & correct subject
let result = std::fs::read_to_string("/dev/shm/email_headers.txt");
let result = std::fs::read_to_string(EMAIL_HEADERS_LOCATION);
assert!(result.is_ok());
assert!(result
.unwrap()
Expand All @@ -1791,7 +1792,7 @@ mod tests {
1
);

let result = std::fs::read_to_string("/dev/shm/email_body.txt");
let result = std::fs::read_to_string(EMAIL_BODY_LOCATION);
assert!(result.is_ok());
assert!(result.unwrap().contains(&password_reset.reset_string));
}
Expand Down Expand Up @@ -1848,9 +1849,9 @@ mod tests {
0
);

let result = std::fs::metadata("/dev/shm/email_headers.txt");
let result = std::fs::metadata(EMAIL_HEADERS_LOCATION);
assert!(result.is_err());
let result = std::fs::metadata("/dev/shm/email_body.txt");
let result = std::fs::metadata(EMAIL_BODY_LOCATION);
assert!(result.is_err());
}

Expand Down
Loading

0 comments on commit 24160e5

Please sign in to comment.