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