vcon-server 0.4.8

Stores vCons encrypted at rest, on local disk or in an S3 bucket (as objects or Parquet files), and returns them to authorized readers.
Documentation
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//! The catalog's schema: the migrations that make it, the command that
//! applies them (`vcon-server migrate`), and the checks a new connection
//! makes against what is installed.
//!
//! - A server never migrates: with several instances, one starting up would
//!   change the schema under the others. It refuses a database at any
//!   other version than the binary's, on every new connection.
//! - `migrate` runs alone, holding the two advisory locks every server and
//!   command will share, with the role `vcon_owner` it reaches through
//!   `SET ROLE`, one transaction per schema version. That role is never
//!   given to a server: a serving connection is refused when its role owns
//!   the catalog or is a member of the role that does.

use tokio_postgres::error::SqlState;
use tokio_postgres::{Client, SimpleQueryMessage, SimpleQueryRow};

use super::connect::with_causes;

/// The migrations, in order; the version of each is its position, from 1.
pub(crate) const MIGRATIONS: &[&str] = &[
    include_str!("schema_v1.sql"),
    include_str!("schema_v2.sql"),
    include_str!("schema_v3.sql"),
    include_str!("schema_v4.sql"),
    include_str!("schema_v5.sql"),
    include_str!("schema_v6.sql"),
    include_str!("schema_v7.sql"),
    include_str!("schema_v8.sql"),
    include_str!("schema_v9.sql"),
    include_str!("schema_v10.sql"),
    include_str!("schema_v11.sql"),
    include_str!("schema_v12.sql"),
    include_str!("schema_v13.sql"),
];

/// The schema version this binary makes and reads:
/// [`MIGRATIONS`]`.len()`, a test holds them equal.
pub(crate) const SCHEMA_VERSION: i32 = 13;

/// The advisory lock every server takes, a constant all instances share.
pub(crate) const SERVERS_LOCK: i64 = 0x7663_6f6e_5352_0001;

/// The advisory lock every command takes.
pub(crate) const COMMANDS_LOCK: i64 = 0x7663_6f6e_434d_0001;

/// What a process does with the database while it holds its locks there,
/// as [`crate::datadir::Sharing`] says for the data directory's locks.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Holds {
    /// A server.
    Serve,
    /// A command that runs beside a server and other such commands.
    Beside,
    /// A command that runs with no server and no other command.
    Alone,
}

impl From<crate::datadir::Sharing> for Holds {
    fn from(sharing: crate::datadir::Sharing) -> Holds {
        match sharing {
            crate::datadir::Sharing::Serve => Holds::Serve,
            crate::datadir::Sharing::Beside => Holds::Beside,
            crate::datadir::Sharing::Alone => Holds::Alone,
        }
    }
}

/// The one statement that tries the locks of `holds` without waiting. It
/// answers two columns, the servers' lock and the commands' lock, each
/// true when the lock was given or is not taken for this work.
///
/// - A server takes the servers' lock for itself: this release serves a
///   database from one instance.
/// - A command beside a server shares the commands' lock with other such
///   commands and takes nothing a server holds.
/// - A command alone takes both for itself, as `migrate` does
///   ([`take_locks`]), so it runs with no server and no other command.
pub(crate) fn lock_text(holds: Holds) -> String {
    match holds {
        Holds::Serve => format!("SELECT pg_try_advisory_lock({SERVERS_LOCK}), true"),
        Holds::Beside => format!("SELECT true, pg_try_advisory_lock_shared({COMMANDS_LOCK})"),
        Holds::Alone => format!(
            "SELECT pg_try_advisory_lock({SERVERS_LOCK}), pg_try_advisory_lock({COMMANDS_LOCK})"
        ),
    }
}

