Cleaning up the admin credentials creation a ton and creating the default bubble

admin user in the users table.
Still need UUIDv7 in as primary keys but we're getting there slowly
This commit is contained in:
shockrah 2025-01-07 22:54:50 -08:00
parent a679f49b18
commit b4aa323577
3 changed files with 41 additions and 9 deletions

View File

@ -7,5 +7,5 @@ edition = "2021"
clap = { version = "4.5.20", features = ["derive"] } clap = { version = "4.5.20", features = ["derive"] }
postgres = "0.19.9" postgres = "0.19.9"
base64 = "0.22.1" base64 = "0.22.1"
serde = "1.0.215" serde = { version = "1.0.215", features = ["derive"] }
serde_json = "1.0.133" serde_json = "1.0.133"

View File

@ -6,6 +6,9 @@ use postgres::{Client, NoTls};
use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine as _}; use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine as _};
use serde::Serialize; use serde::Serialize;
const PASSWORD_LENGTH: usize = 64;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(version, about, long_about = None)] #[command(version, about, long_about = None)]
struct Args { struct Args {
@ -14,10 +17,16 @@ struct Args {
setup: bool, setup: bool,
} }
#[derive(Serialize)]
struct Admin {
username: String,
password: String,
}
#[derive(Serialize)] #[derive(Serialize)]
struct Config { struct Config {
postgres_user: String, postgres: Admin,
postgres_pass: String, bubble: Admin
} }
fn random_string(size: usize) -> String { fn random_string(size: usize) -> String {
@ -28,6 +37,13 @@ fn random_string(size: usize) -> String {
URL_SAFE_NO_PAD.encode(buffer) URL_SAFE_NO_PAD.encode(buffer)
} }
fn admin(username: &str, password_size: usize) -> Admin {
Admin {
username: format!("admin-{}", username),
password: random_string(password_size)
}
}
fn full_setup() -> Result<Config, postgres::Error> { fn full_setup() -> Result<Config, postgres::Error> {
// Check to make sure we have the DB url set to connect // Check to make sure we have the DB url set to connect
const KEY: &'static str = "DB_CONNECTION_STRING" ; const KEY: &'static str = "DB_CONNECTION_STRING" ;
@ -36,7 +52,11 @@ fn full_setup() -> Result<Config, postgres::Error> {
); );
let setup_tables_script = fs::read_to_string("db/setup-tables.sql") let setup_tables_script = fs::read_to_string("db/setup-tables.sql")
.expect("Failed to load file: db/setup-tables.sql"); .expect("Failed to load file: db/setup-tables.sql");
let bubble_admin_password = random_string(32); let postgres_admin = admin("bubble_admin", PASSWORD_LENGTH);
let bubble_admin = admin(
&format!("admin-{}", random_string(8)),
PASSWORD_LENGTH
);
let mut client = Client::connect(&connection_string, NoTls)?; let mut client = Client::connect(&connection_string, NoTls)?;
// Preliminary bs // Preliminary bs
@ -44,16 +64,24 @@ fn full_setup() -> Result<Config, postgres::Error> {
client.execute("CREATE DATABASE bubble;", &[])?; client.execute("CREATE DATABASE bubble;", &[])?;
client.execute("DROP USER IF EXISTS bubble_admin;", &[])?; client.execute("DROP USER IF EXISTS bubble_admin;", &[])?;
client.execute( client.execute(
&format!("CREATE USER bubble_admin WITH ENCRYPTED PASSWORD '{}';", bubble_admin_password), &format!("CREATE USER bubble_admin WITH ENCRYPTED PASSWORD '{}';", postgres_admin.password),
&[] &[]
)?; )?;
// Ensure the admin has ownership of the db we created // Ensure the admin has ownership of the db we created
client.execute("ALTER DATABASE bubble OWNER TO bubble_admin", &[])?; client.execute("ALTER DATABASE bubble OWNER TO bubble_admin", &[])?;
// Service table creation // Service table creation
client.batch_execute(&setup_tables_script)?; client.batch_execute(&setup_tables_script)?;
client.execute(
&format!(
"INSERT INTO users (name, password) VALUES '{}', '{}'",
bubble_admin.username,
bubble_admin.password
),
&[]
)?;
Ok(Config { Ok(Config {
postgres_user: "bubble_admin".into(), postgres: postgres_admin,
postgres_pass: bubble_admin_password bubble: bubble_admin
}) })
} }

View File

@ -1,6 +1,10 @@
CREATE TABLE IF NOT EXISTS users ( CREATE TABLE IF NOT EXISTS users (
id INTEGER, /* */
name VARCHAR(256), id UUID,
/* Acts as a kind of nick name per instance as it assumes no uniqueness */
username VARCHAR(256),
/* Basic salted+hashed password */
password VARCHAR(256),
PRIMARY KEY (id) PRIMARY KEY (id)
); );