Files
visa/src/seed.rs
T
grabowski 2e009700a9
CI / secret scan (push) Successful in 4s
CI / fmt, clippy, tests (push) Successful in 42s
Phase 1: app, Kanidm login, people, passports, addresses, audit log
visa.buildfor.life keeps the immigration records of the foreigners B4L
looks after. This first phase is the base the deadlines and extension
packs build on (docs/PLAN.md, phases 2 to 7).

- One Rust binary (axum 0.8, minijinja, sqlx on PostgreSQL) with its
  templates, static files and migrations compiled in.
- Login through Kanidm (OpenID Connect with PKCE and a nonce, ES256),
  client visa-web, group visa; sessions in PostgreSQL keyed by the
  SHA-256 of the cookie. VISA_INSECURE_NO_LOGIN=1 for loopback
  development only.
- Same-site check and form token on every POST; a CSP without inline
  script or style; security headers.
- People: list with filter, page, create, edit, archive, restore.
- Passports from the machine-readable zone with check digits (ICAO
  9303 TD3); expiry warnings at 12 and 6 months.
- Addresses in Thailand in the order Thai forms use, with the house
  master and the TM.30 receipt.
- Append-only audit log of every view and change, with the names of the
  changed fields but never their values (PDPA); the Activity page.
- Invented demo people (visa seed-demo, refused when login is on).
- 40 tests: unit tests, pages with demo data, the forms, the audit log
  and the whole login against a fake OpenID provider.
- CI: gitleaks over the history, fmt, clippy and the tests against
  PostgreSQL 18; the pre-commit secret scan.
2026-11-17 16:58:48 +07:00

345 lines
8.9 KiB
Rust

