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