| 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 |
pub mod blog; |
| 34 |
pub mod creators; |
| 35 |
pub mod items; |
| 36 |
pub mod media; |
| 37 |
pub mod projects; |
| 38 |
pub mod social; |
| 39 |
|
| 40 |
use std::sync::Arc; |
| 41 |
|
| 42 |
use sqlx::PgPool; |
| 43 |
|
| 44 |
use crate::storage::StorageBackend; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
pub struct SeedMedia { |
| 51 |
|
| 52 |
pub s3: Option<Arc<dyn StorageBackend>>, |
| 53 |
|
| 54 |
pub public_s3: Option<Arc<dyn StorageBackend>>, |
| 55 |
|
| 56 |
pub cdn_base_url: Option<String>, |
| 57 |
} |
| 58 |
|
| 59 |
impl SeedMedia { |
| 60 |
|
| 61 |
|
| 62 |
pub fn none() -> Self { |
| 63 |
Self { |
| 64 |
s3: None, |
| 65 |
public_s3: None, |
| 66 |
cdn_base_url: None, |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
pub const EXAMPLE_EMAIL_DOMAIN: &str = "example.test"; |
| 79 |
|
| 80 |
|
| 81 |
#[derive(Debug, thiserror::Error)] |
| 82 |
pub enum SeedError { |
| 83 |
|
| 84 |
#[error("refusing to seed: ALLOW_EXAMPLE_SEED is not set to 1")] |
| 85 |
NotAllowed, |
| 86 |
|
| 87 |
#[error( |
| 88 |
"refusing to seed: HOST_URL {0:?} is not an approved example host \ |
| 89 |
(testnot.work or localhost only)" |
| 90 |
)] |
| 91 |
ProdHost(String), |
| 92 |
|
| 93 |
#[error( |
| 94 |
"refusing to seed: database holds {0} non-example account(s); the example \ |
| 95 |
seed only runs on a database that contains example data or nothing" |
| 96 |
)] |
| 97 |
RealUsersPresent(i64), |
| 98 |
|
| 99 |
#[error(transparent)] |
| 100 |
Db(#[from] sqlx::Error), |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
#[error(transparent)] |
| 105 |
App(#[from] crate::error::AppError), |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
#[derive(Debug, Clone)] |
| 110 |
pub struct SeedOptions { |
| 111 |
|
| 112 |
pub allow_example_seed: bool, |
| 113 |
|
| 114 |
pub host_url: String, |
| 115 |
} |
| 116 |
|
| 117 |
impl SeedOptions { |
| 118 |
|
| 119 |
pub fn from_env(host_url: &str) -> Self { |
| 120 |
Self { |
| 121 |
allow_example_seed: std::env::var("ALLOW_EXAMPLE_SEED").ok().as_deref() == Some("1"), |
| 122 |
host_url: host_url.to_string(), |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
pub async fn run(pool: &PgPool, opts: &SeedOptions, media: &SeedMedia) -> Result<(), SeedError> { |
| 133 |
|
| 134 |
check_static_guards(opts.allow_example_seed, &opts.host_url)?; |
| 135 |
|
| 136 |
|
| 137 |
let real = count_real_users(pool).await?; |
| 138 |
if real > 0 { |
| 139 |
return Err(SeedError::RealUsersPresent(real)); |
| 140 |
} |
| 141 |
|
| 142 |
tracing::warn!( |
| 143 |
host = %opts.host_url, |
| 144 |
"example seed: all guards passed; wiping prior example data, then seeding" |
| 145 |
); |
| 146 |
|
| 147 |
|
| 148 |
let wiped = reset_example_data(pool).await?; |
| 149 |
tracing::info!( |
| 150 |
wiped_users = wiped, |
| 151 |
"example seed: cleared prior example accounts" |
| 152 |
); |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
let creators = creators::seed_creators(pool).await?; |
| 158 |
let projects = projects::seed_projects(pool, &creators).await?; |
| 159 |
items::seed_items(pool, &projects).await?; |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
media::seed_media(pool, media, &projects).await?; |
| 164 |
|
| 165 |
|
| 166 |
blog::seed_blog(pool, &projects).await?; |
| 167 |
social::seed_social(pool, &projects).await?; |
| 168 |
tracing::warn!("example seed: forum (Phase 5) deferred; refresh-flow swap is Phase 6"); |
| 169 |
|
| 170 |
Ok(()) |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
pub fn check_static_guards(allow_example_seed: bool, host_url: &str) -> Result<(), SeedError> { |
| 176 |
if !allow_example_seed { |
| 177 |
return Err(SeedError::NotAllowed); |
| 178 |
} |
| 179 |
if !is_approved_example_host(host_url) { |
| 180 |
return Err(SeedError::ProdHost(host_url.to_string())); |
| 181 |
} |
| 182 |
Ok(()) |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
fn is_approved_example_host(host_url: &str) -> bool { |
| 189 |
let host = url::Url::parse(host_url) |
| 190 |
.ok() |
| 191 |
.and_then(|u| u.host_str().map(str::to_ascii_lowercase)); |
| 192 |
match host { |
| 193 |
Some(h) => { |
| 194 |
h == "testnot.work" |
| 195 |
|| h.ends_with(".testnot.work") |
| 196 |
|| h == "localhost" |
| 197 |
|| h == "127.0.0.1" |
| 198 |
|| h == "::1" |
| 199 |
} |
| 200 |
|
| 201 |
None => false, |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
async fn count_real_users(pool: &PgPool) -> Result<i64, sqlx::Error> { |
| 209 |
sqlx::query_scalar("SELECT COUNT(*) FROM users WHERE lower(email) NOT LIKE $1") |
| 210 |
.bind(format!("%@{EXAMPLE_EMAIL_DOMAIN}")) |
| 211 |
.fetch_one(pool) |
| 212 |
.await |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
async fn reset_example_data(pool: &PgPool) -> Result<u64, sqlx::Error> { |
| 224 |
let like = format!("%@{EXAMPLE_EMAIL_DOMAIN}"); |
| 225 |
sqlx::query( |
| 226 |
"DELETE FROM projects WHERE user_id IN \ |
| 227 |
(SELECT id FROM users WHERE lower(email) LIKE $1)", |
| 228 |
) |
| 229 |
.bind(&like) |
| 230 |
.execute(pool) |
| 231 |
.await?; |
| 232 |
|
| 233 |
let res = sqlx::query("DELETE FROM users WHERE lower(email) LIKE $1") |
| 234 |
.bind(&like) |
| 235 |
.execute(pool) |
| 236 |
.await?; |
| 237 |
Ok(res.rows_affected()) |
| 238 |
} |
| 239 |
|
| 240 |
#[cfg(test)] |
| 241 |
mod tests { |
| 242 |
use super::*; |
| 243 |
|
| 244 |
#[test] |
| 245 |
fn refuses_without_allow_flag() { |
| 246 |
let err = check_static_guards(false, "https://testnot.work").unwrap_err(); |
| 247 |
assert!(matches!(err, SeedError::NotAllowed)); |
| 248 |
} |
| 249 |
|
| 250 |
#[test] |
| 251 |
fn refuses_prod_apex_even_with_flag() { |
| 252 |
|
| 253 |
let err = check_static_guards(true, "https://makenot.work").unwrap_err(); |
| 254 |
assert!(matches!(err, SeedError::ProdHost(_))); |
| 255 |
} |
| 256 |
|
| 257 |
#[test] |
| 258 |
fn refuses_prod_subdomain() { |
| 259 |
for host in [ |
| 260 |
"https://www.makenot.work", |
| 261 |
"https://app.makenot.work", |
| 262 |
"https://makenot.work/creators", |
| 263 |
] { |
| 264 |
let err = check_static_guards(true, host).unwrap_err(); |
| 265 |
assert!( |
| 266 |
matches!(err, SeedError::ProdHost(_)), |
| 267 |
"should refuse {host}" |
| 268 |
); |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
#[test] |
| 273 |
fn allows_testnot_and_localhost() { |
| 274 |
for host in [ |
| 275 |
"https://testnot.work", |
| 276 |
"https://testnot.work/", |
| 277 |
"https://sub.testnot.work", |
| 278 |
"http://localhost:3000", |
| 279 |
"http://127.0.0.1:8080", |
| 280 |
] { |
| 281 |
assert!( |
| 282 |
check_static_guards(true, host).is_ok(), |
| 283 |
"should allow {host}" |
| 284 |
); |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
#[test] |
| 289 |
fn refuses_lookalike_hosts() { |
| 290 |
|
| 291 |
for host in [ |
| 292 |
"https://testnot.work.evil.com", |
| 293 |
"https://nottestnot.work", |
| 294 |
"https://testnot-work.com", |
| 295 |
"not-a-url", |
| 296 |
"", |
| 297 |
] { |
| 298 |
let err = check_static_guards(true, host).unwrap_err(); |
| 299 |
assert!( |
| 300 |
matches!(err, SeedError::ProdHost(_)), |
| 301 |
"should refuse {host}" |
| 302 |
); |
| 303 |
} |
| 304 |
} |
| 305 |
} |
| 306 |
|