//! Demo data for development: invented people only (specimen names, numbers
//! that belong to nobody). `visa seed-demo` refuses to run when the login is
//! on, so it never writes into production.
use anyhow::Result;
use chrono::NaiveDate;
use sqlx::PgPool;
use crate::audit::{self, Action, Entity, Entry};
use crate::dates;
use crate::mrz;
use crate::people::{self, AddressInput, PassportInput, PersonInput};
pub const MARKER: &str = "Demo record, invented for development.";
fn d(s: &str) -> NaiveDate {
dates::parse_date(s).expect("demo dates are valid")
}
struct Demo {
person: PersonInput,
passports: Vec<PassportInput>,
addresses: Vec<AddressInput>,
archived: bool,
}
#[allow(clippy::too_many_arguments)]
fn person(
surname: &str,
first: &str,
middle: &str,
title: &str,
sex: &str,
born: &str,
place: &str,
nationality: &str,
occupation: &str,
employer: &str,
n: u32,
) -> PersonInput {
PersonInput {
surname: surname.into(),
first_name: first.into(),
middle_name: middle.into(),
title: title.into(),
sex: sex.into(),
date_of_birth: Some(d(born)),
place_of_birth: place.into(),
nationality: nationality.into(),
occupation: occupation.into(),
employer: employer.into(),
phone: format!("+66 80 000 000{n}"),
email: format!(
"{}.{}@example.com",
first.to_lowercase(),
surname.to_lowercase()
),
notes: MARKER.into(),
}
}
fn passport(
p: &PersonInput,
number: &str,
issued: &str,
expires: &str,
at: &str,
with_mrz: bool,
) -> PassportInput {
let given = if p.middle_name.is_empty() {
p.first_name.clone()
} else {
format!("{} {}", p.first_name, p.middle_name)
};
let mrz = if with_mrz {
mrz::compose(&mrz::Compose {
issuing_state: if p.nationality == "DEU" {
"D"
} else {
p.nationality.as_str()
},
surname: &p.surname,
given_names: &given,
number,
nationality: if p.nationality == "DEU" {
"D"
} else {
p.nationality.as_str()
},
date_of_birth: p.date_of_birth.expect("demo people have birth dates"),
sex: &p.sex,
expiry_date: d(expires),
})
} else {
String::new()
};
PassportInput {
number: number.into(),
issuing_country: p.nationality.clone(),
issued_on: Some(d(issued)),
expires_on: d(expires),
issued_at: at.into(),
mrz,
}
}
#[allow(clippy::too_many_arguments)]
fn address(
house_no: &str,
moo: &str,
building: &str,
soi: &str,
road: &str,
subdistrict: &str,
district: &str,
province: &str,
postal: &str,
moved_in: &str,
moved_out: Option<&str>,
tm30: Option<(&str, &str)>,
) -> AddressInput {
AddressInput {
house_no: house_no.into(),
moo: moo.into(),
building: building.into(),
soi: soi.into(),
road: road.into(),
subdistrict: subdistrict.into(),
district: district.into(),
province: province.into(),
postal_code: postal.into(),
moved_in_on: Some(d(moved_in)),
moved_out_on: moved_out.map(d),
house_master: "Somchai Jaidee".into(),
house_master_phone: "+66 80 000 0099".into(),
tm30_reported_on: tm30.map(|(date, _)| d(date)),
tm30_receipt_no: tm30.map(|(_, no)| no.to_string()).unwrap_or_default(),
}
}
/// Dates relative to `today`, so the warnings show whenever the seed runs.
fn demos(today: NaiveDate) -> Vec<Demo> {
let plus = |days: i64| (today + chrono::TimeDelta::days(days)).to_string();
let employer = "Demo Engineering Co., Ltd.";
let erika = person(
"Mustermann",
"Erika",
"",
"Ms",
"F",
"1984-08-12",
"Berlin",
"DEU",
"Electrical engineer",
employer,
1,
);
let erika_pp = passport(
&erika,
"C01X00T47",
"2021-08-02",
"2031-08-01",
"Stadt Koeln",
true,
);
let jane = person(
"Doe",
"Jane",
"Marie",
"Ms",
"F",
"1990-02-14",
"Portland, Oregon",
"USA",
"Product designer",
employer,
2,
);
let jane_old = passport(
&jane,
"Z00000017",
"2012-03-01",
"2022-02-28",
"U.S. Department of State",
false,
);
let jane_pp = passport(
&jane,
"Z00000042",
"2017-03-01",
&plus(95),
"U.S. Department of State",
true,
);
let tomas = person(
"Novak",
"Tomas",
"",
"Mr",
"M",
"1979-11-03",
"Brno",
"CZE",
"Accountant",
"Demo Accounting Partnership",
3,
);
let tomas_pp = passport(&tomas, "40000001", "2018-01-15", &plus(300), "Brno", true);
let old = person(
"Sample",
"Oliver",
"",
"Mr",
"M",
"1961-05-30",
"Leeds",
"GBR",
"Retired",
"",
4,
);
let old_pp = passport(&old, "100000001", "2014-01-10", "2024-01-10", "HMPO", false);
vec![
Demo {
person: erika,
passports: vec![erika_pp],
addresses: vec![
address(
"12",
"",
"Nimman Demo Residence",
"9",
"Nimmanhaemin Road",
"Suthep",
"Mueang Chiang Mai",
"Chiang Mai",
"50200",
"2025-06-01",
Some("2026-02-28"),
Some(("2025-06-02", "TM30-DEMO-0007")),
),
address(
"99/9",
"4",
"Baan Suan Demo",
"7",
"Huay Kaew Road",
"Chang Phueak",
"Mueang Chiang Mai",
"Chiang Mai",
"50300",
"2026-03-01",
None,
Some(("2026-03-02", "TM30-DEMO-0012")),
),
],
archived: false,
},
Demo {
person: jane,
passports: vec![jane_old, jane_pp],
addresses: vec![address(
"1/23",
"",
"Demo Condominium, room 512",
"",
"Chotana Road",
"Chang Phueak",
"Mueang Chiang Mai",
"Chiang Mai",
"50300",
"2026-09-15",
None,
None,
)],
archived: false,
},
Demo {
person: tomas,
passports: vec![tomas_pp],
addresses: vec![address(
"55",
"2",
"",
"",
"",
"Ban Klang",
"Mueang Lamphun",
"Lamphun",
"51000",
"2024-11-01",
None,
Some(("2024-11-01", "TM30-DEMO-0003")),
)],
archived: false,
},
Demo {
person: old,
passports: vec![old_pp],
addresses: vec![],
archived: true,
},
]
}
/// Adds the demo people unless they are there already; true when it did.
pub async fn demo(db: &PgPool, today: NaiveDate) -> Result<bool> {
let present: bool = sqlx::query_scalar("SELECT EXISTS (SELECT 1 FROM people WHERE notes = $1)")
.bind(MARKER)
.fetch_one(db)
.await?;
if present {
return Ok(false);
}
let mut tx = db.begin().await?;
let log = |action, entity, id, person| Entry {
actor: "seed",
action,
entity,
entity_id: Some(id),
person_id: Some(person),
fields: &[],
};
for demo in demos(today) {
let id = people::create_person(&mut tx, &demo.person).await?;
audit::record(&mut *tx, &log(Action::Create, Entity::Person, id, id)).await?;
for p in &demo.passports {
let pid = people::create_passport(&mut tx, id, p).await?;
audit::record(&mut *tx, &log(Action::Create, Entity::Passport, pid, id)).await?;
}
for a in &demo.addresses {
let aid = people::create_address(&mut tx, id, a).await?;
audit::record(&mut *tx, &log(Action::Create, Entity::Address, aid, id)).await?;
}
if demo.archived {
people::set_archived(&mut tx, id, true).await?;
audit::record(&mut *tx, &log(Action::Archive, Entity::Person, id, id)).await?;
}
}
tx.commit().await?;
Ok(true)
}