gobby-core 0.6.1

Shared foundation primitives for Gobby CLI tools
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
use super::*;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EnsureHubOptions {
    pub gobby_home: PathBuf,
    pub service_options: DockerServiceOptions,
    pub candidate_database_urls: Vec<String>,
    pub provision_services: bool,
}

impl EnsureHubOptions {
    pub fn new(gobby_home: PathBuf) -> Self {
        Self {
            service_options: DockerServiceOptions::new(gobby_home.clone()),
            gobby_home,
            candidate_database_urls: Vec::new(),
            provision_services: true,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HubIdentity {
    pub system_identifier: String,
    pub database_name: String,
}

impl HubIdentity {
    fn display_label(&self) -> String {
        format!(
            "system_identifier={}, database={}",
            self.system_identifier, self.database_name
        )
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum HubIdentityProbeResult {
    Known(HubIdentity),
    UnknownInsufficientPrivilege { message: String },
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RecordedHubIdentityStatus {
    SingleReachable,
    VerifiedSameHub,
    IdentityUnknownInsufficientPrivilege { message: String },
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RecordedHubResolution {
    pub database_url: String,
    pub identity_status: RecordedHubIdentityStatus,
}

pub fn ensure_hub(
    options: &EnsureHubOptions,
) -> anyhow::Result<(String, Option<DockerProvisioningReport>)> {
    ensure_hub_with_identity(
        options,
        |name| std::env::var(name).ok(),
        postgres_database_reachable,
        probe_postgres_hub_identity,
        provision_docker_services,
    )
}

#[cfg(test)]
pub(crate) fn ensure_hub_with(
    options: &EnsureHubOptions,
    get_env: impl FnMut(&str) -> Option<String>,
    database_reachable: impl FnMut(&str) -> bool,
    provision: impl FnOnce(&DockerServiceOptions) -> anyhow::Result<DockerProvisioningReport>,
) -> anyhow::Result<(String, Option<DockerProvisioningReport>)> {
    ensure_hub_with_identity(
        options,
        get_env,
        database_reachable,
        |_| {
            Ok(HubIdentityProbeResult::UnknownInsufficientPrivilege {
                message: "identity_unknown_insufficient_privilege: test probe not configured"
                    .to_string(),
            })
        },
        provision,
    )
}

pub(crate) fn ensure_hub_with_identity(
    options: &EnsureHubOptions,
    mut get_env: impl FnMut(&str) -> Option<String>,
    mut database_reachable: impl FnMut(&str) -> bool,
    mut identity_probe: impl FnMut(&str) -> anyhow::Result<HubIdentityProbeResult>,
    provision: impl FnOnce(&DockerServiceOptions) -> anyhow::Result<DockerProvisioningReport>,
) -> anyhow::Result<(String, Option<DockerProvisioningReport>)> {
    let mut override_database_url = None;
    let mut gcore_database_url = None;
    let mut bootstrap_database_url = None;

    for candidate in resolve_hub_database_urls(options, &mut get_env)? {
        match candidate.source {
            HubDatabaseUrlSource::Candidate | HubDatabaseUrlSource::Env => {
                if override_database_url.is_none()
                    && explicit_database_url_reachable(
                        &candidate.database_url,
                        &mut database_reachable,
                    )
                {
                    override_database_url = Some(candidate.database_url);
                }
            }
            HubDatabaseUrlSource::GcoreConfig => {
                gcore_database_url = Some(candidate.database_url);
            }
            HubDatabaseUrlSource::Bootstrap => {
                bootstrap_database_url = Some(candidate.database_url);
            }
        }
    }

    let recorded_resolution = resolve_recorded_hub_database_url(
        gcore_database_url.as_deref(),
        bootstrap_database_url.as_deref(),
        &mut database_reachable,
        &mut identity_probe,
    )?;

    if let Some(override_database_url) = override_database_url {
        if let Some(recorded) = recorded_resolution
            && let Some(resolution) = resolve_recorded_hub_database_url(
                Some(&recorded.database_url),
                Some(&override_database_url),
                &mut database_reachable,
                &mut identity_probe,
            )?
        {
            if let RecordedHubIdentityStatus::IdentityUnknownInsufficientPrivilege { message } =
                &resolution.identity_status
            {
                log::warn!("{message}");
            }
            return Ok((resolution.database_url, None));
        }
        return Ok((override_database_url, None));
    }

    if let Some(resolution) = recorded_resolution {
        if let RecordedHubIdentityStatus::IdentityUnknownInsufficientPrivilege { message } =
            &resolution.identity_status
        {
            log::warn!("{message}");
        }
        return Ok((resolution.database_url, None));
    }

    if !options.provision_services {
        anyhow::bail!(
            "no reachable Gobby PostgreSQL hub found and service provisioning is disabled"
        );
    }

    let report = provision(&options.service_options).context("failed to provision Gobby hub")?;
    Ok((
        default_database_url(options.service_options.postgres_port),
        Some(report),
    ))
}

pub fn resolve_recorded_hub_database_url(
    existing_database_url: Option<&str>,
    daemon_database_url: Option<&str>,
    mut database_reachable: impl FnMut(&str) -> bool,
    mut identity_probe: impl FnMut(&str) -> anyhow::Result<HubIdentityProbeResult>,
) -> anyhow::Result<Option<RecordedHubResolution>> {
    let existing_database_url =
        existing_database_url.and_then(|value| non_empty_trimmed(Some(value.to_string())));
    let daemon_database_url =
        daemon_database_url.and_then(|value| non_empty_trimmed(Some(value.to_string())));

    match (existing_database_url, daemon_database_url) {
        (None, None) => Ok(None),
        (Some(existing), None) => {
            if database_reachable(&existing) {
                Ok(Some(RecordedHubResolution {
                    database_url: existing,
                    identity_status: RecordedHubIdentityStatus::SingleReachable,
                }))
            } else {
                Ok(None)
            }
        }
        (None, Some(daemon)) => {
            if database_reachable(&daemon) {
                Ok(Some(RecordedHubResolution {
                    database_url: daemon,
                    identity_status: RecordedHubIdentityStatus::SingleReachable,
                }))
            } else {
                Ok(None)
            }
        }
        (Some(existing), Some(daemon)) if existing == daemon => {
            if database_reachable(&daemon) {
                Ok(Some(RecordedHubResolution {
                    database_url: daemon,
                    identity_status: RecordedHubIdentityStatus::VerifiedSameHub,
                }))
            } else {
                Ok(None)
            }
        }
        (Some(existing), Some(daemon)) => {
            let existing_reachable = database_reachable(&existing);
            let daemon_reachable = database_reachable(&daemon);

            match (existing_reachable, daemon_reachable) {
                (false, false) => Ok(None),
                (true, false) => Ok(Some(RecordedHubResolution {
                    database_url: existing,
                    identity_status: RecordedHubIdentityStatus::SingleReachable,
                })),
                (false, true) => Ok(Some(RecordedHubResolution {
                    database_url: daemon,
                    identity_status: RecordedHubIdentityStatus::SingleReachable,
                })),
                (true, true) => {
                    let existing_redacted = redacted_postgres_dsn_placeholder("existing");
                    let daemon_redacted = redacted_postgres_dsn_placeholder("daemon");
                    let existing_identity = identity_probe(&existing).with_context(|| {
                        format!("failed to probe PostgreSQL hub identity for {existing_redacted}")
                    })?;
                    let daemon_identity = identity_probe(&daemon).with_context(|| {
                        format!("failed to probe PostgreSQL hub identity for {daemon_redacted}")
                    })?;

                    match (existing_identity, daemon_identity) {
                        (
                            HubIdentityProbeResult::Known(existing_identity),
                            HubIdentityProbeResult::Known(daemon_identity),
                        ) if existing_identity == daemon_identity => {
                            Ok(Some(RecordedHubResolution {
                                database_url: daemon,
                                identity_status: RecordedHubIdentityStatus::VerifiedSameHub,
                            }))
                        }
                        (
                            HubIdentityProbeResult::Known(existing_identity),
                            HubIdentityProbeResult::Known(daemon_identity),
                        ) => Err(CoreError::HubConflict {
                            existing_database_url: existing_redacted,
                            existing_identity: existing_identity.display_label(),
                            daemon_database_url: daemon_redacted,
                            daemon_identity: daemon_identity.display_label(),
                        }
                        .into()),
                        (
                            HubIdentityProbeResult::UnknownInsufficientPrivilege { message },
                            _,
                        )
                        | (
                            _,
                            HubIdentityProbeResult::UnknownInsufficientPrivilege { message },
                        ) => Ok(Some(RecordedHubResolution {
                            database_url: existing,
                            identity_status:
                                RecordedHubIdentityStatus::IdentityUnknownInsufficientPrivilege {
                                    message: format!(
                                        "identity_unknown_insufficient_privilege: preserving existing recorded hub {}; daemon hub {} was not adopted because identity could not be verified ({message})",
                                        existing_redacted,
                                        daemon_redacted,
                                    ),
                                },
                        })),
                    }
                }
            }
        }
    }
}

fn redacted_postgres_dsn_placeholder(source: &str) -> String {
    format!("<redacted-{source}-postgres-dsn>")
}

#[cfg(feature = "postgres")]
pub fn probe_postgres_hub_identity(database_url: &str) -> anyhow::Result<HubIdentityProbeResult> {
    use anyhow::Context;
    use postgres::error::SqlState;

    fn insufficient_privilege(error: &postgres::Error) -> bool {
        error.code() == Some(&SqlState::INSUFFICIENT_PRIVILEGE)
    }

    let mut conn = crate::postgres::connect_readonly(database_url)?;
    let has_privilege = match conn.query_one(
        "SELECT has_function_privilege(current_user, 'pg_control_system()', 'execute')",
        &[],
    ) {
        Ok(row) => row.get::<_, bool>(0),
        Err(error) if insufficient_privilege(&error) => {
            return Ok(HubIdentityProbeResult::UnknownInsufficientPrivilege {
                message: "identity_unknown_insufficient_privilege: current role cannot preflight pg_control_system()".to_string(),
            });
        }
        Err(error) => {
            return Err(error).context("failed to preflight pg_control_system() privilege");
        }
    };

    if !has_privilege {
        return Ok(HubIdentityProbeResult::UnknownInsufficientPrivilege {
            message: "identity_unknown_insufficient_privilege: current role cannot execute pg_control_system()".to_string(),
        });
    }

    let row = match conn.query_one(
        "SELECT system_identifier::text AS system_identifier, current_database() AS database_name FROM pg_control_system()",
        &[],
    ) {
        Ok(row) => row,
        Err(error) if insufficient_privilege(&error) => {
            return Ok(HubIdentityProbeResult::UnknownInsufficientPrivilege {
                message: "identity_unknown_insufficient_privilege: current role cannot execute pg_control_system()".to_string(),
            });
        }
        Err(error) => return Err(error).context("failed to query PostgreSQL hub identity"),
    };

    Ok(HubIdentityProbeResult::Known(HubIdentity {
        system_identifier: row
            .try_get("system_identifier")
            .context("PostgreSQL hub identity did not include system_identifier")?,
        database_name: row
            .try_get("database_name")
            .context("PostgreSQL hub identity did not include database_name")?,
    }))
}

#[cfg(not(feature = "postgres"))]
pub fn probe_postgres_hub_identity(_database_url: &str) -> anyhow::Result<HubIdentityProbeResult> {
    Ok(HubIdentityProbeResult::UnknownInsufficientPrivilege {
        message: "identity_unknown_insufficient_privilege: gobby-core was built without PostgreSQL support".to_string(),
    })
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum HubDatabaseUrlSource {
    Candidate,
    Env,
    GcoreConfig,
    Bootstrap,
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct HubDatabaseUrl {
    source: HubDatabaseUrlSource,
    database_url: String,
}

fn resolve_hub_database_urls(
    options: &EnsureHubOptions,
    get_env: &mut impl FnMut(&str) -> Option<String>,
) -> anyhow::Result<Vec<HubDatabaseUrl>> {
    let mut urls = Vec::new();
    urls.extend(
        options
            .candidate_database_urls
            .iter()
            .filter_map(|value| non_empty_trimmed(Some(value.clone())))
            .map(|database_url| HubDatabaseUrl {
                source: HubDatabaseUrlSource::Candidate,
                database_url,
            }),
    );
    if let Some(database_url) = non_empty_trimmed(get_env("GOBBY_POSTGRES_DSN")) {
        urls.push(HubDatabaseUrl {
            source: HubDatabaseUrlSource::Env,
            database_url,
        });
    }
    if let Some(database_url) = resolve_database_url_from_gcore_config(&options.gobby_home)? {
        urls.push(HubDatabaseUrl {
            source: HubDatabaseUrlSource::GcoreConfig,
            database_url,
        });
    }
    if let Some(database_url) =
        resolve_database_url_from_bootstrap_file(&options.gobby_home.join("bootstrap.yaml"))?
    {
        urls.push(HubDatabaseUrl {
            source: HubDatabaseUrlSource::Bootstrap,
            database_url,
        });
    }
    Ok(urls)
}

fn resolve_database_url_from_gcore_config(home: &Path) -> anyhow::Result<Option<String>> {
    if !services_dir(home).is_dir() || !compose_file_path(home).is_file() {
        return Ok(None);
    }
    let Some(config) = StandaloneConfig::read_at(&gcore_config_path(home))? else {
        return Ok(None);
    };
    Ok(config
        .get("databases.postgres.dsn")
        .and_then(|value| non_empty_trimmed(Some(value.to_string()))))
}

#[derive(Debug, Deserialize)]
struct HubBootstrap {
    hub_backend: Option<String>,
    database_url: Option<String>,
}

fn resolve_database_url_from_bootstrap_file(path: &Path) -> anyhow::Result<Option<String>> {
    if !path.exists() {
        return Ok(None);
    }
    let contents = fs::read_to_string(path)
        .with_context(|| format!("failed to read Gobby bootstrap at {}", path.display()))?;
    let bootstrap: HubBootstrap = serde_yaml::from_str(&contents)
        .with_context(|| format!("failed to parse {}", path.display()))?;
    if matches!(bootstrap.hub_backend.as_deref(), Some(backend) if backend != "postgres") {
        return Ok(None);
    }
    Ok(non_empty_trimmed(bootstrap.database_url))
}

fn non_empty_trimmed(value: Option<String>) -> Option<String> {
    let trimmed = value.as_ref()?.trim();
    if trimmed.is_empty() {
        None
    } else {
        Some(trimmed.to_string())
    }
}

#[cfg(feature = "postgres")]
fn postgres_database_reachable(database_url: &str) -> bool {
    crate::postgres::connect_readonly(database_url).is_ok()
}

#[cfg(not(feature = "postgres"))]
fn postgres_database_reachable(_database_url: &str) -> bool {
    false
}

#[cfg(feature = "postgres")]
fn explicit_database_url_reachable(
    database_url: &str,
    database_reachable: &mut impl FnMut(&str) -> bool,
) -> bool {
    database_reachable(database_url)
}

#[cfg(not(feature = "postgres"))]
fn explicit_database_url_reachable(
    _database_url: &str,
    _database_reachable: &mut impl FnMut(&str) -> bool,
) -> bool {
    // Without the postgres feature, gcore cannot open a connection to probe an
    // explicit hub DSN. Preserve the configured DSN and let the consumer fail
    // later if it actually needs PostgreSQL access.
    log::warn!(
        "postgres feature is disabled; preserving configured PostgreSQL hub {} without a reachability probe",
        redacted_postgres_dsn_placeholder("explicit")
    );
    true
}