/// Why the work `holds` may not go on when [`lock_text`] answered
/// `servers` and `commands`, or `None` when it may.
pub(crate) fn lock_refusal(holds: Holds, servers: bool, commands: bool) -> Option<&'static str> {
    match holds {
        Holds::Serve => (!servers).then_some(SERVER_LOCK_HELD),
        Holds::Beside => (!commands).then_some(
            "a command that runs alone, or a migration, holds this database's lock; run this \
             command again when it has ended",
        ),
        Holds::Alone => (!(servers && commands)).then_some(
            "a server or another command holds this database's locks; this command runs \
             alone: stop every server, wait for the other commands to end, and run it again",
        ),
    }
}

/// Takes the locks of `holds` on `client`, a connection kept for them,
/// without waiting. A work refused one of its two locks keeps neither.
pub(crate) async fn take_locks_for(client: &Client, holds: Holds) -> Result<(), String> {
    let row = checked_row(client, &lock_text(holds)).await?;
    match lock_refusal(holds, is_true(&row, 0), is_true(&row, 1)) {
        None => Ok(()),
        Some(why) => {
            // If this fails the connection is lost, and the server frees
            // the lock when it closes the session.
            let _ = client
                .batch_execute("SELECT pg_advisory_unlock_all()")
                .await;
            Err(why.to_owned())
        }
    }
}

/// What `migrate` did: the schema version found, and the one it left.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct Migrated {
    /// The installed version before; 0 for none.
    pub(crate) from: i32,
    /// The installed version after.
    pub(crate) to: i32,
}

impl Migrated {
    /// The line `migrate` prints.
    #[must_use]
    pub(crate) fn message(self) -> String {
        if self.from == self.to {
            format!("already at schema version {}", self.to)
        } else {
            format!("schema version {} to schema version {}", self.from, self.to)
        }
    }

    /// The JSON line `migrate` prints: both versions, whether the schema
    /// changed, and [`Migrated::message`].
    #[must_use]
    pub(crate) fn line(self) -> serde_json::Value {
        serde_json::json!({
            "migrated": self.from != self.to,
            "from": self.from,
            "to": self.to,
            "message": self.message(),
        })
    }
}

/// Why a connection is refused for the schema version `installed` (`None`
/// for no schema, or none recorded), or `None` when it is the binary's.
#[must_use]
pub(crate) fn version_refusal(installed: Option<i32>) -> Option<String> {
    match installed {
        None => Some("the database has no vCon schema: run vcon-server migrate".to_owned()),
        Some(n) if n == SCHEMA_VERSION => None,
        Some(n) if n < SCHEMA_VERSION => Some(format!(
            "the database schema is version {n}, older than this binary's {SCHEMA_VERSION}: run \
             vcon-server migrate"
        )),
        Some(n) => Some(format!(
            "the database schema is newer than this binary (version {n}, this binary \
             {SCHEMA_VERSION}): upgrade this server"
        )),
    }
}

/// Why `migrate` may not go on: `owner` and `app` say that the two roles
/// exist, and `can_set` that the login role `user` may `SET ROLE
/// vcon_owner`. `None` when it may.
#[must_use]
pub(crate) fn role_refusal(user: &str, owner: bool, app: bool, can_set: bool) -> Option<String> {
    if !owner {
        return Some(
            "the role vcon_owner does not exist; create it as the how-to shows, with no login"
                .to_owned(),
        );
    }
    if !app {
        return Some(
            "the role vcon_app does not exist; create it as the how-to shows, for the server to \
             log in as"
                .to_owned(),
        );
    }
    if !can_set {
        return Some(format!(
            "the role {user:?} cannot act as vcon_owner, which migrate needs; run GRANT \
             vcon_owner TO {user:?} as a superuser, for a role the server does not use"
        ));
    }
    None
}

/// A row, its columns as text (`None` for NULL).
type Row = Vec<Option<String>>;

fn is_true(row: &Row, i: usize) -> bool {
    row.get(i).and_then(Option::as_deref) == Some("t")
}

/// The first row of the answer to `sql`, or why the server cannot be
/// checked.
async fn checked_row(client: &Client, sql: &str) -> Result<Row, String> {
    let messages = client
        .simple_query(sql)
        .await
        .map_err(|e| format!("the server cannot be checked: {}", with_causes(&e)))?;
    let row = first_row(&messages)
        .ok_or_else(|| "the server cannot be checked: no row came back".to_owned())?;
    Ok((0..row.len())
        .map(|i| row.get(i).map(str::to_owned))
        .collect())
}

