mariadb-cli 0.1.1

One-shot agent-native command line client for MariaDB servers
Documentation
//! The one profile command that reaches a server.
//!
//! Its six neighbours touch only the store on disk, and it sits apart from
//! them for that reason: it is the only one that opens a connection, the only
//! one that can fail for a reason outside this host, and the only one that has
//! to declare what reaching the destination cost. Keeping it here means the
//! file next door reads as what it is, which is file handling.

use super::{ConnectionSettings, Envelope, ProfileStore, Result, TestArgs};
use crate::conn::options;
use crate::conn::session::Session;
use crate::runtime::Phase;
use serde_json::json;

/// Opens, confirms and closes the connection.
///
/// The answer says whether this build carries network encryption, because the
/// same command against the same server means something different on a build
/// without it, and a caller must not have to guess which build it is talking to.
pub(super) async fn test(
    args: &TestArgs,
    store: &ProfileStore,
    settings: &ConnectionSettings,
    marked: Option<&str>,
    budget: std::time::Duration,
) -> Result<Envelope> {
    let profile = store.resolve(args.name.as_deref(), marked)?;
    let options = options::build(profile, settings)?;

    // This command reaches a server by a route of its own, outside the one
    // `dispatch::open` serves, because it opens and closes without work in
    // between. That made it the one opening on the surface that declared no
    // cost, which is precisely the reading a caller comes here for: this is
    // the command whose whole answer is what reaching the destination is like
    // (FR-138).
    // The published connect ceiling applies here too, and until this line it
    // did not. Opening by a route of its own meant this command inherited only
    // the host's own idea of when to give up, which is minutes: measured
    // against an address nothing answers on, it spent 133 seconds while the
    // caller had asked for three. The command whose entire answer is whether a
    // destination is up was the one that would not stop waiting for it.
    crate::i18n::report::channel_opening(&profile.name);
    let reaching = std::time::Instant::now();
    let opened =
        crate::runtime::timeout::within(Phase::Connect, budget, Session::open(options)).await;
    let cost = reaching.elapsed().as_millis();
    let session = match opened {
        Ok(session) => {
            crate::i18n::report::channel_opened(&session.facts().version, cost);
            session
        }
        Err(failure) => {
            crate::i18n::report::channel_open_failed(cost);
            return Err(failure);
        }
    };
    let facts = session.facts().clone();
    let warning = facts.version_warning();
    session.close().await?;

    let mut data = json!({
        "profile": profile.name,
        "reachable": true,
        "encrypted_channel_available": crate::ENCRYPTED_CHANNEL_AVAILABLE,
        // The resolved answer, not the destination's own field: with the
        // field absent the stored key decides, and reporting the field would
        // tell the caller "nothing was asked for" when a channel was demanded.
        "encryption_requested": crate::conn::tls::wants_encryption(profile, &settings.encryption),
        // The method this destination declared, answered because the open
        // succeeded with it: the driver publishes no accessor for the method it
        // negotiated, and a method this build cannot complete never gets this
        // far — it is refused by name while the options are built. Absent means
        // the destination declared none and the server's own choice applied.
        "auth_method": profile.auth_method,
        "server": facts.as_value(),
    });
    if let Some(warning) = warning {
        data["warning"] = json!(warning);
    }

    Ok(Envelope::new("conn test", data))
}