hecks 3.7.0 → 3.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/hecks/bluebook/meta_validator/modes.rb +14 -0
- data/lib/hecks/bluebook/meta_validator.rb +4 -2
- data/lib/hecks/corpus/routes.rb +1 -1
- data/lib/hecks/gate/stages.yml +11 -1
- data/lib/hecks/hecks/adapters/site_toolchain/prober.rb +64 -0
- data/lib/hecks/hecks/adapters/site_toolchain.rb +41 -5
- data/lib/hecks/language/bluebook/vocabulary.bluebook +1 -1
- data/lib/hecks/projections/deploy/shared_database_instance/templates/rds.yaml.tmpl +1 -1
- data/lib/hecks/projections/deploy/templates/lambda/template.yaml.tmpl +1 -1
- data/lib/hecks/projections/site/probe.rb +131 -0
- data/lib/hecks/projections/site/site_host/dockerfile.rb +1 -1
- data/lib/hecks/runtime/value/validation.rb +34 -3
- data/lib/hecks/site/bluebook/site.bluebook +31 -0
- data/lib/hecks/site/bluebook/site.ports.hecksagon +6 -0
- data/lib/hecks/version.rb +1 -1
- data/lib/hecks/vocabulary.rb +1 -1
- data/prebuilt/{verdicts-be73f0df9eee0abe2f4aadc968321c4b919d19d424a3ccfe1777a71dabf7d9e7.json → verdicts-91aaf93ce8981bd9a754948f075d95650dc2ec6435015376f5c60992b36a24b1.json} +1 -1
- data/rust/codegen/src/fielded.rs +3 -3
- data/rust/codegen/src/json_codec.rs +2 -2
- data/rust/codegen/src/naming.rs +20 -0
- data/rust/host/Cargo.toml +4 -0
- data/rust/host/HECKS_RELEASE +1 -1
- data/rust/host/src/boot.rs +443 -0
- data/rust/host/src/boundary_fuzz/ir.rs +1 -0
- data/rust/host/src/commerce_ir.rs +459 -0
- data/rust/host/src/extension.rs +100 -0
- data/rust/host/src/ir.rs +0 -448
- data/rust/host/src/journal.rs +1 -1
- data/rust/host/src/lib.rs +40 -0
- data/rust/host/src/main.rs +4 -477
- data/rust/host/src/mint.rs +1 -1
- data/rust/host/src/payments/tests.rs +8 -8
- data/rust/host/src/payments.rs +10 -10
- data/rust/host/src/rate_limit.rs +27 -41
- data/rust/host/src/secrets.rs +15 -0
- data/rust/host/src/web/commerce.rs +121 -0
- data/rust/host/src/web/newsletter.rs +2 -1
- data/rust/host/src/web/newsletter_send.rs +1 -1
- data/rust/host/src/web/registration_receipt.rs +2 -2
- data/rust/host/src/web/registrations.rs +49 -49
- data/rust/host/src/web/resend_webhook.rs +300 -0
- data/rust/host/src/web.rs +43 -47
- metadata +13 -5
- /data/prebuilt/{a0818e2de67364dc1ab22868b6cbf869c62da6f8d9b924c778fa5cce2e76ee94.marshal → 0ee7f83cba59ce7d1c45c65ba166b071779f70143449f584f33ff7989f64eccb.marshal} +0 -0
- /data/prebuilt/{945481eefbdb584649d45f256307053558966a868b9c38dee7ee88f8169610f8.marshal → 6e4a6bcf0aa880e2e6b056e1846b70e5a768090b9dc998bbadeaf5d6bcfd3db8.marshal} +0 -0
data/rust/codegen/src/fielded.rs
CHANGED
|
@@ -30,7 +30,7 @@ pub fn emit_fielded_flat(exemplar: &Exemplar, struct_name: &str, attributes: &[J
|
|
|
30
30
|
let scalar = scalar.unwrap();
|
|
31
31
|
Some(exemplar.render(
|
|
32
32
|
"fielded_arm_optional_scalar",
|
|
33
|
-
&[("\"tmpl_field\"", naming::ruby_inspect_string(&key)), ("tmpl_ident", ident.clone()), ("tmpl_value_expr_placeholder(v)", naming::scalar_to_value(scalar, "v").unwrap())],
|
|
33
|
+
&[("\"tmpl_field\"", naming::ruby_inspect_string(&key)), ("tmpl_ident", ident.clone()), ("tmpl_value_expr_placeholder(v)", naming::scalar_to_value(scalar, &naming::deref_scalar(scalar, "v")).unwrap())],
|
|
34
34
|
))
|
|
35
35
|
} else if optional {
|
|
36
36
|
if is_fielded_nested(crate::attr::type_name(attr), value_objects_by_name) {
|
|
@@ -97,7 +97,7 @@ pub fn emit_fielded_record(exemplar: &Exemplar, aggregate: &Json, value_objects_
|
|
|
97
97
|
} else if let Some(scalar) = scalar {
|
|
98
98
|
Some(exemplar.render(
|
|
99
99
|
"fielded_arm_optional_scalar",
|
|
100
|
-
&[("\"tmpl_field\"", naming::ruby_inspect_string(&key)), ("tmpl_ident", ident.clone()), ("tmpl_value_expr_placeholder(v)", naming::scalar_to_value(scalar, "v").unwrap())],
|
|
100
|
+
&[("\"tmpl_field\"", naming::ruby_inspect_string(&key)), ("tmpl_ident", ident.clone()), ("tmpl_value_expr_placeholder(v)", naming::scalar_to_value(scalar, &naming::deref_scalar(scalar, "v")).unwrap())],
|
|
101
101
|
))
|
|
102
102
|
} else if is_fielded_nested(crate::attr::type_name(attr), value_objects_by_name) {
|
|
103
103
|
Some(exemplar.render("fielded_arm_optional_nested", &[("\"tmpl_field\"", naming::ruby_inspect_string(&key)), ("tmpl_ident", ident)]))
|
|
@@ -184,7 +184,7 @@ fn items_arms(exemplar: &Exemplar, attributes: &[Json], value_objects_by_name: &
|
|
|
184
184
|
let shape = format!("fielded_items_arm_list_{}{}", if optional(attr) { "optional_" } else { "" }, if scalar.is_some() { "scalar" } else { "nested" });
|
|
185
185
|
let mut subs: Vec<(&str, String)> = vec![("\"tmpl_field\"", naming::ruby_inspect_string(&key)), ("tmpl_ident", ident)];
|
|
186
186
|
if let Some(scalar) = scalar {
|
|
187
|
-
subs.push(("tmpl_value_expr_placeholder(v)", naming::scalar_to_value(scalar, "v").unwrap()));
|
|
187
|
+
subs.push(("tmpl_value_expr_placeholder(v)", naming::scalar_to_value(scalar, &naming::deref_scalar(scalar, "v")).unwrap()));
|
|
188
188
|
}
|
|
189
189
|
arms.push(format!(" {}", exemplar.render(&shape, &subs)));
|
|
190
190
|
}
|
|
@@ -130,7 +130,7 @@ pub fn list_element_from_json_mapper(struct_name: &str, key: &str, attr: &Json,
|
|
|
130
130
|
/// Inverse of `list_element_from_json_mapper`: scalar elements have no `.to_json()` method.
|
|
131
131
|
pub fn list_element_to_json_expr(attr: &Json) -> String {
|
|
132
132
|
match naming::effective_scalar_type(crate::attr::type_name(attr)) {
|
|
133
|
-
Some(scalar) => scalar_to_json_expr(scalar, "x"),
|
|
133
|
+
Some(scalar) => scalar_to_json_expr(scalar, &naming::deref_scalar(scalar, "x")),
|
|
134
134
|
None => "x.to_json()".to_string(),
|
|
135
135
|
}
|
|
136
136
|
}
|
|
@@ -392,7 +392,7 @@ pub fn emit_to_json_flat(exemplar: &Exemplar, struct_name: &str, attributes: &[J
|
|
|
392
392
|
} else if list {
|
|
393
393
|
format!("crate::kernel::Json::Array(self.{ident}.iter().map(|x| {}).collect())", elem_to_json.as_deref().unwrap())
|
|
394
394
|
} else if field_optional && scalar.is_some() {
|
|
395
|
-
format!("self.{ident}.as_ref().map(|v| {}).unwrap_or(crate::kernel::Json::Null)", scalar_to_json_expr(scalar.unwrap(), "v"))
|
|
395
|
+
format!("self.{ident}.as_ref().map(|v| {}).unwrap_or(crate::kernel::Json::Null)", scalar_to_json_expr(scalar.unwrap(), &naming::deref_scalar(scalar.unwrap(), "v")))
|
|
396
396
|
} else if field_optional {
|
|
397
397
|
format!("self.{ident}.as_ref().map(|v| v.to_json()).unwrap_or(crate::kernel::Json::Null)")
|
|
398
398
|
} else if let Some(scalar) = scalar {
|
data/rust/codegen/src/naming.rs
CHANGED
|
@@ -248,6 +248,16 @@ pub fn screaming_snake(name: &str) -> String {
|
|
|
248
248
|
out.to_uppercase()
|
|
249
249
|
}
|
|
250
250
|
|
|
251
|
+
/// The expression for a scalar reached through a reference (`v` in `as_ref().map(|v| ..)`, `x` in
|
|
252
|
+
/// `iter().map(|x| ..)`): the copy types are dereferenced, since `Value::Int(v)` takes an `i64`, not an
|
|
253
|
+
/// `&i64`; a `String` is left to the `.clone()` the scalar helpers already apply.
|
|
254
|
+
pub fn deref_scalar(type_name: &str, ident: &str) -> String {
|
|
255
|
+
match type_name {
|
|
256
|
+
"Integer" | "Float" | "TrueClass" | "FalseClass" => format!("*{ident}"),
|
|
257
|
+
_ => ident.to_string(),
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
251
261
|
pub fn scalar_to_value(type_name: &str, rust_expr: &str) -> Option<String> {
|
|
252
262
|
match type_name {
|
|
253
263
|
"String" => Some(format!("Value::Str({rust_expr}.clone())")),
|
|
@@ -316,6 +326,16 @@ pub fn ruby_inspect_string(s: &str) -> String {
|
|
|
316
326
|
mod tests {
|
|
317
327
|
use super::*;
|
|
318
328
|
|
|
329
|
+
#[test]
|
|
330
|
+
fn a_scalar_reached_through_a_reference_is_dereferenced_when_it_is_a_copy_type() {
|
|
331
|
+
// `self.level.as_ref().map(|v| Field::Value(Value::Int(v)))` does not compile: `v` is `&i64`.
|
|
332
|
+
assert_eq!(scalar_to_value("Integer", &deref_scalar("Integer", "v")).as_deref(), Some("Value::Int(*v)"));
|
|
333
|
+
assert_eq!(scalar_to_value("Float", &deref_scalar("Float", "v")).as_deref(), Some("Value::Float(*v)"));
|
|
334
|
+
assert_eq!(scalar_to_value("TrueClass", &deref_scalar("TrueClass", "v")).as_deref(), Some("Value::Bool(*v)"));
|
|
335
|
+
// A String is cloned, so the reference is left alone.
|
|
336
|
+
assert_eq!(scalar_to_value("String", &deref_scalar("String", "v")).as_deref(), Some("Value::Str(v.clone())"));
|
|
337
|
+
}
|
|
338
|
+
|
|
319
339
|
#[test]
|
|
320
340
|
fn refuses_an_aggregate_whose_module_name_is_a_rust_keyword() {
|
|
321
341
|
let refusal = reserved_name_refusal("shop", "shop", &["Pizza", "Match", "Type"]).expect("refused");
|
data/rust/host/Cargo.toml
CHANGED
data/rust/host/HECKS_RELEASE
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
3.
|
|
1
|
+
3.9.1
|
|
@@ -0,0 +1,443 @@
|
|
|
1
|
+
//! Lambda custom-runtime entry point (or a long-lived server under
|
|
2
|
+
//! HECKS_SERVE_MODE=1): boots Postgres/wasm state, then dispatches events.
|
|
3
|
+
|
|
4
|
+
use crate::{approval, auth, extension, ir, journal, lambda_client, log, mint, rate_limit, secrets, server, storage_shape};
|
|
5
|
+
|
|
6
|
+
use lambda_runtime::{service_fn, Error, LambdaEvent};
|
|
7
|
+
use std::path::PathBuf;
|
|
8
|
+
use std::sync::Arc;
|
|
9
|
+
use tokio::sync::Mutex;
|
|
10
|
+
|
|
11
|
+
/// Boots the host: secrets, Postgres, wasm, era minting, then the Lambda runtime loop (or the
|
|
12
|
+
/// long-lived server under HECKS_SERVE_MODE=1). Extensions are read from `extension::installed()`,
|
|
13
|
+
/// so a binary that wants its own must `extension::install` them before calling this.
|
|
14
|
+
pub async fn run() -> Result<(), Error> {
|
|
15
|
+
// HECKS_SERVE_MODE=1 switches the boot into the long-lived server
|
|
16
|
+
// loop below instead of the Lambda custom-runtime loop; every step
|
|
17
|
+
// from here through the era-minting sequence is shared, unchanged,
|
|
18
|
+
// by both modes.
|
|
19
|
+
let boot_started = std::time::Instant::now();
|
|
20
|
+
let serve_mode = std::env::var("HECKS_SERVE_MODE").as_deref() == Ok("1");
|
|
21
|
+
|
|
22
|
+
// HECKS_SESSION_COOKIE names the account cookie; refuse the boot
|
|
23
|
+
// here if it can't be written into a Set-Cookie header, rather
|
|
24
|
+
// than falling back silently at request time.
|
|
25
|
+
auth::resolve_account_cookie(std::env::var("HECKS_SESSION_COOKIE").ok().as_deref())?;
|
|
26
|
+
|
|
27
|
+
// DB_SECRET_ARN: the password is fetched from Secrets Manager here,
|
|
28
|
+
// at cold start, rather than trusted from a plaintext
|
|
29
|
+
// Environment.Variables value, which any principal with
|
|
30
|
+
// lambda:GetFunctionConfiguration could read regardless of the
|
|
31
|
+
// secret-at-rest protection its name suggests. Falls back to
|
|
32
|
+
// DATABASE_URL directly when DB_SECRET_ARN is absent (manual
|
|
33
|
+
// debugging over an SSM tunnel).
|
|
34
|
+
//
|
|
35
|
+
// The fetcher built here is reused below for GOOGLE_OAUTH_SECRET_ID
|
|
36
|
+
// and SESSION_SECRET_ARN too.
|
|
37
|
+
let secret_fetcher = secrets::AwsSecretFetcher::from_env().await;
|
|
38
|
+
let database_url = match std::env::var("DB_SECRET_ARN") {
|
|
39
|
+
Ok(secret_arn) => {
|
|
40
|
+
let db_host = std::env::var("DB_HOST").map_err(|_| "DB_HOST is required when DB_SECRET_ARN is set")?;
|
|
41
|
+
let db_name = std::env::var("DB_NAME").map_err(|_| "DB_NAME is required when DB_SECRET_ARN is set")?;
|
|
42
|
+
let secret_json = secret_fetcher
|
|
43
|
+
.fetch_secret_string(&secret_arn)
|
|
44
|
+
.await
|
|
45
|
+
.map_err(|e| format!("fetching DB_SECRET_ARN from Secrets Manager: {e:#}"))?;
|
|
46
|
+
// The user and port come from the secret too (a client sharing an instance logs in as
|
|
47
|
+
// its own role); the password goes in literal, since parse_database_url below never
|
|
48
|
+
// percent-decodes it.
|
|
49
|
+
secrets::database_url(&secret_json, &db_host, &db_name)?
|
|
50
|
+
}
|
|
51
|
+
Err(_) => std::env::var("DATABASE_URL")
|
|
52
|
+
.map_err(|_| "either DB_SECRET_ARN (+ DB_HOST/DB_NAME) or DATABASE_URL is required")?,
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
// GOOGLE_OAUTH_SECRET_ID/SESSION_SECRET_ARN: the same
|
|
56
|
+
// Secrets-Manager-at-cold-start approach as DB_SECRET_ARN above,
|
|
57
|
+
// applied to auth.rs's GOOGLE_CLIENT_ID/GOOGLE_CLIENT_SECRET and
|
|
58
|
+
// web.rs's SESSION_SECRET.
|
|
59
|
+
//
|
|
60
|
+
// Safety: this runs before any request is dispatched, so nothing
|
|
61
|
+
// else reads or writes the environment concurrently with these
|
|
62
|
+
// `set_var` calls.
|
|
63
|
+
if let Ok(oauth_secret_id) = std::env::var("GOOGLE_OAUTH_SECRET_ID") {
|
|
64
|
+
let secret_json = secret_fetcher
|
|
65
|
+
.fetch_secret_string(&oauth_secret_id)
|
|
66
|
+
.await
|
|
67
|
+
.map_err(|e| format!("fetching GOOGLE_OAUTH_SECRET_ID from Secrets Manager: {e:#}"))?;
|
|
68
|
+
let client_id = secrets::extract_field(&secret_json, "client_id")?;
|
|
69
|
+
let client_secret = secrets::extract_field(&secret_json, "client_secret")?;
|
|
70
|
+
unsafe {
|
|
71
|
+
std::env::set_var("GOOGLE_CLIENT_ID", client_id);
|
|
72
|
+
std::env::set_var("GOOGLE_CLIENT_SECRET", client_secret);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
if let Ok(session_secret_arn) = std::env::var("SESSION_SECRET_ARN") {
|
|
76
|
+
let secret_json = secret_fetcher
|
|
77
|
+
.fetch_secret_string(&session_secret_arn)
|
|
78
|
+
.await
|
|
79
|
+
.map_err(|e| format!("fetching SESSION_SECRET_ARN from Secrets Manager: {e:#}"))?;
|
|
80
|
+
let session_secret = secrets::extract_field(&secret_json, "session_secret")?;
|
|
81
|
+
unsafe {
|
|
82
|
+
std::env::set_var("SESSION_SECRET", session_secret);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
for extension in extension::installed() {
|
|
86
|
+
extension.boot_secrets(&secret_fetcher).await;
|
|
87
|
+
}
|
|
88
|
+
let wasm_path = PathBuf::from(
|
|
89
|
+
std::env::var("HECKS_WASM_PATH").unwrap_or_else(|_| "banking.wasm".to_string()),
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
// Must run before the Postgres connection opens -- `SET search_path`
|
|
93
|
+
// below has to run before `journal::ensure_schema` or anything else
|
|
94
|
+
// touches the connection, or unqualified names resolve against
|
|
95
|
+
// Postgres's default search_path instead of this domain's.
|
|
96
|
+
// HECKS_SCHEMA is optional.
|
|
97
|
+
let domain = std::env::var("HECKS_DOMAIN").map_err(|_| "HECKS_DOMAIN is required")?;
|
|
98
|
+
let schema = std::env::var("HECKS_SCHEMA").ok().filter(|s| !s.is_empty());
|
|
99
|
+
// Checkout on AWS keeps the business's payment keys in a named Secrets
|
|
100
|
+
// Manager secret; refuse before touching the database when none is named.
|
|
101
|
+
for extension in extension::installed() {
|
|
102
|
+
extension.boot_check(&domain)?;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// RDS refuses a plain NoTls connection by default and needs AWS's
|
|
106
|
+
// own RDS CA specifically, not a generic public bundle (see
|
|
107
|
+
// Cargo.toml).
|
|
108
|
+
let mut roots = rustls::RootCertStore::empty();
|
|
109
|
+
for cert in rustls_pemfile::certs(&mut include_bytes!("../rds-ca-bundle.pem").as_slice()) {
|
|
110
|
+
roots.add(cert.map_err(|e| format!("parsing rds-ca-bundle.pem: {e}"))?)?;
|
|
111
|
+
}
|
|
112
|
+
let tls_config = rustls::ClientConfig::builder()
|
|
113
|
+
.with_root_certificates(roots)
|
|
114
|
+
.with_no_client_auth();
|
|
115
|
+
let tls = tokio_postgres_rustls::MakeRustlsConnect::new(tls_config);
|
|
116
|
+
// Not tokio_postgres::connect(url, tls) -- that treats DATABASE_URL
|
|
117
|
+
// as a strict URI, and RDS-generated passwords can contain
|
|
118
|
+
// URI-reserved `?`/`#` that CloudFormation never percent-encodes.
|
|
119
|
+
let mut config = parse_database_url(&database_url)?;
|
|
120
|
+
// RDS requires TLS (rds-ca-bundle.pem); local Postgres over loopback
|
|
121
|
+
// can't be verified against the RDS CA, so localhost uses NoTls.
|
|
122
|
+
let local_postgres = database_url_is_local(&config);
|
|
123
|
+
if !local_postgres {
|
|
124
|
+
config.ssl_mode(tokio_postgres::config::SslMode::Require);
|
|
125
|
+
}
|
|
126
|
+
// NoTlsStream and RustlsStream can't unify in one branch, so the
|
|
127
|
+
// two paths are spawned separately.
|
|
128
|
+
let connect_phase = log::phase_with("db_connect", serde_json::json!({ "tls": !local_postgres }));
|
|
129
|
+
let client = if local_postgres {
|
|
130
|
+
let (client, connection) = config
|
|
131
|
+
.connect(tokio_postgres::NoTls)
|
|
132
|
+
.await
|
|
133
|
+
.map_err(|e| format!("connecting to Postgres via DATABASE_URL: {e:?}"))?;
|
|
134
|
+
tokio::spawn(async move {
|
|
135
|
+
if let Err(e) = connection.await {
|
|
136
|
+
eprintln!("postgres connection error: {e:#}");
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
client
|
|
140
|
+
} else {
|
|
141
|
+
let (client, connection) = config
|
|
142
|
+
.connect(tls)
|
|
143
|
+
.await
|
|
144
|
+
.map_err(|e| format!("connecting to Postgres via DATABASE_URL: {e:?}"))?;
|
|
145
|
+
tokio::spawn(async move {
|
|
146
|
+
if let Err(e) = connection.await {
|
|
147
|
+
eprintln!("postgres connection error: {e:#}");
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
client
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
connect_phase.end();
|
|
154
|
+
|
|
155
|
+
// Every unqualified table/view reference this binary issues
|
|
156
|
+
// resolves through search_path, so this is what makes a shared
|
|
157
|
+
// instance's per-domain schemas transparent to the rest of the
|
|
158
|
+
// binary.
|
|
159
|
+
//
|
|
160
|
+
// CREATE SCHEMA before SET search_path: setting search_path to a
|
|
161
|
+
// schema that doesn't exist yet succeeds in Postgres, so a missing
|
|
162
|
+
// schema only surfaces later as a confusing "no schema has been
|
|
163
|
+
// selected to create in" error from `ensure_schema`. Idempotent: an
|
|
164
|
+
// existing schema is the ordinary case on every boot after the first.
|
|
165
|
+
if let Some(schema) = &schema {
|
|
166
|
+
client
|
|
167
|
+
.batch_execute(&format!("CREATE SCHEMA IF NOT EXISTS {}", journal::quote_ident(schema)))
|
|
168
|
+
.await
|
|
169
|
+
.map_err(|e| format!("creating schema {schema:?}: {e:#}"))?;
|
|
170
|
+
client
|
|
171
|
+
.batch_execute(&format!("SET search_path TO {}", journal::quote_ident(schema)))
|
|
172
|
+
.await
|
|
173
|
+
.map_err(|e| format!("setting search_path to {schema:?}: {e:#}"))?;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
let phase = log::phase("ensure_schema");
|
|
177
|
+
journal::ensure_schema(&client)
|
|
178
|
+
.await
|
|
179
|
+
.map_err(|e| format!("provisioning hecks_lambda_journal: {e:#}"))?;
|
|
180
|
+
phase.end();
|
|
181
|
+
|
|
182
|
+
let ir = ir::ir().ok_or("HECKS_IR_PATH is not set or unreadable — this binary needs its own domain's ir.json sidecar")?;
|
|
183
|
+
ir::refuse_unsupported_persistence_adapters(ir)?;
|
|
184
|
+
let my_hash = storage_shape::mint_hash(ir);
|
|
185
|
+
let my_label = storage_shape::mint_label(ir);
|
|
186
|
+
|
|
187
|
+
let mut aggregates: Vec<mint::Aggregate> = ir::lineage_capable_aggregates(ir)
|
|
188
|
+
.into_iter()
|
|
189
|
+
.map(|(qualified, storage_name)| {
|
|
190
|
+
let name = qualified.rsplit("::").next().unwrap_or(&qualified).to_string();
|
|
191
|
+
mint::Aggregate { name, storage_name }
|
|
192
|
+
})
|
|
193
|
+
.collect();
|
|
194
|
+
// A chapter that `provides "membership"` names who may sign in; that
|
|
195
|
+
// aggregate can be vendored, so it may be missing from this domain's
|
|
196
|
+
// lineage-capable list. Mint its head anyway for Google-auth to read.
|
|
197
|
+
if let Some(membership) = ir::membership_provider(ir) {
|
|
198
|
+
let name = membership.aggregate.rsplit("::").next().unwrap_or(&membership.aggregate).to_string();
|
|
199
|
+
let storage_name = journal::snake(&name);
|
|
200
|
+
if !aggregates.iter().any(|a| a.storage_name == storage_name) {
|
|
201
|
+
aggregates.push(mint::Aggregate { name, storage_name });
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// The era-partitioned lineage subsystem is provisioned only when
|
|
206
|
+
// something needs it: a lineage-capable aggregate, or Google auth
|
|
207
|
+
// configured (ADR 0034 requires it for Member sessions either way).
|
|
208
|
+
let needs_lineage = !aggregates.is_empty() || std::env::var("GOOGLE_CLIENT_ID").is_ok();
|
|
209
|
+
|
|
210
|
+
let era: Option<i32> = if needs_lineage {
|
|
211
|
+
// Idempotent (gated by `provisioner?`); must run before the
|
|
212
|
+
// first `journal::held_eras` call, since `hecks_eras` doesn't
|
|
213
|
+
// exist yet on a truly fresh Postgres instance.
|
|
214
|
+
let phase = log::phase("ensure_base");
|
|
215
|
+
mint::ensure_base(&client, &domain).await.map_err(|e| format!("provisioning hecks_eras for {domain}: {e:#}"))?;
|
|
216
|
+
phase.end();
|
|
217
|
+
|
|
218
|
+
// Compares this binary's own shape hash (`storage_shape`, over
|
|
219
|
+
// `ir::ir()`) against held eras to decide the boot action: boot
|
|
220
|
+
// at a matching era, mint era 1 if the domain is new, or mint
|
|
221
|
+
// the next era if it has drifted -- refusing toward
|
|
222
|
+
// `hecks scaffold_translation` (no edge covers it) or
|
|
223
|
+
// `hecks audit_translation --approve` (needs review) otherwise.
|
|
224
|
+
let era_phase = log::phase("era_resolve");
|
|
225
|
+
let held = journal::held_eras(&client, &domain).await.map_err(|e| format!("checking hecks_eras for {domain}: {e:#}"))?;
|
|
226
|
+
|
|
227
|
+
// The decision itself is a pure, unit-tested function
|
|
228
|
+
// (`mint::decide_boot_action`); everything below just carries
|
|
229
|
+
// out whichever of its four outcomes came back.
|
|
230
|
+
let decision = mint::decide_boot_action(&held, &my_label);
|
|
231
|
+
era_phase.end_with(serde_json::json!({ "held_eras": held.len(), "decision": decision.name() }));
|
|
232
|
+
let ordinal: i32 = match decision {
|
|
233
|
+
// Adopting an existing era still provisions what this binary
|
|
234
|
+
// is about to write into, the same as minting does --
|
|
235
|
+
// skipping it would let a matched-era host write into
|
|
236
|
+
// head-snapshot tables that were never created.
|
|
237
|
+
mint::BootDecision::UseExisting { ordinal } => {
|
|
238
|
+
let phase = log::phase_with("adopt_head_snapshots", serde_json::json!({ "era": ordinal }));
|
|
239
|
+
mint::adopt_head_snapshots(&client, &domain, &aggregates, ordinal)
|
|
240
|
+
.await
|
|
241
|
+
.map_err(|e| format!("adopting era {ordinal} of {domain}: {e:#}"))?;
|
|
242
|
+
phase.end();
|
|
243
|
+
ordinal
|
|
244
|
+
}
|
|
245
|
+
mint::BootDecision::HoldFirst => {
|
|
246
|
+
let source_text =
|
|
247
|
+
ir.get("source_text").and_then(serde_json::Value::as_str).ok_or("ir.json is missing source_text — regenerate with hecks project_rust")?;
|
|
248
|
+
let phase = log::phase_with("hold_first", serde_json::json!({ "era": 1 }));
|
|
249
|
+
mint::hold_first(&client, &domain, source_text, ir, &aggregates, None).await.map_err(|e| format!("minting era 1 of {domain}: {e:#}"))?;
|
|
250
|
+
phase.end();
|
|
251
|
+
1
|
|
252
|
+
}
|
|
253
|
+
mint::BootDecision::LatestUnnamed { ordinal } => {
|
|
254
|
+
return Err(format!(
|
|
255
|
+
"cannot boot: {domain}'s latest held era (ordinal {ordinal}) has no name yet -- this crate can't \
|
|
256
|
+
name it itself (no bluebook parser, ADR 0012's own boundary) -- boot Ruby once against this \
|
|
257
|
+
database to name it (Runtime::EraCheck's own lazy-naming path), then redeploy",
|
|
258
|
+
)
|
|
259
|
+
.into());
|
|
260
|
+
}
|
|
261
|
+
mint::BootDecision::Mint { ordinal, from_ordinal, from_label } => {
|
|
262
|
+
let mut labels: Vec<String> = held.iter().map(|held_era| held_era.label.clone().unwrap_or_default()).collect();
|
|
263
|
+
labels.push(my_label.clone());
|
|
264
|
+
let edges = mint::parse_edges(ir, &domain);
|
|
265
|
+
let chain = mint::edge_chain(&edges, &labels).map_err(|e| {
|
|
266
|
+
format!(
|
|
267
|
+
"cannot boot: {domain} has drifted from era {from_ordinal} ({from_label}) to a shape \
|
|
268
|
+
({my_label}) no translation edge covers -- {e} -- run hecks scaffold_translation, review it \
|
|
269
|
+
with hecks audit_translation, then redeploy",
|
|
270
|
+
)
|
|
271
|
+
})?;
|
|
272
|
+
|
|
273
|
+
// Checks every edge in the chain with a compute/rekey
|
|
274
|
+
// rule, not just the newest -- a chain can carry more
|
|
275
|
+
// than one, matched against the raw ir.json edge
|
|
276
|
+
// (digest-relevant shape) by the same from/to pair.
|
|
277
|
+
let raw_edges = ir.get("translations").and_then(serde_json::Value::as_array).cloned().unwrap_or_default();
|
|
278
|
+
let committed_approvals = approval::committed(ir);
|
|
279
|
+
let approval_phase = log::phase_with("approval_check", serde_json::json!({ "edges": chain.len(), "committed": committed_approvals.len() }));
|
|
280
|
+
for edge in &chain {
|
|
281
|
+
let Some(raw_edge) = raw_edges.iter().find(|candidate| {
|
|
282
|
+
candidate.get("domain").and_then(serde_json::Value::as_str) == Some(domain.as_str())
|
|
283
|
+
&& candidate.get("from").and_then(serde_json::Value::as_str) == Some(edge.from.as_str())
|
|
284
|
+
&& candidate.get("to").and_then(serde_json::Value::as_str) == Some(edge.to.as_str())
|
|
285
|
+
}) else {
|
|
286
|
+
return Err(format!("cannot boot: {domain}'s edge {} -> {} vanished between parsing and approval-checking it", edge.from, edge.to).into());
|
|
287
|
+
};
|
|
288
|
+
approval::check(&client, &domain, raw_edge, ordinal, &committed_approvals).await.map_err(|e| format!("{e:#}"))?;
|
|
289
|
+
}
|
|
290
|
+
approval_phase.end();
|
|
291
|
+
|
|
292
|
+
// Runs before anything is minted, over the live compiled
|
|
293
|
+
// chain (plain SELECTs, never a persisted matview), so a
|
|
294
|
+
// refusal leaves no half-born era.
|
|
295
|
+
let watermarks: std::collections::HashMap<i32, Option<i64>> = held.iter().map(|held_era| (held_era.ordinal, held_era.watermark)).collect();
|
|
296
|
+
let audit_phase = log::phase_with("audit_before_mint", serde_json::json!({ "era": ordinal, "edges": chain.len() }));
|
|
297
|
+
mint::audit_before_mint(&client, &domain, ir, &aggregates, ordinal, &chain, &raw_edges, &watermarks)
|
|
298
|
+
.await
|
|
299
|
+
.map_err(|e| format!("{e:#}"))?;
|
|
300
|
+
audit_phase.end();
|
|
301
|
+
|
|
302
|
+
let held_text = ir.get("source_text").and_then(serde_json::Value::as_str).ok_or("ir.json is missing source_text — regenerate with hecks project_rust")?;
|
|
303
|
+
let mint_phase = log::phase_with("mint_era", serde_json::json!({ "era": ordinal, "edges": chain.len() }));
|
|
304
|
+
mint::mint_era(&client, &domain, ordinal, &my_hash, &my_label, held_text, &aggregates, &chain, None, &mint::lifecycle_defaults(ir))
|
|
305
|
+
.await
|
|
306
|
+
.map_err(|e| format!("minting era {ordinal} of {domain}: {e:#}"))?;
|
|
307
|
+
mint_phase.end();
|
|
308
|
+
ordinal
|
|
309
|
+
}
|
|
310
|
+
};
|
|
311
|
+
Some(ordinal)
|
|
312
|
+
} else {
|
|
313
|
+
None
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
// The mirrored set is the IR's own capable list, by qualified name
|
|
317
|
+
// -- never "everything this kernel can mutate". A domain that binds
|
|
318
|
+
// everything to plain `Postgres` mirrors nothing.
|
|
319
|
+
let mirrored = ir::mirrored_aggregates(ir);
|
|
320
|
+
let lineage_config = Arc::new(journal::LineageConfig { domain, era, mirrored: Some(mirrored) });
|
|
321
|
+
|
|
322
|
+
// Mutex, not a bare Arc<Client> -- dispatch::handle needs
|
|
323
|
+
// Client::transaction (&mut Client) to hold the advisory lock
|
|
324
|
+
// across the rehydrate-then-append sequence (see dispatch.rs).
|
|
325
|
+
let client = Arc::new(Mutex::new(client));
|
|
326
|
+
let wasm_path = Arc::new(wasm_path);
|
|
327
|
+
// One invoker for this process's lifetime -- credentials/region
|
|
328
|
+
// resolve once at boot, not per invocation. See lambda_client.rs:
|
|
329
|
+
// the compiled `.wasm` module can't dispatch this itself (no
|
|
330
|
+
// network inside that sandbox).
|
|
331
|
+
let invoker = Arc::new(lambda_client::AwsLambdaInvoker::from_env().await);
|
|
332
|
+
|
|
333
|
+
// `role` is optional, mirroring `args`: an absent key means no
|
|
334
|
+
// caller asserted a role, and `dispatch::handle`'s `check_role`
|
|
335
|
+
// stays on its unchecked path. Read by `server::dispatch_body` below.
|
|
336
|
+
if serve_mode {
|
|
337
|
+
log::info(
|
|
338
|
+
"boot",
|
|
339
|
+
serde_json::json!({ "mode": "serve", "domain": &lineage_config.domain, "era": &my_label, "boot_ms": boot_started.elapsed().as_millis() as u64 }),
|
|
340
|
+
);
|
|
341
|
+
let version = server::version_body(&my_label, &my_hash, std::env::var("HECKS_BUILD").ok().as_deref());
|
|
342
|
+
let limits = Arc::new(rate_limit::RateLimits::from_env());
|
|
343
|
+
let state = server::ServerState { client, wasm_path, lineage_config, invoker, limits };
|
|
344
|
+
return server::serve(state, version).await;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
lambda_runtime::run(service_fn(move |event: LambdaEvent<serde_json::Value>| {
|
|
348
|
+
let client = Arc::clone(&client);
|
|
349
|
+
let wasm_path = Arc::clone(&wasm_path);
|
|
350
|
+
let lineage_config = Arc::clone(&lineage_config);
|
|
351
|
+
let invoker = Arc::clone(&invoker);
|
|
352
|
+
async move {
|
|
353
|
+
let (body, _context) = event.into_parts();
|
|
354
|
+
server::dispatch_body(body, &client, &wasm_path, &lineage_config, invoker.as_ref()).await
|
|
355
|
+
}
|
|
356
|
+
}))
|
|
357
|
+
.await
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
// Parses `postgres://user:password@host:port/db` without treating it
|
|
361
|
+
// as a URI -- an RDS/Aurora auto-generated password can contain
|
|
362
|
+
// `?`/`#`, which a strict URI parser would treat as delimiters.
|
|
363
|
+
fn parse_database_url(url: &str) -> Result<tokio_postgres::Config, String> {
|
|
364
|
+
let rest = url
|
|
365
|
+
.strip_prefix("postgres://")
|
|
366
|
+
.or_else(|| url.strip_prefix("postgresql://"))
|
|
367
|
+
.ok_or_else(|| format!("DATABASE_URL doesn't start with postgres:// or postgresql://"))?;
|
|
368
|
+
|
|
369
|
+
let (authority, db_and_query) = rest
|
|
370
|
+
.split_once('/')
|
|
371
|
+
.ok_or_else(|| "DATABASE_URL has no '/' separating host from database name".to_string())?;
|
|
372
|
+
let dbname = db_and_query.split(['?', '#']).next().unwrap_or(db_and_query);
|
|
373
|
+
|
|
374
|
+
let (credentials, host_and_port) = match authority.rsplit_once('@') {
|
|
375
|
+
Some((credentials, host_and_port)) => (Some(credentials), host_and_port),
|
|
376
|
+
None => (None, authority),
|
|
377
|
+
};
|
|
378
|
+
let (host, port) = match host_and_port.rsplit_once(':') {
|
|
379
|
+
Some((host, port)) => {
|
|
380
|
+
let port: u16 = port
|
|
381
|
+
.parse()
|
|
382
|
+
.map_err(|e| format!("DATABASE_URL's port {port:?} isn't a valid number: {e}"))?;
|
|
383
|
+
(host, port)
|
|
384
|
+
}
|
|
385
|
+
None => (host_and_port, 5432),
|
|
386
|
+
};
|
|
387
|
+
|
|
388
|
+
let mut config = tokio_postgres::Config::new();
|
|
389
|
+
config.host(host).port(port).dbname(dbname);
|
|
390
|
+
if let Some(credentials) = credentials {
|
|
391
|
+
match credentials.split_once(':') {
|
|
392
|
+
Some((user, password)) => {
|
|
393
|
+
config.user(user).password(password);
|
|
394
|
+
}
|
|
395
|
+
None => {
|
|
396
|
+
config.user(credentials);
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
Ok(config)
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
fn database_url_is_local(config: &tokio_postgres::Config) -> bool {
|
|
404
|
+
use tokio_postgres::config::Host;
|
|
405
|
+
config.get_hosts().iter().any(|host| match host {
|
|
406
|
+
Host::Tcp(name) => name == "localhost" || name == "127.0.0.1" || name == "::1",
|
|
407
|
+
_ => false,
|
|
408
|
+
})
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
#[cfg(test)]
|
|
412
|
+
mod tests {
|
|
413
|
+
use super::*;
|
|
414
|
+
|
|
415
|
+
#[test]
|
|
416
|
+
fn parses_a_password_with_uri_reserved_characters() {
|
|
417
|
+
let config = parse_database_url(
|
|
418
|
+
"postgres://postgres:T3st$Pa?#ss*>Word@db.example.com:5432/pizzas",
|
|
419
|
+
)
|
|
420
|
+
.expect("should parse despite ?/# in the password");
|
|
421
|
+
assert_eq!(config.get_hosts().len(), 1);
|
|
422
|
+
assert_eq!(config.get_ports(), &[5432]);
|
|
423
|
+
assert_eq!(config.get_user(), Some("postgres"));
|
|
424
|
+
assert_eq!(config.get_dbname(), Some("pizzas"));
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
#[test]
|
|
428
|
+
fn parses_a_plain_password_too() {
|
|
429
|
+
let config = parse_database_url("postgres://postgres:plainpassword@localhost:5432/pizzas")
|
|
430
|
+
.expect("should parse a password with no special characters");
|
|
431
|
+
assert_eq!(config.get_user(), Some("postgres"));
|
|
432
|
+
assert_eq!(config.get_dbname(), Some("pizzas"));
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
#[test]
|
|
436
|
+
fn parses_a_local_url_with_no_user_or_port() {
|
|
437
|
+
let config = parse_database_url("postgres://localhost/app_development")
|
|
438
|
+
.expect("should parse a peer-auth local URL");
|
|
439
|
+
assert_eq!(config.get_dbname(), Some("app_development"));
|
|
440
|
+
assert_eq!(config.get_ports(), &[5432]);
|
|
441
|
+
assert!(database_url_is_local(&config));
|
|
442
|
+
}
|
|
443
|
+
}
|