/// The checks of a serving connection: its role does not own the catalog
/// and cannot act as its owner, and the schema is this binary's.
pub(crate) async fn serving_checks(client: &Client, user: &str) -> Result<(), String> {
    const OWNS: &str = "SELECT \
        EXISTS (SELECT 1 FROM pg_tables t WHERE t.schemaname = 'vcon' \
                AND pg_has_role(current_user, t.tableowner, 'MEMBER')) \
        OR EXISTS (SELECT 1 FROM pg_namespace n WHERE n.nspname = 'vcon' \
                AND pg_has_role(current_user, n.nspowner, 'MEMBER'))";
    let owns = checked_row(client, OWNS).await?;
    if is_true(&owns, 0) {
        return Err(format!(
            "the role {user:?} owns the catalog's tables or is a member of the role that does; \
             a server logs in as vcon_app"
        ));
    }
    let installed = installed_version(client, user).await?;
    match version_refusal(installed) {
        Some(why) => Err(why),
        None => Ok(()),
    }
}

/// The schema version `vcon.installed_version()` reports: `None` when no
/// version is recorded or the schema is absent.
async fn installed_version(client: &Client, user: &str) -> Result<Option<i32>, String> {
    let row = match client.simple_query("SELECT vcon.installed_version()").await {
        Ok(messages) => messages,
        Err(e) => {
            return match e.code() {
                Some(&SqlState::INVALID_SCHEMA_NAME) | Some(&SqlState::UNDEFINED_FUNCTION) => {
                    Ok(None)
                }
                Some(&SqlState::INSUFFICIENT_PRIVILEGE) => Err(format!(
                    "the role {user:?} may not use the vCon schema; make it a member of \
                     vcon_app, or log in as vcon_app"
                )),
                _ => Err(format!("the server cannot be checked: {}", with_causes(&e))),
            };
        }
    };
    let text = first_row(&row).and_then(|r| r.get(0).map(str::to_owned));
    text.map_or(Ok(None), |v| {
        v.parse().map(Some).map_err(|_| {
            "the server cannot be checked: the schema version is not a number".to_owned()
        })
    })
}

/// The checks of `migrate`'s connection: both roles exist and the login
/// role may act as `vcon_owner`.
pub(crate) async fn migrating_checks(client: &Client, user: &str) -> Result<(), String> {
    const ROLES: &str = "SELECT to_regrole('vcon_owner') IS NOT NULL, \
        to_regrole('vcon_app') IS NOT NULL, \
        CASE WHEN to_regrole('vcon_owner') IS NULL THEN false \
             ELSE pg_has_role(current_user, 'vcon_owner', 'SET') END";
    let row = checked_row(client, ROLES).await?;
    match role_refusal(user, is_true(&row, 0), is_true(&row, 1), is_true(&row, 2)) {
        Some(why) => Err(why),
        None => Ok(()),
    }
}

/// Applies `migrations` the database has not yet, alone and as
/// `vcon_owner`, each in one transaction. Before it returns it gives up
/// the role and the two locks, so a server started the moment `migrate`
/// ends finds them free: a lock left to the end of the session is held
/// until the server notices the closed connection, which is after the
/// command has exited.
pub(crate) async fn run(client: &mut Client, migrations: &[&str]) -> Result<Migrated, String> {
    let done = match take_locks(client).await {
        Ok(()) => run_locked(client, migrations).await,
        Err(refused) => Err(refused),
    };
    // A failed migration has already rolled back; the session's role and
    // locks are given up either way, one of the two locks too when only
    // the other was refused. If this fails the connection is lost, and
    // the server frees both when it closes the session.
    let _ = client
        .batch_execute("RESET ROLE; SELECT pg_advisory_unlock_all()")
        .await;
    done
}

