Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

brokkr-broker::utils Rust

Utility functions and structures for the Brokkr broker.

This module contains various helper functions and structures used throughout the broker, including admin key management and shutdown procedures.

Structs

brokkr-broker::utils::AdminKey

pub

Derives: Queryable, Selectable, Identifiable, AsChangeset, Debug, Clone

Represents an admin key in the database.

Fields

NameTypeDescription
idUuid
created_atchrono :: DateTime < Utc >
updated_atchrono :: DateTime < Utc >
pak_hashString

brokkr-broker::utils::NewAdminKey

pub

Derives: Insertable

Represents a new admin key to be inserted into the database.

Fields

NameTypeDescription
pak_hashString

Functions

brokkr-broker::utils::shutdown

pub

#![allow(unused)]
fn main() {
async fn shutdown (shutdown_rx : oneshot :: Receiver < () >)
}

Handles the shutdown process for the broker.

This function waits for a shutdown signal and then performs cleanup tasks.

Source
#![allow(unused)]
fn main() {
pub async fn shutdown(shutdown_rx: oneshot::Receiver<()>) {
    let _ = shutdown_rx.await;
    // Remove the temporary key file
    let _ = fs::remove_file("/tmp/key.txt");
}
}

brokkr-broker::utils::first_startup

pub

#![allow(unused)]
fn main() {
fn first_startup (conn : & mut PgConnection , config : & Settings ,) -> Result < () , Box < dyn std :: error :: Error > >
}

Performs first-time startup operations.

This function is called when the broker starts for the first time and sets up the initial admin key.

Source
#![allow(unused)]
fn main() {
pub fn first_startup(
    conn: &mut PgConnection,
    config: &Settings,
) -> Result<(), Box<dyn std::error::Error>> {
    upsert_admin(conn, config)
}
}

brokkr-broker::utils::create_pak

private

#![allow(unused)]
fn main() {
fn create_pak () -> Result < (String , String) , Box < dyn std :: error :: Error > >
}

Creates a new PAK (Privileged Access Key) and its hash.

This function generates a new PAK and returns both the key and its hash.

Source
#![allow(unused)]
fn main() {
fn create_pak() -> Result<(String, String), Box<dyn std::error::Error>> {
    // Generate PAK and hash using the PAK controller
    let controller = pak::create_pak_controller(None);
    controller
        .unwrap()
        .try_generate_key_and_hash()
        .map(|(pak, hash)| (pak.to_string(), hash))
        .map_err(|e| e.into())
}
}

brokkr-broker::utils::upsert_admin

pub

#![allow(unused)]
fn main() {
fn upsert_admin (conn : & mut PgConnection , config : & Settings ,) -> Result < () , Box < dyn std :: error :: Error > >
}

Updates or inserts the admin key and related generator.

This function creates or updates the admin key in the database, creates or updates the associated admin generator, and writes the PAK to a temporary file.

Source
#![allow(unused)]
fn main() {
pub fn upsert_admin(
    conn: &mut PgConnection,
    config: &Settings,
) -> Result<(), Box<dyn std::error::Error>> {
    let pak_hash = match &config.broker.pak_hash {
        Some(hash) if !hash.is_empty() => {
            // Validate the provided hash
            if !validate_pak_hash(hash) {
                return Err("Invalid PAK hash provided in configuration".into());
            }
            hash.clone()
        }
        _ => {
            // Generate new PAK and hash
            let (pak, hash) = create_pak()?;

            // Write PAK to temporary file
            info!("Writing PAK to temporary file");
            let key_path = Path::new("/tmp/brokkr-keys/key.txt");
            fs::create_dir_all(key_path.parent().unwrap())?;
            fs::write(key_path, pak)?;

            hash
        }
    };

    // Update or insert admin key
    let existing_admin_key = admin_role::table
        .select(admin_role::id)
        .first::<Uuid>(conn)
        .optional()?;

    match existing_admin_key {
        Some(id) => {
            diesel::update(admin_role::table.find(id))
                .set(admin_role::pak_hash.eq(&pak_hash))
                .execute(conn)?;
        }
        None => {
            diesel::insert_into(admin_role::table)
                .values(&NewAdminKey {
                    pak_hash: pak_hash.clone(),
                })
                .execute(conn)?;
        }
    }

    // Update or insert admin generator
    use brokkr_models::schema::generators;
    let existing_admin_generator = generators::table
        .filter(generators::name.eq("admin-generator"))
        .select(generators::id)
        .first::<Uuid>(conn)
        .optional()?;

    match existing_admin_generator {
        Some(id) => {
            diesel::update(generators::table.find(id))
                .set((
                    generators::pak_hash.eq(&pak_hash),
                    generators::description.eq("Linked to Admin PAK"),
                ))
                .execute(conn)?;
        }
        None => {
            diesel::insert_into(generators::table)
                .values((
                    generators::name.eq("admin-generator"),
                    generators::description.eq("Linked to Admin PAK"),
                    generators::pak_hash.eq(&pak_hash),
                ))
                .execute(conn)?;
        }
    }

    Ok(())
}
}

brokkr-broker::utils::validate_pak_hash

private

#![allow(unused)]
fn main() {
fn validate_pak_hash (hash : & str) -> bool
}
Source
#![allow(unused)]
fn main() {
fn validate_pak_hash(hash: &str) -> bool {
    // Implement hash validation logic here
    // For example, check if it's a valid SHA-256 hash
    hash.len() == 64 && hash.chars().all(|c| c.is_ascii_hexdigit())
}
}