| 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 |
use uuid::Uuid; |
| 42 |
|
| 43 |
use super::{EXAMPLE_EMAIL_DOMAIN, SeedError}; |
| 44 |
use crate::auth; |
| 45 |
use crate::db::{self, UserId}; |
| 46 |
|
| 47 |
|
| 48 |
pub const PASSWORD_ENV: &str = "MT_HARNESS_PASSWORD"; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
pub const REDIRECT_URI_ENV: &str = "MT_HARNESS_REDIRECT_URI"; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
pub const CLIENT_ID: &str = "mt-astra-harness"; |
| 59 |
|
| 60 |
|
| 61 |
const CLIENT_NAME: &str = "Multithreaded (astra harness)"; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
pub const FAN_ACCOUNT_ID: Uuid = Uuid::from_u128(0x0000_0000_0000_0000_0000_0000_0000_f001); |
| 66 |
|
| 67 |
|
| 68 |
pub const CREATOR_ACCOUNT_ID: Uuid = Uuid::from_u128(0x0000_0000_0000_0000_0000_0000_0000_f002); |
| 69 |
|
| 70 |
|
| 71 |
pub const OWNER_ACCOUNT_ID: Uuid = Uuid::from_u128(0x0000_0000_0000_0000_0000_0000_0000_f003); |
| 72 |
|
| 73 |
|
| 74 |
struct AccountSpec { |
| 75 |
id: Uuid, |
| 76 |
handle: &'static str, |
| 77 |
display_name: &'static str, |
| 78 |
|
| 79 |
|
| 80 |
creator_tier: Option<&'static str>, |
| 81 |
|
| 82 |
fan_plus: bool, |
| 83 |
} |
| 84 |
|
| 85 |
const ACCOUNTS: &[AccountSpec] = &[ |
| 86 |
AccountSpec { |
| 87 |
id: FAN_ACCOUNT_ID, |
| 88 |
handle: "harness_fan", |
| 89 |
display_name: "Harness Fan", |
| 90 |
creator_tier: None, |
| 91 |
fan_plus: true, |
| 92 |
}, |
| 93 |
AccountSpec { |
| 94 |
id: CREATOR_ACCOUNT_ID, |
| 95 |
handle: "harness_creator", |
| 96 |
display_name: "Harness Creator", |
| 97 |
creator_tier: Some("everything"), |
| 98 |
fan_plus: false, |
| 99 |
}, |
| 100 |
AccountSpec { |
| 101 |
id: OWNER_ACCOUNT_ID, |
| 102 |
handle: "harness_owner", |
| 103 |
display_name: "Harness Owner", |
| 104 |
creator_tier: None, |
| 105 |
fan_plus: false, |
| 106 |
}, |
| 107 |
]; |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
#[derive(Clone)] |
| 112 |
pub struct HarnessOptions { |
| 113 |
|
| 114 |
pub password: String, |
| 115 |
|
| 116 |
pub redirect_uri: String, |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
impl std::fmt::Debug for HarnessOptions { |
| 122 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 123 |
f.debug_struct("HarnessOptions") |
| 124 |
.field("password", &"<redacted>") |
| 125 |
.field("redirect_uri", &self.redirect_uri) |
| 126 |
.finish() |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
impl HarnessOptions { |
| 131 |
|
| 132 |
|
| 133 |
pub fn from_env() -> Option<Self> { |
| 134 |
let password = std::env::var(PASSWORD_ENV).ok().filter(|s| !s.is_empty())?; |
| 135 |
let redirect_uri = std::env::var(REDIRECT_URI_ENV) |
| 136 |
.ok() |
| 137 |
.filter(|s| !s.is_empty())?; |
| 138 |
Some(Self { |
| 139 |
password, |
| 140 |
redirect_uri, |
| 141 |
}) |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
pub async fn seed_harness(pool: &sqlx::PgPool, opts: &HarnessOptions) -> Result<(), SeedError> { |
| 152 |
|
| 153 |
|
| 154 |
let password_hash = auth::hash_password_async(opts.password.clone()).await?; |
| 155 |
|
| 156 |
for spec in ACCOUNTS { |
| 157 |
seed_account(pool, spec, &password_hash).await?; |
| 158 |
if spec.fan_plus { |
| 159 |
seed_fan_plus(pool, spec.id).await?; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
seed_client(pool, &opts.redirect_uri).await?; |
| 164 |
|
| 165 |
tracing::warn!( |
| 166 |
accounts = ACCOUNTS.len(), |
| 167 |
client_id = CLIENT_ID, |
| 168 |
redirect_uri = %opts.redirect_uri, |
| 169 |
"example seed: harness accounts and OAuth client seeded (login-capable)" |
| 170 |
); |
| 171 |
Ok(()) |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
async fn seed_account( |
| 183 |
pool: &sqlx::PgPool, |
| 184 |
spec: &AccountSpec, |
| 185 |
password_hash: &str, |
| 186 |
) -> Result<(), SeedError> { |
| 187 |
let email = format!("{}@{EXAMPLE_EMAIL_DOMAIN}", spec.handle.replace('_', "-")); |
| 188 |
sqlx::query( |
| 189 |
r" |
| 190 |
INSERT INTO users ( |
| 191 |
id, username, email, password_hash, display_name, |
| 192 |
can_create_projects, email_verified, creator_tier |
| 193 |
) |
| 194 |
VALUES ($1, $2, $3, $4, $5, $6 IS NOT NULL, TRUE, $6) |
| 195 |
ON CONFLICT (id) DO UPDATE SET |
| 196 |
username = EXCLUDED.username, |
| 197 |
email = EXCLUDED.email, |
| 198 |
password_hash = EXCLUDED.password_hash, |
| 199 |
display_name = EXCLUDED.display_name, |
| 200 |
creator_tier = EXCLUDED.creator_tier, |
| 201 |
can_create_projects = EXCLUDED.can_create_projects, |
| 202 |
-- A harness run that tripped the lockout must not survive the |
| 203 |
-- reseed: the whole point of the reset is a known state. |
| 204 |
failed_login_attempts = 0, |
| 205 |
locked_until = NULL, |
| 206 |
suspended_at = NULL, |
| 207 |
deactivated_at = NULL |
| 208 |
", |
| 209 |
) |
| 210 |
.bind(spec.id) |
| 211 |
.bind(spec.handle) |
| 212 |
.bind(&email) |
| 213 |
.bind(password_hash) |
| 214 |
.bind(spec.display_name) |
| 215 |
.bind(spec.creator_tier) |
| 216 |
.execute(pool) |
| 217 |
.await?; |
| 218 |
|
| 219 |
tracing::info!( |
| 220 |
handle = spec.handle, |
| 221 |
user_id = %spec.id, |
| 222 |
"example seed: harness account" |
| 223 |
); |
| 224 |
Ok(()) |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
async fn seed_fan_plus(pool: &sqlx::PgPool, user_id: Uuid) -> Result<(), SeedError> { |
| 234 |
sqlx::query( |
| 235 |
r" |
| 236 |
INSERT INTO fan_plus_subscriptions ( |
| 237 |
user_id, stripe_subscription_id, stripe_customer_id, status |
| 238 |
) |
| 239 |
VALUES ($1, $2, $3, 'active') |
| 240 |
ON CONFLICT (user_id) DO UPDATE SET |
| 241 |
status = 'active', |
| 242 |
canceled_at = NULL |
| 243 |
", |
| 244 |
) |
| 245 |
.bind(user_id) |
| 246 |
.bind(format!("sub_harness_{user_id}")) |
| 247 |
.bind(format!("cus_harness_{user_id}")) |
| 248 |
.execute(pool) |
| 249 |
.await?; |
| 250 |
Ok(()) |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
async fn seed_client(pool: &sqlx::PgPool, redirect_uri: &str) -> Result<(), SeedError> { |
| 259 |
let existing = db::synckit::get_sync_app_by_api_key(pool, CLIENT_ID).await?; |
| 260 |
let app = match existing { |
| 261 |
Some(app) => app, |
| 262 |
None => { |
| 263 |
db::synckit::create_sync_app( |
| 264 |
pool, |
| 265 |
UserId::from(CREATOR_ACCOUNT_ID), |
| 266 |
CLIENT_NAME, |
| 267 |
CLIENT_ID, |
| 268 |
None, |
| 269 |
None, |
| 270 |
) |
| 271 |
.await? |
| 272 |
} |
| 273 |
}; |
| 274 |
|
| 275 |
sqlx::query("UPDATE sync_apps SET redirect_uris = $2, is_active = TRUE WHERE id = $1") |
| 276 |
.bind(app.id) |
| 277 |
.bind(vec![redirect_uri.to_string()]) |
| 278 |
.execute(pool) |
| 279 |
.await?; |
| 280 |
|
| 281 |
Ok(()) |
| 282 |
} |
| 283 |
|
| 284 |
#[cfg(test)] |
| 285 |
mod tests { |
| 286 |
use super::*; |
| 287 |
|
| 288 |
#[test] |
| 289 |
fn account_ids_are_the_ones_mt_seeds() { |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
assert_eq!( |
| 296 |
FAN_ACCOUNT_ID.to_string(), |
| 297 |
"00000000-0000-0000-0000-00000000f001" |
| 298 |
); |
| 299 |
assert_eq!( |
| 300 |
CREATOR_ACCOUNT_ID.to_string(), |
| 301 |
"00000000-0000-0000-0000-00000000f002" |
| 302 |
); |
| 303 |
assert_eq!( |
| 304 |
OWNER_ACCOUNT_ID.to_string(), |
| 305 |
"00000000-0000-0000-0000-00000000f003" |
| 306 |
); |
| 307 |
} |
| 308 |
|
| 309 |
#[test] |
| 310 |
fn harness_emails_stay_inside_the_reserved_domain() { |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
for spec in ACCOUNTS { |
| 316 |
let email = format!("{}@{EXAMPLE_EMAIL_DOMAIN}", spec.handle.replace('_', "-")); |
| 317 |
assert!(email.ends_with("@example.test"), "{email}"); |
| 318 |
} |
| 319 |
} |
| 320 |
} |
| 321 |
|