/// [`run`], with the two locks held.
async fn run_locked(client: &mut Client, migrations: &[&str]) -> Result<Migrated, String> {
    client
        .batch_execute("SET ROLE vcon_owner")
        .await
        .map_err(|e| format!("the owner role cannot be used: {}", statement_error(&e)))?;
    let from = installed_for_migrate(client).await?;
    let latest = i32::try_from(migrations.len()).expect("the migrations number under 2^31");
    if from > latest {
        return Err(format!(
            "the database schema is newer than this binary (version {from}, this binary {latest}): \
             upgrade this server"
        ));
    }
    for (index, sql) in migrations
        .iter()
        .enumerate()
        .skip(from.try_into().unwrap_or(usize::MAX))
    {
        let version = i32::try_from(index + 1).expect("the migrations number under 2^31");
        apply(client, version, sql).await?;
    }
    Ok(Migrated {
        from,
        to: latest.max(from),
    })
}

/// The two advisory locks, tried without waiting: a server or command that
/// holds either, exclusive or shared, refuses the migration.
async fn take_locks(client: &Client) -> Result<(), String> {
    let row = checked_row(
        client,
        &format!(
            "SELECT pg_try_advisory_lock({SERVERS_LOCK}), pg_try_advisory_lock({COMMANDS_LOCK})"
        ),
    )
    .await?;
    if is_true(&row, 0) && is_true(&row, 1) {
        Ok(())
    } else {
        Err(
            "another server or command holds the database's locks; stop every server and \
             command and run migrate again"
                .to_owned(),
        )
    }
}

/// What a server is told when its lock is held: `migrate` and a command
/// that runs alone try the same lock.
pub(crate) const SERVER_LOCK_HELD: &str = "another server, a command that runs alone, or a \
                                           migration holds this database's lock; one server \
                                           serves a database, and those commands run while none \
                                           does";

/// The version recorded, or 0 for a database no migration has touched.
async fn installed_for_migrate(client: &Client) -> Result<i32, String> {
    let exists = checked_row(
        client,
        "SELECT to_regclass('vcon.schema_version') IS NOT NULL",
    )
    .await?;
    if !is_true(&exists, 0) {
        return Ok(0);
    }
    let row = checked_row(
        client,
        "SELECT coalesce(max(version), 0) FROM vcon.schema_version",
    )
    .await?;
    row.first()
        .and_then(Option::as_deref)
        .and_then(|v| v.parse().ok())
        .ok_or_else(|| "the schema version recorded is not a number".to_owned())
}

/// One migration in one transaction: a failure leaves nothing of it.
async fn apply(client: &mut Client, version: i32, sql: &str) -> Result<(), String> {
    let tx = client
        .transaction()
        .await
        .map_err(|e| format!("version {version}: {}", statement_error(&e)))?;
    tx.batch_execute(sql).await.map_err(|e| {
        if e.code() == Some(&SqlState::DUPLICATE_SCHEMA) {
            "a schema named vcon exists that vcon-server did not make; remove it, or use \
             another database"
                .to_owned()
        } else {
            format!("version {version}: {}", statement_error(&e))
        }
    })?;
    tx.execute(
        "INSERT INTO vcon.schema_version (version) VALUES ($1)",
        &[&version],
    )
    .await
    .map_err(|e| format!("version {version}: {}", statement_error(&e)))?;
    tx.commit()
        .await
        .map_err(|e| format!("version {version}: {}", statement_error(&e)))
}

/// What the server said of a statement, or the error itself. The statement
/// text is not repeated: it is the binary's own and holds no secret, but a
/// failure need not print a page of SQL.
fn statement_error(e: &tokio_postgres::Error) -> String {
    match e.as_db_error() {
        Some(db) => format!("{} (SQLSTATE {})", db.message(), db.code().code()),
        None => with_causes(e),
    }
}

fn first_row(messages: &[SimpleQueryMessage]) -> Option<&SimpleQueryRow> {
    messages.iter().find_map(|m| match m {
        SimpleQueryMessage::Row(row) => Some(row),
        _ => None,
    })
}